예제 #1
0
 private void OpenTag(HtmlNodeOpenTag openTag)
 {
     _node = null;
     // tag : <P> <br> <a> <img>
     switch (openTag.Name.ToLower())
     {
         case "p":
             _node = new HtmlDocNodeBeginTag() { Tag = HtmlTagType.P, Index = ++_nodeIndex, Line = openTag.Line, Column = openTag.Column };
             break;
         case "br":
             _node = new HtmlDocNodeTag() { Tag = HtmlTagType.BR, Index = ++_nodeIndex, Line = openTag.Line, Column = openTag.Column };
             break;
         case "a":
             _node = new HtmlDocNodeBeginTagA() { Index = ++_nodeIndex, Line = openTag.Line, Column = openTag.Column };
             break;
         case "img":
             _node = new HtmlDocNodeTagImg() { Index = ++_nodeIndex, Line = openTag.Line, Column = openTag.Column };
             break;
     }
 }
예제 #2
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);
            }
        }