use of com.gargoylesoftware.htmlunit.css.StyleElement in project htmlunit by HtmlUnit.
the class NamedAttrNodeMapImpl method getStyleMap.
/**
* <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
*
* Returns a sorted map containing style elements, keyed on style element name. We use a
* {@link LinkedHashMap} map so that results are deterministic and are thus testable.
*
* @return a sorted map containing style elements, keyed on style element name
*/
public Map<String, StyleElement> getStyleMap() {
final String styleAttribute = getAttributeDirect("style");
if (styleString_ == styleAttribute) {
return styleMap_;
}
final Map<String, StyleElement> styleMap = new LinkedHashMap<>();
if (ATTRIBUTE_NOT_DEFINED == styleAttribute || DomElement.ATTRIBUTE_VALUE_EMPTY == styleAttribute) {
styleMap_ = styleMap;
styleString_ = styleAttribute;
return styleMap_;
}
final CSSStyleDeclarationImpl cssStyle = new CSSStyleDeclarationImpl(null);
try {
// use the configured cssErrorHandler here to do the same error handling during
// parsing of inline styles like for external css
cssStyle.setCssText(styleAttribute, getPage().getWebClient().getCssErrorHandler());
} catch (final Exception e) {
if (LOG.isErrorEnabled()) {
LOG.error("Error while parsing style value '" + styleAttribute + "'", e);
}
}
for (final Property prop : cssStyle.getProperties()) {
final String key = prop.getName().toLowerCase(Locale.ROOT);
final StyleElement element = new StyleElement(key, prop.getValue().getCssText(), prop.isImportant() ? StyleElement.PRIORITY_IMPORTANT : "", SelectorSpecificity.FROM_STYLE_ATTRIBUTE);
styleMap.put(key, element);
}
styleMap_ = styleMap;
styleString_ = styleAttribute;
// styleString_ = cssStyle.getCssText();
return styleMap_;
}
use of com.gargoylesoftware.htmlunit.css.StyleElement in project htmlunit by HtmlUnit.
the class CSSStyleDeclaration method getStyleAttribute.
/**
* <p>Returns the value of one of the two named style attributes. If both attributes exist,
* the value of the attribute that was declared last is returned. If only one of the
* attributes exists, its value is returned. If neither attribute exists, an empty string
* is returned.</p>
*
* <p>The second named attribute may be shorthand for a the actual desired property.
* The following formats are possible:</p>
* <ol>
* <li><tt>top right bottom left</tt>: All values are explicit.</li>
* <li><tt>top right bottom</tt>: Left is implicitly the same as right.</li>
* <li><tt>top right</tt>: Left is implicitly the same as right, bottom is implicitly the same as top.</li>
* <li><tt>top</tt>: Left, bottom and right are implicitly the same as top.</li>
* </ol>
*
* @param name1 the name of the first style attribute
* @param name2 the name of the second style attribute
* @return the value of one of the two named style attributes
*/
private String getStyleAttribute(final Definition name1, final Definition name2) {
final String value;
if (styleDeclaration_ == null) {
final StyleElement element1 = getStyleElement(name1.getAttributeName());
final StyleElement element2 = getStyleElement(name2.getAttributeName());
if (element2 == null) {
if (element1 == null) {
return "";
}
return element1.getValue();
}
if (element1 != null) {
if (element1.compareTo(element2) > 0) {
return element1.getValue();
}
}
value = element2.getValue();
} else {
final String value1 = styleDeclaration_.getPropertyValue(name1.getAttributeName());
final String value2 = styleDeclaration_.getPropertyValue(name2.getAttributeName());
if ("".equals(value1) && "".equals(value2)) {
return "";
}
if (!"".equals(value1) && "".equals(value2)) {
return value1;
}
value = value2;
}
final String[] values = StringUtils.split(value);
if (name1.name().contains("TOP")) {
if (values.length > 0) {
return values[0];
}
return "";
} else if (name1.name().contains("RIGHT")) {
if (values.length > 1) {
return values[1];
} else if (values.length > 0) {
return values[0];
}
return "";
} else if (name1.name().contains("BOTTOM")) {
if (values.length > 2) {
return values[2];
} else if (values.length > 0) {
return values[0];
}
return "";
} else if (name1.name().contains("LEFT")) {
if (values.length > 3) {
return values[3];
} else if (values.length > 1) {
return values[1];
} else if (values.length > 0) {
return values[0];
} else {
return "";
}
} else {
throw new IllegalStateException("Unsupported definition: " + name1);
}
}
use of com.gargoylesoftware.htmlunit.css.StyleElement in project htmlunit by HtmlUnit.
the class ComputedCSSStyleDeclaration method setDefaultLocalStyleAttribute.
/**
* Makes a local, "computed", modification to this CSS style that won't override other
* style attributes of the same name. This method should be used to set default values
* for style attributes.
*
* @param name the name of the style attribute to set
* @param newValue the value of the style attribute to set
*/
public void setDefaultLocalStyleAttribute(final String name, final String newValue) {
final StyleElement element = new StyleElement(name, newValue, "", SelectorSpecificity.DEFAULT_STYLE_ATTRIBUTE);
localModifications_.put(name, element);
}
use of com.gargoylesoftware.htmlunit.css.StyleElement in project htmlunit by HtmlUnit.
the class ComputedCSSStyleDeclaration method applyLocalStyleAttribute.
private void applyLocalStyleAttribute(final String name, final String newValue, final String priority, final SelectorSpecificity specificity) {
if (!StyleElement.PRIORITY_IMPORTANT.equals(priority)) {
final StyleElement existingElement = localModifications_.get(name);
if (existingElement != null) {
if (existingElement.isImportant()) {
// can't override a !important rule by a normal rule. Ignore it!
return;
} else if (specificity.compareTo(existingElement.getSpecificity()) < 0) {
// can't override a rule with a rule having higher specificity
return;
}
}
}
final StyleElement element = new StyleElement(name, newValue, priority, specificity);
localModifications_.put(name, element);
}
use of com.gargoylesoftware.htmlunit.css.StyleElement in project htmlunit by HtmlUnit.
the class NamedAttrNodeMapImpl method removeStyleAttribute.
/**
* <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
*
* Removes the specified style attribute, returning the value of the removed attribute.
* @param name the attribute name (delimiter-separated, not camel-cased)
* @return the removed value
*/
public String removeStyleAttribute(final String name) {
final Map<String, StyleElement> styleMap = getStyleMap();
final StyleElement value = styleMap.get(name);
if (value == null) {
return "";
}
styleMap.remove(name);
writeStyleToElement(styleMap);
return value.getValue();
}
Aggregations