use of com.gargoylesoftware.css.dom.CSSStyleDeclarationImpl 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.css.dom.CSSStyleDeclarationImpl in project LoboEvolution by LoboEvolution.
the class HTMLElementImpl method getStyle.
/**
* Gets the local style object associated with the element. The properties
* object returned only includes properties from the local style attribute. It
* may return null only if the type of element does not handle stylesheets.
*
* @return a {@link org.loboevolution.html.style.AbstractCSSProperties} object.
*/
public AbstractCSSProperties getStyle() {
AbstractCSSProperties sds;
synchronized (this) {
sds = this.localStyleDeclarationState;
if (sds != null) {
return sds;
}
sds = new AbstractCSSProperties(this);
// Add any declarations in style attribute (last takes precedence).
final String style = getAttribute("style");
if (Strings.isNotBlank(style)) {
final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
try {
final CSSStyleDeclarationImpl sd = parser.parseStyleDeclaration(style);
sds.addStyleDeclaration(sd);
} catch (final Exception err) {
final String id = getId();
final String withId = id == null ? "" : " with ID '" + id + "'";
this.warn("Unable to parse style attribute value for element " + getTagName() + withId + " in " + getDocumentURL() + ".", err);
}
}
this.localStyleDeclarationState = sds;
}
// Synchronization note: Make sure getStyle() does not return multiple values.
return sds;
}
use of com.gargoylesoftware.css.dom.CSSStyleDeclarationImpl in project LoboEvolution by LoboEvolution.
the class HTMLElementTest method testCreateAttributeStyle.
@Test
public void testCreateAttributeStyle() {
Attr attr = document.createAttribute("style");
assertNotNull(attr);
attr.setValue("display:block");
assertEquals(0, attr.getChildNodes().getLength());
assertNull(attr.getFirstChild());
assertNull(attr.getLastChild());
assertNull(attr.getParentNode());
assertEquals("style", attr.getName());
assertEquals("style", attr.getNodeName());
assertEquals("display:block", attr.getValue());
assertEquals("display:block", attr.getNodeValue());
HTMLElementImpl html = (HTMLElementImpl) document.getDocumentElement();
assertNull(html.getStyle());
html.setAttributeNode(attr);
CSSStyleDeclarationImpl style = html.getStyle().getStyleDeclarations().get(0);
assertNotNull(style);
assertEquals("display: block", style.getCssText());
assertEquals("display:block", html.getAttribute("style"));
style.setCssText("margin-top: 10%");
assertEquals("margin-top: 10%", html.getAttribute("style"));
}
use of com.gargoylesoftware.css.dom.CSSStyleDeclarationImpl in project LoboEvolution by LoboEvolution.
the class ElementPropertiesPanel method tableModel.
private TableModel tableModel(Node node) {
if (node.getNodeType() != NodeType.ELEMENT_NODE) {
Toolkit.getDefaultToolkit().beep();
return _defaultTableModel;
}
HTMLElementImpl elm = (HTMLElementImpl) node;
Map<String, String> cssProperties = new HashMap<>();
final String classNames = elm.getClassName();
final String elementName = elm.getTagName();
final String[] classNameArray = Strings.isNotBlank(classNames) ? Strings.split(classNames) : null;
final List<CSSStyleSheetImpl.SelectorEntry> matchingRules = elm.findStyleDeclarations(elementName, classNameArray, false);
for (CSSStyleSheetImpl.SelectorEntry entry : matchingRules) {
final CSSStyleDeclarationImpl styleDeclaration = entry.getRule().getStyle();
styleDeclaration.getProperties().forEach(prop -> {
final String propertyName = prop.getName();
final String propertyValue = styleDeclaration.getPropertyValue(propertyName);
cssProperties.put(propertyName.toLowerCase(), propertyValue);
});
}
List<CSSStyleDeclarationImpl> styleDeclaration = elm.getStyle().getStyleDeclarations();
if (styleDeclaration != null) {
styleDeclaration.forEach(decl -> decl.getProperties().forEach(prop -> {
final String propertyName = prop.getName();
final String propertyValue = decl.getPropertyValue(propertyName);
cssProperties.put(propertyName.toLowerCase(), propertyValue);
}));
}
return new PropertiesTableModel(cssProperties);
}
use of com.gargoylesoftware.css.dom.CSSStyleDeclarationImpl in project LoboEvolution by LoboEvolution.
the class CSSOMParser method parseStyleDeclaration.
/**
* Parses a input string into a CSSOM style declaration.
*
* @param styleDecl the input string
* @return the CSSOM style declaration
* @throws IOException if the underlying SAC parser throws an IOException
*/
public CSSStyleDeclarationImpl parseStyleDeclaration(final String styleDecl) throws IOException {
final CSSStyleDeclarationImpl sd = new CSSStyleDeclarationImpl(null);
parseStyleDeclaration(sd, styleDecl);
return sd;
}
Aggregations