HtmlStyleElement
sealed class HtmlStyleElement : HtmlElement, IHtmlStyleElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, ILinkStyle
Represents the HTML style element.
using AngleSharp.Dom.Css;
using AngleSharp.Extensions;
using AngleSharp.Html;
using AngleSharp.Network;
using AngleSharp.Services.Styling;
namespace AngleSharp.Dom.Html
{
internal sealed class HtmlStyleElement : HtmlElement, IHtmlStyleElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, ILinkStyle
{
private IStyleSheet _sheet;
public bool IsScoped {
get {
return HasOwnAttribute(AttributeNames.Scoped);
}
set {
SetOwnAttribute(AttributeNames.Scoped, value ? string.Empty : null);
}
}
public IStyleSheet Sheet => _sheet ?? (_sheet = CreateSheet());
public bool IsDisabled {
get {
return GetOwnAttribute(AttributeNames.Disabled).ToBoolean(false);
}
set {
SetOwnAttribute(AttributeNames.Disabled, value ? string.Empty : null);
if (_sheet != null)
_sheet.IsDisabled = 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 HtmlStyleElement(Document owner, string prefix = null)
: base(owner, Tags.Style, prefix, NodeFlags.Special | NodeFlags.LiteralText)
{
RegisterAttributeObserver(AttributeNames.Media, UpdateMedia);
}
internal override void NodeIsInserted(Node newNode)
{
base.NodeIsInserted(newNode);
UpdateSheet();
}
internal override void NodeIsRemoved(Node removedNode, Node oldPreviousSibling)
{
base.NodeIsRemoved(removedNode, oldPreviousSibling);
UpdateSheet();
}
private void UpdateMedia(string value)
{
if (_sheet != null)
_sheet.Media.MediaText = value;
}
private void UpdateSheet()
{
if (_sheet != null)
_sheet = CreateSheet();
}
private IStyleSheet CreateSheet()
{
IConfiguration options = base.Owner.Options;
string type = Type ?? MimeTypes.Css;
IStyleEngine styleEngine = options.GetStyleEngine(type);
if (styleEngine == null)
return null;
StyleOptions styleOptions = new StyleOptions();
styleOptions.Element = this;
styleOptions.IsDisabled = IsDisabled;
styleOptions.Title = base.Title;
styleOptions.IsAlternate = false;
styleOptions.Configuration = options;
StyleOptions options2 = styleOptions;
return styleEngine.ParseStylesheet(TextContent, options2);
}
}
}