Search in sources :

Example 21 with ICSSModel

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

the class CSSPageRuleTest method testInsertText5.

public void testInsertText5() 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;
    CSSValueList valueList;
    // rule 5
    rule = ruleList.item(4);
    assertEquals(CSSRule.PAGE_RULE, rule.getType());
    assertTrue(rule instanceof CSSPageRule);
    assertEquals("narrow", ((CSSPageRule) rule).getSelectorText());
    declaration = ((CSSPageRule) rule).getStyle();
    assertEquals(1, declaration.getLength());
    value = declaration.getPropertyCSSValue("size");
    assertTrue(value instanceof CSSValueList);
    valueList = (CSSValueList) value;
    assertEquals(2, valueList.getLength());
    checkPrimitiveNumber(valueList.item(0), new PrimitiveNumber(CSSPrimitiveValue.CSS_CM, 9));
    checkPrimitiveNumber(valueList.item(1), new PrimitiveNumber(CSSPrimitiveValue.CSS_CM, 18));
}
Also used : CSSPageRule(org.w3c.dom.css.CSSPageRule) CSSValue(org.w3c.dom.css.CSSValue) CSSValueList(org.w3c.dom.css.CSSValueList) 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 22 with ICSSModel

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

the class StyleSheetTest method testImportFromParentDirectory.

/**
 * Tests for the import of a stylesheet in a parent directory
 * @throws Exception
 */
public void testImportFromParentDirectory() throws Exception {
    String filePath = "/" + PROJECT_NAME + "/WebContent/style/sub.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.STYLERULE_NODE);
                assertEquals("Stylesheet reference is different than expected.", ((CSSStyleRule) rule).getSelectorText(), "#content");
            }
        }
    } 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 23 with ICSSModel

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

the class StyleSheetTest method testImportCycle.

/**
 * Tests for cycles in imports. If a cycle is encountered, we should not get stuck
 * in a loop or re-import the stylesheet.
 * @throws Exception
 */
public void testImportCycle() throws Exception {
    String filePath = "/" + PROJECT_NAME + "/WebContent/cycle0.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 supplied stylesheet.", 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);
                    // Check that the rules are imported, it would start with the last imported stylesheet's rules and move up
                    assertEquals("Style rules are not equal.", ((CSSStyleRule) rule).getSelectorText(), "#cycle" + (2 - i));
                }
            }
        }
    } 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 24 with ICSSModel

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

the class AbstractCSSModelAdapter method createModel.

/**
 */
protected ICSSModel createModel() {
    // create embedded CSS model (not for external CSS)
    Element element = getElement();
    if (element == null)
        return null;
    String contentType = null;
    if (element instanceof ElementStyleImpl) {
        contentType = ((ElementStyleImpl) element).getEmbeddedStyleType();
    }
    IStructuredModel baseModel = ((IDOMNode) element).getModel();
    ICSSModel newModel = (ICSSModel) baseModel.getModelManager().createUnManagedStructuredModelFor(contentType != null ? contentType : CSS_ID);
    ((CSSModelImpl) newModel).setOwnerDOMNode(element);
    return newModel;
}
Also used : ElementStyleImpl(org.eclipse.wst.html.core.internal.document.ElementStyleImpl) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) Element(org.w3c.dom.Element) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) CSSModelImpl(org.eclipse.wst.css.core.internal.document.CSSModelImpl)

Example 25 with ICSSModel

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

the class HTMLDocumentAdapter method item.

/**
 */
public StyleSheet item(int index) {
    if (this.styleAdapters == null)
        return null;
    List validAdapters = getValidAdapters();
    if (index < 0 || index >= validAdapters.size())
        return null;
    StyleSheet sheet = ((IStyleSheetAdapter) validAdapters.get(index)).getSheet();
    if (sheet == null) {
        // for LINK element whose link is broken
        ICSSModel model = ((AbstractStyleSheetAdapter) validAdapters.get(index)).createModel();
        sheet = ((model != null) ? (StyleSheet) model.getDocument() : null);
    }
    return sheet;
}
Also used : StyleSheet(org.w3c.dom.stylesheets.StyleSheet) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) StyleSheetList(org.w3c.dom.stylesheets.StyleSheetList) ArrayList(java.util.ArrayList) List(java.util.List) IStyleSheetAdapter(org.eclipse.wst.css.core.internal.provisional.adapters.IStyleSheetAdapter)

Aggregations

ICSSModel (org.eclipse.wst.css.core.internal.provisional.document.ICSSModel)82 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)41 CSSRule (org.w3c.dom.css.CSSRule)21 CSSRuleList (org.w3c.dom.css.CSSRuleList)20 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)15 CSSStyleDeclaration (org.w3c.dom.css.CSSStyleDeclaration)14 CSSStyleSheet (org.w3c.dom.css.CSSStyleSheet)14 CSSValue (org.w3c.dom.css.CSSValue)14 ICSSDocument (org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument)12 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)12 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)11 IModelProvideAdapter (org.eclipse.wst.css.core.internal.provisional.adapters.IModelProvideAdapter)8 CSSSourceParser (org.eclipse.wst.css.core.internal.parser.CSSSourceParser)7 INodeNotifier (org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)7 CSSStyleRule (org.w3c.dom.css.CSSStyleRule)7 IFile (org.eclipse.core.resources.IFile)6 Path (org.eclipse.core.runtime.Path)6 CSSSourceFormatter (org.eclipse.wst.css.core.internal.formatter.CSSSourceFormatter)6 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)6 Element (org.w3c.dom.Element)6