Search in sources :

Example 1 with CSSStyleDeclaration

use of org.w3c.dom.css.CSSStyleDeclaration in project xwiki-platform by xwiki.

the class PdfExportImpl method applyInlineStyle.

/**
 * Recursively inline the computed style that applies to a DOM Element into the {@code style} attribute of that
 * Element.
 *
 * @param element the Element whose style should be inlined
 */
private void applyInlineStyle(Element element) {
    for (int i = 0; i < element.nodeCount(); i++) {
        org.dom4j.Node node = element.node(i);
        if (node instanceof CSSStylableElement) {
            CSSStylableElement styleElement = (CSSStylableElement) node;
            CSSStyleDeclaration style = styleElement.getComputedStyle();
            if (style != null && StringUtils.isNotEmpty(style.getCssText())) {
                styleElement.addAttribute("style", style.getCssText());
            }
        }
        if (node instanceof Element) {
            applyInlineStyle((Element) node);
        }
    }
}
Also used : CSSStylableElement(io.sf.carte.doc.dom4j.CSSStylableElement) Element(org.dom4j.Element) CSSStyleDeclaration(org.w3c.dom.css.CSSStyleDeclaration) CSSStylableElement(io.sf.carte.doc.dom4j.CSSStylableElement)

Example 2 with CSSStyleDeclaration

use of org.w3c.dom.css.CSSStyleDeclaration in project xwiki-platform by xwiki.

the class XWikiWikiModel method getImageDimension.

/**
 * Extracts the specified image dimension from the image parameters.
 *
 * @param dimension either {@code width} or {@code height}
 * @param imageParameters the image parameters; may include the {@code width}, {@code height} and {@code style}
 *            parameters
 * @return the value of the passed dimension if it is specified in the image parameters, {@code null} otherwise
 */
private String getImageDimension(String dimension, Map<String, String> imageParameters) {
    // Check first if the style parameter contains information about the given dimension. In-line style has priority
    // over the dimension parameters.
    String value = null;
    String style = imageParameters.get("style");
    if (StringUtils.isNotBlank(style)) {
        try {
            CSSStyleDeclaration sd = this.cssParser.parseStyleDeclaration(new InputSource(new StringReader(style)));
            value = sd.getPropertyValue(dimension);
        } catch (Exception e) {
            // Ignore the style parameter but log a warning to let the user know.
            this.logger.warn("Failed to parse CSS style [{}]", style);
        }
    }
    if (StringUtils.isBlank(value)) {
        // Fall back on the value of the dimension parameter.
        value = imageParameters.get(dimension);
    }
    return value;
}
Also used : InputSource(org.w3c.css.sac.InputSource) StringReader(java.io.StringReader) CSSStyleDeclaration(org.w3c.dom.css.CSSStyleDeclaration) WikiModelException(org.xwiki.rendering.wiki.WikiModelException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 3 with CSSStyleDeclaration

use of org.w3c.dom.css.CSSStyleDeclaration in project webtools.sourceediting by eclipse.

the class CSSPropertySource method getPropertyDescriptors.

/**
 * Returns the current collection of property descriptors.
 *
 * @return a vector containing all descriptors.
 */
public IPropertyDescriptor[] getPropertyDescriptors() {
    time0 = System.currentTimeMillis();
    CSSMetaModel metamodel = CSSMetaModelFinder.getInstance().findMetaModelFor(fNode);
    Iterator iProperties = Collections.EMPTY_LIST.iterator();
    switch(fNode.getNodeType()) {
        case ICSSNode.STYLERULE_NODE:
        case ICSSNode.FONTFACERULE_NODE:
        case ICSSNode.PAGERULE_NODE:
        case ICSSNode.STYLEDECLARATION_NODE:
            CSSMMNode mmParent = new CSSMetaModelUtil(metamodel).getMetaModelNodeFor(fNode);
            if (mmParent != null) {
                iProperties = mmParent.getChildNodes();
            }
            break;
        case ICSSNode.STYLEDECLITEM_NODE:
            CSSMMNode mmNode = new CSSMetaModelUtil(metamodel).getMetaModelNodeFor(fNode);
            if (mmNode != null) {
                iProperties = Collections.singletonList(mmNode).iterator();
            }
            break;
        default:
            break;
    }
    // setup categories
    Map categories = new HashMap();
    Iterator iCategories = metamodel.getCategories();
    while (iCategories.hasNext()) {
        CSSMMCategory category = (CSSMMCategory) iCategories.next();
        categories.put(category.getName(), category.getCaption());
    }
    // collect property names
    Set declaredProperties = new HashSet();
    if (iProperties.hasNext()) {
        CSSStyleDeclaration declaration = getDeclarationNode();
        if (declaration != null) {
            ICSSNodeList nodeList = ((ICSSNode) declaration).getChildNodes();
            int nProps = (nodeList != null) ? nodeList.getLength() : 0;
            for (int i = 0; i < nProps; i++) {
                ICSSNode node = nodeList.item(i);
                if (node instanceof ICSSStyleDeclItem) {
                    String name = ((ICSSStyleDeclItem) node).getPropertyName();
                    if (name != null && 0 < name.length()) {
                        declaredProperties.add(name.toLowerCase());
                    }
                }
            }
        }
    }
    List descriptors = new ArrayList();
    // first: properties from content model
    while (iProperties.hasNext()) {
        CSSMMNode node = (CSSMMNode) iProperties.next();
        if (node.getType() == CSSMMNode.TYPE_PROPERTY || node.getType() == CSSMMNode.TYPE_DESCRIPTOR) {
            // $NON-NLS-1$
            String category = (String) categories.get(node.getAttribute("category"));
            String name = node.getName().toLowerCase();
            if (declaredProperties.contains(name)) {
                declaredProperties.remove(name);
            }
            IPropertyDescriptor descriptor = createPropertyDescriptor(name, category);
            if (descriptor != null) {
                descriptors.add(descriptor);
            }
        }
    }
    // second: existing properties but not in content model
    Iterator iRemains = declaredProperties.iterator();
    while (iRemains.hasNext()) {
        IPropertyDescriptor descriptor = createPropertyDescriptor((String) iRemains.next(), null);
        if (descriptor != null) {
            descriptors.add(descriptor);
        }
    }
    IPropertyDescriptor[] resultArray = new IPropertyDescriptor[descriptors.size()];
    if (PERF_GETDESCRIPTORS) {
        // $NON-NLS-1$ //$NON-NLS-2$
        System.out.println(getClass().getName() + ".getPropertyDescriptors: " + (System.currentTimeMillis() - time0) + "ms");
    }
    return (IPropertyDescriptor[]) descriptors.toArray(resultArray);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) CSSMetaModelUtil(org.eclipse.wst.css.core.internal.metamodel.util.CSSMetaModelUtil) HashMap(java.util.HashMap) CSSMetaModel(org.eclipse.wst.css.core.internal.metamodel.CSSMetaModel) ArrayList(java.util.ArrayList) CSSStyleDeclaration(org.w3c.dom.css.CSSStyleDeclaration) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSNodeList(org.eclipse.wst.css.core.internal.provisional.document.ICSSNodeList) CSSMMNode(org.eclipse.wst.css.core.internal.metamodel.CSSMMNode) CSSMMCategory(org.eclipse.wst.css.core.internal.metamodel.CSSMMCategory) ICSSStyleDeclItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem) Iterator(java.util.Iterator) ICSSNodeList(org.eclipse.wst.css.core.internal.provisional.document.ICSSNodeList) ArrayList(java.util.ArrayList) List(java.util.List) IPropertyDescriptor(org.eclipse.ui.views.properties.IPropertyDescriptor) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 4 with CSSStyleDeclaration

use of org.w3c.dom.css.CSSStyleDeclaration in project webtools.sourceediting by eclipse.

the class CSSPropertySource method getPropertyValue.

/**
 * Returns the current value for the named property.
 *
 * @param name
 *            the name of the property as named by its property descriptor
 * @return the current value of the property
 */
public Object getPropertyValue(Object name) {
    if (name == null) {
        // $NON-NLS-1$
        return "";
    }
    String valueString = null;
    String nameString = name.toString();
    CSSStyleDeclaration declaration = null;
    switch(fNode.getNodeType()) {
        case ICSSNode.STYLEDECLITEM_NODE:
            valueString = ((ICSSStyleDeclItem) fNode).getCSSValueText();
            break;
        case ICSSNode.STYLERULE_NODE:
        case ICSSNode.FONTFACERULE_NODE:
        case ICSSNode.PAGERULE_NODE:
            declaration = (CSSStyleDeclaration) fNode.getFirstChild();
            if (declaration != null) {
                valueString = declaration.getPropertyValue(nameString);
            }
            break;
        case ICSSNode.STYLEDECLARATION_NODE:
            valueString = ((CSSStyleDeclaration) fNode).getPropertyValue(nameString);
            break;
        case ICSSNode.PRIMITIVEVALUE_NODE:
            ICSSNode parent = fNode;
            while (parent != null && !(parent instanceof ICSSStyleDeclItem)) {
                parent = parent.getParentNode();
            }
            if (parent != null) {
                valueString = ((ICSSStyleDeclItem) parent).getCSSValueText();
            }
            break;
        default:
            break;
    }
    if (valueString == null) {
        // $NON-NLS-1$
        valueString = "";
    }
    return valueString;
}
Also used : ICSSStyleDeclItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem) CSSStyleDeclaration(org.w3c.dom.css.CSSStyleDeclaration) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)

Example 5 with CSSStyleDeclaration

use of org.w3c.dom.css.CSSStyleDeclaration in project webtools.sourceediting by eclipse.

the class CSSPropertySource method setPropertyValue.

/**
 * Sets the named property to the given value.
 *
 * @param name
 *            the name of the property being set
 * @param value
 *            the new value for the property
 */
public void setPropertyValue(Object name, Object value) {
    if (name == null) {
        return;
    }
    String valueString = (value != null) ? value.toString() : null;
    String nameString = name.toString();
    CSSStyleDeclaration declaration = getDeclarationNode();
    if (declaration != null) {
        try {
            if (valueString == null || valueString.length() <= 0) {
                declaration.removeProperty(nameString);
            } else {
                String priority = declaration.getPropertyPriority(nameString);
                // $NON-NLS-1$ //$NON-NLS-2$
                declaration.setProperty(nameString, valueString, (priority == null || priority.length() == 0) ? "" : " " + priority);
            }
        } catch (Exception e) {
            IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
            // $NON-NLS-1$
            String title = CSSUIMessages.Title_InvalidValue;
            // $NON-NLS-1$
            String message = CSSUIMessages.Message_InvalidValue;
            MessageDialog.openWarning(window.getShell(), title, message);
        }
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) CSSStyleDeclaration(org.w3c.dom.css.CSSStyleDeclaration)

Aggregations

CSSStyleDeclaration (org.w3c.dom.css.CSSStyleDeclaration)28 CSSRule (org.w3c.dom.css.CSSRule)17 CSSRuleList (org.w3c.dom.css.CSSRuleList)17 CSSValue (org.w3c.dom.css.CSSValue)17 CSSStyleSheet (org.w3c.dom.css.CSSStyleSheet)15 ICSSModel (org.eclipse.wst.css.core.internal.provisional.document.ICSSModel)14 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)14 CSSPageRule (org.w3c.dom.css.CSSPageRule)6 CSSStyleRule (org.w3c.dom.css.CSSStyleRule)5 CSSValueList (org.w3c.dom.css.CSSValueList)5 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)4 CSSFontFaceRule (org.w3c.dom.css.CSSFontFaceRule)4 ICSSStyleDeclItem (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem)3 CSSStyleRuleImpl (com.steadystate.css.dom.CSSStyleRuleImpl)2 CSSStyleSheetImpl (com.steadystate.css.dom.CSSStyleSheetImpl)2 IThemeEngine (org.eclipse.e4.ui.css.swt.theme.IThemeEngine)2 IThemeManager (org.eclipse.e4.ui.css.swt.theme.IThemeManager)2 Shell (org.eclipse.swt.widgets.Shell)2 ICSSValue (org.eclipse.wst.css.core.internal.provisional.document.ICSSValue)2 BundleContext (org.osgi.framework.BundleContext)2