Exemple #1
0
        public void NoReplacementReturnsSameDocument()
        {
            // Given
            string input = @"<html>
                    <head>
                        <title>Foobar</title>
                    </head>
                    <body>
                        <h1>Title</h1>
                        <p>This is some Foobar text</p>
                    </body>
                </html>";
            IDocument document = Substitute.For<IDocument>();
            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(input));
            document.GetStream().Returns(stream);
            IExecutionContext context = Substitute.For<IExecutionContext>();
            Dictionary<string, string> convertedLinks;
            context.TryConvert(new object(), out convertedLinks)
                .ReturnsForAnyArgs(x =>
                {
                    x[1] = x[0];
                    return true;
                });
            AutoLink autoLink = new AutoLink(new Dictionary<string, string>()
            {
                { "Foobaz", "http://www.google.com" }
            });

            // When
            autoLink.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

            // Then
            document.DidNotReceiveWithAnyArgs().Clone((string)null);
            stream.Dispose();
        }
Exemple #2
0
            public void AddsMultipleLinksWhenFirstIsSubstring()
            {
                // Given
                string       input    = @"<html>
                        <head>
                            <title>Foobar</title>
                        </head>
                        <body>
                            <h1>Title</h1>
                            <p>This is some <i>Foobar</i> text Foobaz</p>
                        </body>
                    </html>";
                string       output   = @"<html><head>
                            <title>Foobar</title>
                        </head>
                        <body>
                            <h1>Title</h1>
                            <p>This is some <i><a href=""http://www.google.com"">Foo</a>bar</i> text <a href=""http://www.bing.com"">Foobaz</a></p>
                        
                    </body></html>";
                IDocument    document = Substitute.For <IDocument>();
                MemoryStream stream   = new MemoryStream(Encoding.UTF8.GetBytes(input));

                document.GetStream().Returns(stream);
                IExecutionContext           context = Substitute.For <IExecutionContext>();
                Dictionary <string, string> convertedLinks;

                context.TryConvert(new object(), out convertedLinks)
                .ReturnsForAnyArgs(x =>
                {
                    x[1] = x[0];
                    return(true);
                });
                AutoLink autoLink = new AutoLink(new Dictionary <string, string>()
                {
                    { "Foo", "http://www.google.com" },
                    { "Foobaz", "http://www.bing.com" }
                });

                // When
                autoLink.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                context.Received(1).GetDocument(Arg.Any <IDocument>(), Arg.Any <string>());
                context.Received().GetDocument(document, output.Replace("\r\n", "\n"));
                stream.Dispose();
            }
        public void DoesNotReplaceInAttributes()
        {
            // Given
            string       input    = @"<html>
                    <head>
                        <title>Foobar</title>
                    </head>
                    <body>
                        <h1 title=""Foobar"">Title</h1>
                        <p attr=""Foobar"">This is some Foobar <b ref=""Foobar"">text</b></p>
                    </body>
                </html>";
            string       output   = @"<html><head>
                        <title>Foobar</title>
                    </head>
                    <body>
                        <h1 title=""Foobar"">Title</h1>
                        <p attr=""Foobar"">This is some <a href=""http://www.google.com"">Foobar</a> <b ref=""Foobar"">text</b></p>
                    
                </body></html>".Replace("\r\n", "\n");
            IDocument    document = Substitute.For <IDocument>();
            MemoryStream stream   = new MemoryStream(Encoding.UTF8.GetBytes(input));

            document.GetStream().Returns(stream);
            IExecutionContext           context = Substitute.For <IExecutionContext>();
            Dictionary <string, string> convertedLinks;

            context.TryConvert(new object(), out convertedLinks)
            .ReturnsForAnyArgs(x =>
            {
                x[1] = x[0];
                return(true);
            });
            AutoLink autoLink = new AutoLink(new Dictionary <string, string>()
            {
                { "Foobar", "http://www.google.com" }
            });

            // When
            autoLink.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

            // Then
            document.Received(1).Clone(Arg.Any <string>());
            document.Received().Clone(output);
            stream.Dispose();
        }
Exemple #4
0
            public void AddLinkMethodTakesPrecedence()
            {
                // Given
                string input = @"<html>
                        <head>
                            <title>Foobar</title>
                        </head>
                        <body>
                            <h1>Title</h1>
                            <p>This is some <i>Foobar</i> text Foobaz</p>
                        </body>
                    </html>";
                string output = @"<html><head>
                            <title>Foobar</title>
                        </head>
                        <body>
                            <h1>Title</h1>
                            <p>This is some <i><a href=""http://www.google.com"">Foobar</a></i> text <a href=""http://www.yahoo.com"">Foobaz</a></p>

                    </body></html>";
                IDocument document = Substitute.For<IDocument>();
                MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(input));
                document.GetStream().Returns(stream);
                IExecutionContext context = Substitute.For<IExecutionContext>();
                Dictionary<string, string> convertedLinks;
                context.TryConvert(new object(), out convertedLinks)
                    .ReturnsForAnyArgs(x =>
                    {
                        x[1] = x[0];
                        return true;
                    });
                AutoLink autoLink = new AutoLink(new Dictionary<string, string>()
                {
                    { "Foobar", "http://www.google.com" },
                    { "Foobaz", "http://www.bing.com" }
                }).WithLink("Foobaz", "http://www.yahoo.com");

                // When
                autoLink.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                context.Received(1).GetDocument(Arg.Any<IDocument>(), Arg.Any<string>());
                context.Received().GetDocument(document, output.Replace("\r\n", "\n"));
                stream.Dispose();
            }
            public void NoReplacementWithQuerySelectorReturnsSameDocument()
            {
                // Given
                string inputContent    = $"<div>@x.Select(x => x) <code>Foo bar</code></div>";
                string expectedContent = $"<div>@x.Select(x => x) <code>Foo bar</code></div>";
                TestExecutionContext        context  = new TestExecutionContext();
                TestDocument                document = new TestDocument(inputContent);
                Dictionary <string, string> links    = new Dictionary <string, string>();
                AutoLink autoLink = new AutoLink(links)
                                    .WithQuerySelector("code")
                                    .WithMatchOnlyWholeWord()
                                    .WithStartWordSeparators('<')
                                    .WithEndWordSeparators('>');

                // When
                IList <IDocument> results = autoLink.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                results.Single().ShouldBeSameAs(document);
            }
Exemple #6
0
            public void NonWholeWords()
            {
                // Given
                string       input    = @"<html>
                        <head>
                            <title>Foobar</title>
                        </head>
                        <body>
                            <h1>Title</h1>
                            <p>abc Foo(baz) xyz</p>
                        </body>
                    </html>";
                IDocument    document = Substitute.For <IDocument>();
                MemoryStream stream   = new MemoryStream(Encoding.UTF8.GetBytes(input));

                document.GetStream().Returns(stream);
                IExecutionContext           context = Substitute.For <IExecutionContext>();
                Dictionary <string, string> convertedLinks;

                context.TryConvert(new object(), out convertedLinks)
                .ReturnsForAnyArgs(x =>
                {
                    x[1] = x[0];
                    return(true);
                });
                AutoLink autoLink = new AutoLink(new Dictionary <string, string>()
                {
                    { "Foo", "http://www.google.com" },
                    { "baz", "http://www.yahoo.com" },
                }).WithMatchOnlyWholeWord();

                // When
                autoLink.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                context.DidNotReceiveWithAnyArgs().GetDocument((IDocument)null, (string)null);
                stream.Dispose();
            }
        public void NoReplacementReturnsSameDocument()
        {
            // Given
            string       input    = @"<html>
                    <head>
                        <title>Foobar</title>
                    </head>
                    <body>
                        <h1>Title</h1>
                        <p>This is some Foobar text</p>
                    </body>
                </html>";
            IDocument    document = Substitute.For <IDocument>();
            MemoryStream stream   = new MemoryStream(Encoding.UTF8.GetBytes(input));

            document.GetStream().Returns(stream);
            IExecutionContext           context = Substitute.For <IExecutionContext>();
            Dictionary <string, string> convertedLinks;

            context.TryConvert(new object(), out convertedLinks)
            .ReturnsForAnyArgs(x =>
            {
                x[1] = x[0];
                return(true);
            });
            AutoLink autoLink = new AutoLink(new Dictionary <string, string>()
            {
                { "Foobaz", "http://www.google.com" }
            });

            // When
            autoLink.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

            // Then
            document.DidNotReceiveWithAnyArgs().Clone(null);
            stream.Dispose();
        }
Exemple #8
0
            public void AddLinksToGenericWordsInsideAngleBrackets(string input, string expected)
            {
                // Given
                string inputContent    = $"<html><head></head><body><foo></foo><ul>{input}</ul></body></html>";
                string expectedContent = $"<html><head></head><body><foo></foo><ul>{expected}</ul></body></html>";
                TestExecutionContext        context  = new TestExecutionContext();
                TestDocument                document = new TestDocument(inputContent);
                Dictionary <string, string> links    = new Dictionary <string, string>()
                {
                    { "Foo&lt;T&gt;", "http://www.fooOfT.com" },
                    { "Foo", "http://www.foo.com" },
                };
                AutoLink autoLink = new AutoLink(links)
                                    .WithQuerySelector("code")
                                    .WithMatchOnlyWholeWord()
                                    .WithStartWordSeparators('<')
                                    .WithEndWordSeparators('>');

                // When
                IList <IDocument> results = autoLink.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                Assert.That(results.Select(x => x.Content), Is.EquivalentTo(new[] { expectedContent }));
            }
Exemple #9
0
            public void AddsMultipleLinksInDifferentElements()
            {
                // Given
                const string         input    = @"<html>
                        <head>
                            <title>Foobar</title>
                        </head>
                        <body>
                            <h1>Foobaz</h1>
                            <p>This is some <i>Foobar</i> text Foobaz</p>
                            <p>Another Foobaz paragraph</p>
                        </body>
                    </html>";
                const string         output   = @"<html><head>
                            <title>Foobar</title>
                        </head>
                        <body>
                            <h1>Foobaz</h1>
                            <p>This is some <i><a href=""http://www.google.com"">Foobar</a></i> text <a href=""http://www.bing.com"">Foobaz</a></p>
                            <p>Another <a href=""http://www.bing.com"">Foobaz</a> paragraph</p>
                        
                    </body></html>";
                TestExecutionContext context  = new TestExecutionContext();
                TestDocument         document = new TestDocument(input);
                AutoLink             autoLink = new AutoLink(new Dictionary <string, string>()
                {
                    { "Foobar", "http://www.google.com" },
                    { "Foobaz", "http://www.bing.com" }
                });

                // When
                IList <IDocument> results = autoLink.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                Assert.That(results.Select(x => x.Content), Is.EquivalentTo(new[] { output.Replace("\r\n", "\n") }));
            }
            public void AddsMultipleLinksInDifferentElements()
            {
                // Given
                const string         input    = @"<html>
                        <head>
                            <title>Foobar</title>
                        </head>
                        <body>
                            <h1>Foobaz</h1>
                            <p>This is some <i>Foobar</i> text Foobaz</p>
                            <p>Another Foobaz paragraph</p>
                        </body>
                    </html>";
                const string         output   = @"<html><head>
                            <title>Foobar</title>
                        </head>
                        <body>
                            <h1>Foobaz</h1>
                            <p>This is some <i><a href=""http://www.google.com"">Foobar</a></i> text <a href=""http://www.bing.com"">Foobaz</a></p>
                            <p>Another <a href=""http://www.bing.com"">Foobaz</a> paragraph</p>
                        
                    </body></html>";
                TestExecutionContext context  = new TestExecutionContext();
                TestDocument         document = new TestDocument(input);
                AutoLink             autoLink = new AutoLink(new Dictionary <string, string>()
                {
                    { "Foobar", "http://www.google.com" },
                    { "Foobaz", "http://www.bing.com" }
                });

                // When
                IList <IDocument> results = autoLink.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                results.Single().Content.ShouldBe(output, StringCompareShould.IgnoreLineEndings);
            }
Exemple #11
0
 public void AutoLinkTest()
 {
     var sanitizer = new HtmlSanitizer();
     var autolink = new AutoLink();
     sanitizer.PostProcessNode += (s, e) =>
     {
         var text = e.Node as IDomText;
         if (text != null)
         {
             var autolinked = autolink.Link(text.NodeValue);
             if (autolinked != text.NodeValue)
                 foreach (var node in CQ.Create(autolinked))
                     e.ReplacementNodes.Add(node);
         }
     };
     var html = @"<div>Click here: http://example.com/.</div>";
     Assert.That(sanitizer.Sanitize(html), Is.EqualTo(@"<div>Click here: <a href=""http://example.com/"">http://example.com/</a>.</div>").IgnoreCase);
     Assert.That(sanitizer.Sanitize("Check out www.google.com."), Is.EqualTo(@"Check out <a href=""http://www.google.com"">www.google.com</a>.").IgnoreCase);
 }
Exemple #12
0
        public void DoesNotReplaceInAttributes()
        {
            // Given
            string input = @"<html>
                    <head>
                        <title>Foobar</title>
                    </head>
                    <body>
                        <h1 title=""Foobar"">Title</h1>
                        <p attr=""Foobar"">This is some Foobar <b ref=""Foobar"">text</b></p>
                    </body>
                </html>";
            string output = @"<html><head>
                        <title>Foobar</title>
                    </head>
                    <body>
                        <h1 title=""Foobar"">Title</h1>
                        <p attr=""Foobar"">This is some <a href=""http://www.google.com"">Foobar</a> <b ref=""Foobar"">text</b></p>
                    
                </body></html>".Replace("\r\n", "\n");
            IDocument document = Substitute.For<IDocument>();
            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(input));
            document.GetStream().Returns(stream);
            IExecutionContext context = Substitute.For<IExecutionContext>();
            Dictionary<string, string> convertedLinks;
            context.TryConvert(new object(), out convertedLinks)
                .ReturnsForAnyArgs(x =>
                {
                    x[1] = x[0];
                    return true;
                });
            AutoLink autoLink = new AutoLink(new Dictionary<string, string>()
            {
                { "Foobar", "http://www.google.com" }
            });

            // When
            autoLink.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

            // Then
            document.Received(1).Clone(Arg.Any<string>());
            document.Received().Clone(output);
            stream.Dispose();
        }