Example #1
0
        private void TrimNodeOnStart(HtmlNode node)
        {
            var textNode = node as HtmlText;

            if (textNode != null)
            {
                TrimNodeOnStart(textNode);
            }

            if (node is HtmlComment && settings.RemoveComments)
            {
                var  comment     = (HtmlComment)node;
                bool keepComment = false;
                foreach (var regex in settings.KeepCommentsRegex)
                {
                    var result = regex.Match(comment.Slice.Text, comment.Slice.Start, comment.Slice.Length);
                    if (result.Success)
                    {
                        keepComment = true;
                        break;
                    }
                }

                // Don't remove special ignoring comments
                if (!keepComment)
                {
                    node.Remove();
                }
            }

            // Remove HTML script
            if (settings.RemoveJavaScript && node is HtmlElement && ((HtmlElement)node).Descriptor?.Name == "script")
            {
                node.Remove();
            }

            // If current node requires preserving formatting inside it we need to trim all pending text node that we collected before
            var nodeName = node is HtmlElement ? ((HtmlElement)node).Name : null;

            if (settings.CollapseWhitespaces && !string.IsNullOrEmpty(nodeName) && settings.TagsWithNonCollapsableWhitespaces.ContainsKey(nodeName))
            {
                TrimPendingTextNodes();
            }
        }
Example #2
0
        private void TrimNodeOnStart(HtmlNode node)
        {
            var textNode = node as HtmlText;

            if (textNode != null)
            {
                TrimNodeOnStart(textNode);
            }

            if (node is HtmlComment && settings.RemoveComments)
            {
                var  comment     = (HtmlComment)node;
                bool keepComment = false;
                foreach (var regex in settings.KeepCommentsRegex)
                {
                    var result = regex.Match(comment.Slice.Text, comment.Slice.Start, comment.Slice.Length);
                    if (result.Success)
                    {
                        keepComment = true;
                        break;
                    }
                }

                // Don't remove special ignoring comments
                if (!keepComment)
                {
                    node.Remove();
                }
            }

            // Remove HTML script
            if (settings.RemoveJavaScript && node is HtmlElement && ((HtmlElement)node).Descriptor.Name == "script")
            {
                node.Remove();
            }
        }
Example #3
0
        private void TrimNodeOnStart(HtmlNode node)
        {
            var textNode = node as HtmlText;

            if (textNode != null)
            {
                TrimNodeOnStart(textNode);
            }

            if (node is HtmlComment && settings.RemoveComments)
            {
                var comment = (HtmlComment)node;
                // Don't remove special ignoring comments
                if (!comment.Slice.StartsWith("!"))
                {
                    node.Remove();
                }
            }
        }