Пример #1
0
        public static string WrapElements(this string html, Func <Fragment, bool> matchElement, Func <string> before, Func <string> after, WrapElementsType wrapType)
        {
            StringBuilder sb = new StringBuilder(html.Length);

            void AppendElement(WithinElementDomElementEvent ev)
            {
                if (ev.ElementEvent.Fragment != null)
                {
                    sb.Append(html, ev.ElementEvent.Fragment.Trivia.StartPosition, ev.ElementEvent.Fragment.Trivia.Length);
                }
            }

            var events = HtmlLexer.Read(html).WithInElement(matchElement, false);

            events.ProcessElements(
                outside: AppendElement,
                onEnter: (ev) =>
            {
                switch (wrapType)
                {
                case WrapElementsType.ReplaceMatchedElements:
                    sb.Append(before?.Invoke());
                    break;

                case WrapElementsType.AddOuterWrapper:
                    sb.Append(before?.Invoke());
                    AppendElement(ev);
                    break;

                case WrapElementsType.AddInnerWrapper:
                    AppendElement(ev);
                    sb.Append(before?.Invoke());
                    break;
                }
            },
                inside: AppendElement,
                onExit: (ev) =>
            {
                switch (wrapType)
                {
                case WrapElementsType.ReplaceMatchedElements:
                    sb.Append(after?.Invoke());
                    break;

                case WrapElementsType.AddOuterWrapper:
                    AppendElement(ev);
                    sb.Append(after?.Invoke());
                    break;

                case WrapElementsType.AddInnerWrapper:
                    sb.Append(after?.Invoke());
                    AppendElement(ev);
                    break;
                }
            });

            return(sb.ToString());
        }
Пример #2
0
        public static IEnumerable <string> ExtractElementsList(this string html, Func <Fragment, bool> matchElement, NodeType nodeType, bool textOnly = false)
        {
            StringBuilder sb     = new StringBuilder();
            List <string> output = new List <string>();

            void appendElement(WithinElementDomElementEvent ev)
            {
                if (ev.ElementEvent.Fragment == null)
                {
                    return;
                }

                if (textOnly && ev.ElementEvent.Fragment.FragmentType != FragmentType.Text)
                {
                    return;
                }

                sb.Append(html, ev.ElementEvent.Fragment.Trivia.StartPosition, ev.ElementEvent.Fragment.Trivia.Length);
            };

            void AddToList()
            {
                output.Add(sb.ToString());
                sb.Clear();
            }

            Action <WithinElementDomElementEvent> doNothing = (ev) => { };

            var events = HtmlLexer.Read(html).WithInElement(matchElement, false);

            events.ProcessElements(
                outside: doNothing,
                onEnter: (ev) =>
            {
                if (nodeType == NodeType.OuterNode)
                {
                    appendElement(ev);
                }
            },
                inside: appendElement,
                onExit: (ev) =>
            {
                if (nodeType == NodeType.OuterNode)
                {
                    appendElement(ev);
                }
                AddToList();
            }
                );

            return(output);
        }
Пример #3
0
        public static string ReplaceElements(this string html, Func <Fragment, bool> matchElement, Func <string> replace, NodeType replaceType)
        {
            StringBuilder sb = new StringBuilder(html.Length);

            void AppendElement(WithinElementDomElementEvent ev)
            {
                if (ev.ElementEvent.Fragment != null)
                {
                    sb.Append(html, ev.ElementEvent.Fragment.Trivia.StartPosition, ev.ElementEvent.Fragment.Trivia.Length);
                }
            }

            var events = HtmlLexer.Read(html).WithInElement(matchElement, false);

            bool replaced = false;

            events.ProcessElements(
                outside: AppendElement,
                onEnter: (ev) =>
            {
                switch (replaceType)
                {
                case NodeType.InnerNode:
                    AppendElement(ev);
                    break;
                }
            },
                inside: (_) =>
            {
                if (!replaced)
                {
                    sb.Append(replace?.Invoke());
                    replaced = true;
                }
            },
                onExit: (ev) =>
            {
                switch (replaceType)
                {
                case NodeType.InnerNode:
                    AppendElement(ev);
                    break;
                }
            });

            return(sb.ToString());
        }
Пример #4
0
 public static IEnumerable <DomElementEvent> Execute(string rawHtml) => Execute(HtmlLexer.Read(rawHtml));