예제 #1
0
        public void Constructor_HtmlStyleAttribute_GeneratesHtmlAttributeCollectionTag()
        {
            const string templateLine = "p(a='b')";
            var          tag          = new HamlNodeTag(new HamlLine(templateLine, HamlRuleEnum.Tag, "", 0));

            Assert.That(tag.Children.First(), Is.InstanceOf <HamlNodeHtmlAttributeCollection>());
        }
예제 #2
0
        public void Constructor_TagWithNamespace_DeterminesTagAndNamespaceCorrectly(string templateLine,
                                                                                    string expectedNamespace, string expectedTag)
        {
            var tag = new HamlNodeTag(new HamlLine(templateLine, HamlRuleEnum.Tag, "", 0));

            Assert.That(tag.Namespace, Is.EqualTo(expectedNamespace));
            Assert.That(tag.TagName, Is.EqualTo(expectedTag));
        }
예제 #3
0
        public void Constructor_HtmlStyleAttribute_AttributeCollectionContainsCorrectContent(
            string templateLine, string expectedAttributeContent, int expectedaAttrCount)
        {
            var tag = new HamlNodeTag(new HamlLine(templateLine, HamlRuleEnum.Tag, "", 0));

            Assert.That(tag.Children.First().Content, Is.EqualTo(expectedAttributeContent));
            Assert.That(tag.Children.Count(), Is.EqualTo(expectedaAttrCount));
        }
예제 #4
0
        private void AppendTagStart(HamlNodeTag nodeTag)
        {
            if (nodeTag.IsLeadingWhitespaceTrimmed == false)
            {
                ClassBuilder.Append(nodeTag.Indent);
            }

            ClassBuilder.Append("<" + nodeTag.NamespaceQualifiedTagName);
        }
예제 #5
0
        private void WalkHtmlStyleAttributes(HamlNodeTag nodeTag)
        {
            var attributeTags = nodeTag.Children.Where(x => x.GetType() == typeof(HamlNodeHtmlAttributeCollection));

            foreach (var child in attributeTags)
            {
                new HamlNodeHtmlAttributeCollectionWalker(ClassBuilder, Options)
                .Walk(child);
            }
        }
예제 #6
0
        public void Constructor_InlineContent_GeneratesCorrectChildTag()
        {
            const string templateLine = "p Hello world";
            var          tag          = new HamlNodeTag(new HamlLine(templateLine, HamlRuleEnum.Tag, "", 0));

            Assert.That(tag.Children.First(), Is.InstanceOf <HamlNodeTextContainer>());
            const string expectedText = "Hello world";

            Assert.That(((HamlNodeTextContainer)tag.Children.First()).Content, Is.EqualTo(expectedText));
        }
예제 #7
0
        public void Walk_EmptyAttributeCollectionNode_WritesCorrectAttributes(string tag, string attributes, string expectedOutput)
        {
            var tagNode = new HamlNodeTag(new HamlLine(tag, HamlRuleEnum.Tag, "", 0));

            tagNode.AddChild(new HamlNodeHtmlAttributeCollection(0, attributes));

            _tagWalker.Walk(tagNode);

            Assert.That(_classBuilderMock.Build(""), Is.EqualTo(expectedOutput));
        }
예제 #8
0
        public void Walk_WhitespaceRemoval_GeneratesCorrectOutput(string line1, string line2, string line3, string expectedOutput)
        {
            var tagNode = new HamlNodeTag(new HamlLine(line1, HamlRuleEnum.Tag, "", 0));

            tagNode.AddChild(new HamlNodeTag(new HamlLine(line2, HamlRuleEnum.Tag, "  ", 0)));

            _tagWalker.Walk(tagNode);

            Assert.That(_classBuilderMock.Build(""), Is.EqualTo(expectedOutput));
        }
예제 #9
0
        public void Constructor_SimpleTags_GeneratesCorrectClassChildNodes(string templateLine, string expectedFirstClass, int expectedCount)
        {
            var tag = new HamlNodeTag(new HamlLine(templateLine, HamlRuleEnum.Tag, "", 0));

            var classChildren = tag.Children.OfType <HamlNodeTagClass>();

            Assert.That(classChildren.Count(), Is.EqualTo(expectedCount));
            string att = classChildren.FirstOrDefault() != null?classChildren.First().Content : "";

            Assert.That(att, Is.EqualTo(expectedFirstClass));
        }
예제 #10
0
        private static IList <HamlNodeTextContainer> GetClassValues(HamlNodeTag nodeTag)
        {
            var classValues = (from collection in nodeTag.Children.OfType <HamlNodeHtmlAttributeCollection>()
                               from attr in collection.Children.OfType <HamlNodeHtmlAttribute>()
                               where attr.Name == "class"
                               from attrFragment in attr.Children
                               select(HamlNodeTextContainer) attrFragment).ToList();

            classValues.AddRange(nodeTag.Children.OfType <HamlNodeTagClass>().Select(x => new HamlNodeTextContainer(x.SourceFileLineNum, x.Content)));
            return(classValues);
        }
예제 #11
0
        public void Walk_NonSelfClosingTags_AppendsCorrectTag(string templateLine, string expectedOutput)
        {
            // Arrange
            var tagNode = new HamlNodeTag(new HamlLine(templateLine, HamlRuleEnum.Tag, "", 0));

            // Act
            _tagWalker.Walk(tagNode);

            // Assert
            Assert.That(_classBuilderMock.Build(""), Is.StringContaining(expectedOutput));
        }
예제 #12
0
        private void Walk(HamlNodeTag node)
        {
            _templateILStream.Write('<');
            _templateILStream.Write(node.NamespaceQualifiedTagName);

            var attributes = node.Attributes.OrderBy(a => a.Name).GroupBy(a => a.Name);

            foreach (var attrGroup in attributes)
            {
                _templateILStream.Write(" {0}=\"", attrGroup.Key);

                bool leadingSpace = false;
                var  values       = attrGroup.Select(n => n.Child).OfType <HamlNodeTextLiteral>().OrderBy(n => n.Content).Select(n => n.Content);
                if (values.Any())
                {
                    leadingSpace = true;
                    _templateILStream.Write(values.Aggregate((accum, val) => string.Format("{0} {1}", accum, val)));
                }

                var thunkValues = attrGroup.Select(n => n.Child).OfType <HamlNodeEval>();
                if (thunkValues.Any())
                {
                    if (leadingSpace)
                    {
                        _templateILStream.Write(' ');
                        leadingSpace = false;
                    }
                    foreach (var value in thunkValues)
                    {
                        if (leadingSpace)
                        {
                            _templateILStream.Write(' ');
                        }
                        _codeClassBuilder.PrintExpressionResult(value.Content);
                        leadingSpace = true;
                    }
                }

                _templateILStream.Write('"');
            }
            if (node.Children.Count > 0)
            {
                _templateILStream.Write('>');
                this.Walk(node.Children);
                _templateILStream.Write("</");
                _templateILStream.Write(node.NamespaceQualifiedTagName);
            }
            else if (!node.IsSelfClosing)
            {
                _templateILStream.Write('/');
            }

            _templateILStream.Write('>');
        }
예제 #13
0
        public void Walk_InlineContent_GeneratesCorrectOutput()
        {
            var tagNode = new HamlNodeTag(new HamlLine("p", HamlRuleEnum.Tag));

            tagNode.AddChild(new HamlNodeTextContainer(new HamlLine("Content", HamlRuleEnum.PlainText, "\t", 0, true)));

            _tagWalker.Walk(tagNode);

            const string expectedOutput = "<p>Content</p>";

            Assert.That(_classBuilderMock.Build(""), Is.EqualTo(expectedOutput));
        }
예제 #14
0
        public void Constructor_HtmlStyleAttributeWithContent_GeneratesCorrectChildren()
        {
            const string templateLine = "p(a='b')Content";
            var          tag          = new HamlNodeTag(new HamlLine(templateLine, HamlRuleEnum.Tag, "", 0));

            const string expectedAttrContent = "(a='b')";

            Assert.That(tag.Children.First().Content, Is.EqualTo(expectedAttrContent));
            const string expectedStringContent = "Content";

            Assert.That(tag.Children.ToList()[1].Content, Is.EqualTo(expectedStringContent));
        }
예제 #15
0
        public void Walk_IndentedTag_AppendsIndent()
        {
            // Arrange
            const string indent  = "  ";
            var          tagNode = new HamlNodeTag(new HamlLine("p", HamlRuleEnum.Tag, indent, 0));

            // Act
            _tagWalker.Walk(tagNode);

            // Assert
            Assert.That(_classBuilderMock.Build(""), Is.StringStarting(indent));
        }
예제 #16
0
        public void Walk_SelfClosingTag_AppendsCorrectTag()
        {
            // Arrange
            const string tagName = "foo/";
            var          tagNode = new HamlNodeTag(new HamlLine(tagName, HamlRuleEnum.Tag, "", 0));

            // Act
            _tagWalker.Walk(tagNode);

            // Assert
            const string expectedTag = "<foo />";

            Assert.That(_classBuilderMock.Build(""), Is.StringContaining(expectedTag));
        }
예제 #17
0
        public void Walk_InternalWhitespaceRemoval_GeneratesCorrectOutput()
        {
            var tagNode = new HamlNodeTag(new HamlLine("p<", HamlRuleEnum.Tag, "", 0));

            //tagNode.IsMultiLine = true;

            tagNode.AddChild(new HamlNodeTextContainer(new HamlLine("\n", HamlRuleEnum.PlainText, "", 0)));
            tagNode.AddChild(new HamlNodeTextContainer(new HamlLine("  Hello", HamlRuleEnum.PlainText, "", 0)));

            _tagWalker.Walk(tagNode);

            const string expectedOutput = "<p>Hello</p>";

            Assert.That(_classBuilderMock.Build(""), Is.EqualTo(expectedOutput));
        }
예제 #18
0
        public void Walk_ClassHtmlAttribute_WritesCorrectClassAttribute()
        {
            // Arrange
            var tagNode = new HamlNodeTag(new HamlLine("p", HamlRuleEnum.Tag, "", 0));

            tagNode.AddChild(new HamlNodeHtmlAttributeCollection(0, "(class='class')"));

            // Act
            _tagWalker.Walk(tagNode);

            // Assert
            const string expectedIdAttr = @"class='class'";

            Assert.That(_classBuilderMock.Build(""), Is.StringContaining(expectedIdAttr));
        }
예제 #19
0
        public void Walk_MultipleIdNodes_WritesCorrectIdAttribute()
        {
            // Arrange
            var tagNode = new HamlNodeTag(new HamlLine("p", HamlRuleEnum.Tag, "", 0));

            tagNode.AddChild(new HamlNodeTagId(0, "id1"));
            tagNode.AddChild(new HamlNodeTagId(0, "id2"));

            // Act
            _tagWalker.Walk(tagNode);

            // Assert
            const string expectedIdAttr = @"id='id2'";

            Assert.That(_classBuilderMock.Build(""), Is.StringContaining(expectedIdAttr));
        }
예제 #20
0
        private static IList <HamlNodeTextContainer> GetIdValues(HamlNodeTag nodeTag)
        {
            var idValues = (from collection in nodeTag.Children.OfType <HamlNodeHtmlAttributeCollection>()
                            from attr in collection.Children.OfType <HamlNodeHtmlAttribute>()
                            where attr.Name == "id"
                            from attrFragment in attr.Children
                            select(HamlNodeTextContainer) attrFragment).ToList();

            var idTag = nodeTag.Children.LastOrDefault(x => x.GetType() == typeof(HamlNodeTagId));

            if (idTag != null)
            {
                idValues.Insert(0, new HamlNodeTextContainer(idTag.SourceFileLineNum, idTag.Content));
            }
            return(idValues);
        }
예제 #21
0
        private bool IsPreCloseTagWhitespaceTrimmed(HamlNodeTag nodeTag)
        {
            if (nodeTag.IsMultiLine == false || nodeTag.WhitespaceRemoval == WhitespaceRemoval.Internal)
            {
                return(true);
            }

            var lastNonWhitespaceChild = GetLastNonWhitespaceChild(nodeTag) as HamlNodeTag;

            if (lastNonWhitespaceChild == null)
            {
                return(false);
            }

            return((lastNonWhitespaceChild).WhitespaceRemoval == WhitespaceRemoval.Surrounding);
        }
예제 #22
0
        public void Walk_NestedTags_AppendsCorrectTags()
        {
            // Arrange
            const string tagName    = "p";
            const string nestedText = "Hello world";
            var          tagNode    = new HamlNodeTag(new HamlLine(tagName, HamlRuleEnum.Tag, "", 0));

            tagNode.AddChild(new HamlNodeTextContainer(new HamlLine(nestedText, HamlRuleEnum.PlainText, "", 1, true)));
            // Act
            _tagWalker.Walk(tagNode);

            // Assert
            string expectedTag = string.Format("<{0}>{1}</{0}>", tagName, nestedText);

            Assert.That(_classBuilderMock.Build(""), Is.EqualTo(expectedTag));
        }
예제 #23
0
        public void Walk_IdFollowedByClassNodes_OrderedCorrectly()
        {
            // Arrange
            const string tagId    = "id";
            const string tagClass = "class";
            var          tagNode  = new HamlNodeTag(new HamlLine("p", HamlRuleEnum.Tag, "", 0));

            tagNode.AddChild(new HamlNodeTagId(0, tagId));
            tagNode.AddChild(new HamlNodeTagClass(0, tagClass));

            // Act
            _tagWalker.Walk(tagNode);

            // Assert
            const string expectedTag = @"<p class='class' id='id'></p>";

            Assert.That(_classBuilderMock.Build(""), Is.EqualTo(expectedTag));
        }
예제 #24
0
 private void AppendTagBodyAndClose(HamlNodeTag nodeTag)
 {
     if (nodeTag.IsSelfClosing || Options.IsAutoClosingTag(nodeTag.TagName))
     {
         ClassBuilder.AppendSelfClosingTagSuffix();
     }
     else
     {
         ClassBuilder.Append(">");
         base.Walk(nodeTag);
         if (IsPreCloseTagWhitespaceTrimmed(nodeTag))
         {
             ClassBuilder.AppendFormat("</{0}>", nodeTag.NamespaceQualifiedTagName);
         }
         else
         {
             ClassBuilder.AppendNewLine();
             ClassBuilder.AppendFormat(nodeTag.Indent + "</{0}>", nodeTag.NamespaceQualifiedTagName);
         }
     }
 }
예제 #25
0
 private void AppendAttributes(HamlNodeTag nodeTag)
 {
     MakeClassAttribute(nodeTag);
     MakeIdAttribute(nodeTag);
     WalkHtmlStyleAttributes(nodeTag);
 }
예제 #26
0
        private void MakeClassAttribute(HamlNodeTag nodeTag)
        {
            var classValues = GetClassValues(nodeTag);

            AppendClassAttribute(classValues);
        }
예제 #27
0
        private void MakeIdAttribute(HamlNodeTag nodeTag)
        {
            var idValues = GetIdValues(nodeTag);

            AppendIdAttribute(idValues);
        }
예제 #28
0
 private HamlNode GetLastNonWhitespaceChild(HamlNodeTag nodeTag)
 {
     return(nodeTag.Children.LastOrDefault(x => x.IsWhitespaceNode() == false));
 }
예제 #29
0
        public void Constructor_SimpleTags_GeneratesCorrectTagName(string templateLine, string expectedTagName)
        {
            var tag = new HamlNodeTag(new HamlLine(templateLine, HamlRuleEnum.Tag, "", 0));

            Assert.That(tag.TagName, Is.EqualTo(expectedTagName));
        }
예제 #30
0
        public void Constructor_SimpleTags_DeterminesSelfClosingCorrectly(string templateLine, bool expectedSelfClosing)
        {
            var tag = new HamlNodeTag(new HamlLine(templateLine, HamlRuleEnum.Tag, "", 0));

            Assert.That(tag.IsSelfClosing, Is.EqualTo(expectedSelfClosing));
        }