Esempio n. 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>
        /// 在 CSS 中,选择器是一种模式,用于选择需要添加样式的元素。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Query_Click(object sender, RoutedEventArgs e)
        {
            TextRange tr = new TextRange(rbox_Input.Document.ContentStart, rbox_Input.Document.ContentEnd);

            var selector = this.tbox_CSSSelector.Text.Trim();
            var html     = tr.Text;

            if (string.IsNullOrEmpty(selector))
            {
                EMessageBox.Show("请输入CSS选择器");
                return;
            }

            var angleSharpObj = AngleSharpHelper.GetInstance(html).CSSQuery(selector);

            if (angleSharpObj != null)
            {
                //目前只输出一个结果
                //过完年再搞 ╮(-_-)╭
                this.rbox_Output.Document = new FlowDocument(new Paragraph(new Run(angleSharpObj.OuterHtml)));
            }
            else
            {
                this.rbox_Output.Document = new FlowDocument(new Paragraph(new Run("未匹配到结果")));
            }
        }
Esempio n. 3
0
 private Task ExtractLink(string source)
 {
     return(Task.Run(() => {
         AngleSharpHelper helper = new AngleSharpHelper();
         helper.Init(source);
         var tagAList = helper.CSSQueryAll("a");
         foreach (var item in tagAList)
         {
             var url = item.Attributes["href"]?.Value;
             if (string.IsNullOrEmpty(url) == false)
             {
                 if (RegexUtil.IsUrl(url) == true)
                 {
                     //TODO 需要使用分组构造 排除文件路径 如http://abc.com/test.exe 今天太累了 想不动了
                     AppendText(url);
                     queue.Enqueue(url);
                 }
             }
         }
     }));
 }
        /// <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);
        }