Search in sources :

Example 11 with ICSSStyleSheet

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

the class StyleSheetTest method testImportMethods.

/**
 * Tests that imports can be resolved using the various syntaxes for import.
 * <ul>
 * <li>@import url("example.css");</li>
 * <li>@import url(example.css);</li>
 * <li>@import "example.css";</li>
 * </ul>
 * @throws Exception
 */
public void testImportMethods() throws Exception {
    String filePath = "/" + PROJECT_NAME + "/WebContent/importMethods.css";
    IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(filePath));
    assertTrue("File doesn't exist: " + filePath, file.exists());
    IStructuredModel model = null;
    try {
        model = StructuredModelManager.getModelManager().getModelForRead(file);
        if (model instanceof ICSSModel) {
            ICSSDocument document = ((ICSSModel) model).getDocument();
            if (document instanceof ICSSStyleSheet) {
                CSSRuleList list = ((ICSSStyleSheet) document).getCssRules(true);
                assertTrue("There should be a total of 3 rules.", list.getLength() == 3);
                for (int i = 0; i < list.getLength(); i++) {
                    CSSRule rule = list.item(i);
                    assertTrue("Rule should be a style rule. rule.getType() == " + rule.getType(), rule.getType() == ICSSNode.STYLERULE_NODE);
                    CSSStyleRule style = (CSSStyleRule) rule;
                    assertEquals("Selector text did not match.", "#content", style.getSelectorText().trim());
                }
            }
        }
    } finally {
        if (model != null)
            model.releaseFromRead();
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) CSSRule(org.w3c.dom.css.CSSRule) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) CSSStyleRule(org.w3c.dom.css.CSSStyleRule) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) ICSSDocument(org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet) CSSRuleList(org.w3c.dom.css.CSSRuleList)

Example 12 with ICSSStyleSheet

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

the class StyleSheetTest method testNonExistentImport.

/**
 * Tests for the import of a non-existent stylesheet
 * @throws Exception
 */
public void testNonExistentImport() throws Exception {
    String filePath = "/" + PROJECT_NAME + "/WebContent/importDNE.css";
    IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(filePath));
    assertTrue("File doesn't exist: " + filePath, file.exists());
    IStructuredModel model = null;
    try {
        model = StructuredModelManager.getModelManager().getModelForRead(file);
        if (model instanceof ICSSModel) {
            ICSSDocument document = ((ICSSModel) model).getDocument();
            if (document instanceof ICSSStyleSheet) {
                CSSRuleList list = ((ICSSStyleSheet) document).getCssRules(true);
                assertTrue("There should be a total of 1 rule.", list.getLength() == 1);
                CSSRule rule = list.item(0);
                assertTrue("Rule should be a style rule. rule.getType() == " + rule.getType(), rule.getType() == ICSSNode.IMPORTRULE_NODE);
                assertEquals("Stylesheet reference is different than expected.", ((CSSImportRule) rule).getHref(), "thisfiledoesntexist.css");
            }
        }
    } finally {
        if (model != null)
            model.releaseFromRead();
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) CSSRule(org.w3c.dom.css.CSSRule) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) ICSSDocument(org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet) CSSRuleList(org.w3c.dom.css.CSSRuleList)

Example 13 with ICSSStyleSheet

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

the class StyleSheetTest method testSimpleImport.

/**
 * Tests the simple case of a basic @import or a stylesheet
 * @throws Exception
 */
public void testSimpleImport() throws Exception {
    String filePath = "/" + PROJECT_NAME + "/WebContent/site.css";
    IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(filePath));
    assertTrue("File doesn't exist: " + filePath, file.exists());
    IStructuredModel model = null;
    try {
        model = StructuredModelManager.getModelManager().getModelForRead(file);
        if (model instanceof ICSSModel) {
            ICSSDocument document = ((ICSSModel) model).getDocument();
            if (document instanceof ICSSStyleSheet) {
                CSSRuleList list = ((ICSSStyleSheet) document).getCssRules(true);
                String[] rules = new String[] { "#content", "a", "a:hover" };
                assertTrue("There should be a total of 3 rules supplied stylesheet.", list.getLength() == rules.length);
                for (int i = 0; i < list.getLength(); i++) {
                    CSSRule rule = list.item(i);
                    assertTrue("Rule should be a style rule. rule.getType() == " + rule.getType(), rule.getType() == ICSSNode.STYLERULE_NODE);
                    CSSStyleRule style = (CSSStyleRule) rule;
                    assertEquals("Selector text did not match.", rules[i], style.getSelectorText().trim());
                }
            }
        }
    } finally {
        if (model != null)
            model.releaseFromRead();
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) CSSRule(org.w3c.dom.css.CSSRule) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) CSSStyleRule(org.w3c.dom.css.CSSStyleRule) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) ICSSDocument(org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet) CSSRuleList(org.w3c.dom.css.CSSRuleList)

Example 14 with ICSSStyleSheet

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

the class CSSStyleNotifyAdapter method addStyleListener.

/**
 */
public void addStyleListener(ICSSStyleListener listener) {
    if (listener == null)
        return;
    if (listeners == null)
        listeners = new Vector();
    if (!listeners.contains(listener)) {
        // send new selectors event to listener
        ImportedCollector trav = new ImportedCollector();
        trav.apply(model.getDocument());
        Iterator it = trav.getExternals().iterator();
        while (it.hasNext()) {
            ICSSStyleSheet sheet = (ICSSStyleSheet) it.next();
            // collect selectors
            SelectorsCollector selTrav = new SelectorsCollector();
            selTrav.apply(sheet);
            int nSel = selTrav.getSelectors().size();
            ICSSSelector[] added = new ICSSSelector[nSel];
            for (int i = 0; i < nSel; i++) added[i] = (ICSSSelector) selTrav.getSelectors().get(i);
            // fire event
            CSSStyleEventDeliverer deliverer = new CSSStyleEventDeliverer();
            deliverer.fireTo(listener, sheet.getModel(), null, added, null);
        }
        // add listener
        listeners.add(listener);
    }
}
Also used : ImportedCollector(org.eclipse.wst.css.core.internal.util.ImportedCollector) Iterator(java.util.Iterator) SelectorsCollector(org.eclipse.wst.css.core.internal.util.SelectorsCollector) ICSSSelector(org.eclipse.wst.css.core.internal.provisional.document.ICSSSelector) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet) Vector(java.util.Vector)

Example 15 with ICSSStyleSheet

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

the class CSSStyleNotifyAdapter method removeStyleListener.

/**
 */
public void removeStyleListener(ICSSStyleListener listener) {
    if (listener == null)
        return;
    if (listeners.contains(listener)) {
        listeners.remove(listener);
        // send old selectors event to listener
        ImportedCollector trav = new ImportedCollector();
        trav.apply(model.getDocument());
        Iterator it = trav.getExternals().iterator();
        while (it.hasNext()) {
            ICSSStyleSheet sheet = (ICSSStyleSheet) it.next();
            // collect selectors
            SelectorsCollector selTrav = new SelectorsCollector();
            selTrav.apply(sheet);
            int nSel = selTrav.getSelectors().size();
            ICSSSelector[] removed = new ICSSSelector[nSel];
            for (int i = 0; i < nSel; i++) removed[i] = (ICSSSelector) selTrav.getSelectors().get(i);
            // fire event
            CSSStyleEventDeliverer deliverer = new CSSStyleEventDeliverer();
            deliverer.fireTo(listener, sheet.getModel(), removed, null, null);
        }
    }
}
Also used : ImportedCollector(org.eclipse.wst.css.core.internal.util.ImportedCollector) Iterator(java.util.Iterator) SelectorsCollector(org.eclipse.wst.css.core.internal.util.SelectorsCollector) ICSSSelector(org.eclipse.wst.css.core.internal.provisional.document.ICSSSelector) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet)

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