示例#1
0
        private ElementCloseTag ParseClosingTag()
        {
            int    startPos = _pos;
            string prefix = null, localName = null;

            int index = _pos + 2;           // Skip the </

            if (!ParseName(ref index, out prefix, out localName))
            {
                return(null);
            }

            SkipWhiteSpaces(ref index);

            if (_length <= index)
            {
                return(null);
            }
            char c = _text[index];

            if (c != '>')
            {
                return(null);
            }

            _pos = index + 1;
            ElementCloseTag closeTag = new ElementCloseTag();

            closeTag.SetName(prefix, localName);
            closeTag.SetPosition(startPos, index);
            return(closeTag);
        }
示例#2
0
        public Node ReadNext()
        {
            int startText = -1;

            if (_previouslyReadNode != null)
            {
                Node node = _previouslyReadNode;
                _previouslyReadNode = null;
                return(node);
            }
            for (; _pos < _length - 1 /* we do this -1 since there is no possible node that only has 1 char */; _pos++)
            {
                int  backupPos = _pos;
                char c         = _text[_pos];
                switch (c)
                {
                case '<':
                    if (_text[_pos + 1] == '/')
                    {
                        ElementCloseTag closeTag = ParseClosingTag();
                        if (closeTag == null)
                        {
                            //here we should deal with non-element, only Text?
                            if (startText == -1)
                            {
                                startText = _pos;
                            }
                        }
                        else
                        {
                            if (startText != -1)
                            {
                                TextNode textNode = CheckForMissingText(startText, backupPos, closeTag);
                                if (textNode != null)
                                {
                                    return(textNode);
                                }
                            }
                            return(closeTag);
                        }
                    }
                    else if (_text[_pos + 1] == '!')
                    {
                        Node result = null;
                        if (_pos + 3 < _length && _text[_pos + 2] == '-' && _text[_pos + 3] == '-')
                        {
                            result = ParseComment();
                        }
                        else
                        {
                            result = ParseDocumentType();
                        }
                        if (result == null)
                        {
                            if (startText == -1)
                            {
                                startText = _pos;
                            }
                            break;
                        }
                        else if (startText != -1)
                        {
                            TextNode textNode = CheckForMissingText(startText, backupPos, result);                                     //backupPos +1
                            if (textNode != null)
                            {
                                return(textNode);
                            }
                        }
                        return(result);
                    }
                    else if (_text[_pos + 1] == '?')
                    {
                        ProcessingInstruction piTag = ParseProcessingInstruction();
                        if (piTag == null)
                        {
                            //here we should deal with non-element, only Text?
                            if (startText == -1)
                            {
                                startText = _pos;
                            }
                        }
                        else
                        {
                            if (startText != -1)
                            {
                                TextNode textNode = CheckForMissingText(startText, backupPos, piTag);
                                if (textNode != null)
                                {
                                    return(textNode);
                                }
                            }
                            return(piTag);
                        }
                    }
                    else
                    {
                        Element element = ParseStartTag();
                        if (element != null)
                        {
                            if (startText != -1)
                            {
                                TextNode textNode = CheckForMissingText(startText, backupPos, element);
                                if (textNode != null)
                                {
                                    return(textNode);
                                }
                            }
                            return(element);
                        }
                        else
                        {
                            //here we should deal with non-element, only Text?
                            if (startText == -1)
                            {
                                startText = _pos;
                            }
                        }
                    }
                    break;

                //					case '\r': case '\n':
                //						// only if this is first character from text it will be ignored
                //						break;
                default:
                    if (startText == -1)
                    {
                        startText = _pos;
                    }
                    break;
                }
            }
            if (startText == -1 && _pos == _length - 1)
            {
                startText = _pos;
                _pos++;
            }
            if (startText != -1)
            {
                TextNode textNode = CheckForMissingText(startText, _length, null);
                _pos = _length;
                if (textNode != null)
                {
                    return(textNode);
                }
            }

            return(null);
        }