HtmlIFrameElement
sealed class HtmlIFrameElement : HtmlFrameElementBase, IHtmlInlineFrameElement, IHtmlElement, IElement, INode, IEventTarget, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, IDisposable
Represents the HTML iframe element.
using AngleSharp.Dom.Collections;
using AngleSharp.Dom.Css;
using AngleSharp.Extensions;
using AngleSharp.Html;
using AngleSharp.Network;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace AngleSharp.Dom.Html
{
internal sealed class HtmlIFrameElement : HtmlFrameElementBase, IHtmlInlineFrameElement, IHtmlElement, IElement, INode, IEventTarget, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, IDisposable
{
private CancellationTokenSource _cts;
private SettableTokenList _sandbox;
private Document _doc;
private Task _docTask;
public Alignment Align {
get {
return GetAttribute(AttributeNames.Align).ToEnum(Alignment.Bottom);
}
set {
SetAttribute(AttributeNames.Align, value.ToString());
}
}
public string ContentHtml {
get {
return GetAttribute(AttributeNames.SrcDoc);
}
set {
SetAttribute(AttributeNames.SrcDoc, value);
}
}
public ISettableTokenList Sandbox {
get {
if (_sandbox == null) {
_sandbox = new SettableTokenList(GetAttribute(AttributeNames.Sandbox));
_sandbox.Changed += delegate {
UpdateAttribute(AttributeNames.Sandbox, _sandbox.Value);
};
}
return _sandbox;
}
}
public bool IsSeamless {
get {
return GetAttribute(AttributeNames.SrcDoc) != null;
}
set {
SetAttribute(AttributeNames.SrcDoc, value ? string.Empty : null);
}
}
public bool IsFullscreenAllowed {
get {
return GetAttribute(AttributeNames.AllowFullscreen) != null;
}
set {
SetAttribute(AttributeNames.AllowFullscreen, value ? string.Empty : null);
}
}
public IWindow ContentWindow {
get {
if (_doc == null)
return null;
return _doc.DefaultView;
}
}
public HtmlIFrameElement(Document owner)
: base(owner, Tags.Iframe, NodeFlags.LiteralText)
{
_doc = new Document(owner.NewChildContext(Sandboxes.None));
RegisterAttributeObserver(AttributeNames.Src, UpdateSource);
}
public void Dispose()
{
if (_cts != null)
_cts.Cancel();
_docTask = null;
_cts = null;
_doc.Dispose();
}
private void UpdateSource(string src)
{
if (_cts != null)
_cts.Cancel();
if (!string.IsNullOrEmpty(src)) {
Url url = this.HyperReference(src);
IRequester requester = base.Owner.Options.GetRequester(url.Scheme);
if (requester != null) {
_cts = new CancellationTokenSource();
_docTask = LoadAsync(requester, url, _cts.Token);
}
}
}
private async Task LoadAsync(IRequester requester, Url url, CancellationToken cancel)
{
IResponse response = await requester.LoadAsync(url).ConfigureAwait(false);
if (response != null) {
await _doc.LoadAsync(response, cancel);
response.Dispose();
}
}
}
}