Example #1
0
        public static XhtmlString WrapContent(XhtmlString xhtmlString,
                                              string wrapperClass,
                                              string blockWrapperClass,
                                              IEnumerable <Type> blockTypesToExclude)
        {
            blockTypesToExclude = blockTypesToExclude?.ToList() ?? new List <Type>();
            var textWrapperStart  = new StaticFragment($"<div class=\"{wrapperClass}\">");
            var textWrapperEnd    = new StaticFragment("</div>");
            var blockWrapperStart = blockWrapperClass != null
                ? new StaticFragment($"<section class=\"{blockWrapperClass}\">")
                : new StaticFragment("");
            var blockWrapperEnd = blockWrapperClass != null
                ? new StaticFragment("</section>")
                : new StaticFragment("");

            xhtmlString = xhtmlString.CreateWritableClone();

            var isNextBlock = xhtmlString.Fragments.LastOrDefault() is ContentFragment;

            xhtmlString.Fragments.Add(isNextBlock
                ? blockWrapperEnd
                : textWrapperEnd);

            for (var i = xhtmlString.Fragments.Count - 3; i >= 0; i--)
            {
                var currentFragment = xhtmlString.Fragments[i];
                var isCurrentBlock  = currentFragment is ContentFragment;

                if (!isCurrentBlock && string.IsNullOrWhiteSpace(currentFragment.InternalFormat))
                {
                    continue;
                }

                if (isCurrentBlock && blockTypesToExclude.Any(t => t.IsInstanceOfType(((ContentFragment)currentFragment).GetContent())))
                {
                    continue;
                }

                if (isCurrentBlock)
                {
                    xhtmlString.Fragments.Insert(i + 1, isNextBlock ? blockWrapperStart : textWrapperStart);
                    xhtmlString.Fragments.Insert(i + 1, blockWrapperEnd);
                }
                else if (isNextBlock)
                {
                    xhtmlString.Fragments.Insert(i + 1, blockWrapperStart);
                    xhtmlString.Fragments.Insert(i + 1, textWrapperEnd);
                }

                isNextBlock = isCurrentBlock;
            }

            xhtmlString.Fragments.Insert(0, isNextBlock ? blockWrapperStart : textWrapperStart);
            return(xhtmlString);
        }
Example #2
0
        private static IEnumerable <TestCaseData> TestCases(bool includeTextWrappers, bool includeBlockWrappers)
        {
            var textWrapperStart = includeTextWrappers
                ? new StaticFragment("<div class=\"wrapper\">")
                : new StaticFragment("");
            var textWrapperEnd = includeTextWrappers
                ? new StaticFragment("</div>")
                : new StaticFragment("");
            var blockWrapperStart = includeBlockWrappers
                ? new StaticFragment("<section class=\"blockWrapper\">")
                : new StaticFragment("");
            var blockWrapperEnd = includeBlockWrappers
                ? new StaticFragment("</section>")
                : new StaticFragment("");

            var text            = new StaticFragment("<p>text</p>");
            var contentFragment = CreateContentFragmentMock();

            var stringFragments = new StringFragmentCollection(new List <IStringFragment>
            {
                text
            });
            var expected = new StringFragmentCollection(new List <IStringFragment>
            {
                textWrapperStart,
                text,
                textWrapperEnd
            });

            yield return(new TestCaseData(stringFragments, expected).SetName(
                             "1"));

            stringFragments = new StringFragmentCollection(new List <IStringFragment>
            {
                text,
                contentFragment,
                contentFragment,
                text,
                contentFragment
            });
            expected = new StringFragmentCollection(new List <IStringFragment>
            {
                textWrapperStart,
                text,
                textWrapperEnd,
                blockWrapperStart,
                contentFragment,
                blockWrapperEnd,
                blockWrapperStart,
                contentFragment,
                blockWrapperEnd,
                textWrapperStart,
                text,
                textWrapperEnd,
                blockWrapperStart,
                contentFragment,
                blockWrapperEnd
            });
            yield return(new TestCaseData(stringFragments, expected).SetName(
                             "2"));

            stringFragments = new StringFragmentCollection(new List <IStringFragment>
            {
                contentFragment,
                contentFragment,
                contentFragment
            });
            expected = new StringFragmentCollection(new List <IStringFragment>
            {
                blockWrapperStart,
                contentFragment,
                blockWrapperEnd,
                blockWrapperStart,
                contentFragment,
                blockWrapperEnd,
                blockWrapperStart,
                contentFragment,
                blockWrapperEnd
            });

            yield return(new TestCaseData(stringFragments, expected).SetName(
                             "3"));

            stringFragments = new StringFragmentCollection(new List <IStringFragment>
            {
                contentFragment,
                text,
                contentFragment,
                contentFragment
            });
            expected = new StringFragmentCollection(new List <IStringFragment>
            {
                blockWrapperStart,
                contentFragment,
                blockWrapperEnd,
                textWrapperStart,
                text,
                textWrapperEnd,
                blockWrapperStart,
                contentFragment,
                blockWrapperEnd,
                blockWrapperStart,
                contentFragment,
                blockWrapperEnd
            });
            yield return(new TestCaseData(stringFragments, expected).SetName("4"));

            var emptyStringFragment = new StaticFragment("\n   \r\n");

            stringFragments = new StringFragmentCollection(new List <IStringFragment>
            {
                contentFragment,
                emptyStringFragment,
                contentFragment
            });
            expected = new StringFragmentCollection(new List <IStringFragment>
            {
                blockWrapperStart,
                contentFragment,
                blockWrapperEnd,
                blockWrapperStart,
                emptyStringFragment,
                contentFragment,
                blockWrapperEnd,
            });

            yield return(new TestCaseData(stringFragments, expected).SetName("Whitespace HTML block is not wrapped"));
        }