HtmlScriptElement
sealed class HtmlScriptElement : HtmlElement, IHtmlScriptElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, IGlobalEventHandlers, ILoadableElement
Represents an HTML script element.
http://www.w3.org/TR/html5/scripting-1.html#execute-the-script-block
using AngleSharp.Dom.Css;
using AngleSharp.Dom.Events;
using AngleSharp.Extensions;
using AngleSharp.Html;
using AngleSharp.Network;
using AngleSharp.Network.RequestProcessors;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace AngleSharp.Dom.Html
{
internal sealed class HtmlScriptElement : HtmlElement, IHtmlScriptElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, IGlobalEventHandlers, ILoadableElement
{
private readonly bool _parserInserted;
private readonly ScriptRequestProcessor _request;
private bool _started;
private bool _forceAsync;
public IDownload CurrentDownload => _request?.Download;
public string Source {
get {
return this.GetOwnAttribute(AttributeNames.Src);
}
set {
this.SetOwnAttribute(AttributeNames.Src, value, false);
}
}
public string Type {
get {
return this.GetOwnAttribute(AttributeNames.Type);
}
set {
this.SetOwnAttribute(AttributeNames.Type, value, false);
}
}
public string CharacterSet {
get {
return this.GetOwnAttribute(AttributeNames.Charset);
}
set {
this.SetOwnAttribute(AttributeNames.Charset, value, false);
}
}
public string Text {
get {
return TextContent;
}
set {
TextContent = value;
}
}
public string CrossOrigin {
get {
return this.GetOwnAttribute(AttributeNames.CrossOrigin);
}
set {
this.SetOwnAttribute(AttributeNames.CrossOrigin, value, false);
}
}
public bool IsDeferred {
get {
return this.GetBoolAttribute(AttributeNames.Defer);
}
set {
this.SetBoolAttribute(AttributeNames.Defer, value);
}
}
public bool IsAsync {
get {
return this.GetBoolAttribute(AttributeNames.Async);
}
set {
this.SetBoolAttribute(AttributeNames.Async, value);
}
}
public string Integrity {
get {
return this.GetOwnAttribute(AttributeNames.Integrity);
}
set {
this.SetOwnAttribute(AttributeNames.Integrity, value, false);
}
}
public HtmlScriptElement(Document owner, string prefix = null, bool parserInserted = false, bool started = false)
: base(owner, TagNames.Script, prefix, NodeFlags.Special | NodeFlags.LiteralText)
{
_forceAsync = false;
_started = started;
_parserInserted = parserInserted;
_request = ScriptRequestProcessor.Create(this);
}
protected override void OnParentChanged()
{
base.OnParentChanged();
if (!_parserInserted && Prepare(base.Owner))
RunAsync(CancellationToken.None);
}
internal Task RunAsync(CancellationToken cancel)
{
return _request?.RunAsync(cancel);
}
internal bool Prepare(Document document)
{
IConfiguration option = document.Options;
string ownAttribute = this.GetOwnAttribute(AttributeNames.Event);
string ownAttribute2 = this.GetOwnAttribute(AttributeNames.For);
string source = Source;
bool parserInserted = _parserInserted;
if (_started)
return false;
if (parserInserted)
_forceAsync = !IsAsync;
if (string.IsNullOrEmpty(source) && string.IsNullOrEmpty(Text))
return false;
if (_request.Engine == null)
return false;
if (parserInserted)
_forceAsync = false;
_started = true;
if (!string.IsNullOrEmpty(ownAttribute) && !string.IsNullOrEmpty(ownAttribute2)) {
ownAttribute = ownAttribute.Trim();
ownAttribute2 = ownAttribute2.Trim();
if (ownAttribute.EndsWith("()"))
ownAttribute = ownAttribute.Substring(0, ownAttribute.Length - 2);
bool num = ownAttribute2.Equals(AttributeNames.Window, StringComparison.OrdinalIgnoreCase);
bool flag = ownAttribute.Equals("onload", StringComparison.OrdinalIgnoreCase);
if (!num || !flag)
return false;
}
if (source == null) {
_request.Process(Text);
return true;
}
if (source.Length != 0)
return InvokeLoadingScript(document, this.HyperReference(source));
document.QueueTask(FireErrorEvent);
return false;
}
public override INode Clone(bool deep = true)
{
HtmlScriptElement htmlScriptElement = new HtmlScriptElement(base.Owner, base.Prefix, _parserInserted, _started) {
_forceAsync = _forceAsync
};
CloneElement(htmlScriptElement, deep);
return htmlScriptElement;
}
private bool InvokeLoadingScript(Document document, Url url)
{
bool result = true;
if (_parserInserted && (IsDeferred || IsAsync)) {
document.AddScript(this);
result = false;
}
this.Process(_request, url);
return result;
}
private void FireErrorEvent()
{
this.FireSimpleEvent(EventNames.Error, false, false);
}
}
}