Search in sources :

Example 6 with StyleElement

use of com.gargoylesoftware.htmlunit.css.StyleElement in project htmlunit by HtmlUnit.

the class NamedAttrNodeMapImpl method replaceStyleAttribute.

/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Replaces the value of the named style attribute. If there is no style attribute with the
 * specified name, a new one is added. If the specified value is an empty (or all whitespace)
 * string, this method actually removes the named style attribute.
 * @param name the attribute name (delimiter-separated, not camel-cased)
 * @param value the attribute value
 * @param priority the new priority of the property; <code>"important"</code>or the empty string if none.
 */
public void replaceStyleAttribute(final String name, final String value, final String priority) {
    if (org.apache.commons.lang3.StringUtils.isBlank(value)) {
        removeStyleAttribute(name);
        return;
    }
    final Map<String, StyleElement> styleMap = getStyleMap();
    final StyleElement old = styleMap.get(name);
    final StyleElement element;
    if (old == null) {
        element = new StyleElement(name, value, priority, SelectorSpecificity.FROM_STYLE_ATTRIBUTE);
    } else {
        element = new StyleElement(name, value, priority, SelectorSpecificity.FROM_STYLE_ATTRIBUTE, old.getIndex());
    }
    styleMap.put(name, element);
    writeStyleToElement(styleMap);
}
Also used : StyleElement(com.gargoylesoftware.htmlunit.css.StyleElement)

Example 7 with StyleElement

use of com.gargoylesoftware.htmlunit.css.StyleElement in project htmlunit by HtmlUnit.

the class NamedAttrNodeMapImpl method writeStyleToElement.

/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * @param styleMap the styles
 */
public void writeStyleToElement(final Map<String, StyleElement> styleMap) {
    final StringBuilder builder = new StringBuilder();
    final SortedSet<StyleElement> sortedValues = new TreeSet<>(styleMap.values());
    for (final StyleElement e : sortedValues) {
        if (builder.length() != 0) {
            builder.append(' ');
        }
        builder.append(e.getName());
        builder.append(": ");
        builder.append(e.getValue());
        final String prio = e.getPriority();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(prio)) {
            builder.append(" !");
            builder.append(prio);
        }
        builder.append(';');
    }
    final String value = builder.toString();
    setAttribute("style", value);
}
Also used : TreeSet(java.util.TreeSet) StyleElement(com.gargoylesoftware.htmlunit.css.StyleElement)

Aggregations

StyleElement (com.gargoylesoftware.htmlunit.css.StyleElement)7 CSSStyleDeclarationImpl (com.gargoylesoftware.css.dom.CSSStyleDeclarationImpl)1 Property (com.gargoylesoftware.css.dom.Property)1 CSSException (com.gargoylesoftware.css.parser.CSSException)1 IOException (java.io.IOException)1 LinkedHashMap (java.util.LinkedHashMap)1 NoSuchElementException (java.util.NoSuchElementException)1 TreeSet (java.util.TreeSet)1 DOMException (org.w3c.dom.DOMException)1