Search in sources :

Example 11 with ICSSStyleRule

use of org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule in project webtools.sourceediting by eclipse.

the class TestForNPEInCSSCreation method testCSSModel.

public void testCSSModel() {
    IDOMModel model = FileUtil.createHTMLModel();
    try {
        IStructuredDocument structuredDocument = model.getStructuredDocument();
        structuredDocument.set(getHTMLDocumentText());
        IDOMDocument doc = model.getDocument();
        // get head tag
        NodeList list = doc.getElementsByTagName(HTML40Namespace.ElementName.HEAD);
        Element head = (Element) list.item(0);
        // create and append style element
        Element ele = doc.createElement(HTML40Namespace.ElementName.STYLE);
        ele.setAttribute(HTML40Namespace.ATTR_NAME_TYPE, "text/css");
        String delim = model.getStructuredDocument().getLineDelimiter();
        if (delim == null)
            // $NON-NLS-1$
            delim = "\n";
        StringBuffer buffer = new StringBuffer(delim);
        // $NON-NLS-1$
        buffer.append("<!--");
        buffer.append(delim);
        // $NON-NLS-1$
        buffer.append("-->");
        buffer.append(delim);
        Text text = doc.createTextNode(buffer.toString());
        ele.appendChild(text);
        head.insertBefore(ele, null);
        // get adapter for StyleSheet
        ICSSStyleSheet sheet = (ICSSStyleSheet) ((IStyleSheetAdapter) ((INodeNotifier) ele).getAdapterFor(IStyleSheetAdapter.class)).getSheet();
        // create style declaration
        ICSSStyleRule rule = sheet.createCSSStyleRule();
        rule.getStyle().setProperty("background-color", "lime", "");
        rule.getStyle().setProperty("background-color", "blue", "");
        rule.getStyle().setProperty("background-color", "#0080ff", "");
    // model.save();
    } finally {
        if (model != null) {
            model.releaseFromEdit();
        }
    }
}
Also used : IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) Text(org.w3c.dom.Text) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier) ICSSStyleRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule)

Example 12 with ICSSStyleRule

use of org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule in project webtools.sourceediting by eclipse.

the class StyleRuleFormatter method formatPre.

/**
 */
protected void formatPre(ICSSNode node, StringBuffer source) {
    CSSCleanupStrategy stgy = getCleanupStrategy(node);
    int start = ((IndexedRegion) node).getStartOffset();
    int end = (node.getFirstChild() != null && ((IndexedRegion) node.getFirstChild()).getEndOffset() > 0) ? ((IndexedRegion) node.getFirstChild()).getStartOffset() : getChildInsertPos(node);
    if (end > 0) {
        // format source
        IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
        CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy);
        for (int i = 0; i < regions.length; i++) {
            if (i != 0)
                appendSpaceBetween(node, regions[i - 1], regions[i], source);
            source.append(decoratedSelectorRegion(regions[i], (i != 0) ? regions[i - 1] : null, stgy));
        }
    } else {
        // generate source
        String str = ((ICSSStyleRule) node).getSelectorText();
        // appendSpaceBefore(node,str,source);
        source.append(str);
        // $NON-NLS-1$
        appendSpaceBefore(node, "{", source);
        // $NON-NLS-1$
        source.append("{");
    }
    appendDelimBefore(node, null, source);
}
Also used : IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) CSSCleanupStrategy(org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy) ICSSStyleRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule)

Example 13 with ICSSStyleRule

use of org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule in project webtools.sourceediting by eclipse.

the class CSSModelParser method insertBraceClose.

/**
 */
private CSSNodeImpl insertBraceClose(IStructuredDocumentRegion region) {
    ICSSNode target = CSSModelUtil.findBraceContainer(fCreationContext.getTargetNode());
    if (!(target instanceof CSSStructuredDocumentRegionContainer)) {
        return null;
    }
    CSSStructuredDocumentRegionContainer parent = (CSSStructuredDocumentRegionContainer) target;
    if (CSSModelUtil.isInterruption(parent, region)) {
        ICSSNode node = parent.getParentNode();
        if (node instanceof CSSNodeImpl) {
            fCreationContext.setReparseStart(parent.getStartOffset());
            fCreationContext.setReparseEnd(parent.getEndOffset());
            ((CSSNodeImpl) node).removeChild(parent);
        }
        return null;
    }
    if (parent instanceof ICSSPageRule || parent instanceof ICSSMediaRule || parent instanceof CSSFontFaceRule || parent instanceof ICSSStyleRule) {
        CSSModelUtil.expandStructuredDocumentRegionContainer(parent, region);
        if (!CSSModelUtil.isBraceClosed(target)) {
            fCreationContext.setTargetNode(parent.getParentNode());
            fCreationContext.setNextNode(parent.getNextSibling());
            return parent;
        }
    }
    return null;
}
Also used : ICSSMediaRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSMediaRule) ICSSPageRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSPageRule) CSSFontFaceRule(org.w3c.dom.css.CSSFontFaceRule) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSStyleRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule)

Aggregations

ICSSStyleRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule)13 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)6 Iterator (java.util.Iterator)5 ICSSSelectorList (org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorList)4 ICSSStyleDeclaration (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ICSSPageRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSPageRule)3 ICSSSelector (org.eclipse.wst.css.core.internal.provisional.document.ICSSSelector)3 ICSSStyleDeclItem (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem)3 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)3 CSSFontFaceRule (org.w3c.dom.css.CSSFontFaceRule)3 CSSMMNode (org.eclipse.wst.css.core.internal.metamodel.CSSMMNode)2 ICSSMediaRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSMediaRule)2 ICSSStyleSheet (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet)2 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)2 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)2 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 Preferences (org.eclipse.core.runtime.Preferences)1