Search in sources :

Example 26 with CSSRule

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

the class CSSPageRuleTest method testInsertText3.

public void testInsertText3() throws IOException {
    ICSSModel model = getModel();
    IStructuredDocument structuredDocument = model.getStructuredDocument();
    structuredDocument.set(FileUtil.createString("src/org/eclipse/wst/css/core/tests/testfiles", "CSSPageRuleTest.css"));
    CSSStyleSheet sheet = (CSSStyleSheet) model.getDocument();
    CSSRuleList ruleList = sheet.getCssRules();
    assertEquals(6, ruleList.getLength());
    CSSRule rule;
    CSSStyleDeclaration declaration;
    CSSValue value;
    // rule 3
    rule = ruleList.item(2);
    assertEquals(CSSRule.PAGE_RULE, rule.getType());
    assertTrue(rule instanceof CSSPageRule);
    assertEquals(":left", ((CSSPageRule) rule).getSelectorText());
    declaration = ((CSSPageRule) rule).getStyle();
    assertEquals(2, declaration.getLength());
    value = declaration.getPropertyCSSValue("margin-left");
    checkPrimitiveNumber(value, new PrimitiveNumber(CSSPrimitiveValue.CSS_CM, 4));
    value = declaration.getPropertyCSSValue("margin-right");
    checkPrimitiveNumber(value, new PrimitiveNumber(CSSPrimitiveValue.CSS_CM, 3));
}
Also used : CSSPageRule(org.w3c.dom.css.CSSPageRule) CSSValue(org.w3c.dom.css.CSSValue) CSSRule(org.w3c.dom.css.CSSRule) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) CSSStyleSheet(org.w3c.dom.css.CSSStyleSheet) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) CSSStyleDeclaration(org.w3c.dom.css.CSSStyleDeclaration) CSSRuleList(org.w3c.dom.css.CSSRuleList)

Example 27 with CSSRule

use of org.w3c.dom.css.CSSRule 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 28 with CSSRule

use of org.w3c.dom.css.CSSRule 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 29 with CSSRule

use of org.w3c.dom.css.CSSRule 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 30 with CSSRule

use of org.w3c.dom.css.CSSRule 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

CSSRule (org.w3c.dom.css.CSSRule)33 CSSRuleList (org.w3c.dom.css.CSSRuleList)29 ICSSModel (org.eclipse.wst.css.core.internal.provisional.document.ICSSModel)21 CSSStyleSheet (org.w3c.dom.css.CSSStyleSheet)20 CSSStyleDeclaration (org.w3c.dom.css.CSSStyleDeclaration)17 CSSValue (org.w3c.dom.css.CSSValue)15 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)14 ICSSStyleSheet (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet)11 ICSSDocument (org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument)10 CSSStyleRule (org.w3c.dom.css.CSSStyleRule)7 CSSPageRule (org.w3c.dom.css.CSSPageRule)6 IFile (org.eclipse.core.resources.IFile)5 Path (org.eclipse.core.runtime.Path)5 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)5 CSSValueList (org.w3c.dom.css.CSSValueList)5 CSSFontFaceRule (org.w3c.dom.css.CSSFontFaceRule)4 ICSSImportRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSImportRule)3 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)3 CSSStyleRuleImpl (com.steadystate.css.dom.CSSStyleRuleImpl)2 CSSStyleSheetImpl (com.steadystate.css.dom.CSSStyleSheetImpl)2