Exemple #1
0
        private HtmlReaderStringValue ReadStringValue()
        {
            bool quote = false;
            //_propertyQuote = "";
            HtmlReaderStringValue value = new HtmlReaderStringValue();
            //value.Quote = "";
            char quoteChar = (char)PeekChar();

            if (quoteChar == '"' || quoteChar == '\'')
            {
                quote = true;
                //_propertyQuote = quoteChar.ToString();
                value.Quote = quoteChar;
                GetChar();
            }

            _stringBuilder.Remove(0, _stringBuilder.Length);
            while (true)
            {
                GetChar();
                if (_charInt == -1 ||
                    (quote && (_char == quoteChar || ((_char == '"' || _char == '\'') && PeekChar() == '>'))) ||
                    (!quote && (_char == ' ' || _char == '\t' || _char == '\r' || _char == '\n')) ||
                    _char == '>' || (_char == '/' && PeekChar() == '>')
                    )
                {
                    break;
                }
                _stringBuilder.Append(_char);
            }
            //_propertyValue = _stringBuilder.ToString();
            //_propertyValue = HtmlCharCodes.TranslateCode(_propertyValue);
            value.Value = HtmlCharCodes.TranslateCode(_stringBuilder.ToString());
            return(value);
        }
Exemple #2
0
        private HtmlReaderStringValue ReadStringValue()
        {
            HtmlReaderStringValue value = new HtmlReaderStringValue();

            bool quote     = false;
            char quoteChar = (char)_charStreamReader.PeekChar();

            if (quoteChar == '"' || quoteChar == '\'')
            {
                quote       = true;
                value.Quote = quoteChar;
                _charStreamReader.ReadChar();
            }

            char car = '\0';

            Func <bool> isString_v1 = () =>
            {
                if (quote)
                {
                    if (car == quoteChar)
                    {
                        _charStreamReader.ReadChar();
                        return(false);
                    }
                    if ((car == '"' || car == '\'') && _charStreamReader.PeekChar(1) == '>')
                    {
                        _charStreamReader.ReadChar();
                        return(false);
                    }
                }
                else
                {
                    if (car == ' ' || car == '\t' || car == '\r' || car == '\n')
                    {
                        return(false);
                    }
                    if (car == '>' || (car == '/' && _charStreamReader.PeekChar(1) == '>'))
                    {
                        return(false);
                    }
                }
                // bad test see _useReadAttributeValue_v2
                if (car == '>' || (car == '/' && _charStreamReader.PeekChar(1) == '>'))
                {
                    return(false);
                }
                return(true);
            };

            Func <bool> isString_v2 = () =>
            {
                if (quote)
                {
                    if (car == quoteChar)
                    {
                        _charStreamReader.ReadChar();
                        return(false);
                    }
                    if ((car == '"' || car == '\'') && _charStreamReader.PeekChar(1) == '>')
                    {
                        _charStreamReader.ReadChar();
                        return(false);
                    }
                }
                else
                {
                    if (car == ' ' || car == '\t' || car == '\r' || car == '\n')
                    {
                        return(false);
                    }
                    if (car == '>' || (car == '/' && _charStreamReader.PeekChar(1) == '>'))
                    {
                        return(false);
                    }
                }
                return(true);
            };

            Func <bool> isString;

            if (!_useReadAttributeValue_v2)
            {
                isString = isString_v1;
            }
            else
            {
                isString = isString_v2;
            }

            _stringBuilder.Remove(0, _stringBuilder.Length);
            while (true)
            {
                int code = _charStreamReader.PeekChar();
                if (code == -1)
                {
                    break;
                }
                car = (char)code;
                //if ((quote && (car == quoteChar || ((car == '"' || car == '\'') && _charStreamReader.PeekChar(1) == '>')))
                //    || (!quote && (car == ' ' || car == '\t' || car == '\r' || car == '\n'))
                //    || car == '>' || (car == '/' && _charStreamReader.PeekChar(1) == '>'))
                //    break;
                //if (quote)
                //{
                //    if (car == quoteChar)
                //    {
                //        _charStreamReader.ReadChar();
                //        break;
                //    }
                //    if ((car == '"' || car == '\'') && _charStreamReader.PeekChar(1) == '>')
                //    {
                //        _charStreamReader.ReadChar();
                //        break;
                //    }
                //}
                //else
                //{
                //    if (car == ' ' || car == '\t' || car == '\r' || car == '\n')
                //        break;
                //    if (car == '>' || (car == '/' && _charStreamReader.PeekChar(1) == '>'))
                //        break;
                //}
                if (!isString())
                {
                    break;
                }
                _stringBuilder.Append(car);
                _charStreamReader.ReadChar();
            }
            value.Value = HtmlCharCodes.TranslateCode(_stringBuilder.ToString());
            return(value);
        }
Exemple #3
0
        private IEnumerable <HtmlNode> ReadOpenTag()
        {
            // read <
            _charStreamReader.ReadChar();
            HtmlNodeOpenTag openTag = new HtmlNodeOpenTag {
                Index  = _htmlNodeIndex++, Line = _disableLineColumn ? 0 : _charStreamReader.Line,
                Column = _disableLineColumn ? 0 : _charStreamReader.Column
            };

            // read tag name
            openTag.Name = ReadTagName();

            //if (string.Compare(openTag.Name, "script", true) == 0)
            if (!_disableScriptTreatment && string.Compare(openTag.Name, "script", true) == 0)
            {
                openTag.IsScript = true;
            }

            yield return(openTag);

            int code = _charStreamReader.PeekChar();

            if (code == -1)
            {
                yield break;
            }

            char car = (char)code;

            ReadSeparator();

            int line   = 0;
            int column = 0;

            code = _charStreamReader.PeekChar();
            car  = (char)code;
            // read properties
            if (car != '/' && car != '>')
            {
                while (true)
                {
                    // read property name
                    _stringBuilder.Remove(0, _stringBuilder.Length);
                    line   = 0;
                    column = 0;
                    while (true)
                    {
                        code = _charStreamReader.PeekChar();
                        if (code == -1)
                        {
                            break;
                        }
                        car = (char)code;
                        if (car == ' ' || car == '\t' || car == '\r' || car == '\n' || car == '=' || car == '>' || car == '<' || car == '/')
                        {
                            break;
                        }
                        _stringBuilder.Append(car);
                        _charStreamReader.ReadChar();
                        if (line == 0)
                        {
                            line   = _charStreamReader.Line;
                            column = _charStreamReader.Column;
                        }
                    }
                    if (_stringBuilder.Length == 0)
                    {
                        break;
                    }

                    HtmlNodeProperty property = new HtmlNodeProperty
                    {
                        Index  = _htmlNodeIndex++,
                        Line   = _disableLineColumn ? 0 : line,
                        Column = _disableLineColumn ? 0 : column
                    };

                    property.Name = _stringBuilder.ToString();
                    ReadSeparator();

                    if ((char)_charStreamReader.PeekChar() == '=')
                    {
                        _charStreamReader.ReadChar();
                        ReadSeparator();
                        HtmlReaderStringValue value = ReadStringValue();
                        property.Quote = value.Quote;
                        property.Value = value.Value;
                        ReadSeparator();
                    }
                    yield return(property);
                }
            }

            code = _charStreamReader.PeekChar();
            car  = (char)code;

            bool endTag = false;

            line   = 0;
            column = 0;
            if (car == '/')
            {
                endTag = true;
                _charStreamReader.ReadChar();
                line   = _charStreamReader.Line;
                column = _charStreamReader.Column;
                code   = _charStreamReader.PeekChar();
                car    = (char)code;
            }

            if (car == '>')
            {
                _charStreamReader.ReadChar();
                if (endTag)
                {
                    yield return(CreateHtmlNodeEndTag(openTag.Name, line, column));
                }
                else if (_generateCloseTag)
                {
                    yield return(CreateHtmlNodeCloseTag(openTag.Name, _charStreamReader.Line, _charStreamReader.Column));
                }
            }
        }