HtmlLinkElement
sealed class HtmlLinkElement : HtmlElement, IHtmlLinkElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, IGlobalEventHandlers, ILinkStyle, ILinkImport, ILoadableElement
Represents the HTML link element.
using AngleSharp.Dom.Collections;
using AngleSharp.Dom.Css;
using AngleSharp.Dom.Events;
using AngleSharp.Extensions;
using AngleSharp.Html;
using AngleSharp.Html.LinkRels;
using AngleSharp.Network;
using AngleSharp.Services;
using System.Threading.Tasks;
namespace AngleSharp.Dom.Html
{
internal sealed class HtmlLinkElement : HtmlElement, IHtmlLinkElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, IGlobalEventHandlers, 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?.Processor?.Download;
public string Href {
get {
return this.GetUrlAttribute(AttributeNames.Href);
}
set {
this.SetOwnAttribute(AttributeNames.Href, value, false);
}
}
public string TargetLanguage {
get {
return this.GetOwnAttribute(AttributeNames.HrefLang);
}
set {
this.SetOwnAttribute(AttributeNames.HrefLang, value, false);
}
}
public string Charset {
get {
return this.GetOwnAttribute(AttributeNames.Charset);
}
set {
this.SetOwnAttribute(AttributeNames.Charset, value, false);
}
}
public string Relation {
get {
return this.GetOwnAttribute(AttributeNames.Rel);
}
set {
this.SetOwnAttribute(AttributeNames.Rel, value, false);
}
}
public ITokenList RelationList {
get {
if (_relList == null) {
_relList = new TokenList(this.GetOwnAttribute(AttributeNames.Rel));
_relList.Changed += delegate(string value) {
UpdateAttribute(AttributeNames.Rel, value);
};
}
return _relList;
}
}
public ISettableTokenList Sizes {
get {
if (_sizes == null) {
_sizes = new SettableTokenList(this.GetOwnAttribute(AttributeNames.Sizes));
_sizes.Changed += delegate(string value) {
UpdateAttribute(AttributeNames.Sizes, value);
};
}
return _sizes;
}
}
public string Rev {
get {
return this.GetOwnAttribute(AttributeNames.Rev);
}
set {
this.SetOwnAttribute(AttributeNames.Rev, value, false);
}
}
public bool IsDisabled {
get {
return this.GetOwnAttribute(AttributeNames.Disabled).ToBoolean(false);
}
set {
this.SetOwnAttribute(AttributeNames.Disabled, value ? string.Empty : null, false);
}
}
public string Target {
get {
return this.GetOwnAttribute(AttributeNames.Target);
}
set {
this.SetOwnAttribute(AttributeNames.Target, value, false);
}
}
public string Media {
get {
return this.GetOwnAttribute(AttributeNames.Media);
}
set {
this.SetOwnAttribute(AttributeNames.Media, value, false);
}
}
public string Type {
get {
return this.GetOwnAttribute(AttributeNames.Type);
}
set {
this.SetOwnAttribute(AttributeNames.Type, value, false);
}
}
public string Integrity {
get {
return this.GetOwnAttribute(AttributeNames.Integrity);
}
set {
this.SetOwnAttribute(AttributeNames.Integrity, value, false);
}
}
public IStyleSheet Sheet => (_relation as StyleSheetLinkRelation)?.Sheet;
public IDocument Import => (_relation as ImportLinkRelation)?.Import;
public string CrossOrigin {
get {
return this.GetOwnAttribute(AttributeNames.CrossOrigin);
}
set {
this.SetOwnAttribute(AttributeNames.CrossOrigin, value, false);
}
}
static HtmlLinkElement()
{
Element.RegisterCallback(AttributeNames.Sizes, delegate(HtmlLinkElement element, string value) {
element._sizes?.Update(value);
});
Element.RegisterCallback(AttributeNames.Media, delegate(HtmlLinkElement element, string value) {
element.UpdateMedia(value);
});
Element.RegisterCallback(AttributeNames.Disabled, delegate(HtmlLinkElement element, string value) {
element.UpdateDisabled(value);
});
Element.RegisterCallback(AttributeNames.Href, delegate(HtmlLinkElement element, string value) {
element.UpdateSource(value);
});
Element.RegisterCallback(AttributeNames.Rel, delegate(HtmlLinkElement element, string value) {
element.UpdateRelation(value);
});
}
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);
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)
{
_relList?.Update(value);
_relation = CreateFirstLegalRelation();
UpdateSource(this.GetOwnAttribute(AttributeNames.Href));
}
private void UpdateSource(string value)
{
Task task = _relation?.LoadAsync();
base.Owner?.DelayLoad(task);
}
private BaseLinkRelation CreateFirstLegalRelation()
{
ITokenList relationList = RelationList;
Document owner = base.Owner;
ILinkRelationFactory linkRelationFactory = (owner != null) ? owner.Options.GetFactory<ILinkRelationFactory>() : null;
foreach (string item in relationList) {
BaseLinkRelation baseLinkRelation = linkRelationFactory.Create(this, item);
if (baseLinkRelation != null)
return baseLinkRelation;
}
return null;
}
}
}