HtmlLinkElement
sealed class HtmlLinkElement : HtmlElement, IHtmlLinkElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, ILinkStyle, ILinkImport, ILoadableElement
Represents the HTML link element.
using AngleSharp.Dom.Collections;
using AngleSharp.Dom.Css;
using AngleSharp.Extensions;
using AngleSharp.Html;
using AngleSharp.Html.LinkRels;
using AngleSharp.Network;
using System.Threading.Tasks;
namespace AngleSharp.Dom.Html
{
internal sealed class HtmlLinkElement : HtmlElement, IHtmlLinkElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, ILinkStyle, ILinkImport, ILoadableElement
{
private BaseLinkRelation _relation;
private TokenList _relList;
private SettableTokenList _sizes;
internal bool IsVisited { get; set; }
internal bool IsActive { get; set; }
public IDownload CurrentDownload => ((_relation != null) ? _relation.Processor : null)?.Download;
public string Href {
get {
return this.GetUrlAttribute(AttributeNames.Href);
}
set {
this.SetOwnAttribute(AttributeNames.Href, value);
}
}
public string TargetLanguage {
get {
return this.GetOwnAttribute(AttributeNames.HrefLang);
}
set {
this.SetOwnAttribute(AttributeNames.HrefLang, value);
}
}
public string Charset {
get {
return this.GetOwnAttribute(AttributeNames.Charset);
}
set {
this.SetOwnAttribute(AttributeNames.Charset, value);
}
}
public string Relation {
get {
return this.GetOwnAttribute(AttributeNames.Rel);
}
set {
this.SetOwnAttribute(AttributeNames.Rel, value);
}
}
public ITokenList RelationList {
get {
if (_relList == null) {
_relList = new TokenList(this.GetOwnAttribute(AttributeNames.Rel));
CreateBindings(_relList, AttributeNames.Rel);
}
return _relList;
}
}
public ISettableTokenList Sizes {
get {
if (_sizes == null) {
_sizes = new SettableTokenList(this.GetOwnAttribute(AttributeNames.Sizes));
CreateBindings(_sizes, AttributeNames.Sizes);
}
return _sizes;
}
}
public string Rev {
get {
return this.GetOwnAttribute(AttributeNames.Rev);
}
set {
this.SetOwnAttribute(AttributeNames.Rev, value);
}
}
public bool IsDisabled {
get {
return this.GetOwnAttribute(AttributeNames.Disabled).ToBoolean(false);
}
set {
this.SetOwnAttribute(AttributeNames.Disabled, value ? string.Empty : null);
}
}
public string Target {
get {
return this.GetOwnAttribute(AttributeNames.Target);
}
set {
this.SetOwnAttribute(AttributeNames.Target, value);
}
}
public string Media {
get {
return this.GetOwnAttribute(AttributeNames.Media);
}
set {
this.SetOwnAttribute(AttributeNames.Media, value);
}
}
public string Type {
get {
return this.GetOwnAttribute(AttributeNames.Type);
}
set {
this.SetOwnAttribute(AttributeNames.Type, value);
}
}
public IStyleSheet Sheet => (_relation as StyleSheetLinkRelation)?.Sheet;
public IDocument Import => (_relation as ImportLinkRelation)?.Import;
public HtmlLinkElement(Document owner, string prefix = null)
: base(owner, TagNames.Link, prefix, NodeFlags.SelfClosing | NodeFlags.Special)
{
}
internal override void SetupElement()
{
base.SetupElement();
string ownAttribute = this.GetOwnAttribute(AttributeNames.Rel);
RegisterAttributeObserver(AttributeNames.Media, UpdateMedia);
RegisterAttributeObserver(AttributeNames.Disabled, UpdateDisabled);
RegisterAttributeObserver(AttributeNames.Href, UpdateSource);
RegisterAttributeObserver(AttributeNames.Rel, UpdateRelation);
if (ownAttribute != null)
UpdateRelation(ownAttribute);
}
private void UpdateMedia(string value)
{
IStyleSheet sheet = Sheet;
if (sheet != null)
sheet.Media.MediaText = value;
}
private void UpdateDisabled(string value)
{
IStyleSheet sheet = Sheet;
if (sheet != null)
sheet.IsDisabled = (value != null);
}
private void UpdateRelation(string value)
{
BaseLinkRelation relation = _relation;
_relation = CreateFirstLegalRelation();
UpdateSource(this.GetOwnAttribute(AttributeNames.Href));
}
private void UpdateSource(string value)
{
Document owner = base.Owner;
if (_relation != null && owner != null) {
Task task = _relation.LoadAsync();
owner.DelayLoad(task);
}
}
private BaseLinkRelation CreateFirstLegalRelation()
{
ITokenList relationList = RelationList;
foreach (string item in relationList) {
BaseLinkRelation baseLinkRelation = Factory.LinkRelations.Create(this, item);
if (baseLinkRelation != null)
return baseLinkRelation;
}
return null;
}
}
}