Search in sources :

Example 1 with ICSSStyleSheet

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

the class AbstractCSSSourceFormatter method appendSpaceBefore.

/**
 */
protected void appendSpaceBefore(ICSSNode node, CompoundRegion toAppend, StringBuffer source) {
    if (node == null || toAppend == null || source == null)
        return;
    if (isCleanup() && !getCleanupStrategy(node).isFormatSource())
        // for not formatting case on cleanup action
        return;
    String type = toAppend.getType();
    Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences();
    boolean needIndent = !(node instanceof ICSSStyleSheet);
    if (type == CSSRegionContexts.CSS_COMMENT) {
        // check whether previous region is 'S' and has CR-LF
        String delim = getLineDelimiter(node);
        RegionIterator it = new RegionIterator(toAppend.getDocumentRegion(), toAppend.getTextRegion());
        it.prev();
        ITextRegion prev = it.prev();
        // bug390904
        if (prev.getType() == CSSRegionContexts.CSS_LBRACE && TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getFullText(prev), 0)[0] > 0) {
            source.append(delim);
            source.append(getIndent(node));
            source.append(getIndentString());
        } else if (prev.getType() == CSSRegionContexts.CSS_S && TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getText(prev), 0)[0] >= 0) {
            source.append(delim);
            source.append(getIndent(node));
            if (needIndent)
                source.append(getIndentString());
        } else {
            appendSpaceBefore(node, toAppend.getText(), source);
        }
    } else if (type == CSSRegionContexts.CSS_LBRACE && preferences.getBoolean(CSSCorePreferenceNames.WRAPPING_NEWLINE_ON_OPEN_BRACE)) {
        String delim = getLineDelimiter(node);
        source.append(delim);
        source.append(getIndent(node));
    // } else if (type == CSSRegionContexts.CSS_CURLY_BRACE_CLOSE) {
    // } else if (type == CSSRegionContexts.CSS_INCLUDES || type ==
    // CSSRegionContexts.CSS_DASHMATCH) {
    } else if (type == CSSRegionContexts.CSS_DECLARATION_SEPARATOR && node instanceof ICSSStyleDeclItem) {
        int n = preferences.getInt(CSSCorePreferenceNames.FORMAT_PROP_PRE_DELIM);
        // no delimiter case
        while (n-- > 0) // $NON-NLS-1$
        source.append(" ");
    } else if (type == CSSRegionContexts.CSS_DECLARATION_VALUE_OPERATOR || type == CSSRegionContexts.CSS_DECLARATION_VALUE_PARENTHESIS_CLOSE) {
        if (preferences.getInt(CSSCorePreferenceNames.LINE_WIDTH) > 0 && (!preferences.getBoolean(CSSCorePreferenceNames.WRAPPING_PROHIBIT_WRAP_ON_ATTR) || node.getOwnerDocument().getNodeType() != ICSSNode.STYLEDECLARATION_NODE)) {
            int length = getLastLineLength(node, source);
            int append = 1;
            if (length + append > preferences.getInt(CSSCorePreferenceNames.LINE_WIDTH)) {
                source.append(getLineDelimiter(node));
                source.append(getIndent(node));
                if (needIndent)
                    source.append(getIndentString());
            }
        }
    } else if (CSSRegionContexts.CSS_FOREIGN_ELEMENT == type || CSSRegionContexts.CSS_DECLARATION_DELIMITER == type) {
        return;
    } else
        appendSpaceBefore(node, toAppend.getText(), source);
}
Also used : ICSSStyleDeclItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) RegionIterator(org.eclipse.wst.css.core.internal.util.RegionIterator) Preferences(org.eclipse.core.runtime.Preferences) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet)

Example 2 with ICSSStyleSheet

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

the class CSSNodeAdapter method internalActionPerformed.

/**
 * Insert the method's description here.
 */
protected void internalActionPerformed() {
    if (notifyQueue == null) {
        return;
    }
    boolean refresh_all = false;
    boolean refresh_rule = false;
    int pos_all = 0;
    List targets = new ArrayList();
    for (int i = 0; i < notifyQueue.size(); i++) {
        NotifyContext context = (NotifyContext) notifyQueue.get(i);
        if (context.notifier instanceof ICSSStyleSheet) {
            refresh_all = true;
            pos_all = i;
        }
        if (context.notifier instanceof ICSSStyleDeclaration) {
            refresh_rule = true;
            targets.add(context);
        // pos_rule = i;
        }
    // ((NotifyContext) notifyQueue.get(i)).fire();
    }
    if (refresh_all) {
        ((NotifyContext) notifyQueue.get(pos_all)).fire();
    } else if (refresh_rule) {
        Iterator i = targets.iterator();
        while (i.hasNext()) {
            ((NotifyContext) i.next()).fire();
        }
    // else if (refresh_rule) internalRefreshAll();
    } else {
        for (int i = 0; i < notifyQueue.size(); i++) {
            ((NotifyContext) notifyQueue.get(i)).fire();
        }
    }
    notifyQueue.clear();
}
Also used : ICSSStyleDeclaration(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) MediaList(org.w3c.dom.stylesheets.MediaList) List(java.util.List) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet)

Example 3 with ICSSStyleSheet

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

the class CSSMediaRuleTest method testCreateRule.

public void testCreateRule() {
    ICSSStyleSheet sheet = getStyleSheet();
    ICSSDocument doc = sheet;
    ICSSMediaRule newRule = doc.createCSSMediaRule();
    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 CSSMediaRule);
    CSSMediaRule mediaRule = (CSSMediaRule) rule;
    MediaList mediaList = mediaRule.getMedia();
    assertEquals(2, mediaList.getLength());
    assertEquals("media1", mediaList.item(0));
    assertEquals("media2", mediaList.item(1));
    assertEquals("@media media1, media2 {" + FileUtil.commonEOL + "}", mediaRule.getCssText());
}
Also used : ICSSMediaRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSMediaRule) MediaList(org.w3c.dom.stylesheets.MediaList) CSSRule(org.w3c.dom.css.CSSRule) ICSSMediaRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSMediaRule) CSSMediaRule(org.w3c.dom.css.CSSMediaRule) 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)

Example 4 with ICSSStyleSheet

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

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

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