예제 #1
0
        public void Format_NamedObjectOneProperty_RendersSimpleObject()
        {
            var input = new[]
            {
                ModelGrammar.TokenObjectBegin("Yada"),
                ModelGrammar.TokenProperty("key"),
                ModelGrammar.TokenPrimitive("value"),
                ModelGrammar.TokenObjectEnd
            };

            var expected = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("div")),
                MarkupGrammar.TokenElementBegin(new DataName("dl")),
                MarkupGrammar.TokenAttribute(new DataName("title")),
                MarkupGrammar.TokenPrimitive(new DataName("Yada")),
                MarkupGrammar.TokenElementBegin(new DataName("dt")),
                MarkupGrammar.TokenPrimitive(new DataName("key")),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementBegin(new DataName("dd")),
                MarkupGrammar.TokenPrimitive("value"),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd
            };

            var transformer = new HtmlOutTransformer();
            var actual      = transformer.Transform(input).ToArray();

            Assert.Equal(expected, actual);
        }
        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);
        }
예제 #3
0
        public void GetTokens_HtmlContent_ReturnsSequence()
        {
            const string input = @"<div class=""content""><p style=""color:red""><strong>Lorem ipsum</strong> dolor sit amet, <i>consectetur</i> adipiscing elit.</p></div>";

            var expected = 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,
            };

            var tokenizer = new XmlReader.XmlTokenizer();
            var actual    = tokenizer.GetTokens(input).ToArray();

            Assert.Equal(expected, actual);
        }
예제 #4
0
        public void Format_NamespacedObjectOneProperty_CorrectlyEmitsNamespace()
        {
            var input = new[]
            {
                ModelGrammar.TokenObjectBegin("foo"),
                ModelGrammar.TokenProperty(new DataName("key", String.Empty, "http://json.org")),
                ModelGrammar.TokenPrimitive("value"),
                ModelGrammar.TokenObjectEnd
            };

            var expected = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("div")),
                MarkupGrammar.TokenElementBegin(new DataName("dl")),
                MarkupGrammar.TokenAttribute(new DataName("title")),
                MarkupGrammar.TokenPrimitive(new DataName("foo")),
                MarkupGrammar.TokenElementBegin(new DataName("dt")),
                MarkupGrammar.TokenPrimitive(new DataName("key", String.Empty, "http://json.org")),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementBegin(new DataName("dd")),
                MarkupGrammar.TokenPrimitive("value"),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd
            };

            var transformer = new HtmlOutTransformer();
            var actual      = transformer.Transform(input).ToArray();

            Assert.Equal(expected, actual);
        }
예제 #5
0
        public void GetTokens_SingleAttributeEmptyValue_ReturnsSequence()
        {
            var input = new[]
            {
                ModelGrammar.TokenArrayBeginUnnamed,
                ModelGrammar.TokenPrimitive(new DataName("root")),
                ModelGrammar.TokenObjectBeginUnnamed,
                ModelGrammar.TokenProperty(new DataName("emptyValue")),
                ModelGrammar.TokenPrimitive(String.Empty),
                ModelGrammar.TokenObjectEnd,
                ModelGrammar.TokenArrayEnd
            };
            var expected = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("root")),
                MarkupGrammar.TokenAttribute(new DataName("emptyValue")),
                MarkupGrammar.TokenPrimitive(String.Empty),
                MarkupGrammar.TokenElementEnd
            };

            var converter = new JsonMLWriter.JsonMLOutTransformer();
            var actual    = converter.Transform(input).ToArray();

            Assert.Equal(expected, actual);
        }
예제 #6
0
        public void GetTokens_DifferentPrefixSameNamespace_ReturnsSequence()
        {
            var input = new[]
            {
                ModelGrammar.TokenArrayBeginUnnamed,
                ModelGrammar.TokenPrimitive(new DataName("foo", String.Empty, "http://example.org")),
                ModelGrammar.TokenObjectBeginUnnamed,
                ModelGrammar.TokenProperty(new DataName("key", "blah", "http://example.org")),
                ModelGrammar.TokenPrimitive("value"),
                ModelGrammar.TokenObjectEnd,
                ModelGrammar.TokenArrayEnd
            };
            var expected = 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
            };

            var converter = new JsonMLWriter.JsonMLOutTransformer();
            var actual    = converter.Transform(input).ToArray();

            Assert.Equal(expected, actual);
        }
예제 #7
0
        public void GetTokens_AttributeWhitespaceQuotDelims_ReturnsSequence()
        {
            var input = new[]
            {
                ModelGrammar.TokenArrayBeginUnnamed,
                ModelGrammar.TokenPrimitive(new DataName("root")),
                ModelGrammar.TokenObjectBeginUnnamed,
                ModelGrammar.TokenProperty(new DataName("white")),
                ModelGrammar.TokenPrimitive(" extra whitespace around quote delims "),
                ModelGrammar.TokenObjectEnd,
                ModelGrammar.TokenArrayEnd
            };
            var expected = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("root")),
                MarkupGrammar.TokenAttribute(new DataName("white")),
                MarkupGrammar.TokenPrimitive(" extra whitespace around quote delims "),
                MarkupGrammar.TokenElementEnd
            };

            var converter = new JsonMLWriter.JsonMLOutTransformer();
            var actual    = converter.Transform(input).ToArray();

            Assert.Equal(expected, actual);
        }
예제 #8
0
        public void GetTokens_MultipleAttributes_ReturnsSequence()
        {
            var input = new[]
            {
                ModelGrammar.TokenArrayBeginUnnamed,
                ModelGrammar.TokenPrimitive(new DataName("root")),
                ModelGrammar.TokenObjectBeginUnnamed,
                ModelGrammar.TokenProperty(new DataName("no-value")),
                ModelGrammar.TokenPrimitive(String.Empty),
                ModelGrammar.TokenProperty(new DataName("whitespace")),
                ModelGrammar.TokenPrimitive(" this contains whitespace "),
                ModelGrammar.TokenProperty(new DataName("anyQuotedText")),
                ModelGrammar.TokenPrimitive("/\\\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"),
                ModelGrammar.TokenObjectEnd,
                ModelGrammar.TokenArrayEnd
            };
            var expected = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("root")),
                MarkupGrammar.TokenAttribute(new DataName("no-value")),
                MarkupGrammar.TokenPrimitive(String.Empty),
                MarkupGrammar.TokenAttribute(new DataName("whitespace")),
                MarkupGrammar.TokenPrimitive(" this contains whitespace "),
                MarkupGrammar.TokenAttribute(new DataName("anyQuotedText")),
                MarkupGrammar.TokenPrimitive("/\\\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"),
                MarkupGrammar.TokenElementEnd
            };

            var converter = new JsonMLWriter.JsonMLOutTransformer();
            var actual    = converter.Transform(input).ToArray();

            Assert.Equal(expected, actual);
        }
예제 #9
0
        public void GetTokens_HtmlContentPrettyPrinted_ReturnsSequence()
        {
            var input = new[]
            {
                ModelGrammar.TokenArrayBeginUnnamed,
                ModelGrammar.TokenPrimitive(new DataName("div")),
                ModelGrammar.TokenObjectBeginUnnamed,
                ModelGrammar.TokenProperty(new DataName("class")),
                ModelGrammar.TokenPrimitive("content"),
                ModelGrammar.TokenObjectEnd,
                ModelGrammar.TokenPrimitive("\r\n\t"),
                ModelGrammar.TokenArrayBeginUnnamed,
                ModelGrammar.TokenPrimitive(new DataName("p")),
                ModelGrammar.TokenObjectBeginUnnamed,
                ModelGrammar.TokenProperty(new DataName("style")),
                ModelGrammar.TokenPrimitive("color:red"),
                ModelGrammar.TokenObjectEnd,
                ModelGrammar.TokenPrimitive("\r\n\t\t"),
                ModelGrammar.TokenArrayBeginUnnamed,
                ModelGrammar.TokenPrimitive(new DataName("strong")),
                ModelGrammar.TokenPrimitive("Lorem ipsum"),
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenPrimitive(" dolor sit amet, "),
                ModelGrammar.TokenArrayBeginUnnamed,
                ModelGrammar.TokenPrimitive(new DataName("i")),
                ModelGrammar.TokenPrimitive("consectetur"),
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenPrimitive(" adipiscing elit.\r\n\t"),
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenPrimitive("\r\n"),
                ModelGrammar.TokenArrayEnd
            };
            var expected = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("div")),
                MarkupGrammar.TokenAttribute(new DataName("class")),
                MarkupGrammar.TokenPrimitive("content"),
                MarkupGrammar.TokenPrimitive("\r\n\t"),
                MarkupGrammar.TokenElementBegin(new DataName("p")),
                MarkupGrammar.TokenAttribute(new DataName("style")),
                MarkupGrammar.TokenPrimitive("color:red"),
                MarkupGrammar.TokenPrimitive("\r\n\t\t"),
                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.\r\n\t"),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenPrimitive("\r\n"),
                MarkupGrammar.TokenElementEnd,
            };

            var converter = new JsonMLWriter.JsonMLOutTransformer();
            var actual    = converter.Transform(input).ToArray();

            Assert.Equal(expected, actual);
        }
예제 #10
0
            private void EmitTag(List <Token <MarkupTokenType> > output, DataName elementName, IDictionary <DataName, Token <ModelTokenType> > attributes, MarkupTokenType tagType)
            {
                if (this.pendingNewLine)
                {
                    if (this.Settings.PrettyPrint)
                    {
                        this.depth++;
                        this.EmitNewLine(output);
                    }
                    this.pendingNewLine = false;
                }

                PrefixScopeChain.Scope scope = new PrefixScopeChain.Scope();
                scope.TagName = elementName;

                if (!this.ScopeChain.ContainsNamespace(elementName.NamespaceUri))
                {
                    scope[elementName.Prefix] = elementName.NamespaceUri;
                }
                this.ScopeChain.Push(scope);

                switch (tagType)
                {
                case MarkupTokenType.ElementVoid:
                {
                    output.Add(MarkupGrammar.TokenElementVoid(elementName));
                    break;
                }

                case MarkupTokenType.ElementEnd:
                {
                    output.Add(MarkupGrammar.TokenElementEnd);
                    break;
                }

                default:
                case MarkupTokenType.ElementBegin:
                {
                    output.Add(MarkupGrammar.TokenElementBegin(elementName));
                    break;
                }
                }

                if (attributes != null)
                {
                    foreach (var attr in attributes)
                    {
                        output.Add(MarkupGrammar.TokenAttribute(attr.Key));
                        output.Add(attr.Value.ChangeType(MarkupTokenType.Primitive));
                    }

                    attributes.Clear();
                }
            }
        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);
        }
예제 #12
0
        public void GetTokens_DifferentPrefixSameNamespace_ReturnsSequence()
        {
            const string input    = @"<foo xmlns=""http://example.org"" xmlns:blah=""http://example.org"" blah:key=""value"" />";
            var          expected = new[]
            {
                MarkupGrammar.TokenElementVoid(new DataName("foo", String.Empty, "http://example.org")),
                MarkupGrammar.TokenAttribute(new DataName("key", "blah", "http://example.org")),
                MarkupGrammar.TokenPrimitive("value")
            };

            var tokenizer = new XmlReader.XmlTokenizer();
            var actual    = tokenizer.GetTokens(input).ToArray();

            Assert.Equal(expected, actual);
        }
예제 #13
0
        public void Format_ParentAndAttributeDifferentNamespaces_CorrectlyEmitsNamespaces()
        {
            var input = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("foo", String.Empty, "http://json.org")),
                MarkupGrammar.TokenAttribute(new DataName("key", String.Empty, "http://jsonfx.net", true)),
                MarkupGrammar.TokenPrimitive("value"),
                MarkupGrammar.TokenElementEnd,
            };
            var expected = @"<foo p1:key=""value"" xmlns:p1=""http://jsonfx.net"" xmlns=""http://json.org"" />";

            var formatter = new XmlWriter.XmlFormatter(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);
        }
예제 #15
0
        public void GetTokens_SingleAttributeWhitespace_ReturnsSequence()
        {
            const string input    = @"<root whitespace="" this contains whitespace ""></root>";
            var          expected = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("root")),
                MarkupGrammar.TokenAttribute(new DataName("whitespace")),
                MarkupGrammar.TokenPrimitive(" this contains whitespace "),
                MarkupGrammar.TokenElementEnd
            };

            var tokenizer = new XmlReader.XmlTokenizer();
            var actual    = tokenizer.GetTokens(input).ToArray();

            Assert.Equal(expected, actual);
        }
예제 #16
0
        public void GetTokens_AttributeWhitespaceQuotDelims_ReturnsSequence()
        {
            const string input    = @"<root white  =  "" extra whitespace around quote delims "" ></root>";
            var          expected = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("root")),
                MarkupGrammar.TokenAttribute(new DataName("white")),
                MarkupGrammar.TokenPrimitive(" extra whitespace around quote delims "),
                MarkupGrammar.TokenElementEnd
            };

            var tokenizer = new XmlReader.XmlTokenizer();
            var actual    = tokenizer.GetTokens(input).ToArray();

            Assert.Equal(expected, actual);
        }
예제 #17
0
        public void GetTokens_SingleAttributeEmptyValue_ReturnsSequence()
        {
            const string input    = @"<root emptyValue=""""></root>";
            var          expected = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("root")),
                MarkupGrammar.TokenAttribute(new DataName("emptyValue")),
                MarkupGrammar.TokenPrimitive(String.Empty),
                MarkupGrammar.TokenElementEnd
            };

            var tokenizer = new XmlReader.XmlTokenizer();
            var actual    = tokenizer.GetTokens(input).ToArray();

            Assert.Equal(expected, actual);
        }
예제 #18
0
        public void Format_SingleAttributeEmptyValue_ReturnsMarkup()
        {
            var input = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("root")),
                MarkupGrammar.TokenAttribute(new DataName("emptyValue")),
                MarkupGrammar.TokenPrimitive(String.Empty),
                MarkupGrammar.TokenElementEnd
            };
            const string expected = @"<root emptyValue="""" />";

            var formatter = new XmlWriter.XmlFormatter(new DataWriterSettings());
            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);
        }
예제 #20
0
        private void TransformObject(List <Token <MarkupTokenType> > output, IStream <Token <ModelTokenType> > input)
        {
            Token <ModelTokenType> token = input.Pop();

            output.Add(MarkupGrammar.TokenElementBegin(HtmlOutTransformer.ObjectTagName));
            if (!token.Name.IsEmpty)
            {
                output.Add(MarkupGrammar.TokenAttribute(HtmlOutTransformer.HintAttributeName));
                output.Add(MarkupGrammar.TokenPrimitive(token.Name));
            }

            while (!input.IsCompleted)
            {
                token = input.Peek();
                switch (token.TokenType)
                {
                case ModelTokenType.ObjectEnd:
                {
                    input.Pop();

                    output.Add(MarkupGrammar.TokenElementEnd);
                    return;
                }

                case ModelTokenType.Property:
                {
                    input.Pop();

                    output.Add(MarkupGrammar.TokenElementBegin(HtmlOutTransformer.ObjectPropertyKeyTagName));
                    output.Add(MarkupGrammar.TokenPrimitive(token.Name));
                    output.Add(MarkupGrammar.TokenElementEnd);

                    output.Add(MarkupGrammar.TokenElementBegin(HtmlOutTransformer.ObjectPropertyValueTagName));
                    this.TransformValue(output, input);
                    output.Add(MarkupGrammar.TokenElementEnd);
                    break;
                }

                default:
                {
                    throw new TokenException <ModelTokenType>(
                              token,
                              String.Format(ErrorUnexpectedToken, token.TokenType));
                }
                }
            }
        }
예제 #21
0
        private void TransformPrimitive(List <Token <MarkupTokenType> > output, IStream <Token <ModelTokenType> > input)
        {
            Token <ModelTokenType> token = input.Pop();

            bool hasName = !token.Name.IsEmpty;

            if (hasName)
            {
                output.Add(MarkupGrammar.TokenElementBegin(HtmlOutTransformer.PrimitiveTagName));
                output.Add(MarkupGrammar.TokenAttribute(HtmlOutTransformer.HintAttributeName));
                output.Add(MarkupGrammar.TokenPrimitive(token.Name));
            }
            output.Add(token.ChangeType(MarkupTokenType.Primitive));
            if (hasName)
            {
                output.Add(MarkupGrammar.TokenElementEnd);
            }
        }
예제 #22
0
        public void Format_MultipleAttributes_ReturnsMarkup()
        {
            var input = new[]
            {
                MarkupGrammar.TokenElementVoid(new DataName("root")),
                MarkupGrammar.TokenAttribute(new DataName("no-value")),
                MarkupGrammar.TokenPrimitive(String.Empty),
                MarkupGrammar.TokenAttribute(new DataName("whitespace")),
                MarkupGrammar.TokenPrimitive(" this contains whitespace "),
                MarkupGrammar.TokenAttribute(new DataName("anyQuotedText")),
                MarkupGrammar.TokenPrimitive("/\\\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?")
            };
            const string expected = @"<root no-value="""" whitespace="" this contains whitespace "" anyQuotedText=""/\" + "\uCAFE\uBABE\uAB98\uFCDE\uBCDA\uEF4A\n\r\t" + @"`1~!@#$%^&amp;*()_+-=[]{}|;:',./&lt;&gt;?"" />";

            var formatter = new XmlWriter.XmlFormatter(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);
        }
예제 #24
0
        public void GetTokens_MultipleAttributes_ReturnsSequence()
        {
            const string input    = @"<root no-value="""" whitespace="" this contains whitespace "" anyQuotedText=""" + "/\\\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\n\r\t`1~!@#$%^&amp;*()_+-=[]{}|;:',./&lt;&gt;?" + @"""></root>";
            var          expected = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("root")),
                MarkupGrammar.TokenAttribute(new DataName("anyQuotedText")),
                MarkupGrammar.TokenPrimitive("/\\\uCAFE\uBABE\uAB98\uFCDE\uBCDA\uEF4A   `1~!@#$%^&*()_+-=[]{}|;:',./<>?"),
                MarkupGrammar.TokenAttribute(new DataName("no-value")),
                MarkupGrammar.TokenPrimitive(String.Empty),
                MarkupGrammar.TokenAttribute(new DataName("whitespace")),
                MarkupGrammar.TokenPrimitive(" this contains whitespace "),
                MarkupGrammar.TokenElementEnd
            };

            var tokenizer = new XmlReader.XmlTokenizer();
            var actual    = tokenizer.GetTokens(input).ToArray();

            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);
        }
예제 #26
0
        public void Format_ObjectAndAttributeDifferentNamespaces_CorrectlyEmitsNamespaces()
        {
            var input = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("foo", String.Empty, "http://json.org")),
                MarkupGrammar.TokenAttribute(new DataName("key", String.Empty, "http://jsonfx.net", true)),
                MarkupGrammar.TokenPrimitive("value"),
                MarkupGrammar.TokenElementEnd,
            };
            var expected = new[]
            {
                ModelGrammar.TokenObjectBegin(new DataName("foo", String.Empty, "http://json.org")),
                ModelGrammar.TokenProperty(new DataName("key", String.Empty, "http://jsonfx.net", true)),
                ModelGrammar.TokenPrimitive("value"),
                ModelGrammar.TokenObjectEnd
            };

            var transformer = new XmlReader.XmlInTransformer(new DataReaderSettings());
            var actual      = transformer.Transform(input).ToArray();

            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);
        }
예제 #28
0
            /// <summary>
            /// Gets a token sequence from the XmlReader
            /// </summary>
            /// <param name="reader"></param>
            /// <returns></returns>
            public IEnumerable <Token <MarkupTokenType> > GetTokens(System.Xml.XmlReader reader)
            {
                if (reader == null)
                {
                    throw new ArgumentNullException("reader");
                }

                while (true)
                {
                    // have to isolate try-catch away from yields
                    try
                    {
                        if (!reader.Read())
                        {
                            ((IDisposable)reader).Dispose();
                            break;
                        }
                    }
                    catch (System.Xml.XmlException ex)
                    {
                        throw new DeserializationException(ex.Message, ex.LinePosition, ex.LineNumber, -1, ex);
                    }
                    catch (Exception ex)
                    {
                        throw new DeserializationException(ex.Message, -1, ex);
                    }

                    switch (reader.NodeType)
                    {
                    case System.Xml.XmlNodeType.Element:
                    {
                        DataName tagName   = new DataName(reader.LocalName, reader.Prefix, reader.NamespaceURI);
                        bool     isVoidTag = reader.IsEmptyElement;

                        IDictionary <DataName, string> attributes;
                        if (reader.HasAttributes)
                        {
                            attributes = new CanonicalList();
                            while (reader.MoveToNextAttribute())
                            {
                                if (String.IsNullOrEmpty(reader.Prefix) && reader.LocalName == "xmlns" ||
                                    reader.Prefix == "xmlns")
                                {
                                    continue;
                                }

                                attributes[new DataName(reader.LocalName, reader.Prefix, reader.NamespaceURI)] = reader.Value;
                            }
                        }
                        else
                        {
                            attributes = null;
                        }

                        if (isVoidTag)
                        {
                            yield return(MarkupGrammar.TokenElementVoid(tagName));
                        }
                        else
                        {
                            yield return(MarkupGrammar.TokenElementBegin(tagName));
                        }

                        if (attributes != null)
                        {
                            foreach (var attribute in attributes)
                            {
                                yield return(MarkupGrammar.TokenAttribute(attribute.Key));

                                yield return(MarkupGrammar.TokenPrimitive(attribute.Value));
                            }
                        }
                        break;
                    }

                    case System.Xml.XmlNodeType.EndElement:
                    {
                        yield return(MarkupGrammar.TokenElementEnd);

                        break;
                    }

                    case System.Xml.XmlNodeType.Attribute:
                    {
                        yield return(MarkupGrammar.TokenAttribute(new DataName(reader.Name, reader.Prefix, reader.NamespaceURI, true)));

                        yield return(MarkupGrammar.TokenPrimitive(reader.Value));

                        break;
                    }

                    case System.Xml.XmlNodeType.Text:
                    {
                        yield return(MarkupGrammar.TokenPrimitive(reader.Value));

                        break;
                    }

                    case System.Xml.XmlNodeType.SignificantWhitespace:
                    case System.Xml.XmlNodeType.Whitespace:
                    {
                        yield return(MarkupGrammar.TokenPrimitive(reader.Value));

                        break;
                    }

                    case System.Xml.XmlNodeType.CDATA:
                    {
                        yield return(MarkupGrammar.TokenPrimitive(reader.Value));

                        break;
                    }

                    case System.Xml.XmlNodeType.Entity:
                    case System.Xml.XmlNodeType.EntityReference:
                    case System.Xml.XmlNodeType.EndEntity:
                    {
                        break;
                    }

                    case System.Xml.XmlNodeType.ProcessingInstruction:
                    case System.Xml.XmlNodeType.XmlDeclaration:
                    {
                        yield return(MarkupGrammar.TokenUnparsed("?", "?", reader.Name + " " + reader.Value));

                        break;
                    }

                    case System.Xml.XmlNodeType.Comment:
                    {
                        yield return(MarkupGrammar.TokenUnparsed("!--", "--", reader.Value));

                        break;
                    }

                    case System.Xml.XmlNodeType.DocumentType:
                    {
                        yield return(MarkupGrammar.TokenUnparsed("!DOCTYPE ", "", reader.Value));

                        break;
                    }

                    case System.Xml.XmlNodeType.Notation:
                    {
                        yield return(MarkupGrammar.TokenUnparsed("!NOTATION ", "", reader.Value));

                        break;
                    }

                    case System.Xml.XmlNodeType.None:
                    {
                        ((IDisposable)reader).Dispose();
                        yield break;
                    }

                    case System.Xml.XmlNodeType.Document:
                    case System.Xml.XmlNodeType.DocumentFragment:
                    default:
                    {
                        continue;
                    }
                    }
                }
                ;
            }