/** * Clear (remove) all of the attributes in this node. * @return this, for chaining */ public Node clearAttributes() { Iterator<Attribute> it = attributes().iterator(); while (it.hasNext()) { it.next(); it.remove(); } return this; }
/** * Set an attribute (key=value). If the attribute already exists, it is replaced. The attribute key comparison is * <b>case insensitive</b>. * @param attributeKey The attribute key. * @param attributeValue The attribute value. * @return this (for chaining) */ public Node attr(String attributeKey, String attributeValue) { attributes().putIgnoreCase(attributeKey, attributeValue); return this; }
private boolean isMetaRefresh(Node node) { if (!"meta".equalsIgnoreCase(node.nodeName())) { return false; } String attributeValue = node.attributes().getIgnoreCase("http-equiv"); return "refresh".equalsIgnoreCase(attributeValue.trim()); } }
private void copyAttributes(org.jsoup.nodes.Node source, Element el) { for (Attribute attribute : source.attributes()) { // valid xml attribute names are: ^[a-zA-Z_:][-a-zA-Z0-9_:.] String key = attribute.getKey().replaceAll("[^-a-zA-Z0-9_:.]", ""); if (key.matches("[a-zA-Z_:][-a-zA-Z0-9_:.]*")) el.setAttribute(key, attribute.getValue()); } }
/** * Test if this element has an attribute. <b>Case insensitive</b> * @param attributeKey The attribute key to check. * @return true if the attribute exists, false if not. */ public boolean hasAttr(String attributeKey) { Validate.notNull(attributeKey); if (attributeKey.startsWith("abs:")) { String key = attributeKey.substring("abs:".length()); if (attributes().hasKeyIgnoreCase(key) && !absUrl(key).equals("")) return true; } return attributes().hasKeyIgnoreCase(attributeKey); }
/** * Remove an attribute from this element. * @param attributeKey The attribute to remove. * @return this (for chaining) */ public Node removeAttr(String attributeKey) { Validate.notNull(attributeKey); attributes().removeIgnoreCase(attributeKey); return this; }
/** * Get an attribute's value by its key. <b>Case insensitive</b> * <p> * To get an absolute URL from an attribute that may be a relative URL, prefix the key with <code><b>abs</b></code>, * which is a shortcut to the {@link #absUrl} method. * </p> * E.g.: * <blockquote><code>String url = a.attr("abs:href");</code></blockquote> * * @param attributeKey The attribute key. * @return The attribute, or empty string if not present (to avoid nulls). * @see #attributes() * @see #hasAttr(String) * @see #absUrl(String) */ public String attr(String attributeKey) { Validate.notNull(attributeKey); if (!hasAttributes()) return EmptyString; String val = attributes().getIgnoreCase(attributeKey); if (val.length() > 0) return val; else if (attributeKey.startsWith("abs:")) return absUrl(attributeKey.substring("abs:".length())); else return ""; }
void insert(Token.Comment commentToken) { Comment comment = new Comment(commentToken.getData()); Node insert = comment; if (commentToken.bogus) { // xml declarations are emitted as bogus comments (which is right for html, but not xml) // so we do a bit of a hack and parse the data as an element to pull the attributes out String data = comment.getData(); if (data.length() > 1 && (data.startsWith("!") || data.startsWith("?"))) { Document doc = Jsoup.parse("<" + data.substring(1, data.length() -1) + ">", baseUri, Parser.xmlParser()); if (doc.childNodeSize() > 0) { Element el = doc.child(0); insert = new XmlDeclaration(settings.normalizeTag(el.tagName()), data.startsWith("!")); insert.attributes().addAll(el.attributes()); } // else, we couldn't parse it as a decl, so leave as a comment } } insertNode(insert); }
void processAttributes(Node node) { Attributes attributes = myState.myAttributes; if (myOptions.outputAttributesIdAttr || !myOptions.outputAttributesNamesRegex.isEmpty()) { final org.jsoup.nodes.Attributes nodeAttributes = node.attributes(); boolean idDone = false; if (myOptions.outputAttributesIdAttr) { String id = nodeAttributes.get("id"); if (id == null || id.isEmpty()) { id = nodeAttributes.get("name"); } if (id != null && !id.isEmpty()) { attributes.replaceValue("id", id); idDone = true; } } if (!myOptions.outputAttributesNamesRegex.isEmpty()) { for (org.jsoup.nodes.Attribute attribute : nodeAttributes) { if (idDone && (attribute.getKey().equals("id") || attribute.getKey().equals("name"))) { continue; } if (attribute.getKey().matches(myOptions.outputAttributesNamesRegex)) { attributes.replaceValue(attribute.getKey(), attribute.getValue()); } } } } }
@Override public void readDesign(Element design, DesignContext designContext) { super.readDesign(design, designContext); String altText = ""; for (Node child : design.childNodes()) { if (child instanceof Element && ((Element) child).tagName().equals("source") && child.hasAttr("href")) { addSource(DesignAttributeHandler.readAttribute("href", child.attributes(), Resource.class)); } else { altText += child.toString(); } } altText = altText.trim(); if (!altText.isEmpty()) { setAltText(altText); } }
public void dumpHtmlTree(FormattingAppendable out, Node node) { out.line().append(node.nodeName()); for (org.jsoup.nodes.Attribute attribute : node.attributes().asList()) { out.append(' ').append(attribute.getKey()).append("=\"").append(attribute.getValue()).append("\""); } out.line().indent(); for (Node child : node.childNodes()) { dumpHtmlTree(out, child); } out.unIndent(); }
@Override public boolean hasAttribute(final Node node, final String name) { return node.attributes().hasKey(name); }
@Override public String getAttribute(final Node node, final String name) { return node.attributes().get(name).trim(); }
private void copyAttributes(org.jsoup.nodes.Node source, Element el) { for (Attribute attribute : source.attributes()) { // valid xml attribute names are: ^[a-zA-Z_:][-a-zA-Z0-9_:.] String key = attribute.getKey().replaceAll("[^-a-zA-Z0-9_:.]", ""); if (key.matches("[a-zA-Z_:][-a-zA-Z0-9_:.]*")) el.setAttribute(key, attribute.getValue()); } }
private void copyAttributes(org.jsoup.nodes.Node source, org.w3c.dom.Element el) { for (Attribute attribute : source.attributes()) { // valid xml attribute names are: ^[a-zA-Z_:][-a-zA-Z0-9_:.] String key = attribute.getKey().replaceAll("[^-a-zA-Z0-9_:.]", ""); if (key.matches("[a-zA-Z_:][-a-zA-Z0-9_:.]*")) el.setAttribute(key, attribute.getValue()); } } }
@Override public void head(Node node, int depth) { // Two way bindings should only be in property bindings, not // inside text content. for (Attribute attribute : node.attributes()) { String value = attribute.getValue(); //It is legal for attributes in templates not to have values, //which is a short form for giving the attribute the value 'true'. //These attributes don't contain bindings (they're just 'true'), so we //skip them. if (value == null) { continue; } matcher.reset(value); if (matcher.matches()) { String path = matcher.group(1); addTwoWayBindingPath(path); } } }
private NodeWrapper(Node wrapped) { this.wrapped = wrapped; Attributes attributes = wrapped.attributes(); if (nonNull(attributes)) { String value = attributes.get("class"); if (nonNull(value)) { value = whitespace.matcher(value).replaceAll(" "); String[] split = value.split(" "); Arrays.sort(split); classAttribute = join(split, " "); } value = attributes.get("style"); if (nonNull(value)) { value = whitespace.matcher(value).replaceAll(" "); String[] split = value.split(";"); Arrays.sort(split); styleAttribute = join(split, ";"); } } }
private void getHtmlStartingFromNode(String nodeType, String attrib, String attribValue, StringBuilder builder, List<Node> nodesList, boolean append) { for (Node node : nodesList) { String nodeName = node.nodeName(); if (append) { if (node.nodeName().equalsIgnoreCase("#text") && node.outerHtml().charAt(0) != ' ') builder.append(" "); builder.append(node.outerHtml()); if (!node.nodeName().equalsIgnoreCase("#text")) builder.append(" "); continue; } if (nodeName.equalsIgnoreCase(nodeType)) { if (node.attributes().hasKey(attrib) && node.attr(attrib).equalsIgnoreCase(attribValue)) append = true; } getHtmlStartingFromNode(nodeType, attrib, attribValue, builder, node.childNodes(), append); } }
private void getHtmlStartingFromNode(String nodeType, String attrib, String attribValue, StringBuilder builder, List<Node> nodesList, boolean append) { for (Node node : nodesList) { String nodeName = node.nodeName(); if (append) { if (node.nodeName().equalsIgnoreCase("#text") && node.outerHtml().charAt(0) != ' ') builder.append(" "); builder.append(node.outerHtml()); if (!node.nodeName().equalsIgnoreCase("#text")) builder.append(" "); continue; } if (nodeName.equalsIgnoreCase(nodeType)) { if (node.attributes().hasKey(attrib) && node.attr(attrib).equalsIgnoreCase(attribValue)) append = true; } getHtmlStartingFromNode(nodeType, attrib, attribValue, builder, node.childNodes(), append); } }
private EditDialog parseEditDialog(Node node) { EditDialog editDialog = new EditDialog(); editDialog.setElement(node.nodeName()); for (Attribute attribute : node.attributes().asList()) { editDialog.addAttribute(attribute.getKey(), attribute.getValue()); } return editDialog; }