Example #1
0
        private int ProcessTextNode(StringCollection tokens, int index, HtmlElement htmlElement, HtmlNodeCollection nodes)
        {
            if (tokens == null)
            {
                throw new ArgumentNullException(nameof(tokens));
            }

            if (nodes == null)
            {
                throw new ArgumentNullException(nameof(nodes));
            }

            var value = tokens[index];

            if (_removeEmptyElementText)
            {
                value = RemoveWhitespace(value);
            }

            value = DecodeScript(value);
            if (_removeEmptyElementText && (value == null || value.Length == 0))
            {
                return(++index);
            }

            if (htmlElement == null || !htmlElement.NoEscaping)
            {
                value = HtmlElementEncoder.DecodeValue(value);
            }

            nodes.Add(new HtmlText(value));

            return(++index);
        }
Example #2
0
        private int ProcessStartTag(StringCollection tokens, int index, Action <HtmlElement> processHtmlElement)
        {
            if (tokens == null)
            {
                throw new ArgumentNullException(nameof(tokens));
            }

            if (++index >= tokens.Count)
            {
                return(-1);
            }

            var tagName = tokens[index++];
            var element = new HtmlElement(tagName);

            while (index < tokens.Count &&
                   !EndTagIdentifier.Equals(tokens[index], StringComparison.OrdinalIgnoreCase) &&
                   !EndTagIdentifierVariation1.Equals(tokens[index], StringComparison.OrdinalIgnoreCase))
            {
                var attributeName  = tokens[index++];
                var attributeValue = string.Empty;

                if (index < tokens.Count &&
                    EqualityIdentifier.Equals(tokens[index], StringComparison.OrdinalIgnoreCase))
                {
                    attributeValue = HtmlElementEncoder.DecodeValue(++index < tokens.Count ? tokens[index] : null);
                    index++;
                }
                else if (index < tokens.Count)
                {
                    attributeValue = null;
                }

                var attribute = new HtmlAttribute(attributeName, attributeValue);
                element.Attributes.Add(attribute);
            }

            processHtmlElement?.Invoke(element);

            if (index < tokens.Count &&
                EndTagIdentifierVariation1.Equals(tokens[index], StringComparison.OrdinalIgnoreCase))
            {
                element.IsTerminated = true;
                index++;
            }
            else if (index < tokens.Count &&
                     EndTagIdentifier.Equals(tokens[index], StringComparison.OrdinalIgnoreCase))
            {
                index++;
            }

            return(index);
        }