Пример #1
0
        public void HandlesBaseUri()
        {
            Tag tag = Tag.ValueOf("a");
            Attributes attribs = new Attributes();
            attribs["relHref"] = "/foo";
            attribs["absHref"] = "http://bar/qux";

            Element noBase = new Element(tag, "", attribs);
            Assert.AreEqual("", noBase.AbsUrl("relHref")); // with no base, should NOT fallback to href attrib, whatever it is
            Assert.AreEqual("http://bar/qux", noBase.AbsUrl("absHref")); // no base but valid attrib, return attrib

            Element withBase = new Element(tag, "http://foo/", attribs);
            Assert.AreEqual("http://foo/foo", withBase.AbsUrl("relHref")); // construct abs from base + rel
            Assert.AreEqual("http://bar/qux", withBase.AbsUrl("absHref")); // href is abs, so returns that
            Assert.AreEqual("", withBase.AbsUrl("noval"));

            Element dodgyBase = new Element(tag, "wtf://no-such-protocol/", attribs);
            Assert.AreEqual("http://bar/qux", dodgyBase.AbsUrl("absHref")); // base fails, but href good, so get that
            Assert.AreEqual("", dodgyBase.AbsUrl("relHref")); // base fails, only rel href, so return nothing 
        }
Пример #2
0
 private bool TestValidProtocol(Element el, Nodes.Attribute attr, ICollection<Whitelist.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 (Whitelist.Protocol protocol in protocols)
     {
         string prot = protocol.ToString() + ":";
         if (value.ToLower().StartsWith(prot, StringComparison.Ordinal))
         {
             return true;
         }
     }
     return false;
 }
Пример #3
0
 internal void MaybeSetBaseUri(Element @base)
 {
     if (baseUriSetFromDoc)
     {
         // only listen to the first <base href> in parse
         return;
     }
     string href = @base.AbsUrl("href");
     if (href.Length != 0)
     {
         // ignore <base target> etc
         baseUri = href;
         baseUriSetFromDoc = true;
         doc.BaseUri = href;
     }
 }