HtmlLinkElement
sealed class HtmlLinkElement : HtmlElement, IHtmlLinkElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, ILinkStyle, IDisposable
Represents the HTML link element.
using AngleSharp.Css;
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 HtmlLinkElement : HtmlElement, IHtmlLinkElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, ILinkStyle, IDisposable
{
private IStyleSheet _sheet;
private Url _buffer;
private TokenList _relList;
private SettableTokenList _sizes;
private Task _loadingTask;
private CancellationTokenSource _cts;
internal bool IsVisited { get; set; }
internal bool IsActive { get; set; }
public Url Url => this.HyperReference(GetOwnAttribute(AttributeNames.Href));
public string Href {
get {
return Url.Href;
}
set {
SetOwnAttribute(AttributeNames.Href, value);
}
}
public string TargetLanguage {
get {
return GetOwnAttribute(AttributeNames.HrefLang);
}
set {
SetOwnAttribute(AttributeNames.HrefLang, value);
}
}
public string Charset {
get {
return GetOwnAttribute(AttributeNames.Charset);
}
set {
SetOwnAttribute(AttributeNames.Charset, value);
}
}
public string Relation {
get {
return GetOwnAttribute(AttributeNames.Rel);
}
set {
SetOwnAttribute(AttributeNames.Rel, value);
}
}
public ITokenList RelationList {
get {
if (_relList == null) {
_relList = new TokenList(GetOwnAttribute(AttributeNames.Rel));
_relList.Changed += delegate {
UpdateAttribute(AttributeNames.Rel, _relList.ToString());
};
}
return _relList;
}
}
public ISettableTokenList Sizes {
get {
if (_sizes == null) {
_sizes = new SettableTokenList(GetOwnAttribute(AttributeNames.Sizes));
_sizes.Changed += delegate {
UpdateAttribute(AttributeNames.Sizes, _sizes.Value);
};
}
return _sizes;
}
}
public string Rev {
get {
return GetOwnAttribute(AttributeNames.Rev);
}
set {
SetOwnAttribute(AttributeNames.Rev, value);
}
}
public bool IsDisabled {
get {
return GetOwnAttribute(AttributeNames.Disabled).ToBoolean(false);
}
set {
SetOwnAttribute(AttributeNames.Disabled, value ? string.Empty : null);
}
}
public string Target {
get {
return GetOwnAttribute(AttributeNames.Target);
}
set {
SetOwnAttribute(AttributeNames.Target, value);
}
}
public string Media {
get {
return GetOwnAttribute(AttributeNames.Media);
}
set {
SetOwnAttribute(AttributeNames.Media, value);
}
}
public string Type {
get {
return GetOwnAttribute(AttributeNames.Type);
}
set {
SetOwnAttribute(AttributeNames.Type, value);
}
}
public IStyleSheet Sheet => _sheet;
public HtmlLinkElement(Document owner, string prefix = null)
: base(owner, Tags.Link, prefix, NodeFlags.SelfClosing | NodeFlags.Special)
{
RegisterAttributeObserver(AttributeNames.Media, UpdateMedia);
RegisterAttributeObserver(AttributeNames.Disabled, UpdateDisabled);
RegisterAttributeObserver(AttributeNames.Href, delegate {
TargetChanged();
});
RegisterAttributeObserver(AttributeNames.Type, delegate {
TargetChanged();
});
}
private void UpdateMedia(string value)
{
if (_sheet != null)
_sheet.Media.MediaText = value;
}
private void UpdateDisabled(string value)
{
if (_sheet != null)
_sheet.IsDisabled = (value != null);
}
public void Dispose()
{
if (_cts != null)
_cts.Cancel();
_cts = null;
_loadingTask = null;
}
private void TargetChanged()
{
if (base.Owner.Options.IsStyling()) {
if (_cts != null)
_cts.Cancel();
Url url = Url;
if (url != null && (_buffer == null || !url.Equals(_buffer))) {
_buffer = url;
_cts = new CancellationTokenSource();
_loadingTask = LoadAsync(url, _cts.Token);
}
}
}
private async Task LoadAsync(Url url, CancellationToken cancel)
{
ResourceRequest request = this.CreateRequestFor(url);
IResponse response = await AwaitExtensions.ConfigureAwait(Owner.Loader.FetchAsync(request, cancel), false);
if (response != null) {
IConfiguration options = Owner.Options;
_sheet = options.ParseStyling(response, new StyleOptions {
Element = this,
Title = Title,
IsDisabled = IsDisabled,
IsAlternate = RelationList.Contains(Keywords.Alternate),
Configuration = options
}, Type);
response.Dispose();
}
}
}
}