Exemple #1
0
        public void MonikerRangeTestNotClosed()
        {
            //arange
            var source1 = @"::: moniker range=""start""";
            var source2 = @"::: moniker range=""start""
::: moniker-end";

            // assert
            var expected = @"<div range=""start"">
</div>
";
            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                TestUtility.AssertEqual(expected, source2, TestUtility.MarkupWithoutSourceInfo);

                Assert.Empty(listener.Items);

                TestUtility.AssertEqual(expected, source1, TestUtility.MarkupWithoutSourceInfo);
            }
            Logger.UnregisterListener(listener);

            Assert.Single(listener.Items);
            Assert.Equal("No \"::: moniker-end\" found for \"start\", MonikerRange does not end explictly.", listener.Items[0].Message);
        }
Exemple #2
0
        public void PermitsNestedBlocks()
        {
            var source = @"::: zone target=""chromeless""
* foo
* bar
* baz
::: zone-end
";

            var expected = @"<div class=""zone has-target"" data-target=""chromeless"">
<ul>
<li>foo</li>
<li>bar</li>
<li>baz</li>
</ul>
</div>
";
            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                TestUtility.AssertEqual(expected, source, TestUtility.MarkupWithoutSourceInfo);
            }
            Logger.UnregisterListener(listener);

            Assert.Empty(listener.Items);
        }
Exemple #3
0
        public void TripleColonWithInMonikerTestBlockUnClosed()
        {
            var source = new StringBuilder()
                         .AppendLine("::: moniker range=\"chromeless\"")
                         .AppendLine("::: zone target=\"docs\"")
                         .AppendLine("## Alt text")
                         .AppendLine("::: moniker-end")
                         .ToString();

            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                var service  = TestUtility.CreateMarkdownService();
                var document = service.Parse(source, "fakepath.md");

                var monikerBlock = document.OfType <MonikerRangeBlock>().FirstOrDefault();
                Assert.NotNull(monikerBlock);
                var nestedZoneBlock = monikerBlock.OfType <TripleColonBlock>().FirstOrDefault();
                Assert.NotNull(nestedZoneBlock);
                Assert.False(nestedZoneBlock.Closed);
            }
            Logger.UnregisterListener(listener);
        }
Exemple #4
0
        private string Markup(string content, IMarkdownObjectRewriter rewriter, TestLoggerListener listener = null)
        {
            var pipelineBuilder  = new MarkdownPipelineBuilder();
            var documentRewriter = new MarkdownDocumentVisitor(rewriter);

            pipelineBuilder.DocumentProcessed += document =>
            {
                ValidationExtension.SetSchemaName(document);
                documentRewriter.Visit(document);
            };
            var pipeline = pipelineBuilder.Build();

            if (listener != null)
            {
                Logger.RegisterListener(listener);
            }

            var html = Markdown.ToHtml(content, pipeline);

            if (listener != null)
            {
                Logger.UnregisterListener(listener);
            }

            return(html);
        }
Exemple #5
0
        public void TripleColonTestSelfClosing()
        {
            var source = @"::: zone target=""chromeless""
::: form action=""create-resource"" submitText=""Create"" :::
::: zone-end
";

            var expected = @"<div class=""zone has-target"" data-target=""chromeless"">
<form class=""chromeless-form"" data-action=""create-resource"">
<div></div>
<button class=""button is-primary"" disabled=""disabled"" type=""submit"">Create</button>
</form>
</div>
".Replace("\r\n", "\n");

            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                TestUtility.AssertEqual(expected, source, TestUtility.MarkupWithoutSourceInfo);
            }
            Logger.UnregisterListener(listener);

            // Listener should have no error or warning message.
            Assert.Empty(listener.Items);
        }
Exemple #6
0
        public void MissingEndTag()
        {
            //arange
            var source1 = @"::: zone target=""chromeless""";
            var source2 = @"::: zone target=""chromeless""
::: zone-end";

            // assert
            var expected = @"<div data-zone=""chromeless"">
</div>
";
            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                TestUtility.AssertEqual(expected, source2, TestUtility.MarkupWithoutSourceInfo);

                Assert.Empty(listener.Items);

                TestUtility.AssertEqual(expected, source1, TestUtility.MarkupWithoutSourceInfo);
            }
            Logger.UnregisterListener(listener);

            Assert.Single(listener.Items);
            Assert.Equal("Invalid zone on line 0. No \"::: zone-end\" found. Blocks should be explicitly closed.", listener.Items[0].Message);
        }
Exemple #7
0
        public void ComplexImageTestBlockGeneral()
        {
            var source = @"
:::image type=""icon"" source=""example.svg"":::

:::image type=""complex"" source=""example.jpg"" alt-text=""example""::: 
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
:::image-end:::

:::image source=""example.jpg"" alt-text=""example"":::
";

            var expected = @"<img role=""presentation"" src=""example.svg"">
<img alt=""example"" aria-describedby=""a00f6"" src=""example.jpg"">
<div id=""a00f6"" class=""visually-hidden"">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
<img alt=""example"" src=""example.jpg"">
".Replace("\r\n", "\n");

            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                TestUtility.AssertEqual(expected, source, TestUtility.MarkupWithoutSourceInfo);
            }
            Logger.UnregisterListener(listener);
        }
Exemple #8
0
        public void TestHtmlBlockTagNotInRelationValidation()
        {
            var content = @"
<div class='a'>
    <i>x</i>
    <EM>y</EM>
    <h1>
        z
        <pre><code>
            a*b*c
        </code></pre>
    </h1>
</div>
<script>alert(1);</script>";

            var builder = MarkdownValidatorBuilder.Create(null, null);

            builder.AddTagValidators(new[]
            {
                new MarkdownTagValidationRule
                {
                    TagNames = new List <string> {
                        "h1", "code", "pre", "div"
                    },
                    MessageFormatter = "Invalid tag({0})!",
                    Behavior         = TagValidationBehavior.Error,
                    OpeningTagOnly   = true,
                    Relation         = TagRelation.NotIn
                }
            });

            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(MarkdownValidatePhaseName);

            using (new LoggerPhaseScope(MarkdownValidatePhaseName))
            {
                var html = Markup(content, builder.CreateRewriter(DefaultContext), listener);

                Assert.Equal(@"<div class='a'>
    <i>x</i>
    <EM>y</EM>
    <h1>
        z
        <pre><code>
            a*b*c
        </code></pre>
    </h1>
</div>
<script>alert(1);</script>
".Replace("\r\n", "\n"), html);
            }
            Assert.Equal(3, listener.Items.Count);
            Assert.Equal(new[]
            {
                "Invalid tag(i)!",
                "Invalid tag(EM)!",
                "Invalid tag(script)!"
            }, from item in listener.Items select item.Message);
        }
Exemple #9
0
        public void TestBlockLevelInclusion_CycleInclude()
        {
            var root = @"
# Hello World

Test Include File

[!include[refa](a.md)]

";

            var refa = @"
# Hello Include File A

This is a file A included by another file.

[!include[refb](b.md)]

";

            var refb = @"
# Hello Include File B

[!include[refa](a.md)]
";

            TestUtility.WriteToFile("r/root.md", root);
            TestUtility.WriteToFile("r/a.md", refa);
            TestUtility.WriteToFile("r/b.md", refb);

            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter("CircularReferenceTest");

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope("CircularReferenceTest"))
            {
                var result   = TestUtility.MarkupWithoutSourceInfo(root, "r/root.md");
                var expected = @"<h1 id=""hello-world"">Hello World</h1>
<p>Test Include File</p>
<h1 id=""hello-include-file-a"">Hello Include File A</h1>
<p>This is a file A included by another file.</p>
<h1 id=""hello-include-file-b"">Hello Include File B</h1>
[!include[refa](a.md)]";

                Assert.Equal(expected.Replace("\r\n", "\n"), result.Html);
            }
            Logger.UnregisterListener(listener);
            Assert.Collection(listener.Items, log => Assert.Equal(
                                  "Build has identified file(s) referencing each other: 'r/root.md' --> '~/r/a.md' --> '~/r/b.md' --> '~/r/a.md'",
                                  log.Message));
        }
Exemple #10
0
        public void ChromelessFormsAttributeValueRequired()
        {
            var content  = @"::: form submitText :::";
            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                TestUtility.MarkupWithoutSourceInfo(content);
            }
            Logger.UnregisterListener(listener);

            // Listener should have an error message and not output.
            Assert.NotEmpty(listener.Items.Where(x => x.Code == "invalid-form"));
        }
Exemple #11
0
        public void ImageWithIconTypeTestBlockGeneral()
        {
            var source = @":::image type=""icon"" source=""example.svg"":::";

            var expected = @"<img role=""presentation"" src=""example.svg"">
".Replace("\r\n", "\n");

            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                TestUtility.AssertEqual(expected, source, TestUtility.MarkupWithoutSourceInfo);
            }
            Logger.UnregisterListener(listener);
        }
Exemple #12
0
        public void TripleColonTestBlockClosed()
        {
            var source = @"::: zone target=""chromeless""
::: form action=""create-resource"" submitText=""Create"" :::
::: zone-end
";

            var expected = @"<div class=""zone has-target"" data-target=""chromeless"">
<form class=""chromeless-form"" data-action=""create-resource"">
<div></div>
<button class=""button is-primary"" disabled=""disabled"" type=""submit"">Create</button>
</form>
</div>
".Replace("\r\n", "\n");

            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                var service  = TestUtility.CreateMarkdownService();
                var document = service.Parse(source, "fakepath.md");
                var blocks   = new List <TripleColonBlock>();
                var stack    = new Stack <ContainerBlock>();
                stack.Push(document);

                // Get all triplecolon blocks in the document using a depth-first iterative tree traversal strategy.
                do
                {
                    var block    = stack.Pop();
                    var children = block.Where(x => x.GetType() == typeof(TripleColonBlock)).Select(x => x as TripleColonBlock);
                    blocks.AddRange(children);
                    foreach (var child in children)
                    {
                        stack.Push(child);
                    }
                } while (stack.Count > 0);

                foreach (var block in blocks)
                {
                    Assert.True(block.Closed);
                    Assert.False(block.IsOpen);
                }
            }
            Logger.UnregisterListener(listener);
        }
Exemple #13
0
        public void PivotCommaDelimited()
        {
            //arange
            var source = @"::: zone pivot = ""a,b""
::: zone-end";

            // assert
            var expected = "<div data-pivot=\"a b\">\n</div>\n";
            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                TestUtility.AssertEqual(expected, source, TestUtility.MarkupWithoutSourceInfo);
            }
            Logger.UnregisterListener(listener);

            Assert.Empty(listener.Items);
        }
Exemple #14
0
        public void ImageBlockTestBlockClosed()
        {
            var source = @":::image source=""example.jpg"" type=""complex"" alt-text=""example"":::Lorem Ipsum
:::image-end:::";

            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                var service  = TestUtility.CreateMarkdownService();
                var document = service.Parse(source, "fakepath.md");

                var imageBlock = document.OfType <TripleColonBlock>().FirstOrDefault();
                Assert.NotNull(imageBlock);
                Assert.True(imageBlock.Closed);
            }
            Logger.UnregisterListener(listener);
        }
Exemple #15
0
        public void RenderZoneTestInvalid()
        {
            //arange
            var source = @"::: zone render=""chromeless";

            // assert
            var expected = @"<p>::: zone render=&quot;chromeless</p>
";
            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                TestUtility.AssertEqual(expected, source, TestUtility.MarkupWithoutSourceInfo);
            }
            Logger.UnregisterListener(listener);

            Assert.Single(listener.Items);
            Assert.Equal("Zone render does not have ending character (\").", listener.Items[0].Message);
        }
Exemple #16
0
        public void NoOverlap()
        {
            //arange
            var content = @"::: zone target=""chromeless""
::: moniker range=""start""
::: zone-end
::: moniker-end
";

            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                TestUtility.MarkupWithoutSourceInfo(content);
            }
            Logger.UnregisterListener(listener);

            Assert.Equal("Invalid zone on line 0.  A zone cannot end before blocks nested within it have ended.", listener.Items.First(x => x.Code == "invalid-zone").Message);
        }
Exemple #17
0
        public void PdfPivotInvalid()
        {
            //arange
            var source = @"::: zone target = ""pdf""  pivot = ""foo""  ";

            // assert
            var expected = @"<p>::: zone target = &quot;pdf&quot;  pivot = &quot;foo&quot;</p>
";
            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                TestUtility.AssertEqual(expected, source, TestUtility.MarkupWithoutSourceInfo);
            }
            Logger.UnregisterListener(listener);

            Assert.Single(listener.Items);
            Assert.Equal("Invalid zone on line 0. \"::: zone target = \"pdf\"  pivot = \"foo\"  \" is invalid. Pivot not permitted on pdf target.", listener.Items[0].Message);
        }
Exemple #18
0
        public void AttributeMissingClosingQuote()
        {
            //arange
            var source = @"::: zone target=""chromeless";

            // assert
            var expected = @"<p>::: zone target=&quot;chromeless</p>
";
            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                TestUtility.AssertEqual(expected, source, TestUtility.MarkupWithoutSourceInfo);
            }
            Logger.UnregisterListener(listener);

            Assert.Single(listener.Items);
            Assert.Equal("Invalid zone on line 0. \"::: zone target=\"chromeless\" is invalid. Invalid attribute \"target\". Values must be terminated with a double quote.", listener.Items[0].Message);
        }
Exemple #19
0
        public void MonikerRangeTestInvalid()
        {
            //arange
            var source = @"::: moniker range=""azure-rest-1.0";

            // assert
            var expected = @"<p>::: moniker range=&quot;azure-rest-1.0</p>
";
            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                TestUtility.AssertEqual(expected, source, TestUtility.MarkupWithoutSourceInfo);
            }
            Logger.UnregisterListener(listener);

            Assert.Single(listener.Items);
            Assert.Equal("MonikerRange does not have ending charactor (\").", listener.Items[0].Message);
        }
Exemple #20
0
        public void InvalidAttribute()
        {
            //arange
            var source = @"::: zone *=""pdf""";

            // assert
            var expected = @"<p>::: zone *=&quot;pdf&quot;</p>
";
            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                TestUtility.AssertEqual(expected, source, TestUtility.MarkupWithoutSourceInfo);
            }
            Logger.UnregisterListener(listener);

            Assert.Single(listener.Items);
            Assert.Equal("Invalid zone on line 0. \"::: zone *=\"pdf\"\" is invalid. Invalid attribute.", listener.Items[0].Message);
        }
Exemple #21
0
        public void DuplicateAttribute()
        {
            //arange
            var source = @"::: zone target=""pdf"" target=""docs""";

            // assert
            var expected = @"<p>::: zone target=&quot;pdf&quot; target=&quot;docs&quot;</p>
";
            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                TestUtility.AssertEqual(expected, source, TestUtility.MarkupWithoutSourceInfo);
            }
            Logger.UnregisterListener(listener);

            Assert.Single(listener.Items);
            Assert.Equal("Invalid zone on line 0. \"::: zone target=\"pdf\" target=\"docs\"\" is invalid. Attribute \"target\" specified multiple times.", listener.Items[0].Message);
        }
Exemple #22
0
        public void RenderZoneTestNotNested()
        {
            //arange
            var content = @"::: zone render=""chromeless""
::: zone render=""pdf""
::: zone-end
::: zone-end
";

            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                TestUtility.MarkupWithoutSourceInfo(content);
            }
            Logger.UnregisterListener(listener);

            Assert.Single(listener.Items);
            Assert.Equal("Zone render cannot be nested.", listener.Items[0].Message);
        }
Exemple #23
0
        public void NotNested()
        {
            //arange
            var content = @"::: zone target=""chromeless""
::: zone target=""pdf""
::: zone-end
::: zone-end
";

            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                TestUtility.MarkupWithoutSourceInfo(content);
            }
            Logger.UnregisterListener(listener);

            Assert.Single(listener.Items);
            Assert.Equal("Invalid zone on line 1. \"::: zone target=\"chromeless\"\r\n::: zone target=\"pdf\"\r\n::: zone-end\r\n::: zone-end\r\n\" is invalid. Zones cannot be nested.", listener.Items[0].Message);
        }
Exemple #24
0
        public void TextAfterEndTag()
        {
            //arange
            var source = @":::zone
:::zone-end asdjklf";

            // assert
            var expected = "<p>:::zone\n:::zone-end asdjklf</p>\n";

            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                TestUtility.AssertEqual(expected, source, TestUtility.MarkupWithoutSourceInfo);
            }
            Logger.UnregisterListener(listener);

            Assert.Single(listener.Items);
            Assert.Equal("Invalid zone on line 0. \":::zone\r\n:::zone-end asdjklf\" is invalid. Either target or privot must be specified.", listener.Items[0].Message);
        }
Exemple #25
0
        public void PivotInvalid2()
        {
            //arange
            var source = @"::: zone pivot = ""a b""
::: zone-end";

            // assert
            var expected = "<p>::: zone pivot = &quot;a b&quot;\n::: zone-end</p>\n";

            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                TestUtility.AssertEqual(expected, source, TestUtility.MarkupWithoutSourceInfo);
            }
            Logger.UnregisterListener(listener);

            Assert.Single(listener.Items);
            Assert.Equal("Invalid zone on line 0. \"::: zone pivot = \"a b\"\r\n::: zone-end\" is invalid. Invalid pivot \"a b\". Pivot must be a comma-delimited list of pivot names. Pivot names must be lower-case and contain only letters, numbers or dashes.", listener.Items[0].Message);
        }
Exemple #26
0
        public void ImageTestNotImageBlock()
        {
            var source = @":::row:::
:::column:::
    This is where your content goes.
:::column-end:::
:::row-end:::
";

            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(LoggerPhase);

            Logger.RegisterListener(listener);
            using (new LoggerPhaseScope(LoggerPhase))
            {
                var service  = TestUtility.CreateMarkdownService();
                var document = service.Parse(source, "fakepath.md");

                var imageBlock = document.OfType <TripleColonBlock>().FirstOrDefault();
                Assert.Null(imageBlock);
            }
            Logger.UnregisterListener(listener);
        }
Exemple #27
0
        public void TestHtmlBlockTagValidation()
        {
            var content = @"
<div class='a'>
    <i>x</i>
    <EM>y</EM>
    <h1>
        z
        <pre><code>
            a*b*c
        </code></pre>
    </h1>
</div>
<script>alert(1);</script>";

            var builder = MarkdownValidatorBuilder.Create(
                null,
                new CompositionContainer(
                    new ContainerConfiguration()
                    .WithAssembly(typeof(ValidationTest).Assembly)
                    .CreateContainer()));

            builder.AddTagValidators(new[]
            {
                new MarkdownTagValidationRule
                {
                    TagNames = new List <string> {
                        "em", "div"
                    },
                    MessageFormatter = "Invalid tag({0})!",
                    Behavior         = TagValidationBehavior.Error,
                    OpeningTagOnly   = true,
                },
                new MarkdownTagValidationRule
                {
                    TagNames = new List <string> {
                        "h1"
                    },
                    MessageFormatter = "Warning tag({0})!",
                    Behavior         = TagValidationBehavior.Warning,
                },
                new MarkdownTagValidationRule
                {
                    TagNames = new List <string> {
                        "script"
                    },
                    MessageFormatter = "Warning tag({0})!",
                    Behavior         = TagValidationBehavior.Warning,
                    OpeningTagOnly   = true
                },
                new MarkdownTagValidationRule
                {
                    TagNames = new List <string> {
                        "pre"
                    },
                    MessageFormatter = "Warning tag({0})!",
                    Behavior         = TagValidationBehavior.Warning,
                }
            });

            builder.AddValidators(new[]
            {
                new MarkdownValidationRule
                {
                    ContractName = HtmlMarkdownObjectValidatorProvider.ContractName,
                }
            });

            builder.LoadEnabledRulesProvider();
            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(MarkdownValidatePhaseName);

            using (new LoggerPhaseScope(MarkdownValidatePhaseName))
            {
                var html = Markup(content, builder.CreateRewriter(DefaultContext), listener);

                Assert.Equal(@"<div class='a'>
    <i>x</i>
    <EM>y</EM>
    <h1>
        z
        <pre><code>
            a*b*c
        </code></pre>
    </h1>
</div>
<script>alert(1);</script>
".Replace("\r\n", "\n"), html);
            }
            Assert.Equal(8, listener.Items.Count);
            Assert.Equal(new[]
            {
                "Html Tag!",
                "Invalid tag(div)!",
                "Invalid tag(EM)!",
                "Warning tag(h1)!",
                "Warning tag(pre)!",
                "Warning tag(pre)!",
                "Warning tag(h1)!",
                "Warning tag(script)!"
            }, from item in listener.Items select item.Message);
        }
Exemple #28
0
        public void TestHtmlInlineTagValidation()
        {
            var content = @"This is inline html: <div class='a'><i>x</i><EM>y</EM><h1>z<pre><code>a*b*c</code></pre></h1></div>

<script>alert(1);</script> end.";

            var builder = MarkdownValidatorBuilder.Create(
                null,
                new CompositionContainer(
                    new ContainerConfiguration()
                    .WithAssembly(typeof(ValidationTest).Assembly)
                    .CreateContainer()));

            builder.AddTagValidators(new[]
            {
                new MarkdownTagValidationRule
                {
                    TagNames = new List <string> {
                        "em", "div"
                    },
                    MessageFormatter = "Invalid tag({0})!",
                    Behavior         = TagValidationBehavior.Error,
                    OpeningTagOnly   = true,
                },
                new MarkdownTagValidationRule
                {
                    TagNames = new List <string> {
                        "h1"
                    },
                    MessageFormatter = "Warning tag({0})!",
                    Behavior         = TagValidationBehavior.Warning,
                },
                new MarkdownTagValidationRule
                {
                    TagNames = new List <string> {
                        "script"
                    },
                    MessageFormatter = "Warning tag({0})!",
                    Behavior         = TagValidationBehavior.Warning,
                    OpeningTagOnly   = true
                },
                new MarkdownTagValidationRule
                {
                    TagNames = new List <string> {
                        "pre"
                    },
                    MessageFormatter = "Warning tag({0})!",
                    Behavior         = TagValidationBehavior.Warning,
                },
            });

            var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(MarkdownValidatePhaseName);
            var html     = Markup(content, builder.CreateRewriter(), listener);

            Assert.Equal(@"<p>This is inline html: <div class='a'><i>x</i><EM>y</EM><h1>z<pre><code>a<em>b</em>c</code></pre></h1></div></p>
<script>alert(1);</script> end.
".Replace("\r\n", "\n"), html);
            Assert.Equal(7, listener.Items.Count);
            Assert.Equal(new[]
            {
                "Invalid tag(div)!",
                "Invalid tag(EM)!",
                "Warning tag(h1)!",
                "Warning tag(pre)!",
                "Warning tag(pre)!",
                "Warning tag(h1)!",
                "Warning tag(script)!"
            }, from item in listener.Items select item.Message);
        }