Refine search
/** * Finds any namespaces defined in this element. Returns any tag prefix. */ private String updateNamespaces(org.jsoup.nodes.Element el) { // scan the element for namespace declarations // like: xmlns="blah" or xmlns:prefix="blah" Attributes attributes = el.attributes(); for (Attribute attr : attributes) { String key = attr.getKey(); String prefix; if (key.equals(xmlnsKey)) { prefix = ""; } else if (key.startsWith(xmlnsPrefix)) { prefix = key.substring(xmlnsPrefix.length()); } else { continue; } namespacesStack.peek().put(prefix, attr.getValue()); } // get the element prefix if any int pos = el.tagName().indexOf(":"); return pos > 0 ? el.tagName().substring(0, pos) : ""; }
@Override protected boolean isSafeAttribute(String tagName, Element el, Attribute attr) { if ("src".equalsIgnoreCase(attr.getKey())) { String src = attr.getValue(); if (StrUtils.isNotBlank(src) && src.toLowerCase().startsWith("javascript")) { return false; } } return ("img".equals(tagName) && "src".equals(attr.getKey()) && attr.getValue().startsWith("data:;base64")) || super.isSafeAttribute(tagName, el, attr); } }
for (int i = stack.size() - 1; i > 0; i--) { Element el = stack.get(i); if (el.nodeName().equals("li")) { tb.processEndTag("li"); break; if (tb.isSpecial(el) && !StringUtil.inSorted(el.nodeName(), Constants.InBodyStartLiBreakers)) break; if (!html.hasAttr(attribute.getKey())) html.attributes().put(attribute); Element body = stack.get(1); for (Attribute attribute : startTag.getAttributes()) { if (!body.hasAttr(attribute.getKey())) body.attributes().put(attribute); if (!StringUtil.inSorted(attr.getKey(), Constants.InBodyStartInputAttribs)) inputAttribs.put(attr);
Configuration conf = Configuration.getConfiguration(); TemplateResolver templateResolver = conf.getTemplateResolver(); target = elem.attr(ExtNodeConstants.EMBED_NODE_ATTR_TARGET); if (target == null || target.isEmpty()) { String message = "Target not defined[" + elem.toString() + "]"; throw new TemplateException(message); Elements headChildren = head.children(); for (Element child : headChildren) { if (StringUtil.in(child.tagName(), "script", "link", ExtNodeConstants.BLOCK_NODE_TAG)) { while (attrs.hasNext()) { attr = attrs.next(); wrappingNode.attr(attr.getKey(), attr.getValue());
@Override public void readDesign(Element design, DesignContext designContext) { Attributes attr = design.attributes(); if (design.hasAttr(attribute)) { DesignAttributeHandler.assignValue(this, attribute, design.attr(attribute)); supported.addAll(getCustomAttributes()); for (Attribute a : attr) { if (!a.getKey().startsWith(":") && !supported.contains(a.getKey())) { designContext.setCustomAttribute(this, a.getKey(), a.getValue());
private void setOuterHtml(Document doc) { int nrChildren = doc.body().children().size(); if (nrChildren != 1) { String message = "HTML must contain exactly one top level element (ignoring text nodes). Found " + nrChildren; if (nrChildren > 1) { String tagNames = doc.body().children().stream() .map(org.jsoup.nodes.Element::tagName) .collect(Collectors.joining(", ")); message += " elements with the tag names " + tagNames; } throw new IllegalArgumentException(message); } org.jsoup.nodes.Element root = doc.body().child(0); Attributes attrs = root.attributes(); Component.setElement(this, new Element(root.tagName())); attrs.forEach(a -> { String name = a.getKey(); String value = a.getValue(); getElement().setAttribute(name, value); }); doc.outputSettings().prettyPrint(false); setInnerHtml(root.html()); }
/** * Factory method that creates context from the {@link Fragment}. All services and params are * extracted to separate entries. * * @param fragment - fragment from which the context will be created. * @return a FragmentContext that wraps given fragment. */ public static FragmentContext from(Fragment fragment) { Document document = Jsoup.parseBodyFragment(fragment.content()); Element scriptTag = document.body().child(0); List<Attribute> attributes = scriptTag.attributes().asList(); Map<String, Attribute> serviceAttributes = attributes.stream() .filter(attribute -> attribute.getKey().matches(DATA_SERVICE)) .collect(Collectors .toMap(attribute -> ServiceAttributeUtil.extractNamespace(attribute.getKey()), Function.identity())); Map<String, Attribute> paramsAttributes = attributes.stream() .filter(attribute -> attribute.getKey().matches(DATA_PARAMS)) .collect(Collectors .toMap(attribute -> ServiceAttributeUtil.extractNamespace(attribute.getKey()), Function.identity())); return new FragmentContext() .fragment(fragment) .services( serviceAttributes.entrySet().stream() .map(entry -> new ServiceEntry(entry.getValue(), paramsAttributes.get(entry.getKey()))) .collect(Collectors.toList()) ); }
private static void removeDisallowedAttributes(Element node) { for (Element childElement : node.children()) { removeDisallowedAttributes(childElement); } List<String> keysToRemove = new LinkedList<>(); for (Attribute attribute : node.attributes()) { if (!ATTRIBUTES_TO_RETAIN_IN_HTML.contains(attribute.getKey())) { keysToRemove.add(attribute.getKey()); } } for (String key: keysToRemove) { node.removeAttr(key); } }
private Map<String, String> getAttributes(Element element) { Map<String, String> attributes = new HashMap<>(); for (Attribute attribute : element.attributes()) { if(!attribute.getValue().isEmpty()) attributes.put(attribute.getKey(), attribute.getValue()); } return attributes; }
@Override public boolean matches(Element root, Element element) { List<org.jsoup.nodes.Attribute> values = element.attributes().asList(); for (org.jsoup.nodes.Attribute attribute : values) { if (lowerCase(attribute.getKey()).startsWith(keyPrefix)) return true; } return false; }
/** * Clears the children and attributes of the given element. * * @param design * the element to be cleared */ public static void clearElement(Element design) { Attributes attr = design.attributes(); for (Attribute a : attr.asList()) { attr.remove(a.getKey()); } List<Node> children = new ArrayList<>(); children.addAll(design.childNodes()); for (Node node : children) { node.remove(); } }
private boolean testValidProtocol(Element el, Attribute attr, Set<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.getKey()); if (value.length() == 0) value = attr.getValue(); // if it could not be made abs, run as-is to allow custom unknown protocols if (!preserveRelativeLinks) attr.setValue(value); for (Protocol protocol : protocols) { String prot = protocol.toString(); if (prot.equals("#")) { // allows anchor links if (isValidAnchor(value)) { return true; } else { continue; } } prot += ":"; if (lowerCase(value).startsWith(prot)) { return true; } } return false; }
public void remove() { attributes.remove(attr.getKey()); } }
private void clearFromActionAttributes(Element item) { item.attributes().asList().stream() .filter(attr -> attr.getKey().matches(ACTION_FORM_ATTRIBUTES_PATTERN)) .forEach(attr -> item.removeAttr(attr.getKey())); }
private static Map<String, String> getSignalToUrlMapping(Element scriptDocument) { return scriptDocument.getElementsByAttributeStarting(FORM_SIGNAL_ATTR_PREFIX).stream() .flatMap(element -> element.attributes().asList().stream()) .filter(allAttr -> allAttr.getKey().startsWith(FORM_SIGNAL_ATTR_PREFIX)) .collect( Collectors.toMap(e -> e.getKey().replace(FORM_SIGNAL_ATTR_PREFIX, StringUtils.EMPTY), Entry::getValue)); } }
@Override public String getNodeValue() throws DOMException { return attribute.getValue(); }