Search in sources :

Example 1 with ICSSImportRule

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

the class CSSProposalGeneratorForAtmarkRule method getCandidateImportRule.

/**
 */
private CSSCACandidate getCandidateImportRule() {
    // check content model
    CSSMetaModelUtil util = new CSSMetaModelUtil(fContext.getMetaModel());
    if (!util.collectNodesByType(CSSMMNode.TYPE_IMPORT_RULE).hasNext()) {
        return null;
    }
    // charset and import can precede import rule.
    int offset = fContext.getCursorPos();
    if (0 < offset) {
        SelectionCollector trav = new SelectionCollector();
        trav.setRegion(0, offset - 1);
        trav.apply(fContext.getModel().getDocument());
        Iterator i = trav.getSelectedNodes().iterator();
        while (i.hasNext()) {
            Object obj = i.next();
            if (obj instanceof ICSSNode && !(obj instanceof ICSSDocument || obj instanceof ICSSCharsetRule || obj instanceof ICSSImportRule)) {
                return null;
            }
        }
    }
    int cursorPos = 0;
    String ident = (fUseUpperCase) ? IMPORT.toUpperCase() : IMPORT.toLowerCase();
    StringBuffer buf = new StringBuffer();
    buf.append(ident);
    // $NON-NLS-1$
    buf.append(" ");
    cursorPos = buf.length();
    StringAndOffset sao;
    sao = generateURI();
    buf.append(sao.fString);
    cursorPos += sao.fOffset;
    sao = generateSemicolon();
    buf.append(sao.fString);
    String text = buf.toString();
    if (isMatch(text)) {
        CSSCACandidate item = new CSSCACandidate();
        item.setReplacementString(buf.toString());
        item.setCursorPosition(cursorPos);
        item.setDisplayString(ident);
        item.setImageType(CSSImageType.RULE_IMPORT);
        return item;
    } else {
        return null;
    }
}
Also used : CSSMetaModelUtil(org.eclipse.wst.css.core.internal.metamodel.util.CSSMetaModelUtil) ICSSCharsetRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSCharsetRule) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSDocument(org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument) Iterator(java.util.Iterator) SelectionCollector(org.eclipse.wst.css.core.internal.util.SelectionCollector) ICSSImportRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSImportRule)

Example 2 with ICSSImportRule

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

the class CSSModelImpl method valueChanged.

void valueChanged(CSSNodeImpl node, String oldValue) {
    if (!fStructuredDocumentUpdate) {
        CSSModelUpdater updater = getUpdater();
        updater.valueChanged(node, oldValue);
    }
    ICSSSelector[] removed = null, added = null;
    if (node != null) {
        if (node.getNodeType() == ICSSNode.ATTR_NODE && ((CSSAttrImpl) node).getName().equals(ICSSStyleRule.SELECTOR)) {
            CSSAttrImpl attr = (CSSAttrImpl) node;
            // collect changed selector
            ICSSSelectorList list = new CSSSelectorListImpl(attr.getValue());
            added = new ICSSSelector[list.getLength()];
            for (int i = 0; i < list.getLength(); i++) added[i] = list.getSelector(i);
            // get old value
            list = new CSSSelectorListImpl(oldValue);
            removed = new ICSSSelector[list.getLength()];
            for (int i = 0; i < list.getLength(); i++) removed[i] = list.getSelector(i);
        } else if (node instanceof ICSSValue) {
            ICSSNode rule = node;
            while (rule != null) {
                if (rule instanceof ICSSStyleRule)
                    break;
                rule = rule.getParentNode();
            }
            if (rule != null) {
                ICSSSelectorList list = ((ICSSStyleRule) rule).getSelectors();
                added = new ICSSSelector[list.getLength()];
                for (int i = 0; i < list.getLength(); i++) added[i] = list.getSelector(i);
            }
        }
    }
    if (removed != null || added != null || getDocument().getNodeType() == ICSSNode.STYLEDECLARATION_NODE) {
        // send selector changed event
        getStyleNotifier().fire(removed, added, null);
    }
    // for href attribute
    if (getStyleListeners() != null && getStyleListeners().size() > 0) {
        if (node != null && node.getNodeType() == ICSSNode.ATTR_NODE && ((CSSAttrImpl) node).getName().equals(ICSSImportRule.HREF)) {
            ((ICSSImportRule) ((ICSSAttr) node).getOwnerCSSNode()).getStyleSheet();
        }
    }
}
Also used : ICSSValue(org.eclipse.wst.css.core.internal.provisional.document.ICSSValue) ICSSSelectorList(org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorList) ICSSSelector(org.eclipse.wst.css.core.internal.provisional.document.ICSSSelector) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSImportRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSImportRule) ICSSStyleRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule)

Example 3 with ICSSImportRule

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

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

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

the class CSSStyleSheetImpl method getRules.

private void getRules(CSSRuleListImpl list, ICSSStyleSheet sheet, Stack refs) {
    String href = sheet.getHref();
    if (href != null) {
        // Avoid circular @imports
        if (refs.contains(href))
            return;
        refs.push(href);
    }
    boolean acceptImports = true;
    for (ICSSNode node = sheet.getFirstChild(); node != null; node = node.getNextSibling()) {
        // @import rules must precede all other rules, according to the spec
        if (node.getNodeType() == ICSSNode.IMPORTRULE_NODE && acceptImports) {
            CSSStyleSheet importSheet = ((ICSSImportRule) node).getStyleSheet();
            if (importSheet instanceof ICSSStyleSheet)
                getRules(list, (ICSSStyleSheet) importSheet, refs);
            else
                list.appendNode(node);
        } else // Add the rule to the list
        if (node instanceof CSSRule) {
            list.appendNode(node);
            if (node.getNodeType() != ICSSNode.CHARSETRULE_NODE)
                acceptImports = false;
        }
    }
    if (href != null)
        refs.pop();
}
Also used : CSSRule(org.w3c.dom.css.CSSRule) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet) CSSStyleSheet(org.w3c.dom.css.CSSStyleSheet) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet) ICSSImportRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSImportRule)

Aggregations

ICSSImportRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSImportRule)6 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)5 ICSSStyleSheet (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet)3 ICSSDocument (org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument)2 CSSRule (org.w3c.dom.css.CSSRule)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 Preferences (org.eclipse.core.runtime.Preferences)1 CSSCleanupStrategy (org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)1 CSSMetaModelUtil (org.eclipse.wst.css.core.internal.metamodel.util.CSSMetaModelUtil)1 ICSSCharsetRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSCharsetRule)1 ICSSSelector (org.eclipse.wst.css.core.internal.provisional.document.ICSSSelector)1 ICSSSelectorList (org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorList)1 ICSSStyleRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule)1 ICSSValue (org.eclipse.wst.css.core.internal.provisional.document.ICSSValue)1 SelectionCollector (org.eclipse.wst.css.core.internal.util.SelectionCollector)1 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)1 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)1 CSSImportRule (org.w3c.dom.css.CSSImportRule)1 CSSRuleList (org.w3c.dom.css.CSSRuleList)1