HtmlImageElement
sealed class HtmlImageElement : HtmlElement, IHtmlImageElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle
Represents the image element.
using AngleSharp.Dom.Css;
using AngleSharp.Extensions;
using AngleSharp.Html;
using AngleSharp.Network;
using AngleSharp.Services.Media;
using System.Threading.Tasks;
namespace AngleSharp.Dom.Html
{
internal sealed class HtmlImageElement : HtmlElement, IHtmlImageElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle
{
private IImageInfo _img;
private IDownload _download;
public string ActualSource {
get {
if (_img == null || _img.Source == null)
return string.Empty;
return _img.Source.Href;
}
}
public string SourceSet {
get {
return this.GetOwnAttribute(AttributeNames.SrcSet);
}
set {
this.SetOwnAttribute(AttributeNames.SrcSet, value);
}
}
public string Sizes {
get {
return this.GetOwnAttribute(AttributeNames.Sizes);
}
set {
this.SetOwnAttribute(AttributeNames.Sizes, value);
}
}
public string Source {
get {
return this.GetUrlAttribute(AttributeNames.Src);
}
set {
this.SetOwnAttribute(AttributeNames.Src, value);
}
}
public string AlternativeText {
get {
return this.GetOwnAttribute(AttributeNames.Alt);
}
set {
this.SetOwnAttribute(AttributeNames.Alt, value);
}
}
public string CrossOrigin {
get {
return this.GetOwnAttribute(AttributeNames.CrossOrigin);
}
set {
this.SetOwnAttribute(AttributeNames.CrossOrigin, value);
}
}
public string UseMap {
get {
return this.GetOwnAttribute(AttributeNames.UseMap);
}
set {
this.SetOwnAttribute(AttributeNames.UseMap, value);
}
}
public int DisplayWidth {
get {
return this.GetOwnAttribute(AttributeNames.Width).ToInteger(OriginalWidth);
}
set {
this.SetOwnAttribute(AttributeNames.Width, value.ToString());
}
}
public int DisplayHeight {
get {
return this.GetOwnAttribute(AttributeNames.Height).ToInteger(OriginalHeight);
}
set {
this.SetOwnAttribute(AttributeNames.Height, value.ToString());
}
}
public int OriginalWidth {
get {
if (!IsCompleted)
return 0;
return _img.Width;
}
}
public int OriginalHeight {
get {
if (!IsCompleted)
return 0;
return _img.Height;
}
}
public bool IsCompleted => _img != null;
public bool IsMap {
get {
return this.HasOwnAttribute(AttributeNames.IsMap);
}
set {
this.SetOwnAttribute(AttributeNames.IsMap, value ? string.Empty : null);
}
}
public HtmlImageElement(Document owner, string prefix = null)
: base(owner, TagNames.Img, prefix, NodeFlags.SelfClosing | NodeFlags.Special)
{
}
private void GetImage(Url source)
{
if (source.IsInvalid)
source = null;
else if (_img != null && source.Equals(_img.Source)) {
return;
}
if (_download != null && !_download.IsCompleted)
_download.Cancel();
Document owner = base.Owner;
if (source != null && owner != null) {
IResourceLoader loader = owner.Loader;
if (loader != null) {
ResourceRequest request = this.CreateRequestFor(source);
IDownload download = loader.DownloadAsync(request);
Task<bool> task = this.ProcessResource(download, delegate(IImageInfo result) {
_img = result;
});
owner.DelayLoad(task);
_download = download;
}
}
}
private void UpdateSource()
{
Url imageCandidate = this.GetImageCandidate();
GetImage(imageCandidate);
}
internal override void SetupElement()
{
base.SetupElement();
RegisterAttributeObserver(AttributeNames.Src, delegate {
UpdateSource();
});
RegisterAttributeObserver(AttributeNames.SrcSet, delegate {
UpdateSource();
});
RegisterAttributeObserver(AttributeNames.Sizes, delegate {
UpdateSource();
});
RegisterAttributeObserver(AttributeNames.CrossOrigin, delegate {
UpdateSource();
});
UpdateSource();
}
}
}