Beispiel #1
0
        private void VisitUnless(SpecialNode specialNode, SpecialNodeInspector inspector)
        {
            var conditionAttr = inspector.TakeAttribute("condition") ?? inspector.TakeAttribute("unless");

            var unlessChunk = new ConditionalChunk {
                Type = ConditionalType.Unless, Condition = AsCode(conditionAttr), Position = Locate(inspector.OriginalNode)
            };

            Chunks.Add(unlessChunk);
            using (new Frame(this, unlessChunk.Body))
                Accept(specialNode.Body);
        }
Beispiel #2
0
        public void ElseBlockFollowsIf()
        {
            var condition1 = new ConditionalChunk {
                Condition = "x != 12"
            };

            condition1.Body.Add(new SendLiteralChunk {
                Text = "fail"
            });
            var else1 = new ConditionalChunk {
                Type = ConditionalType.Else
            };

            else1.Body.Add(new SendLiteralChunk {
                Text = "ok1"
            });

            var condition2 = new ConditionalChunk {
                Condition = "x == 12"
            };

            condition2.Body.Add(new SendLiteralChunk {
                Text = "ok2"
            });
            var else2 = new ConditionalChunk {
                Type = ConditionalType.Else
            };

            else2.Body.Add(new SendLiteralChunk {
                Text = "fail"
            });

            var chunks = Chunks(
                new LocalVariableChunk {
                Name = "x", Value = "12"
            },
                new SendLiteralChunk {
                Text = "a"
            },
                condition1,
                else1,
                condition2,
                else2,
                new SendLiteralChunk {
                Text = "b"
            });

            _compiler.CompileView(chunks, chunks);
            var contents = ExecuteView();

            Assert.AreEqual("aok1ok2b", contents);
        }
Beispiel #3
0
        public void ChainingElseIfNestsProperly()
        {
            var condition1 = new ConditionalChunk {
                Type = ConditionalType.If, Condition = "x == 1"
            };

            condition1.Body.Add(new SendLiteralChunk {
                Text = "a"
            });
            var condition2 = new ConditionalChunk {
                Type = ConditionalType.ElseIf, Condition = "x == 2"
            };

            condition2.Body.Add(new SendLiteralChunk {
                Text = "b"
            });
            var condition3 = new ConditionalChunk {
                Type = ConditionalType.ElseIf, Condition = "x == 3"
            };

            condition3.Body.Add(new SendLiteralChunk {
                Text = "c"
            });
            var condition4 = new ConditionalChunk {
                Type = ConditionalType.Else
            };

            condition4.Body.Add(new SendLiteralChunk {
                Text = "d"
            });

            var loop = new ForEachChunk {
                Code = "x in @numbers"
            };

            loop.Body.Add(condition1);
            loop.Body.Add(condition2);
            loop.Body.Add(condition3);
            loop.Body.Add(condition4);

            var chunks = Chunks(
                loop);

            _compiler.CompileView(chunks, chunks);
            var contents = ExecuteView(new StubViewData {
                { "numbers", new[] { 0, 1, 2, 3, 4, 7, 2, -4 } }
            });

            Assert.AreEqual("dabcddbd", contents);
        }
Beispiel #4
0
        private void VisitUnless(SpecialNode specialNode, SpecialNodeInspector inspector)
        {
            AttributeNode    attr = inspector.TakeAttribute("condition") ?? inspector.TakeAttribute("unless");
            ConditionalChunk item = new ConditionalChunk {
                Type      = ConditionalType.Unless,
                Condition = this.AsCode(attr),
                Position  = this.Locate(inspector.OriginalNode)
            };

            this.Chunks.Add(item);
            using (new Frame(this, item.Body))
            {
                base.Accept(specialNode.Body);
            }
        }
        private void VisitElseIf(SpecialNode specialNode, SpecialNodeInspector inspector)
        {
            if (!SatisfyElsePrecondition())
            {
                throw new CompilerException("An 'elseif' may only follow an 'if' or 'elseif'.");
            }

            var conditionAttr = inspector.TakeAttribute("condition");
            var elseIfChunk   = new ConditionalChunk {
                Type = ConditionalType.ElseIf, Condition = AsCode(conditionAttr), Position = Locate(inspector.OriginalNode)
            };

            Chunks.Add(elseIfChunk);
            using (new Frame(this, elseIfChunk.Body))
                Accept(specialNode.Body);
        }
        private void VisitIf(SpecialNode specialNode, SpecialNodeInspector inspector)
        {
            var conditionAttr = inspector.TakeAttribute("condition") ?? inspector.TakeAttribute("if");

            var onceAttr = inspector.TakeAttribute("once");

            if (conditionAttr == null && onceAttr == null)
            {
                throw new CompilerException("Element must contain an if, condition, or once attribute");
            }

            Frame ifFrame = null;

            if (conditionAttr != null)
            {
                var ifChunk = new ConditionalChunk {
                    Type = ConditionalType.If, Condition = AsCode(conditionAttr), Position = Locate(inspector.OriginalNode)
                };
                Chunks.Add(ifChunk);
                ifFrame = new Frame(this, ifChunk.Body);
            }

            Frame onceFrame = null;

            if (onceAttr != null)
            {
                var onceChunk = new ConditionalChunk {
                    Type = ConditionalType.Once, Condition = onceAttr.AsCodeInverted(), Position = Locate(inspector.OriginalNode)
                };
                Chunks.Add(onceChunk);
                onceFrame = new Frame(this, onceChunk.Body);
            }

            Accept(specialNode.Body);

            if (onceFrame != null)
            {
                onceFrame.Dispose();
            }

            if (ifFrame != null)
            {
                ifFrame.Dispose();
            }
        }
Beispiel #7
0
        protected override void Visit(ConditionalChunk chunk)
        {
            switch (chunk.Type)
            {
            case ConditionalType.If:
            {
                CodeIndent(chunk)
                .Write("if (")
                .WriteCode(chunk.Condition)
                .WriteLine(")");
            }
            break;

            case ConditionalType.ElseIf:
            {
                CodeIndent(chunk)
                .Write("else if (")
                .WriteCode(chunk.Condition)
                .WriteLine(")");
            }
            break;

            case ConditionalType.Else:
            {
                _source.WriteLine("else");
            }
            break;

            case ConditionalType.Once:
            {
                CodeIndent(chunk)
                .Write("if (Once(")
                .WriteCode(chunk.Condition)
                .WriteLine("))");
            }
            break;

            default:
                throw new CompilerException("Unexpected conditional type " + chunk.Type);
            }
            CodeDefault();
            AppendOpenBrace();
            Accept(chunk.Body);
            AppendCloseBrace();
        }
Beispiel #8
0
        private void VisitElseIf(SpecialNode specialNode, SpecialNodeInspector inspector)
        {
            if (!this.SatisfyElsePrecondition())
            {
                throw new CompilerException("An 'elseif' may only follow an 'if' or 'elseif'.");
            }
            AttributeNode    attr = inspector.TakeAttribute("condition");
            ConditionalChunk item = new ConditionalChunk {
                Type      = ConditionalType.ElseIf,
                Condition = this.AsCode(attr),
                Position  = this.Locate(inspector.OriginalNode)
            };

            this.Chunks.Add(item);
            using (new Frame(this, item.Body))
            {
                base.Accept(specialNode.Body);
            }
        }
Beispiel #9
0
        private void VisitIf(SpecialNode specialNode, SpecialNodeInspector inspector)
        {
            AttributeNode attr  = inspector.TakeAttribute("condition") ?? inspector.TakeAttribute("if");
            AttributeNode node2 = inspector.TakeAttribute("once");

            if ((attr == null) && (node2 == null))
            {
                throw new CompilerException("Element must contain an if, condition, or once attribute");
            }
            Frame frame = null;

            if (attr != null)
            {
                ConditionalChunk item = new ConditionalChunk {
                    Type      = ConditionalType.If,
                    Condition = this.AsCode(attr),
                    Position  = this.Locate(inspector.OriginalNode)
                };
                this.Chunks.Add(item);
                frame = new Frame(this, item.Body);
            }
            Frame frame2 = null;

            if (node2 != null)
            {
                ConditionalChunk chunk3 = new ConditionalChunk {
                    Type      = ConditionalType.Once,
                    Condition = node2.AsCodeInverted(),
                    Position  = this.Locate(inspector.OriginalNode)
                };
                this.Chunks.Add(chunk3);
                frame2 = new Frame(this, chunk3.Body);
            }
            base.Accept(specialNode.Body);
            if (frame2 != null)
            {
                frame2.Dispose();
            }
            if (frame != null)
            {
                frame.Dispose();
            }
        }
Beispiel #10
0
        protected override void Visit(ConditionNode conditionNode)
        {
            ConditionalChunk item = new ConditionalChunk {
                Condition = conditionNode.Code,
                Type      = ConditionalType.If,
                Position  = this.Locate(conditionNode)
            };

            this.Chunks.Add(item);
            if (this._sendAttributeOnce != null)
            {
                item.Body.Add(this._sendAttributeOnce);
            }
            if (this._sendAttributeIncrement != null)
            {
                item.Body.Add(this._sendAttributeIncrement);
            }
            using (new Frame(this, item.Body))
            {
                base.Accept(conditionNode.Nodes);
            }
        }
Beispiel #11
0
        protected override void Visit(ConditionalChunk chunk)
        {
            switch (chunk.Type)
            {
            case ConditionalType.If:
                CodeIndent(chunk).Write("If ").WriteCode(chunk.Condition).WriteLine(" Then");
                break;

            case ConditionalType.ElseIf:
                _source.ClearEscrowLine();
                CodeIndent(chunk).Write("ElseIf ").WriteCode(chunk.Condition).WriteLine(" Then");
                break;

            case ConditionalType.Else:
                _source.ClearEscrowLine();
                _source.WriteLine("Else");
                break;

            case ConditionalType.Once:
                _source.Write("If Once(").WriteCode(chunk.Condition).WriteLine(") Then");
                break;

            case ConditionalType.Unless:
                CodeIndent(chunk).Write("If Not ").WriteCode(chunk.Condition).WriteLine(" Then");
                break;

            default:
                throw new CompilerException(string.Format("Unknown ConditionalChunk type {0}", chunk.Type));
            }

            _source
            .AddIndent();
            PushScope();
            Accept(chunk.Body);
            PopScope();
            _source
            .RemoveIndent()
            .EscrowLine("End If");
        }
        protected override void Visit(ConditionalChunk chunk)
        {
            switch (chunk.Type)
            {
            case ConditionalType.If:
            {
                _source.Append("if (").Append(chunk.Condition).AppendLine(") {");
                Accept(chunk.Body);
                _source.AppendLine("}");
            }
            break;

            case ConditionalType.ElseIf:
            {
                _source.Append("else if (").Append(chunk.Condition).AppendLine(") {");
                Accept(chunk.Body);
                _source.AppendLine("}");
            }
            break;

            case ConditionalType.Else:
            {
                _source.AppendLine("else {");
                Accept(chunk.Body);
                _source.AppendLine("}");
            }
            break;

            case ConditionalType.Unless:
            {
                _source.Append("if (!(").Append(chunk.Condition).AppendLine(")) {");
                Accept(chunk.Body);
                _source.AppendLine("}");
            }
            break;
            }
        }
Beispiel #13
0
        protected override void Visit(ConditionalChunk chunk)
        {
            switch (chunk.Type)
            {
            case ConditionalType.If:
                _source.Write("if ").WriteLine(chunk.Condition);
                break;

            case ConditionalType.ElseIf:
                _source.ClearEscrowLine();
                _source.Write("elsif ").WriteLine(chunk.Condition);
                break;

            case ConditionalType.Else:
                _source.ClearEscrowLine();
                _source.Write("else ").WriteLine(chunk.Condition);
                break;

            case ConditionalType.Once:
                _source.Write("if once(").Write(chunk.Condition).WriteLine(")");
                break;

            case ConditionalType.Unless:
                _source.Write("unless ").WriteLine(chunk.Condition);
                break;

            default:
                throw new CompilerException(string.Format("Unknown ConditionalChunk type {0}", chunk.Type));
            }
            _source.Indent++;
            _variables.PushScope();
            Accept(chunk.Body);
            _variables.PopScope();
            _source.Indent--;
            _source.EscrowLine("end");
        }
Beispiel #14
0
        private void Visit(ConditionalChunk chunk, bool terminateClosingBrace)
        {
            switch (chunk.Type)
            {
                case ConditionalType.If:
                    {
                        CodeIndent(chunk)
                            .Write("if (")
                            .WriteCode(chunk.Condition)
                            .WriteLine(") then ");
                    }
                    break;
                case ConditionalType.ElseIf:
                    {
                        CodeIndent(chunk)
                            .Write("else if (")
                            .WriteCode(chunk.Condition)
                            .WriteLine(") then ");
                    }
                    break;
                case ConditionalType.Else:
                    {
                        _source.WriteLine("else");
                    }
                    break;
                case ConditionalType.Once:
                    {
                        CodeIndent(chunk)
                            .Write("if (Once(")
                            .WriteCode(chunk.Condition)
                            .WriteLine(")) then ");
                    }
                    break;
                default:
                    throw new CompilerException("Unexpected conditional type " + chunk.Type);
            }
            //CodeDefault();
            AppendOpenBrace();
            Accept(chunk.Body);

            if (terminateClosingBrace)
            {
                AppendCloseBrace();
            }
            else
            {
                AppendCloseBraceNoSemiColon();
            }
        }
Beispiel #15
0
 protected override void Visit(ConditionalChunk chunk)
 {
     Visit(chunk, true);
 }
 protected abstract void Visit(ConditionalChunk chunk);
Beispiel #17
0
        protected override void Visit(ConditionNode conditionNode)
        {
            var conditionChunk = new ConditionalChunk
                                     {
                                         Condition = conditionNode.Code,
                                         Type = ConditionalType.If,
                                         Position = Locate(conditionNode)
                                     };
            Chunks.Add(conditionChunk);

            if (_sendAttributeOnce != null)
                conditionChunk.Body.Add(_sendAttributeOnce);
            if (_sendAttributeIncrement != null)
                conditionChunk.Body.Add(_sendAttributeIncrement);

            using (new Frame(this, conditionChunk.Body))
            {
                Accept(conditionNode.Nodes);
            }
        }
Beispiel #18
0
        protected override void Visit(ConditionalChunk chunk)
        {
            switch (chunk.Type)
            {
                case ConditionalType.If:
                    CodeIndent(chunk).Write("If ").WriteCode(chunk.Condition).WriteLine(" Then");
                    break;
                case ConditionalType.ElseIf:
                    _source.ClearEscrowLine();
                    CodeIndent(chunk).Write("ElseIf ").WriteCode(chunk.Condition).WriteLine(" Then");
                    break;
                case ConditionalType.Else:
                    _source.ClearEscrowLine();
                    _source.WriteLine("Else");
                    break;
                case ConditionalType.Once:
                    _source.Write("If Once(").WriteCode(chunk.Condition).WriteLine(") Then");
                    break;
                default:
                    throw new CompilerException(string.Format("Unknown ConditionalChunk type {0}", chunk.Type));
            }

            _source
                .AddIndent();
            PushScope();
            Accept(chunk.Body);
            PopScope();
            _source
                .RemoveIndent()
                .EscrowLine("End If");
        }
Beispiel #19
0
 protected override void Visit(ConditionalChunk chunk)
 {
     Accept(chunk.Body);
 }
Beispiel #20
0
        private void VisitUnless(SpecialNode specialNode, SpecialNodeInspector inspector)
        {
            var conditionAttr = inspector.TakeAttribute("condition") ?? inspector.TakeAttribute("unless");

            var unlessChunk = new ConditionalChunk { Type = ConditionalType.Unless, Condition = AsCode(conditionAttr), Position = Locate(inspector.OriginalNode) };
            Chunks.Add(unlessChunk);
            using (new Frame(this, unlessChunk.Body))
                Accept(specialNode.Body);
        }
 protected override void Visit(ConditionalChunk chunk)
 {
     switch (chunk.Type)
     {
         case ConditionalType.If:
             {
                 //CodeIndent(chunk).AppendLine(string.Format("if ({0})", chunk.Condition));
                 CodeIndent(chunk)
                     .Append("if (")
                     .AppendCode(chunk.Condition)
                     .AppendLine(")");
                 CodeDefault();
                 PushScope();
                 AppendIndent().AppendLine("{");
                 Indent += 4;
                 Accept(chunk.Body);
                 Indent -= 4;
                 AppendIndent().AppendLine(string.Format("}} // if ({0})",
                                                         chunk.Condition.ToString().Replace("\r", "").Replace("\n", " ")));
                 PopScope();
             }
             break;
         case ConditionalType.ElseIf:
             {
                 //CodeIndent(chunk).AppendLine(string.Format("else if ({0})", chunk.Condition));
                 CodeIndent(chunk)
                     .Append("else if (")
                     .AppendCode(chunk.Condition)
                     .AppendLine(")");
                 CodeDefault();
                 PushScope();
                 AppendIndent().AppendLine("{");
                 Indent += 4;
                 Accept(chunk.Body);
                 Indent -= 4;
                 AppendIndent().AppendLine(string.Format("}} // else if ({0})",
                                                         chunk.Condition.ToString().Replace("\r", "").Replace("\n", " ")));
                 PopScope();
             }
             break;
         case ConditionalType.Else:
             {
                 AppendIndent().AppendLine("else");
                 PushScope();
                 CodeIndent(chunk).AppendLine("{");
                 CodeDefault();
                 Indent += 4;
                 Accept(chunk.Body);
                 Indent -= 4;
                 AppendIndent().AppendLine("}");
                 PopScope();
             }
             break;
         case ConditionalType.Once:
             {
                 CodeIndent(chunk).Append("if (Once(").Append(chunk.Condition).AppendLine("))");
                 PushScope();
                 AppendIndent().AppendLine("{");
                 CodeDefault();
                 Indent += 4;
                 Accept(chunk.Body);
                 Indent -= 4;
                 AppendIndent().AppendLine("}");
                 PopScope();
             }
             break;
         default:
             throw new CompilerException("Unexpected conditional type " + chunk.Type);
     }
 }
 protected override void Visit(ConditionalChunk chunk)
 {
     if (!Snippets.IsNullOrEmpty(chunk.Condition))
         Examine(chunk.Condition);
     Accept(chunk.Body);
 }
Beispiel #23
0
        private void VisitIf(SpecialNode specialNode, SpecialNodeInspector inspector)
        {
            var conditionAttr = inspector.TakeAttribute("condition") ?? inspector.TakeAttribute("if");

            var onceAttr = inspector.TakeAttribute("once");

            if (conditionAttr == null && onceAttr == null)
            {
                throw new CompilerException("Element must contain an if, condition, or once attribute");
            }

            Frame ifFrame = null;
            if (conditionAttr != null)
            {
                var ifChunk = new ConditionalChunk { Type = ConditionalType.If, Condition = AsCode(conditionAttr), Position = Locate(inspector.OriginalNode) };
                Chunks.Add(ifChunk);
                ifFrame = new Frame(this, ifChunk.Body);
            }

            Frame onceFrame = null;
            if (onceAttr != null)
            {
                var onceChunk = new ConditionalChunk { Type = ConditionalType.Once, Condition = onceAttr.AsCodeInverted(), Position = Locate(inspector.OriginalNode) };
                Chunks.Add(onceChunk);
                onceFrame = new Frame(this, onceChunk.Body);
            }

            Accept(specialNode.Body);

            if (onceFrame != null)
                onceFrame.Dispose();

            if (ifFrame != null)
                ifFrame.Dispose();
        }
 protected override void Visit(ConditionalChunk chunk)
 {
     chunk.Condition = this.Process(chunk, chunk.Condition);
     base.Visit(chunk);
 }
 protected override void Visit(ConditionalChunk chunk)
 {
     switch (chunk.Type)
     {
         case ConditionalType.If:
             {
                 _source.Append("if (").Append(chunk.Condition).AppendLine(") {");
                 Accept(chunk.Body);
                 _source.AppendLine("}");
             }
             break;
         case ConditionalType.ElseIf:
             {
                 _source.Append("else if (").Append(chunk.Condition).AppendLine(") {");
                 Accept(chunk.Body);
                 _source.AppendLine("}");
             }
             break;
         case ConditionalType.Else:
             {
                 _source.AppendLine("else {");
                 Accept(chunk.Body);
                 _source.AppendLine("}");
             }
             break;
     }
 }
Beispiel #26
0
        protected override void Visit(AttributeNode attributeNode)
        {
            var accumulatedNodes = new List<Node>();
            var processedNodes = new List<Node>();
            foreach (var node in attributeNode.Nodes)
            {
                if (node is ConditionNode)
                {
                    // condition nodes take the prior unconditional nodes as content
                    var conditionNode = (ConditionNode)node;
                    MovePriorNodesUnderCondition(conditionNode, accumulatedNodes);

                    // prior nodes and condition are set for output
                    processedNodes.AddRange(accumulatedNodes);
                    processedNodes.Add(conditionNode);

                    accumulatedNodes.Clear();
                }
                else
                {
                    // other types add to the unconditional list
                    accumulatedNodes.Add(node);
                }
            }

            processedNodes.AddRange(accumulatedNodes);

            var allNodesAreConditional = processedNodes.All(node => node is ConditionNode);

            if (allNodesAreConditional == false || processedNodes.Any() == false)
            {
                // This attribute may not disapper - send it literally
                AddLiteral(" " + attributeNode.Name + "=\"");
                foreach (var node in processedNodes)
                    Accept(node);
                AddLiteral("\"");
            }
            else
            {
                var scope = new ScopeChunk();
                scope.Body.Add(new LocalVariableChunk { Name = "__just__once__", Value = new Snippets("0") });

                _sendAttributeOnce = new ConditionalChunk { Type = ConditionalType.If, Condition = new Snippets("__just__once__ < 1") };
                _sendAttributeOnce.Body.Add(new SendLiteralChunk { Text = " " + attributeNode.Name + "=\"" });
                _sendAttributeIncrement = new AssignVariableChunk { Name = "__just__once__", Value = "1" };

                Chunks.Add(scope);

                using (new Frame(this, scope.Body))
                {
                    foreach (var node in processedNodes)
                        Accept(node);
                }

                _sendAttributeOnce = null;
                _sendAttributeIncrement = null;

                var ifWasSent = new ConditionalChunk { Type = ConditionalType.If, Condition = new Snippets("__just__once__ > 0") };
                scope.Body.Add(ifWasSent);
                ifWasSent.Body.Add(new SendLiteralChunk { Text = "\"" });
            }
        }
 protected override void Visit(ConditionalChunk chunk)
 {
     Accept(chunk.Body);
 }
Beispiel #28
0
        private void VisitElseIf(SpecialNode specialNode, SpecialNodeInspector inspector)
        {
            if (!SatisfyElsePrecondition())
                throw new CompilerException("An 'elseif' may only follow an 'if' or 'elseif'.");

            var conditionAttr = inspector.TakeAttribute("condition");
            var elseIfChunk = new ConditionalChunk { Type = ConditionalType.ElseIf, Condition = AsCode(conditionAttr), Position = Locate(inspector.OriginalNode) };
            Chunks.Add(elseIfChunk);
            using (new Frame(this, elseIfChunk.Body))
                Accept(specialNode.Body);
        }
 protected override void Visit(ConditionalChunk chunk)
 {
     chunk.Condition = Process(chunk, chunk.Condition);
     base.Visit(chunk);
 }
Beispiel #30
0
        protected override void Visit(ConditionalChunk chunk)
        {
            switch (chunk.Type)
            {
                case ConditionalType.If:
                    {
                        CodeIndent(chunk)
                            .Write("if (")
                            .WriteCode(chunk.Condition)
                            .WriteLine(")");
                    }

                    break;
                case ConditionalType.ElseIf:
                    {
                        CodeIndent(chunk)
                            .Write("else if (")
                            .WriteCode(chunk.Condition)
                            .WriteLine(")");
                    }

                    break;
                case ConditionalType.Else:
                    {
                        _source.WriteLine("else");
                    }

                    break;
                case ConditionalType.Once:
                    {
                        CodeIndent(chunk)
                            .Write("if (Once(")
                            .WriteCode(chunk.Condition)
                            .WriteLine("))");
                    }

                    break;
                default:
                    throw new CompilerException("Unexpected conditional type " + chunk.Type);
            }

            CodeDefault();
            AppendOpenBrace();
            Accept(chunk.Body);
            AppendCloseBrace();
        }
Beispiel #31
0
        protected override void Visit(AttributeNode attributeNode)
        {
            var accumulatedNodes = new List <Node>();
            var processedNodes   = new List <Node>();

            foreach (var node in attributeNode.Nodes)
            {
                if (node is ConditionNode)
                {
                    // condition nodes take the prior unconditional nodes as content
                    var conditionNode = (ConditionNode)node;
                    MovePriorNodesUnderCondition(conditionNode, accumulatedNodes);

                    // prior nodes and condition are set for output
                    processedNodes.AddRange(accumulatedNodes);
                    processedNodes.Add(conditionNode);

                    accumulatedNodes.Clear();
                }
                else
                {
                    // other types add to the unconditional list
                    accumulatedNodes.Add(node);
                }
            }
            processedNodes.AddRange(accumulatedNodes);

            var allNodesAreConditional = processedNodes.All(node => node is ConditionNode);

            if (allNodesAreConditional == false || processedNodes.Any() == false)
            {
                // This attribute may not disapper - send it literally
                AddLiteral(" " + attributeNode.Name + "=\"");
                foreach (var node in processedNodes)
                {
                    Accept(node);
                }
                AddLiteral("\"");
            }
            else
            {
                var scope = new ScopeChunk();
                scope.Body.Add(new LocalVariableChunk {
                    Name = "__just__once__", Value = new Snippets("0")
                });

                _sendAttributeOnce = new ConditionalChunk {
                    Type = ConditionalType.If, Condition = new Snippets("__just__once__ < 1")
                };
                _sendAttributeOnce.Body.Add(new SendLiteralChunk {
                    Text = " " + attributeNode.Name + "=\""
                });
                _sendAttributeIncrement = new AssignVariableChunk {
                    Name = "__just__once__", Value = "1"
                };


                Chunks.Add(scope);

                using (new Frame(this, scope.Body))
                {
                    foreach (var node in processedNodes)
                    {
                        Accept(node);
                    }
                }
                _sendAttributeOnce      = null;
                _sendAttributeIncrement = null;

                var ifWasSent = new ConditionalChunk {
                    Type = ConditionalType.If, Condition = new Snippets("__just__once__ > 0")
                };
                scope.Body.Add(ifWasSent);
                ifWasSent.Body.Add(new SendLiteralChunk {
                    Text = "\""
                });
            }
        }
 protected abstract void Visit(ConditionalChunk chunk);
Beispiel #33
0
        protected override void Visit(AttributeNode attributeNode)
        {
            List <Node> priorNodes = new List <Node>();
            List <Node> source     = new List <Node>();

            foreach (Node node in attributeNode.Nodes)
            {
                if (node is ConditionNode)
                {
                    ConditionNode condition = (ConditionNode)node;
                    MovePriorNodesUnderCondition(condition, priorNodes);
                    source.AddRange(priorNodes);
                    source.Add(condition);
                    priorNodes.Clear();
                }
                else
                {
                    priorNodes.Add(node);
                }
            }
            source.AddRange(priorNodes);
            if (!source.All <Node>(node => (node is ConditionNode)) || !source.Any <Node>())
            {
                this.AddLiteral(string.Format(" {0}={1}", attributeNode.Name, attributeNode.QuotChar));
                foreach (Node node3 in source)
                {
                    base.Accept(node3);
                }
                this.AddLiteral(attributeNode.QuotChar.ToString());
            }
            else
            {
                ScopeChunk         item   = new ScopeChunk();
                LocalVariableChunk chunk3 = new LocalVariableChunk {
                    Name  = "__just__once__",
                    Value = new Snippets("0")
                };
                item.Body.Add(chunk3);
                ConditionalChunk chunk4 = new ConditionalChunk {
                    Type      = ConditionalType.If,
                    Condition = new Snippets("__just__once__ < 1")
                };
                this._sendAttributeOnce = chunk4;
                SendLiteralChunk chunk5 = new SendLiteralChunk {
                    Text = " " + attributeNode.Name + "=\""
                };
                this._sendAttributeOnce.Body.Add(chunk5);
                AssignVariableChunk chunk6 = new AssignVariableChunk {
                    Name  = "__just__once__",
                    Value = "1"
                };
                this._sendAttributeIncrement = chunk6;
                this.Chunks.Add(item);
                using (new Frame(this, item.Body))
                {
                    foreach (Node node4 in source)
                    {
                        base.Accept(node4);
                    }
                }
                this._sendAttributeOnce      = null;
                this._sendAttributeIncrement = null;
                ConditionalChunk chunk2 = new ConditionalChunk {
                    Type      = ConditionalType.If,
                    Condition = new Snippets("__just__once__ > 0")
                };
                item.Body.Add(chunk2);
                SendLiteralChunk chunk8 = new SendLiteralChunk {
                    Text = "\""
                };
                chunk2.Body.Add(chunk8);
            }
        }