Пример #1
0
        public INode Parse(HtmlParser parser, HtmlPortion current)
        {
            if (!IsStyle(current))
            {
                return(null);
            }

            var attr         = parser.GetAttribute(current);
            var end          = current.StartIndexOf(new Regex(@"</style\s*>"));
            var contentIndex = end - current.Current;

            current.Next();
            var content = current.Substring(contentIndex);

            current.Jump(contentIndex);
            if (attr.IsSingle)
            {
                return(new StyleTag("<style", attr.Attributes, "", _info));
            }
            var result = new StyleTag("style", attr.Attributes, content, _info);

            current.Jump(current.IndexOf(new Regex(@"</style\s*>")));
            current.Jump();
            return(result);
        }
Пример #2
0
        public INode Parse(HtmlParser parser, HtmlPortion current)
        {
            if (current.IsStartTagChar() && current.IsNext('!'))
            {
                var text = current.Substring(' ');
                var t    = text.ToLower() == "<!doctype ";
                if (t)
                {
                    var index = current.IndexOf('>');
                    current.Jump(index);
                    current.Next();
                    return(new DocumentTypeTag());
                }
            }

            return(null);
        }
Пример #3
0
        public INode Parse(HtmlParser parser, HtmlPortion current)
        {
            if (!IsValid(current))
            {
                return(null);
            }
            current.Next(4);
            var content = string.Empty;

            while (current.HasNext)
            {
                if (current.Is("-->"))
                {
                    current.Next(2);
                    break;
                }
                content += current.Char;
                current.Next();
            }
            current.Jump();
            return(new Comment(content, _info));
        }
Пример #4
0
        public AttributeParseResult Parse(HtmlPortion current)
        {
            if (!IsValid(current))
            {
                throw new HtmlParseException("tag is not valid");
            }
            current.Next();
            var attributes = new Attributes();
            var attribute  = new AttributeSingle();

            while (current.HasNext)
            {
                if (current.IsWhiteSpace)
                {
                    if (attribute.ValueEmpty && !attribute.KeyEmpty)
                    {
                        attributes.Add(attribute.Pull());
                    }
                    current.Jump();
                    continue;
                }

                if (current.Char == '/' && current[current.NextNonWhiteSpace()] == '>')
                {
                    var next = current.NextNonWhiteSpace();
                    current.Jump(next);
                    if (!attributes.Any())
                    {
                        if (attribute.ValueEmpty && !attribute.KeyEmpty)
                        {
                            attributes.Add(attribute.Pull());
                        }
                        if (!attributes.Any())
                        {
                            throw new HtmlParseException("tag is not valid");
                        }
                    }
                    var tagName = attributes.First().Key;
                    return(new AttributeParseResult(true, tagName, new Attributes(attributes.Skip(1).ToArray())));
                }

                if (current.Char == '>')
                {
                    if (attributes.IsEmpty)
                    {
                        if (!attribute.Empty)
                        {
                            attributes.Add(attribute.Pull());
                        }
                    }
                    if (attributes.IsEmpty)
                    {
                        throw new HtmlParseException("tag is not valid");
                    }
                    var tagName = attributes.First().Key;
                    return(new AttributeParseResult(false, tagName, new Attributes(attributes.Skip(1).ToArray())));
                }

                if (current.Is('=') && AttributeStringProcess.IsValueStart(current.NextNonWhiteSpaceChar()))
                {
                    current.Jump();
                    attribute.SetType(AttributeNameValue.Value);
                    attribute.Insert(new AttributeStringProcess(current).GetValue());
                    attributes.Add(attribute.Pull());
                    attribute.SetType(AttributeNameValue.Key);
                    current.Jump();
                    continue;
                }
                attribute.Insert(current.Char);
                current.Next();
            }
            throw new HtmlParseException("tag is not valid");
        }