Search in sources :

Example 1 with CSSStyleDeclarationImpl

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_;
}
Also used : StyleElement(com.gargoylesoftware.htmlunit.css.StyleElement) CSSStyleDeclarationImpl(com.gargoylesoftware.css.dom.CSSStyleDeclarationImpl) Property(com.gargoylesoftware.css.dom.Property) CSSException(com.gargoylesoftware.css.parser.CSSException) DOMException(org.w3c.dom.DOMException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with CSSStyleDeclarationImpl

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;
}
Also used : CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) CSS3Parser(com.gargoylesoftware.css.parser.javacc.CSS3Parser) CSSStyleDeclarationImpl(com.gargoylesoftware.css.dom.CSSStyleDeclarationImpl) DOMException(com.gargoylesoftware.css.dom.DOMException)

Example 3 with CSSStyleDeclarationImpl

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"));
}
Also used : HTMLElementImpl(org.loboevolution.html.dom.domimpl.HTMLElementImpl) CSSStyleDeclarationImpl(com.gargoylesoftware.css.dom.CSSStyleDeclarationImpl) Test(org.junit.Test) LoboUnitTest(org.loboevolution.driver.LoboUnitTest)

Example 4 with CSSStyleDeclarationImpl

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);
}
Also used : HTMLElementImpl(org.loboevolution.html.dom.domimpl.HTMLElementImpl) CSSStyleDeclarationImpl(com.gargoylesoftware.css.dom.CSSStyleDeclarationImpl) HTMLElementImpl(org.loboevolution.html.dom.domimpl.HTMLElementImpl) TableColumnModel(javax.swing.table.TableColumnModel) DefaultTableModel(javax.swing.table.DefaultTableModel) NodeType(org.loboevolution.type.NodeType) HashMap(java.util.HashMap) java.awt(java.awt) List(java.util.List) Strings(org.loboevolution.common.Strings) Node(org.loboevolution.html.node.Node) Map(java.util.Map) CSSStyleSheetImpl(com.gargoylesoftware.css.dom.CSSStyleSheetImpl) javax.swing(javax.swing) TableModel(javax.swing.table.TableModel) CSSStyleSheetImpl(com.gargoylesoftware.css.dom.CSSStyleSheetImpl) HashMap(java.util.HashMap) CSSStyleDeclarationImpl(com.gargoylesoftware.css.dom.CSSStyleDeclarationImpl)

Example 5 with CSSStyleDeclarationImpl

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;
}
Also used : CSSStyleDeclarationImpl(com.gargoylesoftware.css.dom.CSSStyleDeclarationImpl)

Aggregations

CSSStyleDeclarationImpl (com.gargoylesoftware.css.dom.CSSStyleDeclarationImpl)11 Test (org.junit.Test)4 LoboUnitTest (org.loboevolution.driver.LoboUnitTest)4 CSSStyleSheetImpl (com.gargoylesoftware.css.dom.CSSStyleSheetImpl)3 DOMException (com.gargoylesoftware.css.dom.DOMException)2 CSSOMParser (com.gargoylesoftware.css.parser.CSSOMParser)2 CSS3Parser (com.gargoylesoftware.css.parser.javacc.CSS3Parser)2 HTMLElementImpl (org.loboevolution.html.dom.domimpl.HTMLElementImpl)2 Node (org.loboevolution.html.node.Node)2 Property (com.gargoylesoftware.css.dom.Property)1 CSSException (com.gargoylesoftware.css.parser.CSSException)1 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)1 StyleElement (com.gargoylesoftware.htmlunit.css.StyleElement)1 java.awt (java.awt)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1