AngleSharp by Florian Rappl

<PackageReference Include="AngleSharp" Version="0.8.7" />

.NET API 1,175,040 bytes

 HtmlLinkElement

Represents the HTML link element.
using AngleSharp.Dom.Collections; using AngleSharp.Dom.Css; using AngleSharp.Extensions; using AngleSharp.Html; using AngleSharp.Network; using AngleSharp.Services.Styling; 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 { private IStyleSheet _sheet; private TokenList _relList; private SettableTokenList _sizes; 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)); CreateBindings(_relList, AttributeNames.Rel); } return _relList; } } public ISettableTokenList Sizes { get { if (_sizes == null) { _sizes = new SettableTokenList(GetOwnAttribute(AttributeNames.Sizes)); CreateBindings(_sizes, AttributeNames.Sizes); } 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); string href = AttributeNames.Href; Action<string> callback = delegate(string value) { UpdateSource(value); }; RegisterAttributeObserver(href, callback); } private void UpdateMedia(string value) { if (_sheet != null) _sheet.Media.MediaText = value; } private void UpdateDisabled(string value) { if (_sheet != null) _sheet.IsDisabled = (value != null); } private void FinishLoading(Task<IResponse> task) { string type = Type ?? MimeTypes.Css; IConfiguration options = base.Owner.Options; IStyleEngine styleEngine = options.GetStyleEngine(type); if (task.IsCompleted && !task.IsFaulted && styleEngine != null && RelationList.Contains(Keywords.StyleSheet)) { StyleOptions styleOptions = new StyleOptions(); styleOptions.Element = this; styleOptions.Title = base.Title; styleOptions.IsDisabled = IsDisabled; styleOptions.IsAlternate = RelationList.Contains(Keywords.Alternate); styleOptions.Configuration = options; StyleOptions options2 = styleOptions; using (IResponse response = task.Result) try { _sheet = styleEngine.ParseStylesheet(response, options2); } catch { } } this.FireLoadOrErrorEvent(task); } private void UpdateSource(string value) { this.CancelTasks(); if (!string.IsNullOrEmpty(value)) { ResourceRequest request = this.CreateRequestFor(Url); this.CreateTask((CancellationToken cancel) => base.Owner.Loader.FetchAsync(request, cancel)).ContinueWith(delegate(Task<IResponse> m) { FinishLoading(m); }); } } } }