HtmlObjectElement
sealed class HtmlObjectElement : HtmlFormControlElement, IHtmlObjectElement, IHtmlElement, IElement, INode, IEventTarget, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, IValidation, IDisposable
Represents the HTML object element.
using AngleSharp.Dom.Css;
using AngleSharp.Extensions;
using AngleSharp.Html;
using AngleSharp.Services.Media;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace AngleSharp.Dom.Html
{
internal sealed class HtmlObjectElement : HtmlFormControlElement, IHtmlObjectElement, IHtmlElement, IElement, INode, IEventTarget, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, IValidation, IDisposable
{
private IDocument _contentDocument;
private IWindow _contentWindow;
private Task<IObjectInfo> _resourceTask;
private CancellationTokenSource _cts;
public string Source {
get {
return GetAttribute(AttributeNames.Data);
}
set {
SetAttribute(AttributeNames.Data, value);
}
}
public string Type {
get {
return GetAttribute(AttributeNames.Type);
}
set {
SetAttribute(AttributeNames.Type, value);
}
}
public bool TypeMustMatch {
get {
return GetAttribute(AttributeNames.TypeMustMatch) != null;
}
set {
SetAttribute(AttributeNames.TypeMustMatch, value ? string.Empty : null);
}
}
public string UseMap {
get {
return GetAttribute(AttributeNames.UseMap);
}
set {
SetAttribute(AttributeNames.UseMap, value);
}
}
public int DisplayWidth {
get {
return GetAttribute(AttributeNames.Width).ToInteger(OriginalWidth);
}
set {
SetAttribute(AttributeNames.Width, value.ToString());
}
}
public int DisplayHeight {
get {
return GetAttribute(AttributeNames.Height).ToInteger(OriginalHeight);
}
set {
SetAttribute(AttributeNames.Height, value.ToString());
}
}
public int OriginalWidth {
get {
if (_resourceTask == null)
return 0;
if (!_resourceTask.IsCompleted || _resourceTask.Result == null)
return 0;
return _resourceTask.Result.Width;
}
}
public int OriginalHeight {
get {
if (_resourceTask == null)
return 0;
if (!_resourceTask.IsCompleted || _resourceTask.Result == null)
return 0;
return _resourceTask.Result.Height;
}
}
public IDocument ContentDocument => _contentDocument;
public IWindow ContentWindow => _contentWindow;
public HtmlObjectElement(Document owner)
: base(owner, Tags.Object, NodeFlags.Scoped)
{
_contentDocument = null;
_contentWindow = null;
RegisterAttributeObserver(AttributeNames.Src, UpdateSource);
}
public void Dispose()
{
if (_cts != null)
_cts.Cancel();
_cts = null;
_resourceTask = null;
}
protected override bool CanBeValidated()
{
return false;
}
private void UpdateSource(string value)
{
if (_cts != null)
_cts.Cancel();
if (!string.IsNullOrEmpty(value)) {
Url url = this.HyperReference(value);
_cts = new CancellationTokenSource();
_resourceTask = LoadAsync(url, _cts.Token);
}
}
private async Task<IObjectInfo> LoadAsync(Url url, CancellationToken cancel)
{
IObjectInfo result = await Owner.Options.LoadResource<IObjectInfo>(url, cancel).ConfigureAwait(false);
this.FireSimpleEvent(EventNames.Load, false, false);
return result;
}
}
}