public void Format_HtmlContent_ReturnsMarkup()
        {
            var input = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("div")),
                MarkupGrammar.TokenAttribute(new DataName("class")),
                MarkupGrammar.TokenPrimitive("content"),
                MarkupGrammar.TokenElementBegin(new DataName("p")),
                MarkupGrammar.TokenAttribute(new DataName("style")),
                MarkupGrammar.TokenPrimitive("color:red"),
                MarkupGrammar.TokenElementBegin(new DataName("strong")),
                MarkupGrammar.TokenPrimitive("Lorem ipsum"),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenPrimitive(" dolor sit amet, "),
                MarkupGrammar.TokenElementBegin(new DataName("i")),
                MarkupGrammar.TokenPrimitive("consectetur"),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenPrimitive(" adipiscing elit."),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
            };
            const string expected = @"<div class=""content""><p style=""color:red""><strong>Lorem ipsum</strong> dolor sit amet, <i>consectetur</i> adipiscing elit.</p></div>";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_CodeCommentAroundMarkup_ReturnsSingleUnparsedBlock()
        {
            var input = new[]
            {
                MarkupGrammar.TokenUnparsed("%--", "--%",
                                            @"
<html>
	<body style=""color:lime"">
		<!-- not much to say here -->
	</body>
</html>
")
            };
            const string expected =
                @"<%--
<html>
	<body style=""color:lime"">
		<!-- not much to say here -->
	</body>
</html>
--%>";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_EmptyInput_ReturnsEmptySequence()
        {
            var          input    = new Token <MarkupTokenType> [0];
            const string expected = "";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_UnclosedOpenTag_ReturnsMarkup()
        {
            var input = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("root"))
            };
            const string expected = @"<root>";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_MixedEntities_ReturnsMarkup()
        {
            var input = new[]
            {
                MarkupGrammar.TokenPrimitive(@"there should <b>e decoded chars & inside this text")
            };
            const string expected = @"there should &lt;b&gt;e decoded chars &amp; inside this text";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_XmlDeclaration_ReturnsUnparsed()
        {
            var input = new[]
            {
                MarkupGrammar.TokenUnparsed("?", "?", @"xml version=""1.0""")
            };
            const string expected = @"<?xml version=""1.0""?>";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_MarkupLikeText_ReturnsTextValue()
        {
            var input = new[]
            {
                MarkupGrammar.TokenPrimitive(@"value>""0"" && value<""10"" ?""valid"":""error""")
            };
            const string expected = @"value&gt;""0"" &amp;&amp; value&lt;""10"" ?""valid"":""error""";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_XmlEntityB_ReturnsMarkup()
        {
            var input = new[]
            {
                MarkupGrammar.TokenPrimitive("B")
            };
            const string expected = @"B";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_AspNetPageDeclaration_ReturnsUnparsed()
        {
            var input = new[]
            {
                MarkupGrammar.TokenUnparsed("%@", "%", @" Page Language=""C#"" AutoEventWireup=""true"" CodeBehind=""Default.aspx.cs"" Inherits=""Foo._Default"" ")
            };
            const string expected = @"<%@ Page Language=""C#"" AutoEventWireup=""true"" CodeBehind=""Default.aspx.cs"" Inherits=""Foo._Default"" %>";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_XmlComment_ReturnsUnparsed()
        {
            var input = new[]
            {
                MarkupGrammar.TokenUnparsed("!--", "--", @" a quick note ")
            };
            const string expected = @"<!-- a quick note -->";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_UnopenedCloseTag_ReturnsMarkup()
        {
            var input = new []
            {
                MarkupGrammar.TokenElementEnd
            };
            const string expected = @"";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_NamespacePrefixTag_ReturnsMarkup()
        {
            var input = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("root", "prefix", "http://example.com/schema")),
                MarkupGrammar.TokenElementEnd,
            };
            const string expected = @"<prefix:root xmlns:prefix=""http://example.com/schema""></prefix:root>";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_EntityWithTrailingText_ReturnsMarkup()
        {
            var input = new[]
            {
                MarkupGrammar.TokenPrimitive("&"),
                MarkupGrammar.TokenPrimitive("trailing")
            };
            const string expected = @"&amp;trailing";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_IncompleteTag_ReturnsEmptyAttrib()
        {
            var input = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("div")),
                MarkupGrammar.TokenAttribute(new DataName("test")),
            };
            const string expected = @"<div test>";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_PrefixWithoutNamespace_ReturnsPrefix()
        {
            var input = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("div")),
                MarkupGrammar.TokenAttribute(new DataName("visible", "jbst", null)),
                MarkupGrammar.TokenPrimitive(false),
                MarkupGrammar.TokenElementEnd,
            };
            const string expected = @"<div jbst:visible=""false""></div>";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_HtmlEntityEuroEncodeNonAscii_ReturnsMarkup()
        {
            var input = new[]
            {
                MarkupGrammar.TokenPrimitive("€")
            };
            const string expected = @"&#x20AC;";

            var formatter = new HtmlFormatter(new DataWriterSettings())
            {
                EncodeNonAscii = true
            };
            var actual = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_SingleAttributeWhitespace_ReturnsMarkup()
        {
            var input = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("root")),
                MarkupGrammar.TokenAttribute(new DataName("whitespace")),
                MarkupGrammar.TokenPrimitive(" this contains whitespace "),
                MarkupGrammar.TokenElementEnd
            };
            const string expected = @"<root whitespace="" this contains whitespace ""></root>";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_XmlEntityHex_ReturnsMarkup()
        {
            var input = new[]
            {
                MarkupGrammar.TokenPrimitive("\uABCD")
            };
            const string expected = "\uABCD";

            var formatter = new HtmlFormatter(new DataWriterSettings())
            {
                EncodeNonAscii = false
            };
            var actual = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_MathML_ReturnsMarkup()
        {
            var input = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("p")),
                MarkupGrammar.TokenPrimitive(@"You can add a string to a number, but this stringifies the number:"),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenPrimitive("\r\n"),
                MarkupGrammar.TokenElementBegin(new DataName("math")),
                MarkupGrammar.TokenPrimitive("\r\n\t"),
                MarkupGrammar.TokenElementBegin(new DataName("ms")),
                MarkupGrammar.TokenPrimitive(@"x<y"),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenPrimitive("\r\n\t"),
                MarkupGrammar.TokenElementBegin(new DataName("mo")),
                MarkupGrammar.TokenPrimitive(@"+"),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenPrimitive("\r\n\t"),
                MarkupGrammar.TokenElementBegin(new DataName("mn")),
                MarkupGrammar.TokenPrimitive(@"3"),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenPrimitive("\r\n\t"),
                MarkupGrammar.TokenElementBegin(new DataName("mo")),
                MarkupGrammar.TokenPrimitive(@"="),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenPrimitive("\r\n\t"),
                MarkupGrammar.TokenElementBegin(new DataName("ms")),
                MarkupGrammar.TokenPrimitive(@"x<y3"),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenPrimitive("\r\n"),
                MarkupGrammar.TokenElementEnd,
            };
            const string expected =
                @"<p>You can add a string to a number, but this stringifies the number:</p>
<math>
	<ms>x&lt;y</ms>
	<mo>+</mo>
	<mn>3</mn>
	<mo>=</mo>
	<ms>x&lt;y3</ms>
</math>";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_ParentAndChildDifferentDefaultNamespaces_ReturnsMarkup()
        {
            var input = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("foo", String.Empty, "http://json.org")),
                MarkupGrammar.TokenElementBegin(new DataName("child", String.Empty, "http://jsonfx.net")),
                MarkupGrammar.TokenPrimitive("text value"),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
            };
            const string expected = @"<foo xmlns=""http://json.org""><child xmlns=""http://jsonfx.net"">text value</child></foo>";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_NamespacedChildTag_ReturnsMarkup()
        {
            var input = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("foo")),
                MarkupGrammar.TokenElementBegin(new DataName("child", String.Empty, "http://example.com/schema")),
                MarkupGrammar.TokenPrimitive("value"),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd
            };
            const string expected = @"<foo><child xmlns=""http://example.com/schema"">value</child></foo>";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_ParentAndChildSharePrefixedNamespace_ReturnsMarkup()
        {
            var input = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("foo", "bar", "http://example.org")),
                MarkupGrammar.TokenElementBegin(new DataName("child", "bar", "http://example.org")),
                MarkupGrammar.TokenPrimitive("value"),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
            };
            const string expected = @"<bar:foo xmlns:bar=""http://example.org""><bar:child>value</bar:child></bar:foo>";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_OverlappingTagsAutoBalanced_ReturnsMarkupAsIs()
        {
            var input = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("odd")),
                MarkupGrammar.TokenElementBegin(new DataName("auto-closed")),
                MarkupGrammar.TokenElementBegin(new DataName("even")),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd
            };
            const string expected = @"<odd><auto-closed><even></even></auto-closed></odd>";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_OverlappingNamespacedTagsErrorRecovery_ReturnsAutoBalanced()
        {
            var input = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("odd", "a", "http://example.com/odd/a")),
                MarkupGrammar.TokenElementBegin(new DataName("auto-closed", "b", "http://example.com/auto-closed/b")),
                MarkupGrammar.TokenElementBegin(new DataName("even", "c", "http://example.com/even/c")),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
            };
            const string expected = @"<a:odd xmlns:a=""http://example.com/odd/a""><b:auto-closed xmlns:b=""http://example.com/auto-closed/b""><c:even xmlns:c=""http://example.com/even/c""></c:even></b:auto-closed></a:odd>";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_SingleEmptyAttributeHtmlStyle_ReturnsMarkup()
        {
            var input = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("root")),
                MarkupGrammar.TokenAttribute(new DataName("noValue")),
                MarkupGrammar.TokenPrimitive(String.Empty),
                MarkupGrammar.TokenElementEnd
            };
            const string expected = @"<root noValue></root>";

            var formatter = new HtmlFormatter(new DataWriterSettings())
            {
                EmptyAttributes = HtmlFormatter.EmptyAttributeType.Html
            };
            var actual = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_XmlDocTypeExternal_ReturnsUnparsed()
        {
            var input = new[]
            {
                MarkupGrammar.TokenUnparsed("!", "",
                                            @"DOCTYPE html PUBLIC
	""-//W3C//DTD XHTML 1.1//EN""
	""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"""    )
            };
            const string expected =
                @"<!DOCTYPE html PUBLIC
	""-//W3C//DTD XHTML 1.1//EN""
	""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"">"    ;

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        //[Fact(Skip="Embedded DOCTYPE not supported")]
        public void Format_XmlDocTypeLocal_ReturnsUnparsed()
        {
            var input = new[]
            {
                MarkupGrammar.TokenUnparsed("!", "",
                                            @"DOCTYPE doc [
	<!ATTLIST normId id ID #IMPLIED>
	<!ATTLIST normNames attr NMTOKENS #IMPLIED>
]")
            };
            const string expected =
                @"<!DOCTYPE doc [
	<!ATTLIST normId id ID #IMPLIED>
	<!ATTLIST normNames attr NMTOKENS #IMPLIED>
]>";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_DifferentPrefixSameNamespace_ReturnsMarkup()
        {
            // Not sure if this is correct: http://stackoverflow.com/questions/3312390
            // "The namespace name for an unprefixed attribute name always has no value"
            // "The attribute value in a default namespace declaration MAY be empty.
            // This has the same effect, within the scope of the declaration, of there being no default namespace."
            // http://www.w3.org/TR/xml-names/#defaulting

            var input = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("foo", String.Empty, "http://example.org")),
                MarkupGrammar.TokenAttribute(new DataName("key", "blah", "http://example.org")),
                MarkupGrammar.TokenPrimitive("value"),
                MarkupGrammar.TokenElementEnd,
            };
            const string expected = @"<foo xmlns=""http://example.org"" key=""value""></foo>";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_MultipleAttributesCanonicalForm_ReturnsCanonicalMarkup()
        {
            var input = new[]
            {
                MarkupGrammar.TokenElementVoid(new DataName("root")),
                MarkupGrammar.TokenAttribute(new DataName("whitespace")),
                MarkupGrammar.TokenPrimitive(" this contains whitespace "),
                MarkupGrammar.TokenAttribute(new DataName("no-value")),
                MarkupGrammar.TokenPrimitive(String.Empty),
                MarkupGrammar.TokenAttribute(new DataName("anyQuotedText")),
                MarkupGrammar.TokenPrimitive("/\\\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?")
            };
            const string expected = @"<root anyQuotedText=""/\" + "\uCAFE\uBABE\uAB98\uFCDE\uBCDA\uEF4A" + @"&#x8;&#xC;&#xA;&#xD;&#x9;`1~!@#$%^&amp;*()_+-=[]{}|;:&apos;,./&lt;>?"" no-value whitespace="" this contains whitespace ""></root>";

            var formatter = new HtmlFormatter(new DataWriterSettings())
            {
                CanonicalForm = true
            };
            var actual = formatter.Format(input);

            Assert.Equal(expected, actual);
        }
        public void Format_NestedDefaultNamespaces_ReturnsMarkup()
        {
            var input = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("outer", String.Empty, "http://example.org/outer")),
                MarkupGrammar.TokenElementBegin(new DataName("middle-1", String.Empty, "http://example.org/inner")),
                MarkupGrammar.TokenElementBegin(new DataName("inner", String.Empty, "http://example.org/inner")),
                MarkupGrammar.TokenPrimitive("this should be inner"),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementBegin(new DataName("middle-2", String.Empty, "http://example.org/outer")),
                MarkupGrammar.TokenPrimitive("this should be outer"),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
            };
            const string expected = @"<outer xmlns=""http://example.org/outer""><middle-1 xmlns=""http://example.org/inner""><inner>this should be inner</inner></middle-1><middle-2>this should be outer</middle-2></outer>";

            var formatter = new HtmlFormatter(new DataWriterSettings());
            var actual    = formatter.Format(input);

            Assert.Equal(expected, actual);
        }