示例#1
0
        private bool TestValidProtocol(Element el, NSoup_UWP.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);
        }
示例#2
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_UWP.Nodes.Attribute attribute;
                    if (_pendingAttributeValue == null)
                    {
                        attribute = new NSoup_UWP.Nodes.Attribute(_pendingAttributeName, "");
                    }
                    else
                    {
                        attribute = new NSoup_UWP.Nodes.Attribute(_pendingAttributeName, _pendingAttributeValue.ToString());
                    }

                    _attributes.Add(attribute);
                }
                _pendingAttributeName = null;
                if (_pendingAttributeValue != null)
                {
                    _pendingAttributeValue.Remove(0, _pendingAttributeValue.Length);
                }
            }
示例#3
0
        public bool IsSafeAttribute(string tagName, Element el, NSoup_UWP.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));
        }