HtmlScriptElement
sealed class HtmlScriptElement : HtmlElement, IHtmlScriptElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IGlobalEventHandlers, ILoadableElement, IConstructableScriptElement, IConstructableElement, IConstructableNode
Represents an HTML script element.
http://www.w3.org/TR/html5/scripting-1.html#execute-the-script-block
using AngleSharp.Dom;
using AngleSharp.Dom.Events;
using AngleSharp.Html.Construction;
using AngleSharp.Io;
using AngleSharp.Io.Processors;
using AngleSharp.Text;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace AngleSharp.Html.Dom
{
[System.Runtime.CompilerServices.NullableContext(2)]
[System.Runtime.CompilerServices.Nullable(0)]
internal sealed class HtmlScriptElement : HtmlElement, IHtmlScriptElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IGlobalEventHandlers, ILoadableElement, IConstructableScriptElement, IConstructableElement, IConstructableNode
{
private readonly bool _parserInserted;
[System.Runtime.CompilerServices.Nullable(1)]
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);
}
}
[System.Runtime.CompilerServices.Nullable(1)]
public string Text {
[System.Runtime.CompilerServices.NullableContext(1)]
get {
return TextContent;
}
[System.Runtime.CompilerServices.NullableContext(1)]
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);
}
}
[System.Runtime.CompilerServices.NullableContext(1)]
public HtmlScriptElement(Document owner, [System.Runtime.CompilerServices.Nullable(2)] 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 = new ScriptRequestProcessor(owner.Context, this);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public override Node Clone(Document owner, bool deep)
{
HtmlScriptElement htmlScriptElement = new HtmlScriptElement(owner, base.Prefix, _parserInserted, _started);
CloneElement(htmlScriptElement, owner, deep);
htmlScriptElement._forceAsync = _forceAsync;
return htmlScriptElement;
}
protected override void OnParentChanged()
{
base.OnParentChanged();
if (!_parserInserted && Prepare(base.Owner))
RunAsync(CancellationToken.None);
}
[System.Runtime.CompilerServices.NullableContext(1)]
internal Task RunAsync(CancellationToken cancel)
{
return _request?.RunAsync(cancel);
}
[System.Runtime.CompilerServices.NullableContext(1)]
internal bool Prepare(Document document)
{
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 (ownAttribute != null && ownAttribute.Length > 0 && ownAttribute2 != null && ownAttribute2.Length > 0) {
ownAttribute = ownAttribute.Trim();
ownAttribute2 = ownAttribute2.Trim();
if (ownAttribute.EndsWith("()"))
ownAttribute = ownAttribute.Substring(0, ownAttribute.Length - 2);
bool num = ownAttribute2.Isi(AttributeNames.Window);
bool flag = ownAttribute.Isi("onload");
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;
}
[System.Runtime.CompilerServices.NullableContext(1)]
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()
{
base.Owner.QueueTask(delegate {
this.FireSimpleEvent(EventNames.Error, false, false);
});
}
[System.Runtime.CompilerServices.NullableContext(1)]
Task IConstructableScriptElement.RunAsync(CancellationToken cancel)
{
return RunAsync(cancel);
}
[System.Runtime.CompilerServices.NullableContext(1)]
bool IConstructableScriptElement.Prepare(IConstructableDocument document)
{
return Prepare((Document)document);
}
}
}