HtmlScriptElement
sealed class HtmlScriptElement : HtmlElement, IHtmlScriptElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle
Represents an HTML script element.
using AngleSharp.Dom.Css;
using AngleSharp.Extensions;
using AngleSharp.Html;
using AngleSharp.Network;
using AngleSharp.Services.Scripting;
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
{
private bool _started;
private bool _parserInserted;
private bool _wasParserInserted;
private bool _forceAsync;
private bool _readyToBeExecuted;
private Task<IResponse> _current;
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 ownAttribute = GetOwnAttribute(AttributeNames.Language);
string text = Type ?? ((ownAttribute != null) ? ("text/" + ownAttribute) : null);
if (!string.IsNullOrEmpty(text))
return text;
return MimeTypes.DefaultJavaScript;
}
}
public string Source {
get {
return GetOwnAttribute(AttributeNames.Src);
}
set {
SetOwnAttribute(AttributeNames.Src, value);
}
}
public string Type {
get {
return GetOwnAttribute(AttributeNames.Type);
}
set {
SetOwnAttribute(AttributeNames.Type, value);
}
}
public string CharacterSet {
get {
return GetOwnAttribute(AttributeNames.Charset);
}
set {
SetOwnAttribute(AttributeNames.Charset, value);
}
}
public string Text {
get {
return TextContent;
}
set {
TextContent = value;
}
}
public string CrossOrigin {
get {
return GetOwnAttribute(AttributeNames.CrossOrigin);
}
set {
SetOwnAttribute(AttributeNames.CrossOrigin, value);
}
}
public bool IsDeferred {
get {
return GetOwnAttribute(AttributeNames.Defer) != null;
}
set {
SetOwnAttribute(AttributeNames.Defer, value ? string.Empty : null);
}
}
public bool IsAsync {
get {
return GetOwnAttribute(AttributeNames.Async) != null;
}
set {
SetOwnAttribute(AttributeNames.Async, value ? string.Empty : null);
}
}
public HtmlScriptElement(Document owner, string prefix = null)
: base(owner, Tags.Script, prefix, NodeFlags.Special | NodeFlags.LiteralText)
{
}
internal void Run()
{
if (_current != null) {
if (_current.Exception != null || _current.IsFaulted)
Error();
else if (!CancelledBeforeScriptExecute()) {
IScriptEngine scriptEngine = base.Owner.Options.GetScriptEngine(ScriptLanguage);
using (IResponse response = _current.Result)
scriptEngine.Evaluate(response, CreateOptions());
AfterScriptExecute();
if (Source != null)
Load();
else
base.Owner.QueueTask(Load);
}
}
}
internal void Prepare()
{
if (!_started && base.Owner != null) {
IConfiguration options = base.Owner.Options;
IScriptEngine scriptEngine = options.GetScriptEngine(ScriptLanguage);
if (scriptEngine != null) {
_wasParserInserted = _parserInserted;
_parserInserted = false;
_forceAsync = (_wasParserInserted && !IsAsync);
if (!string.IsNullOrEmpty(Source) || !string.IsNullOrEmpty(Text)) {
if (_wasParserInserted) {
_parserInserted = true;
_forceAsync = false;
}
_started = true;
string ownAttribute = GetOwnAttribute(AttributeNames.Event);
string ownAttribute2 = GetOwnAttribute(AttributeNames.For);
if (!string.IsNullOrEmpty(ownAttribute) && !string.IsNullOrEmpty(ownAttribute2)) {
ownAttribute = ownAttribute.Trim();
ownAttribute2 = ownAttribute2.Trim();
if (ownAttribute.EndsWith("()"))
ownAttribute = ownAttribute.Substring(0, ownAttribute.Length - 2);
if (!ownAttribute2.Equals(AttributeNames.Window, StringComparison.OrdinalIgnoreCase) || !ownAttribute.Equals("onload", StringComparison.OrdinalIgnoreCase))
return;
}
string source = Source;
if (source != null) {
if (source == string.Empty)
base.Owner.QueueTask(Error);
else
InvokeLoadingScript(this.HyperReference(source));
} else if (_parserInserted && base.Owner.HasScriptBlockingStyleSheet()) {
_readyToBeExecuted = true;
} else {
scriptEngine.Evaluate(Text, CreateOptions());
}
}
}
}
}
private void InvokeLoadingScript(Url url)
{
if (_parserInserted && !IsAsync) {
if (IsDeferred)
base.Owner.AddScript(this);
} else if (!IsAsync && !_forceAsync) {
base.Owner.AddScript(this);
} else {
base.Owner.AddScript(this);
}
ResourceRequest request = this.CreateRequestFor(url);
CorsSetting setting = CrossOrigin.ToEnum(CorsSetting.None);
_current = base.Owner.Tasks.Add((CancellationToken cancel) => base.Owner.Loader.FetchWithCorsAsync(request, setting, OriginBehavior.Taint, cancel));
_current.ContinueWith((Task<IResponse> m) => _readyToBeExecuted = ((_parserInserted && !IsAsync) || _readyToBeExecuted));
}
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()
{
ScriptOptions scriptOptions = new ScriptOptions();
scriptOptions.Context = base.Owner.DefaultView;
scriptOptions.Document = base.Owner;
scriptOptions.Element = this;
scriptOptions.Encoding = TextEncoding.Resolve(CharacterSet);
return scriptOptions;
}
}
}