Пример #1
0
        public static string WrapInTag(
            string pageBlocksHtml,
            string wrappingTagName,
            bool allowMultiplePageBlocks,
            Dictionary <string, string> additonalHtmlAttributes,
            Dictionary <string, string> editorAttributes = null
            )
        {
            const string DEFAULT_TAG = "div";

            var parser   = new HtmlParser();
            var document = parser.Parse(pageBlocksHtml.Trim());

            IElement wrapper;

            // No need to wrap if its a single page block with a single outer node.
            if (!allowMultiplePageBlocks && document.Body.Children.Length == 1 && wrappingTagName == null)
            {
                wrapper = document.Body.Children.First();
            }
            else
            {
                wrapper = document.CreateElement(wrappingTagName ?? DEFAULT_TAG);
                AngleSharpHelper.WrapChildren(document.Body, wrapper);
            }

            AngleSharpHelper.MergeAttributes(wrapper, additonalHtmlAttributes);

            if (editorAttributes != null)
            {
                AngleSharpHelper.MergeAttributes(wrapper, editorAttributes);
            }

            return(wrapper.OuterHtml);
        }
        /// <summary>
        /// Parses the rendered html string of a block, when in edit mode. It adds attributes used for hovering and interacting
        /// with a block in siteviewer mode. It also adds a css class.
        /// </summary>
        private string ParseBlockHtmlForEditing(string blockHtml, IEntityVersionPageBlockRenderDetails blockViewModel)
        {
            string entityType = blockViewModel is CustomEntityVersionPageBlockRenderDetails ? "custom-entity" : "page";

            var attrs = new Dictionary <string, string>();

            attrs.Add("class", "cofoundry-sv__block");
            attrs.Add("data-cms-" + entityType + "-region-block", string.Empty);

            if (blockViewModel != null)
            {
                attrs.Add("data-cms-version-block-id", blockViewModel.EntityVersionPageBlockId.ToString());
                attrs.Add("data-cms-page-block-type-id", blockViewModel.BlockType.PageBlockTypeId.ToString());
                attrs.Add("data-cms-page-block-title", blockViewModel.BlockType.Name.ToString());
            }

            var parser   = new HtmlParser();
            var document = parser.Parse(blockHtml.Trim());

            var elements = document.Body.Children;

            if (elements.Length == 1)
            {
                var element = elements.Single();

                if (element.NodeType == NodeType.Element)
                {
                    AngleSharpHelper.MergeAttributes(element, attrs);

                    return(element.OuterHtml);
                }
            }

            var wrapper = document.CreateElement("div");

            AngleSharpHelper.WrapChildren(document.Body, wrapper);
            AngleSharpHelper.MergeAttributes(wrapper, attrs);

            return(wrapper.OuterHtml);
        }