/// <summary>Remove allowed URL protocols for an element's URL attribute.</summary> /// <remarks> /// Remove allowed URL protocols for an element's URL attribute. /// <para /> /// E.g.: <code>removeProtocols("a", "href", "ftp")</code> /// </remarks> /// <param name="tag">Tag the URL protocol is for</param> /// <param name="key">Attribute key</param> /// <param name="protocols">List of invalid protocols</param> /// <returns>this, for chaining</returns> public virtual iText.StyledXmlParser.Jsoup.Safety.Whitelist RemoveProtocols(String tag, String key, params String[] protocols) { Validate.NotEmpty(tag); Validate.NotEmpty(key); Validate.NotNull(protocols); Whitelist.TagName tagName = Whitelist.TagName.ValueOf(tag); Whitelist.AttributeKey attrKey = Whitelist.AttributeKey.ValueOf(key); if (this.protocols.ContainsKey(tagName)) { IDictionary <Whitelist.AttributeKey, ICollection <Whitelist.Protocol> > attrMap = this.protocols.Get(tagName); if (attrMap.ContainsKey(attrKey)) { ICollection <Whitelist.Protocol> protSet = attrMap.Get(attrKey); foreach (String protocol in protocols) { Validate.NotEmpty(protocol); Whitelist.Protocol prot = Whitelist.Protocol.ValueOf(protocol); protSet.Remove(prot); } if (protSet.IsEmpty()) { // Remove protocol set if empty attrMap.JRemove(attrKey); if (attrMap.IsEmpty()) { // Remove entry for tag if empty this.protocols.JRemove(tagName); } } } } return(this); }
/// <summary>Add allowed URL protocols for an element's URL attribute.</summary> /// <remarks> /// Add allowed URL protocols for an element's URL attribute. This restricts the possible values of the attribute to /// URLs with the defined protocol. /// <para /> /// E.g.: <code>addProtocols("a", "href", "ftp", "http", "https")</code> /// <para /> /// To allow a link to an in-page URL anchor (i.e. <code><a href="#anchor"></code>, add a <code>#</code>:<br /> /// E.g.: <code>addProtocols("a", "href", "#")</code> /// </remarks> /// <param name="tag">Tag the URL protocol is for</param> /// <param name="key">Attribute key</param> /// <param name="protocols">List of valid protocols</param> /// <returns>this, for chaining</returns> public virtual iText.StyledXmlParser.Jsoup.Safety.Whitelist AddProtocols(String tag, String key, params String [] protocols) { Validate.NotEmpty(tag); Validate.NotEmpty(key); Validate.NotNull(protocols); Whitelist.TagName tagName = Whitelist.TagName.ValueOf(tag); Whitelist.AttributeKey attrKey = Whitelist.AttributeKey.ValueOf(key); IDictionary <Whitelist.AttributeKey, ICollection <Whitelist.Protocol> > attrMap; ICollection <Whitelist.Protocol> protSet; if (this.protocols.ContainsKey(tagName)) { attrMap = this.protocols.Get(tagName); } else { attrMap = new Dictionary <Whitelist.AttributeKey, ICollection <Whitelist.Protocol> >(); this.protocols.Put(tagName, attrMap); } if (attrMap.ContainsKey(attrKey)) { protSet = attrMap.Get(attrKey); } else { protSet = new HashSet <Whitelist.Protocol>(); attrMap.Put(attrKey, protSet); } foreach (String protocol in protocols) { Validate.NotEmpty(protocol); Whitelist.Protocol prot = Whitelist.Protocol.ValueOf(protocol); protSet.Add(prot); } return(this); }