Exemplo n.º 1
0
            protected Attributes _attributes; // start tags get attributes on construction. End tags get attributes on first new attribute (but only for parser convenience, not used).

            public void NewAttribute()
            {
                if (_attributes == null)
                {
                    _attributes = new Attributes();
                }

                if (_pendingAttributeName != null)
                {
                    NSoup.Nodes.Attribute attribute;
                    if (_pendingAttributeValue == null)
                    {
                        attribute = new NSoup.Nodes.Attribute(_pendingAttributeName, "");
                    }
                    else
                    {
                        attribute = new NSoup.Nodes.Attribute(_pendingAttributeName, _pendingAttributeValue.ToString());
                    }

                    _attributes.Add(attribute);
                }
                _pendingAttributeName = null;
                if (_pendingAttributeValue != null)
                {
                    _pendingAttributeValue.Remove(0, _pendingAttributeValue.Length);
                }
            }
Exemplo n.º 2
0
        public bool IsSafeAttribute(string tagName, Element el, NSoup.Nodes.Attribute attr)
        {
            TagName      tag = TagName.ValueOf(tagName);
            AttributeKey key = AttributeKey.ValueOf(attr.Key);

            if (_attributes.ContainsKey(tag))
            {
                if (_attributes[tag].Contains(key))
                {
                    if (_protocols.ContainsKey(tag))
                    {
                        Dictionary <AttributeKey, HashSet <Protocol> > attrProts = _protocols[tag];
                        // ok if not defined protocol; otherwise test
                        return(!attrProts.ContainsKey(key) || TestValidProtocol(el, attr, attrProts[key]));
                    }
                    else
                    { // attribute found, no protocols defined, so OK
                        return(true);
                    }
                }
            }

            // no attributes defined for tag, try :all tag
            return(!tagName.Equals(":all") && IsSafeAttribute(":all", el, attr));
        }
Exemplo n.º 3
0
        private bool TestValidProtocol(Element el, NSoup.Nodes.Attribute attr, HashSet <Protocol> protocols)
        {
            // try to resolve relative urls to abs, and optionally update the attribute so output html has abs.
            // rels without a baseuri get removed
            string value = el.AbsUrl(attr.Key);

            if (value.Length == 0)
            {
                value = attr.Value; // if it could not be made abs, run as-is to allow custom unknown protocols
            }
            if (!_preserveRelativeLinks)
            {
                attr.Value = value;
            }

            foreach (Protocol protocol in protocols)
            {
                string prot = protocol.ToString() + ":";
                if (value.ToLowerInvariant().StartsWith(prot))
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 4
0
            protected Attributes _attributes; // start tags get attributes on construction. End tags get attributes on first new attribute (but only for parser convenience, not used).

            public void NewAttribute()
            {
                if (_attributes == null)
                {
                    _attributes = new Attributes();
                }

                if (_pendingAttributeName != null)
                {
                    NSoup.Nodes.Attribute attribute;
                    if (_pendingAttributeValue == null)
                    {
                        attribute = new NSoup.Nodes.Attribute(_pendingAttributeName, "");
                    }
                    else
                    {
                        attribute = new NSoup.Nodes.Attribute(_pendingAttributeName, _pendingAttributeValue.ToString());
                    }

                    _attributes.Add(attribute);
                }
                _pendingAttributeName = null;
                if (_pendingAttributeValue != null)
                {
                    _pendingAttributeValue.Remove(0, _pendingAttributeValue.Length);
                }
            }