HtmlStyleElement
sealed class HtmlStyleElement : HtmlElement, IHtmlStyleElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, IGlobalEventHandlers, ILinkStyle
Represents the HTML style element.
using AngleSharp.Dom.Css;
using AngleSharp.Dom.Events;
using AngleSharp.Extensions;
using AngleSharp.Html;
using AngleSharp.Network;
using AngleSharp.Services.Styling;
using System.Threading;
using System.Threading.Tasks;
namespace AngleSharp.Dom.Html
{
internal sealed class HtmlStyleElement : HtmlElement, IHtmlStyleElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, IGlobalEventHandlers, ILinkStyle
{
private IStyleSheet _sheet;
public bool IsScoped {
get {
return this.GetBoolAttribute(AttributeNames.Scoped);
}
set {
this.SetBoolAttribute(AttributeNames.Scoped, value);
}
}
public IStyleSheet Sheet => _sheet;
public bool IsDisabled {
get {
return this.GetBoolAttribute(AttributeNames.Disabled);
}
set {
this.SetBoolAttribute(AttributeNames.Disabled, value);
if (_sheet != null)
_sheet.IsDisabled = value;
}
}
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 HtmlStyleElement(Document owner, string prefix = null)
: base(owner, TagNames.Style, prefix, NodeFlags.Special | NodeFlags.LiteralText)
{
}
internal override void SetupElement()
{
base.SetupElement();
UpdateSheet();
}
internal override void NodeIsInserted(Node newNode)
{
base.NodeIsInserted(newNode);
UpdateSheet();
}
internal override void NodeIsRemoved(Node removedNode, Node oldPreviousSibling)
{
base.NodeIsRemoved(removedNode, oldPreviousSibling);
UpdateSheet();
}
internal void UpdateMedia(string value)
{
if (_sheet != null)
_sheet.Media.MediaText = value;
}
private void UpdateSheet()
{
Document owner = base.Owner;
if (owner != null) {
IConfiguration options = owner.Options;
IBrowsingContext context = owner.Context;
string type = Type ?? MimeTypeNames.Css;
IStyleEngine styleEngine = options.GetStyleEngine(type);
if (styleEngine != null) {
Task task = CreateSheetAsync(styleEngine, context);
owner.DelayLoad(task);
}
}
}
private async Task CreateSheetAsync(IStyleEngine engine, IBrowsingContext context)
{
CancellationToken none = CancellationToken.None;
IResponse response = VirtualResponse.Create(delegate(VirtualResponse res) {
res.Content(TextContent).Address((Url)null);
});
StyleOptions options = new StyleOptions(context) {
Element = this,
IsDisabled = IsDisabled,
IsAlternate = false
};
Task<IStyleSheet> task = engine.ParseStylesheetAsync(response, options, none);
IStyleSheet sheet = _sheet;
IStyleSheet styleSheet = _sheet = await task.ConfigureAwait(false);
}
}
}