Example #1
0
        public void InterpolationToken_Uniqueness()
        {
            var interpolationToken = new InterpolationToken
            {
                Content = new StringSlice("foo"),
                Indent  = 0,
                ContentStartPosition = 0,
                ContentEndPosition   = 2,
                IsClosed             = true,
                TagStartPosition     = 0,
                TagEndPosition       = 2
            };

            var interpolationToken2 = new InterpolationToken
            {
                Content = new StringSlice("foo"),
                Indent  = 0,
                ContentStartPosition = 0,
                ContentEndPosition   = 2,
                IsClosed             = true,
                TagStartPosition     = 0,
                TagEndPosition       = 2
            };

            Assert.False(interpolationToken.Equals(null));
            Assert.Equal(interpolationToken, interpolationToken2);
            Assert.Equal(interpolationToken.GetHashCode(), interpolationToken2.GetHashCode());
        }
Example #2
0
        /// <summary>
        /// Tries to match interpolation tags from the current slice
        /// </summary>
        /// <param name="processor">The processor</param>
        /// <param name="slice">The slice</param>
        /// <returns>If the match was successful</returns>
        public override bool Match(Processor processor, ref StringSlice slice)
        {
            var tagStart         = slice.Start - processor.CurrentTags.StartTag.Length;
            var index            = slice.Start;
            var escapeResult     = true;
            var isTripleMustache = false;

            while (slice[index].IsWhitespace())
            {
                index++;
            }

            var match = slice[index];

            if (match == '&')
            {
                escapeResult = false;
                index++;
            }
            else if (match == '{')
            {
                escapeResult     = false;
                isTripleMustache = true;
                index++;
            }

            while (slice[index].IsWhitespace())
            {
                index++;
            }

            slice.Start = index;
            var startIndex = index;

            var endTag = isTripleMustache ? '}' + processor.CurrentTags.EndTag : processor.CurrentTags.EndTag;

            while (!slice.IsEmpty && !slice.Match(endTag))
            {
                slice.NextChar();
            }

            var content = new StringSlice(slice.Text, startIndex, slice.Start - 1);

            content.TrimEnd();
            var contentEnd = content.End + 1;

            var tag = new InterpolationToken
            {
                EscapeResult         = escapeResult,
                TagStartPosition     = tagStart,
                ContentStartPosition = startIndex,
                IsClosed             = true
            };

            if (!slice.Match(endTag))
            {
                throw new StubbleException($"Unclosed Tag at {slice.Start.ToString()}");
            }

            tag.ContentEndPosition = contentEnd;
            tag.TagEndPosition     = slice.Start + endTag.Length;
            slice.Start           += endTag.Length;

            processor.CurrentToken          = tag;
            processor.HasSeenNonSpaceOnLine = true;

            return(true);
        }