Пример #1
0
        async Task <IResponse> PrepareAsync(IRequester requester, Url url, CancellationToken cancel)
        {
            if (_parserInserted && !IsAsync)
            {
                if (IsDeferred)
                {
                    Owner.AddScript(this);
                }
            }
            else if (!IsAsync && !_forceAsync)
            {
                //Add to end of list of scripts (in order) --> sufficient
                Owner.AddScript(this);
            }
            else
            {
                //Just add to the set of scripts
                Owner.AddScript(this);
            }

            var response = await requester.LoadWithCorsAsync(url, CrossOrigin.ToEnum(CorsSetting.None), Owner.Origin, OriginBehavior.Taint, cancel);

            if (_parserInserted && !IsAsync)
            {
                _readyToBeExecuted = true;
            }

            return(response);
        }
Пример #2
0
        async Task LoadScriptAsync(IResourceLoader loader, ResourceRequest request)
        {
            var setting  = CrossOrigin.ToEnum(CorsSetting.None);
            var behavior = OriginBehavior.Taint;
            var response = await loader.FetchWithCorsAsync(request, setting, behavior).ConfigureAwait(false);

            var completion = new TaskCompletionSource <Boolean>();

            _runScript = () =>
            {
                RunFromResponse(response);
                response.Dispose();
                completion.SetResult(true);
            };
            await completion.Task.ConfigureAwait(false);
        }
Пример #3
0
        Boolean InvokeLoadingScript(Url url)
        {
            var fromParser = true;

            //Just add to the (end of) set of scripts
            if ((_parserInserted && IsDeferred && !IsAsync) || !_parserInserted || IsAsync)
            {
                Owner.AddScript(this);
                fromParser = false;
            }

            var request  = this.CreateRequestFor(url);
            var setting  = CrossOrigin.ToEnum(CorsSetting.None);
            var behavior = OriginBehavior.Taint;

            this.CreateTask(async c =>
            {
                var response = await Owner.Loader.FetchWithCorsAsync(request, setting, behavior, c).ConfigureAwait(false);

                if (response != null)
                {
                    _runScript = () =>
                    {
                        RunFromResponse(response);
                        response.Dispose();
                    };
                }
                else
                {
                    FireErrorEvent();
                }

                return(response);
            });

            return(fromParser);
        }
Пример #4
0
        /// <summary>
        /// More information available at:
        /// http://www.w3.org/TR/html5/scripting-1.html#prepare-a-script
        /// </summary>
        internal void Prepare()
        {
            var options = Owner.Options;

            if (_started)
            {
                return;
            }

            _wasParserInserted = _parserInserted;
            _parserInserted    = false;

            _forceAsync = _wasParserInserted && !IsAsync;

            if ((String.IsNullOrEmpty(Source) && String.IsNullOrEmpty(Text)) || Owner == null)
            {
                return;
            }

            if (options.GetScriptEngine(ScriptLanguage) == null)
            {
                return;
            }

            if (_wasParserInserted)
            {
                _parserInserted = true;
                _forceAsync     = false;
            }

            _started = true;

            if (!Owner.Options.IsScripting)
            {
                return;
            }

            var eventAttr = GetAttribute(AttributeNames.Event);
            var forAttr   = GetAttribute(AttributeNames.For);

            if (!String.IsNullOrEmpty(eventAttr) && !String.IsNullOrEmpty(forAttr))
            {
                eventAttr = eventAttr.Trim();
                forAttr   = forAttr.Trim();

                if (eventAttr.EndsWith("()"))
                {
                    eventAttr = eventAttr.Substring(0, eventAttr.Length - 2);
                }

                if (!forAttr.Equals(AttributeNames.Window, StringComparison.OrdinalIgnoreCase) || !eventAttr.Equals("onload", StringComparison.OrdinalIgnoreCase))
                {
                    return;
                }
            }

            var src = Source;

            if (src != null)
            {
                if (src == String.Empty)
                {
                    Owner.QueueTask(Error);
                    return;
                }

                var url       = this.HyperRef(src);
                var requester = options.GetRequester(url.Scheme);

                if (requester == null)
                {
                    return;
                }

                _load = requester.LoadWithCorsAsync(url, CrossOrigin.ToEnum(CorsSetting.None), Owner.Origin, OriginBehavior.Taint);

                if (_parserInserted && !IsAsync)
                {
                    if (IsDeferred)
                    {
                        Owner.AddScript(this);
                    }

                    _load.ContinueWith(task => _readyToBeExecuted = true);
                }
                else if (!IsAsync && !_forceAsync)
                {
                    //The element must be added to the end of the list of scripts that will execute in order as soon as possible associated
                    //with the Document of the script element at the time the prepare a script algorithm started.
                    Owner.AddScript(this);
                }
                else
                {
                    //The element must be added to the set of scripts that will execute as soon as possible of the Document of the
                    //script element at the time the prepare a script algorithm started.
                    Owner.AddScript(this);
                }
            }
            else if (_parserInserted)
            {
                //and either the parser that created the script is an XML parser or it's an HTML parser whose script nesting level is
                //not greater than one, and the Document of the HTML parser or XML parser that created the script element has a style
                //sheet that is blocking scripts
                _readyToBeExecuted = true;
                //TODO do not run immediately, i.e. remove the following line
                options.RunScript(Text, CreateOptions(), ScriptLanguage);
            }
            else
            {
                options.RunScript(Text, CreateOptions(), ScriptLanguage);
            }
        }