public async Task DocumentWriteConsecutiveWithCustomScriptEngine()
        {
            var scripting = new CallbackScriptEngine(options =>
            {
                options.Document.Write("foo");
                options.Document.Write("bar");
            });
            var config = Configuration.Default.WithScripts(scripting).WithMockRequester();
            var source = "<title>Some title</title><body><script type='c-sharp' src='foo.cs'></script>";
            var document = await BrowsingContext.New(config).OpenAsync(m => m.Content(source).Address("http://www.example.com"));

            Assert.AreEqual("foobar", document.Body.TextContent);
        }
Beispiel #2
0
        public void DocumentWriteDynamicallyWithCustomScriptEngineAndText()
        {
            var scripting = new CallbackScriptEngine(options => options.Document.Write("<b>Dynamically written</b>"));
            var config = Configuration.Default.WithScripts(scripting);
            var source = "<title>Some title</title><body><script type='c-sharp'>//...</script>";
            var document = source.ToHtmlDocument(config);
            var bold = document.QuerySelector("b");

            Assert.IsNotNull(document);
            Assert.IsNotNull(document.Body.TextContent);
            Assert.AreEqual("//...Dynamically written", document.Body.TextContent);
            Assert.AreEqual(1, document.QuerySelectorAll("b").Length);
            Assert.AreEqual("Dynamically written", bold.TextContent);
        }
Beispiel #3
0
        public void ChangeTitleDynamicallyWithCustomScriptEngineScriptElementInjectedLater()
        {
            var expectedTitle = "Other title";
            var scripting = new CallbackScriptEngine(options => options.Document.Title = expectedTitle);
            var config = Configuration.Default.WithScripts(scripting);
            var source = "<title>Original title</title>";
            var document = source.ToHtmlDocument(config);

            var script = document.CreateElement("script");
            script.SetAttribute("type", "c-sharp");
            script.TextContent = "// ...";
            document.Body.AppendChild(script);

            Assert.AreEqual(expectedTitle, document.Title);
        }
Beispiel #4
0
        public async Task DocumentWriteDynamicallyWithCustomScriptEngineAndSource()
        {
            var baseAddress = "http://www.example.com";
            var filename = "foo.cs";
            var hasFoo = false;
            var scripting = new CallbackScriptEngine(options => options.Document.Write("<b>Dynamically written</b>"));
            var config = Configuration.Default.WithScripts(scripting).WithMockRequester(request => hasFoo = request.Address.Href == baseAddress + "/" + filename);
            var source = "<title>Some title</title><body><script type='c-sharp' src='" + filename + "'></script>";
            var document = await BrowsingContext.New(config).OpenAsync(m => m.Content(source).Address(baseAddress));
            var bold = document.QuerySelector("b");

            Assert.IsTrue(hasFoo);
            Assert.AreEqual("Dynamically written", document.Body.TextContent);
            Assert.AreEqual(1, document.QuerySelectorAll("b").Length);
            Assert.AreEqual("Dynamically written", bold.TextContent);
        }
 public async Task ProxyShouldBeAvailableDuringLoading()
 {
     var windowIsNotNull = false;
     var scripting = new CallbackScriptEngine(options => windowIsNotNull = options.Context.Proxy != null);
     var config = Configuration.Default.WithScripts(scripting).WithMockRequester();
     var source = "<title>Some title</title><body><script type='c-sharp' src='foo.cs'></script>";
     var document = await BrowsingContext.New(config).OpenAsync(m => m.Content(source).Address("http://www.example.com"));
     Assert.IsTrue(windowIsNotNull);
 }
Beispiel #6
0
        public async Task DynamicallyAddedScriptWithSourceShouldBeExecutedAfterAppending()
        {
            var didRun = false;
            var scripting = new CallbackScriptEngine(options => didRun = true);
            var config = Configuration.Default.WithScripts(scripting).WithMockRequester();
            var source = "<title>Some title</title><body>";
            var document = await BrowsingContext.New(config).OpenAsync(m => m.Content(source).Address("http://www.example.com"));

            var script = document.CreateElement("script");
            script.SetAttribute("type", scripting.Type);
            script.SetAttribute("src", "foo.cs");

            Assert.IsFalse(didRun);

            document.Body.AppendChild(script);

            Assert.IsTrue(didRun);
        }
Beispiel #7
0
        public async Task JavaScriptWithIntegrityButNoCorsShouldNotBeChecked()
        {
            var hasBeenChecked = false;
            var hasBeenParsed = false;
            var scripting = new CallbackScriptEngine(_ =>
            {
                hasBeenParsed = true;
            }, Network.MimeTypeNames.DefaultJavaScript);
            var provider = new MockIntegrityProvider((raw, integrity) =>
            {
                hasBeenChecked = true;
                return false;
            });
            var config = Configuration.Default.WithScripts(scripting).With(provider).WithMockRequester();
            var content = @"<body>
<script src=""https://code.jquery.com/jquery-2.2.4.js"" integrity=""sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI=""></script>
</body>";
            var document = await BrowsingContext.New(config).OpenAsync(res => res.Content(content).Address("http://localhost"));

            Assert.IsFalse(hasBeenChecked);
            Assert.IsTrue(hasBeenParsed);
        }
Beispiel #8
0
        public async Task GetDownloadsOfExampleDocumentWithCssAndJsShouldYieldAllResources()
        {
            var scripting = new CallbackScriptEngine(_ => { }, AngleSharp.Network.MimeTypeNames.DefaultJavaScript);
            var config = Configuration.Default.WithCss().WithScripts(scripting).WithDefaultLoader(setup => setup.IsResourceLoadingEnabled = true);
            var content = @"<link rel=stylesheet type=text/css href=bootstraph.css>
<link rel=stylesheet type=text/css href=fontawesome.css>
<link rel=stylesheet type=text/css href=style.css>
<body>
    <img src=foo.png>
    <iframe src=foo.html></iframe>
    <script>alert('Hello World!');</script>
    <script src=test.js></script>
</body>";
            var document = await BrowsingContext.New(config).OpenAsync(res => res.Content(content).Address("http://localhost"));
            var downloads = document.GetDownloads().ToArray();

            Assert.AreEqual(6, downloads.Length);

            foreach (var download in downloads)
            {
                Assert.IsTrue(download.IsCompleted);
                Assert.IsNotNull(download.Originator);
            }

            Assert.AreEqual(document.QuerySelectorAll("link").Skip(0).First(), downloads[0].Originator);
            Assert.AreEqual(document.QuerySelectorAll("link").Skip(1).First(), downloads[1].Originator);
            Assert.AreEqual(document.QuerySelectorAll("link").Skip(2).First(), downloads[2].Originator);
            Assert.AreEqual(document.QuerySelector("img"), downloads[3].Originator);
            Assert.AreEqual(document.QuerySelector("iframe"), downloads[4].Originator);
            Assert.AreEqual(document.QuerySelectorAll("script").Skip(1).First(), downloads[5].Originator);
        }
Beispiel #9
-2
        public async Task DocumentWriteDynamicallyWithCustomScriptEngineAndSourceNested()
        {
            var index = 0;
            var content = new[]
            {
                "<script type='c-sharp' src='foo2.cs'></script>",
                "<b>Dynamically written</b>"
            };
            var scripting = new CallbackScriptEngine(options => options.Document.Write(content[index++]));
            var config = Configuration.Default.WithScripts(scripting).WithMockRequester();
            var source = "<title>Some title</title><body><script type='c-sharp' src='foo.cs'></script>";
            var document = await BrowsingContext.New(config).OpenAsync(m => m.Content(source).Address("http://www.example.com"));
            var bold = document.QuerySelector("b");

            Assert.AreEqual("Dynamically written", document.Body.TextContent);
            Assert.AreEqual(1, document.QuerySelectorAll("b").Length);
            Assert.AreEqual("Dynamically written", bold.TextContent);
            Assert.AreEqual(2, index);
        }