HtmlScriptElement
sealed class HtmlScriptElement : HtmlElement, IHtmlScriptElement, IHtmlElement, IElement, INode, IEventTarget, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, IDisposable
Represents an HTML script element.
using AngleSharp.Dom.Css;
using AngleSharp.Extensions;
using AngleSharp.Html;
using AngleSharp.Network;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace AngleSharp.Dom.Html
{
internal sealed class HtmlScriptElement : HtmlElement, IHtmlScriptElement, IHtmlElement, IElement, INode, IEventTarget, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, IDisposable
{
private bool _started;
private bool _parserInserted;
private bool _wasParserInserted;
private bool _forceAsync;
private bool _readyToBeExecuted;
private CancellationTokenSource _cts;
private Task<IResponse> _loadingTask;
internal bool IsReady => _readyToBeExecuted;
internal bool IsParserInserted {
get {
return _parserInserted;
}
set {
_parserInserted = value;
}
}
internal bool IsAlreadyStarted {
get {
return _started;
}
set {
_started = value;
}
}
public string ScriptLanguage {
get {
string text = Type ?? ((GetAttribute(AttributeNames.Language) != null) ? ("text/" + GetAttribute(AttributeNames.Language)) : null);
if (string.IsNullOrEmpty(text))
return MimeTypes.DefaultJavaScript;
return text;
}
}
public string Source {
get {
return GetAttribute(AttributeNames.Src);
}
set {
SetAttribute(AttributeNames.Src, value);
}
}
public string Type {
get {
return GetAttribute(AttributeNames.Type);
}
set {
SetAttribute(AttributeNames.Type, value);
}
}
public string CharacterSet {
get {
return GetAttribute(AttributeNames.Charset);
}
set {
SetAttribute(AttributeNames.Charset, value);
}
}
public string Text {
get {
return TextContent;
}
set {
TextContent = value;
}
}
public string CrossOrigin {
get {
return GetAttribute(AttributeNames.CrossOrigin);
}
set {
SetAttribute(AttributeNames.CrossOrigin, value);
}
}
public bool IsDeferred {
get {
return GetAttribute(AttributeNames.Defer) != null;
}
set {
SetAttribute(AttributeNames.Defer, value ? string.Empty : null);
}
}
public bool IsAsync {
get {
return GetAttribute(AttributeNames.Async) != null;
}
set {
SetAttribute(AttributeNames.Async, value ? string.Empty : null);
}
}
public HtmlScriptElement(Document owner)
: base(owner, Tags.Script, NodeFlags.Special | NodeFlags.LiteralText)
{
}
internal void Run()
{
if (_loadingTask != null) {
if (_loadingTask.Exception != null || _loadingTask.IsFaulted)
Error();
else if (!CancelledBeforeScriptExecute()) {
using (IResponse response = _loadingTask.Result)
base.Owner.Options.RunScript(response, CreateOptions(), ScriptLanguage);
AfterScriptExecute();
if (Source != null)
Load();
else
base.Owner.QueueTask(Load);
}
}
}
internal void Prepare()
{
if (!_started && base.Owner != null) {
IConfiguration options = base.Owner.Options;
_wasParserInserted = _parserInserted;
_parserInserted = false;
_forceAsync = (_wasParserInserted && !IsAsync);
if ((!string.IsNullOrEmpty(Source) || !string.IsNullOrEmpty(Text)) && options.GetScriptEngine(ScriptLanguage) != null) {
if (_wasParserInserted) {
_parserInserted = true;
_forceAsync = false;
}
_started = true;
if (base.Owner.Options.IsScripting) {
string attribute = GetAttribute(AttributeNames.Event);
string attribute2 = GetAttribute(AttributeNames.For);
if (!string.IsNullOrEmpty(attribute) && !string.IsNullOrEmpty(attribute2)) {
attribute = attribute.Trim();
attribute2 = attribute2.Trim();
if (attribute.EndsWith("()"))
attribute = attribute.Substring(0, attribute.Length - 2);
if (!attribute2.Equals(AttributeNames.Window, StringComparison.OrdinalIgnoreCase) || !attribute.Equals("onload", StringComparison.OrdinalIgnoreCase))
return;
}
string source = Source;
if (source != null) {
if (source == string.Empty)
base.Owner.QueueTask(Error);
else {
Url url = this.HyperReference(source);
IRequester requester = options.GetRequester(url.Scheme);
if (requester != null) {
_cts = new CancellationTokenSource();
_loadingTask = PrepareAsync(requester, url, _cts.Token);
}
}
} else if (_parserInserted && base.Owner.HasScriptBlockingStyleSheet()) {
_readyToBeExecuted = true;
} else {
options.RunScript(Text, CreateOptions(), ScriptLanguage);
}
}
}
}
}
private async Task<IResponse> PrepareAsync(IRequester requester, Url url, CancellationToken cancel)
{
if (_parserInserted && !IsAsync) {
if (IsDeferred)
Owner.AddScript(this);
} else if (!IsAsync && !_forceAsync) {
Owner.AddScript(this);
} else {
Owner.AddScript(this);
}
IResponse result = await requester.LoadWithCorsAsync(url, CrossOrigin.ToEnum(CorsSetting.None), Owner.Origin, OriginBehavior.Taint, cancel);
if (_parserInserted && !IsAsync)
_readyToBeExecuted = true;
return result;
}
private void Load()
{
this.FireSimpleEvent(EventNames.Load, false, false);
}
private void Error()
{
this.FireSimpleEvent(EventNames.Error, false, false);
}
private bool CancelledBeforeScriptExecute()
{
return this.FireSimpleEvent(EventNames.BeforeScriptExecute, false, true);
}
private void AfterScriptExecute()
{
this.FireSimpleEvent(EventNames.AfterScriptExecute, true, false);
}
private ScriptOptions CreateOptions()
{
return new ScriptOptions {
Context = base.Owner.DefaultView,
Document = base.Owner,
Element = this,
Encoding = TextEncoding.Resolve(CharacterSet)
};
}
public void Dispose()
{
if (_cts != null)
_cts.Cancel();
_cts = null;
_loadingTask = null;
}
}
}