Пример #1
0
        private PaintInfo GetPaintInfo(string primaryText)
        {
            var paintInfo = new PaintInfo();

            try
            {
                var sourceContext = new SourceContext(primaryText, 0, _path);
                var result        = _grammar.Nodes(new Position(sourceContext));

                paintInfo.Paint = result.Rest.GetPaint()
                                  .OfType <Paint <SparkTokenType> >()
                                  .Where(p => string.Equals(p.Begin.SourceContext.FileName, _path,
                                                            StringComparison.InvariantCultureIgnoreCase))
                                  .Select(p => new _SOURCEPAINTING
                {
                    start = p.Begin.Offset,
                    end   = p.End.Offset,
                    color = (int)p.Value
                })
                                  .ToArray();

                paintInfo.Count = paintInfo.Paint.Length;
            }
            catch (Exception ex)
            {
                paintInfo.ParseError = ex;
            }

            if (paintInfo.Count == 0)
            {
                paintInfo.Paint = new _SOURCEPAINTING[1];
            }
            return(paintInfo);
        }
Пример #2
0
        public override IList <Chunk> GetChunks(VisitorContext context, string path)
        {
            context.SyntaxProvider = this;
            context.ViewPath       = path;

            var sourceContext = CreateSourceContext(context.ViewPath, context.ViewFolder);
            var position      = new Position(sourceContext);

            var result = _grammar.Nodes(position);

            if (result.Rest.PotentialLength() != 0)
            {
                ThrowParseException(context.ViewPath, position, result.Rest);
            }

            context.Paint = result.Rest.GetPaint();

            var nodes = result.Value;

            foreach (var visitor in BuildNodeVisitors(context))
            {
                visitor.Accept(nodes);
                nodes = visitor.Nodes;
            }

            var chunkBuilder = new ChunkBuilderVisitor(context);

            chunkBuilder.Accept(nodes);
            return(chunkBuilder.Chunks);
        }
Пример #3
0
        public static IList <Chunk> ParseElementChunks(string content, int position)
        {
            var node           = ParseNode(content, position);
            var contentToParse = node is ElementNode?GetElementNodeAsString((ElementNode)node) : content;

            var grammar        = new MarkupGrammar();
            var visitorContext = new VisitorContext {
                SyntaxProvider = new DefaultSyntaxProvider(new ParserSettings())
            };
            var result = grammar.Nodes(Source(contentToParse));
            var nodes  = result.Value;

            foreach (var visitor in BuildChunkVisitors(visitorContext))
            {
                visitor.Accept(nodes);
                nodes = visitor.Nodes;
            }

            var chunkBuilder = new ChunkBuilderVisitor(visitorContext);

            chunkBuilder.Accept(nodes);
            var chunks = chunkBuilder.Chunks;

            return(chunks);
        }
        public void ChainConditionalAttribute()
        {
            var grammar = new MarkupGrammar();

            string input = "<div if=\"false\">hello</div><div elseif=\"true\">world</div><else>that's all</else>";
            var nodes = grammar.Nodes(new Position(new SourceContext(input))).Value;
            var visitor0 = new SpecialNodeVisitor(new VisitorContext());
            visitor0.Accept(nodes);
            var visitor = new ConditionalAttributeVisitor(new VisitorContext());
            visitor.Accept(visitor0.Nodes);

            Assert.AreEqual(3, visitor.Nodes.Count);
            Assert.IsAssignableFrom(typeof(SpecialNode), visitor.Nodes[0]);
            Assert.IsAssignableFrom(typeof(SpecialNode), visitor.Nodes[1]);
            Assert.IsAssignableFrom(typeof(SpecialNode), visitor.Nodes[2]);

            var ifNode = (SpecialNode)visitor.Nodes[0];
            Assert.AreEqual("if", ifNode.Element.Name);

            var elseifNode = (SpecialNode)visitor.Nodes[1];
            Assert.AreEqual("elseif", elseifNode.Element.Name);

            var elseNode = (SpecialNode)visitor.Nodes[2];
            Assert.AreEqual("else", elseNode.Element.Name);
        }
        public void ChainConditionalAttribute()
        {
            var grammar = new MarkupGrammar();

            string input    = "<div if=\"false\">hello</div><div elseif=\"true\">world</div><else>that's all</else>";
            var    nodes    = grammar.Nodes(new Position(new SourceContext(input))).Value;
            var    visitor0 = new SpecialNodeVisitor(new VisitorContext());

            visitor0.Accept(nodes);
            var visitor = new ConditionalAttributeVisitor(new VisitorContext());

            visitor.Accept(visitor0.Nodes);

            Assert.AreEqual(3, visitor.Nodes.Count);
            Assert.IsAssignableFrom(typeof(SpecialNode), visitor.Nodes[0]);
            Assert.IsAssignableFrom(typeof(SpecialNode), visitor.Nodes[1]);
            Assert.IsAssignableFrom(typeof(SpecialNode), visitor.Nodes[2]);

            var ifNode = (SpecialNode)visitor.Nodes[0];

            Assert.AreEqual("if", ifNode.Element.Name);

            var elseifNode = (SpecialNode)visitor.Nodes[1];

            Assert.AreEqual("elseif", elseifNode.Element.Name);

            var elseNode = (SpecialNode)visitor.Nodes[2];

            Assert.AreEqual("else", elseNode.Element.Name);
        }
Пример #6
0
        public IList <Node> ParseNodes(string content)
        {
            var grammar = new MarkupGrammar();
            var result  = grammar.Nodes(Source(content));

            return(result.Value);
        }
Пример #7
0
        public void PaintingNodes()
        {
            var input   = "<div><p class='subtle'>Hello World</p> ${Tada} </div>";
            var grammar = new MarkupGrammar();
            var result  = grammar.Nodes(new Position(new SourceContext(input)));

            Assert.AreEqual(8, result.Value.Count);
            Assert.AreEqual(10, result.Rest.GetPaint().OfType <Paint <Node> >().Count());
        }
Пример #8
0
        public void BangSyntaxHasRawContentWhenDisabled()
        {
            var settings = new ParserSettings { AutomaticEncoding = false };
            var grammar = new MarkupGrammar(settings);
            var result = grammar.Nodes(Source("!{'hello world'}"));

            Assert.AreEqual(1, result.Value.Count);
            Assert.AreEqual("\"hello world\"", (string)((ExpressionNode)result.Value[0]).Code);
            Assert.IsFalse(((ExpressionNode)result.Value[0]).AutomaticEncoding);
        }
Пример #9
0
        public void DollarHasEncodedContentWhenEnabled()
        {
            var settings = new ParserSettings {
                AutomaticEncoding = true
            };
            var grammar = new MarkupGrammar(settings);
            var result  = grammar.Nodes(Source("${'hello world'}"));

            Assert.AreEqual(1, result.Value.Count);
            Assert.AreEqual("\"hello world\"", (string)((ExpressionNode)result.Value[0]).Code);
            Assert.IsTrue(((ExpressionNode)result.Value[0]).AutomaticEncoding);
        }
Пример #10
0
        public void BangSyntaxHasRawContentWhenDisabled()
        {
            var settings = new ParserSettings {
                AutomaticEncoding = false
            };
            var grammar = new MarkupGrammar(settings);
            var result  = grammar.Nodes(Source("!{'hello world'}"));

            Assert.AreEqual(1, result.Value.Count);
            Assert.AreEqual("\"hello world\"", (string)((ExpressionNode)result.Value[0]).Code);
            Assert.IsFalse(((ExpressionNode)result.Value[0]).AutomaticEncoding);
        }
        public void CorrectSectionsCreated()
        {
            var grammar = new MarkupGrammar();
            var nodes = grammar.Nodes(new Position(new SourceContext(
                "<foo>1<tr>2<td>3</foo> <bar>4</td>5</tr>6</bar> stuff <baaz>yadda<baaz></baaz><quux><quux/></baaz>")));
            var visitor = new ViewComponentSectionChunkBuilderVisitor();
            visitor.Accept(nodes.Value);
            Assert.AreEqual(3, visitor.Sections.Count);
            
            Assert.AreEqual(1, visitor.Sections["foo"].Count);

            Assert.AreEqual("1<tr>2<td>3", ((SendLiteralChunk)visitor.Sections["foo"][0]).Text);
        }
Пример #12
0
        public void CustomMarkerForStatements()
        {
            var settings = new ParserSettings {
                AutomaticEncoding = true, StatementMarker = "hi"
            };
            var grammar = new MarkupGrammar(settings);
            var result  = grammar.Nodes(Source("  hibernate  \r\n"));

            Assert.AreEqual(2, result.Value.Count);
            var statement = result.Value.OfType <StatementNode>().Single();

            Assert.That(statement.Code.ToString(), Is.EqualTo("bernate  "));
        }
Пример #13
0
        public void HashSyntaxForStatementsByDefault()
        {
            var settings = new ParserSettings {
                AutomaticEncoding = true
            };
            var grammar = new MarkupGrammar(settings);
            var result  = grammar.Nodes(Source("  #foo  \r\n"));

            Assert.AreEqual(2, result.Value.Count);
            var statement = result.Value.OfType <StatementNode>().Single();

            Assert.That(statement.Code.ToString(), Is.EqualTo("foo  "));
        }
Пример #14
0
        public void HashSyntaxIgnoredWhenCustomMarkerProvided()
        {
            var settings = new ParserSettings {
                AutomaticEncoding = true, StatementMarker = "hi"
            };
            var grammar = new MarkupGrammar(settings);
            var result  = grammar.Nodes(Source("  #foo  \r\n"));

            Assert.AreEqual(1, result.Value.Count);
            var statement = result.Value.OfType <StatementNode>().Any();

            Assert.That(statement, Is.False);
        }
Пример #15
0
        public void CorrectSectionsCreated()
        {
            var grammar = new MarkupGrammar();
            var nodes   = grammar.Nodes(new Position(new SourceContext(
                                                         "<foo>1<tr>2<td>3</foo> <bar>4</td>5</tr>6</bar> stuff <baaz>yadda<baaz></baaz><quux><quux/></baaz>")));
            var visitor = new ViewComponentSectionChunkBuilderVisitor();

            visitor.Accept(nodes.Value);
            Assert.AreEqual(3, visitor.Sections.Count);

            Assert.AreEqual(1, visitor.Sections["foo"].Count);

            Assert.AreEqual("1<tr>2<td>3", ((SendLiteralChunk)visitor.Sections["foo"][0]).Text);
        }
Пример #16
0
        public IList <Node> ParseNodes(string content, params AbstractNodeVisitor[] visitors)
        {
            var result = _grammar.Nodes(new Position(new SourceContext(content)));

            _paint = result.Rest.GetPaint();
            var nodes = result.Value;

            foreach (var visitor in visitors)
            {
                visitor.Accept(nodes);
                nodes = visitor.Nodes;
            }
            return(nodes);
        }
        public void CorrectSectionsCreated()
        {
            var grammar = new MarkupGrammar(ParserSettings.DefaultBehavior);
            var nodes = grammar.Nodes(new Position(new SourceContext(
                                                       "<foo>1<tr>2<td>3</foo> <bar>4</td>5</tr>6</bar> stuff <baaz>yadda<baaz></baaz><quux><quux/></baaz>")));
            var details = new ViewComponentDetailsAttribute("Testing") { Sections = "foo,baaz,bar,quux" };
            var visitor = new ViewComponentVisitor(new ChunkBuilderVisitor(new VisitorContext{Paint=nodes.Rest.GetPaint()}), new ViewComponentInfo { Details = details });
            visitor.Accept(nodes.Value);
            Assert.AreEqual(3, visitor.Sections.Count);

            Assert.AreEqual(1, visitor.Sections["foo"].Count);

            Assert.AreEqual("1<tr>2<td>3", ((SendLiteralChunk)visitor.Sections["foo"][0]).Text);
        }
        public void DetectIfAttribute()
        {
            var grammar = new MarkupGrammar();
            string input = "<div if=\"true\">hello</div>";
            var nodes = grammar.Nodes(new Position(new SourceContext(input))).Value;
            var visitor = new ConditionalAttributeVisitor(new VisitorContext());
            visitor.Accept(nodes);

            Assert.AreEqual(1, visitor.Nodes.Count);
            Assert.IsAssignableFrom(typeof(SpecialNode), visitor.Nodes[0]);

            var ifNode = visitor.Nodes[0] as SpecialNode;
            Assert.IsNotNull(ifNode);
            Assert.AreEqual("if", ifNode.Element.Name);
        }
        public void DetectUnlessAttribute()
        {
            var    grammar = new MarkupGrammar();
            string input   = "<div unless=\"true\">hello</div>";
            var    nodes   = grammar.Nodes(new Position(new SourceContext(input))).Value;
            var    visitor = new ConditionalAttributeVisitor(new VisitorContext());

            visitor.Accept(nodes);

            Assert.AreEqual(1, visitor.Nodes.Count);
            Assert.IsAssignableFrom(typeof(SpecialNode), visitor.Nodes[0]);

            var unlessNode = visitor.Nodes[0] as SpecialNode;

            Assert.IsNotNull(unlessNode);
            Assert.AreEqual("unless", unlessNode.Element.Name);
        }
        public void RemoveLinesBeforeSpecialNodes()
        {
            var grammar = new MarkupGrammar();
            var nodes = grammar.Nodes(Source("<p>\r\n  <test if='true'>\r\n    <span>was true</span>\r\n  </test>\r\n</p>"));
            var specialNodeVisitor = new SpecialNodeVisitor(new VisitorContext());
            specialNodeVisitor.Accept(nodes.Value);
            omit.Accept(specialNodeVisitor.Nodes);

            Assert.AreEqual(5, omit.Nodes.Count);
            Assert.IsAssignableFrom(typeof(ElementNode), omit.Nodes[0]);
            Assert.IsAssignableFrom(typeof(TextNode), omit.Nodes[1]);
            Assert.AreEqual("", ((TextNode)omit.Nodes[1]).Text);
            Assert.IsAssignableFrom(typeof(SpecialNode), omit.Nodes[2]);

            var childNodes = ((SpecialNode) omit.Nodes[2]).Body;
            Assert.AreEqual(5, childNodes.Count);
            Assert.IsAssignableFrom(typeof(TextNode), childNodes[0]);
            Assert.AreEqual("\r\n    ", ((TextNode)childNodes[0]).Text);
            Assert.IsAssignableFrom(typeof(TextNode), childNodes[4]);
            Assert.AreEqual("", ((TextNode)childNodes[4]).Text);
        }
Пример #21
0
        public void PaintingAttributes()
        {
            var input = "<div id='rea' class='foo'/>";
            var grammar = new MarkupGrammar();
            var result = grammar.Nodes(new Position(new SourceContext(input)));
            Assert.AreEqual(1, result.Value.Count);

            var paints = result.Rest.GetPaint();
            Assert.AreEqual(1, paints.Select(p => p.Value).OfType<ElementNode>().Count());
            Assert.AreEqual(2, paints.Select(p => p.Value).OfType<AttributeNode>().Count());
            Assert.AreEqual(2, paints.Select(p => p.Value).OfType<TextNode>().Count());
            Assert.AreEqual(5, paints.OfType<Paint<Node>>().Count());

            var attrId = paints.Single(p => (p.Value is AttributeNode) && ((AttributeNode)p.Value).Name == "id");
            var attrClass = paints.Single(p => (p.Value is AttributeNode) && ((AttributeNode)p.Value).Name == "class");

            Assert.AreEqual(5, attrId.Begin.Offset);
            Assert.AreEqual(13, attrId.End.Offset);
            Assert.AreEqual(14, attrClass.Begin.Offset);
            Assert.AreEqual(25, attrClass.End.Offset);
        }
        public void CorrectSectionsCreated()
        {
            var grammar = new MarkupGrammar(ParserSettings.DefaultBehavior);
            var nodes   = grammar.Nodes(new Position(new SourceContext(
                                                         "<foo>1<tr>2<td>3</foo> <bar>4</td>5</tr>6</bar> stuff <baaz>yadda<baaz></baaz><quux><quux/></baaz>")));
            var details = new ViewComponentDetailsAttribute("Testing")
            {
                Sections = "foo,baaz,bar,quux"
            };
            var visitor = new ViewComponentVisitor(new ChunkBuilderVisitor(new VisitorContext {
                Paint = nodes.Rest.GetPaint()
            }), new ViewComponentInfo {
                Details = details
            });

            visitor.Accept(nodes.Value);
            Assert.AreEqual(3, visitor.Sections.Count);

            Assert.AreEqual(1, visitor.Sections["foo"].Count);

            Assert.AreEqual("1<tr>2<td>3", ((SendLiteralChunk)visitor.Sections["foo"][0]).Text);
        }
Пример #23
0
        public void PaintingAttributes()
        {
            var input   = "<div id='rea' class='foo'/>";
            var grammar = new MarkupGrammar();
            var result  = grammar.Nodes(new Position(new SourceContext(input)));

            Assert.AreEqual(1, result.Value.Count);

            var paints = result.Rest.GetPaint();

            Assert.AreEqual(1, paints.Select(p => p.Value).OfType <ElementNode>().Count());
            Assert.AreEqual(2, paints.Select(p => p.Value).OfType <AttributeNode>().Count());
            Assert.AreEqual(2, paints.Select(p => p.Value).OfType <TextNode>().Count());
            Assert.AreEqual(5, paints.OfType <Paint <Node> >().Count());

            var attrId    = paints.Single(p => (p.Value is AttributeNode) && ((AttributeNode)p.Value).Name == "id");
            var attrClass = paints.Single(p => (p.Value is AttributeNode) && ((AttributeNode)p.Value).Name == "class");

            Assert.AreEqual(5, attrId.Begin.Offset);
            Assert.AreEqual(13, attrId.End.Offset);
            Assert.AreEqual(14, attrClass.Begin.Offset);
            Assert.AreEqual(25, attrClass.End.Offset);
        }
Пример #24
0
        public void EntityTextSeries()
        {
            var result = grammar.Nodes(Source("hello&nbsp;world"));

            Assert.IsNotNull(result);
            Assert.AreEqual(3, result.Value.Count);
            Assert.IsAssignableFrom(typeof(TextNode), result.Value[0]);
            Assert.IsAssignableFrom(typeof(EntityNode), result.Value[1]);
            Assert.IsAssignableFrom(typeof(TextNode), result.Value[2]);
        }
Пример #25
0
 public static IList<Node> ParseNodes(string content)
 {
     var grammar = new MarkupGrammar();
     var result = grammar.Nodes(Source(content));
     return result.Value;
 }
Пример #26
0
        public static IList<Chunk> ParseElementChunks(string content, int position)
        {
            var node = ParseNode(content, position);
            var contentToParse = node is ElementNode ? GetElementNodeAsString((ElementNode)node) : content;

            var grammar = new MarkupGrammar();
            var visitorContext = new VisitorContext { SyntaxProvider = new DefaultSyntaxProvider(new ParserSettings()) };
            var result = grammar.Nodes(Source(contentToParse));
            var nodes = result.Value;
            foreach (var visitor in BuildChunkVisitors(visitorContext))
            {
                visitor.Accept(nodes);
                nodes = visitor.Nodes;
            }

            var chunkBuilder = new ChunkBuilderVisitor(visitorContext);
            chunkBuilder.Accept(nodes);
            var chunks = chunkBuilder.Chunks;
            return chunks;
        }
Пример #27
0
 public void PaintingNodes()
 {
     var input = "<div><p class='subtle'>Hello World</p> ${Tada} </div>";
     var grammar = new MarkupGrammar();
     var result = grammar.Nodes(new Position(new SourceContext(input)));
     Assert.AreEqual(8, result.Value.Count);
     Assert.AreEqual(10, result.Rest.GetPaint().OfType<Paint<Node>>().Count());
 }
Пример #28
0
        public void CustomMarkerForStatements()
        {
            var settings = new ParserSettings { AutomaticEncoding = true, StatementMarker="hi" };
            var grammar = new MarkupGrammar(settings);
            var result = grammar.Nodes(Source("  hibernate  \r\n"));

            Assert.AreEqual(2, result.Value.Count);
            var statement = result.Value.OfType<StatementNode>().Single();
            Assert.That(statement.Code.ToString(), Is.EqualTo("bernate  "));
        }
Пример #29
0
        public void DollarHasEncodedContentWhenEnabled()
        {
            var settings = new ParserSettings { AutomaticEncoding = true };
            var grammar = new MarkupGrammar(settings);
            var result = grammar.Nodes(Source("${'hello world'}"));

            Assert.AreEqual(1, result.Value.Count);
            Assert.AreEqual("\"hello world\"", (string)((ExpressionNode)result.Value[0]).Code);
            Assert.IsTrue(((ExpressionNode)result.Value[0]).AutomaticEncoding);
        }
Пример #30
0
        public void HashSyntaxForStatementsByDefault()
        {
            var settings = new ParserSettings { AutomaticEncoding = true };
            var grammar = new MarkupGrammar(settings);
            var result = grammar.Nodes(Source("  #foo  \r\n"));

            Assert.AreEqual(2, result.Value.Count);
            var statement = result.Value.OfType<StatementNode>().Single();
            Assert.That(statement.Code.ToString(), Is.EqualTo("foo  "));
        }
Пример #31
0
        public void HashSyntaxIgnoredWhenCustomMarkerProvided()
        {
            var settings = new ParserSettings { AutomaticEncoding = true, StatementMarker = "hi" };
            var grammar = new MarkupGrammar(settings);
            var result = grammar.Nodes(Source("  #foo  \r\n"));

            Assert.AreEqual(1, result.Value.Count);
            var statement = result.Value.OfType<StatementNode>().Any();
            Assert.That(statement, Is.False);
        }