Search in sources :

Example 6 with ICSSStyleSheet

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

the class CSSImportRuleTest method testCreateRule.

public void testCreateRule() {
    ICSSStyleSheet sheet = getStyleSheet();
    ICSSDocument doc = sheet;
    ICSSImportRule newRule = doc.createCSSImportRule();
    newRule.setHref("dummy.css");
    MediaList newList = newRule.getMedia();
    newList.appendMedium("media1");
    newList.appendMedium("media2");
    sheet.insertRuleBefore(newRule, null);
    CSSRuleList ruleList = sheet.getCssRules();
    CSSRule rule = ruleList.item(0);
    assertTrue(rule instanceof CSSImportRule);
    CSSImportRule importRule = (CSSImportRule) rule;
    assertEquals("dummy.css", importRule.getHref());
    MediaList mediaList = importRule.getMedia();
    assertEquals(2, mediaList.getLength());
    assertEquals("media1", mediaList.item(0));
    assertEquals("media2", mediaList.item(1));
    assertEquals("@import url(\"dummy.css\") media1, media2;", importRule.getCssText());
}
Also used : MediaList(org.w3c.dom.stylesheets.MediaList) CSSRule(org.w3c.dom.css.CSSRule) ICSSImportRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSImportRule) CSSImportRule(org.w3c.dom.css.CSSImportRule) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet) ICSSDocument(org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument) ICSSImportRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSImportRule) CSSRuleList(org.w3c.dom.css.CSSRuleList)

Example 7 with ICSSStyleSheet

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

the class CSSProposalArranger method buildProposals.

/**
 */
void buildProposals() {
    fProposals.clear();
    /*
		 * String text; ICompletionProposal item; text = "---- Test
		 * Information ----"; item = new CompletionProposal("",
		 * fContext.getReplaceBegin(), 0, 0, null, text, null, null);
		 * fProposals.add(item);
		 * 
		 * text = "Target: \"" + fContext.getRegionText() + "\"";
		 * 
		 * item = new CompletionProposal("", fContext.getReplaceBegin(), 0, 0,
		 * null, text, null, null); fProposals.add(item);
		 * 
		 * text = fContext.getTargetNode().getClass().toString(); int
		 * lastPeriodPos = text.lastIndexOf('.'); text = "Node: " +
		 * text.substring(lastPeriodPos + 1); item = new
		 * CompletionProposal("", fContext.getReplaceBegin(), 0, 0, null,
		 * text, null, null); fProposals.add(item);
		 */
    ICSSNode targetNode = fContext.getTargetNode();
    // int targetPos = fContext.getTargetPos();
    if (targetNode instanceof ICSSStyleSheet) {
        buildProposalsForAnyRule();
    } else if ((targetNode instanceof ICSSMediaRule && fContext.isTargetPosAfterOf(CSSRegionContexts.CSS_LBRACE)) || (targetNode instanceof ICSSStyleRule && fContext.isTargetPosBeforeOf(CSSRegionContexts.CSS_LBRACE))) {
        buildProposalsForAnyRule();
    // buildProposalsForStyleRule();
    } else if ((targetNode instanceof ICSSPageRule && fContext.isTargetPosBeforeOf(CSSRegionContexts.CSS_LBRACE))) {
        buildProposalsForPageRulePseudoClass();
    } else if ((targetNode instanceof ICSSStyleRule || targetNode instanceof CSSFontFaceRule || targetNode instanceof ICSSPageRule || targetNode instanceof ICSSStyleDeclaration) && (targetNode.getOwnerDocument() instanceof ICSSStyleDeclaration || fContext.targetFollows(CSSRegionContexts.CSS_DECLARATION_DELIMITER) || fContext.targetFollows(CSSRegionContexts.CSS_LBRACE))) {
        buildProposalsForDeclarationName();
    } else if (targetNode instanceof ICSSStyleDeclItem) {
        if (fContext.isTargetPosAfterOf(CSSRegionContexts.CSS_DECLARATION_SEPARATOR)) {
            buildProposalsForDeclarationValue();
        } else {
            buildProposalsForDeclarationName();
        }
    } else if (targetNode instanceof ICSSPrimitiveValue) {
        buildProposalsForDeclarationValue();
    }
/*
		 * else if (targetNode instanceof ICSSPrimitiveValue || ((targetNode
		 * instanceof ICSSStyleRule || targetNode instanceof CSSFontFaceRule ||
		 * targetNode instanceof ICSSStyleDeclaration || targetNode instanceof
		 * ICSSStyleDeclItem) &&
		 * fContext.isTargetPosAfterOf(CSSRegionContexts.COLON))) {
		 * buildProposalsForDeclarationValue(); }
		 */
// for Test
}
Also used : ICSSMediaRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSMediaRule) ICSSPageRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSPageRule) ICSSStyleDeclaration(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration) ICSSStyleDeclItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem) ICSSPrimitiveValue(org.eclipse.wst.css.core.internal.provisional.document.ICSSPrimitiveValue) CSSFontFaceRule(org.w3c.dom.css.CSSFontFaceRule) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet) ICSSStyleRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule)

Example 8 with ICSSStyleSheet

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

the class AbstractCssTraverser method traverse.

private final short traverse(ICSSNode node) {
    if (node == null)
        return TRAV_CONT;
    travStack.push(node);
    // pre-action
    short ret = preNode(node);
    if (ret == TRAV_CONT) {
        if (fTraverseImported && (node.getNodeType() == ICSSNode.IMPORTRULE_NODE)) {
            ICSSImportRule rule = (ICSSImportRule) node;
            // traverse external style-sheet
            ICSSStyleSheet sheet = (ICSSStyleSheet) rule.getStyleSheet();
            if (sheet != null && !traversedSheets.contains(sheet)) {
                // prevent
                // loop
                traversedSheets.add(sheet);
                short retExt = traverse(sheet);
                if (retExt == TRAV_STOP) {
                    travStack.pop();
                    return retExt;
                }
            }
        }
        // collect children
        ArrayList children = new ArrayList();
        ICSSNode child = node.getFirstChild();
        if (fTraverseImportFirst) {
            ArrayList others = new ArrayList();
            while (child != null) {
                if (child.getNodeType() == ICSSNode.IMPORTRULE_NODE)
                    children.add(child);
                else
                    others.add(child);
                child = child.getNextSibling();
            }
            children.addAll(others);
        } else {
            while (child != null) {
                children.add(child);
                child = child.getNextSibling();
            }
        }
        // traverse children
        for (int i = 0; i < children.size(); i++) {
            child = (ICSSNode) children.get(i);
            short retChild = traverse(child);
            if (retChild == TRAV_STOP) {
                travStack.pop();
                return retChild;
            }
        }
    } else if (ret == TRAV_STOP) {
        travStack.pop();
        return ret;
    }
    // post-action
    ret = postNode(node);
    travStack.pop();
    return (ret == TRAV_PRUNE) ? TRAV_CONT : ret;
}
Also used : ArrayList(java.util.ArrayList) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSImportRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSImportRule)

Example 9 with ICSSStyleSheet

use of org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet 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 10 with ICSSStyleSheet

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

the class CSSCharsetRuleTest method testCreateRule.

public void testCreateRule() {
    ICSSStyleSheet sheet = getStyleSheet();
    ICSSDocument doc = sheet;
    CSSCharsetRule newRule = doc.createCSSCharsetRule();
    newRule.setEncoding("iso-8859-1");
    sheet.insertRuleBefore(newRule, null);
    CSSRuleList ruleList = sheet.getCssRules();
    CSSRule rule = ruleList.item(0);
    assertTrue(rule instanceof CSSCharsetRule);
    CSSCharsetRule charsetRule = (CSSCharsetRule) rule;
    assertEquals("iso-8859-1", charsetRule.getEncoding());
    assertEquals(RULE, charsetRule.getCssText());
}
Also used : CSSCharsetRule(org.w3c.dom.css.CSSCharsetRule) CSSRule(org.w3c.dom.css.CSSRule) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet) ICSSDocument(org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument) CSSRuleList(org.w3c.dom.css.CSSRuleList)

Aggregations

ICSSStyleSheet (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet)19 CSSRule (org.w3c.dom.css.CSSRule)9 ICSSDocument (org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument)8 CSSRuleList (org.w3c.dom.css.CSSRuleList)8 IFile (org.eclipse.core.resources.IFile)5 Path (org.eclipse.core.runtime.Path)5 ICSSModel (org.eclipse.wst.css.core.internal.provisional.document.ICSSModel)5 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)5 Iterator (java.util.Iterator)3 ICSSImportRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSImportRule)3 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)3 MediaList (org.w3c.dom.stylesheets.MediaList)3 ArrayList (java.util.ArrayList)2 Preferences (org.eclipse.core.runtime.Preferences)2 IModelProvideAdapter (org.eclipse.wst.css.core.internal.provisional.adapters.IModelProvideAdapter)2 ICSSMediaRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSMediaRule)2 ICSSSelector (org.eclipse.wst.css.core.internal.provisional.document.ICSSSelector)2 ICSSStyleDeclItem (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem)2 ICSSStyleDeclaration (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration)2 ICSSStyleRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule)2