Search in sources :

Example 16 with ICSSDocument

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

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

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

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

the class AbstractCSSSourceFormatter method getCleanupStrategy.

/**
 * Insert the method's description here.
 *
 * @return org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy
 * @param node
 *            org.eclipse.wst.css.core.model.interfaces.ICSSNode
 */
protected CSSCleanupStrategy getCleanupStrategy(ICSSNode node) {
    CSSCleanupStrategy currentStrategy = CSSCleanupStrategyImpl.getInstance();
    ICSSDocument doc = node.getOwnerDocument();
    if (doc == null)
        return currentStrategy;
    ICSSModel model = doc.getModel();
    if (model == null)
        return currentStrategy;
    if (model.getStyleSheetType() != ICSSModel.EXTERNAL) {
    // TODO - TRANSITION Nakamori-san, or Kit, how can we move to
    // "HTML" plugin?
    // can we subclass?
    // currentStrategy = CSSInHTMLCleanupStrategyImpl.getInstance();
    }
    return currentStrategy;
}
Also used : ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) ICSSDocument(org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument) CSSCleanupStrategy(org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)

Example 20 with ICSSDocument

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

the class FormatProcessorCSS method formatModel.

public void formatModel(IStructuredModel structuredModel, int start, int length) {
    CSSFormatUtil formatUtil = CSSFormatUtil.getInstance();
    if (structuredModel instanceof ICSSModel) {
        // BUG102822 take advantage of IDocumentExtension4
        IDocumentExtension4 docExt4 = null;
        if (structuredModel.getStructuredDocument() instanceof IDocumentExtension4) {
            docExt4 = (IDocumentExtension4) structuredModel.getStructuredDocument();
        }
        DocumentRewriteSession rewriteSession = null;
        try {
            DocumentRewriteSessionType rewriteType = (length > MAX_SMALL_FORMAT_SIZE) ? DocumentRewriteSessionType.UNRESTRICTED : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
            rewriteSession = (docExt4 == null || docExt4.getActiveRewriteSession() != null) ? null : docExt4.startRewriteSession(rewriteType);
            ICSSDocument doc = ((ICSSModel) structuredModel).getDocument();
            IStructuredDocumentRegion startRegion = structuredModel.getStructuredDocument().getRegionAtCharacterOffset(start);
            IStructuredDocumentRegion endRegion = structuredModel.getStructuredDocument().getRegionAtCharacterOffset(start + length);
            if (startRegion != null && endRegion != null) {
                start = startRegion.getStart();
                CSSSourceFormatter formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) doc);
                StringBuffer buf = formatter.format(doc, new Region(start, (endRegion.getEnd() - start)));
                if (buf != null) {
                    formatUtil.replaceSource(doc.getModel(), start, endRegion.getEnd() - start, buf.toString());
                }
            }
        } finally {
            // BUG102822 take advantage of IDocumentExtension4
            if (docExt4 != null && rewriteSession != null)
                docExt4.stopRewriteSession(rewriteSession);
        }
    } else if (structuredModel instanceof IDOMModel) {
        List cssnodes = formatUtil.collectCSSNodes(structuredModel, start, length);
        if (cssnodes != null && !cssnodes.isEmpty()) {
            ICSSModel model = null;
            // BUG102822 take advantage of IDocumentExtension4
            IDocumentExtension4 docExt4 = null;
            if (structuredModel.getStructuredDocument() instanceof IDocumentExtension4) {
                docExt4 = (IDocumentExtension4) structuredModel.getStructuredDocument();
            }
            DocumentRewriteSession rewriteSession = null;
            try {
                DocumentRewriteSessionType rewriteType = (length > MAX_SMALL_FORMAT_SIZE) ? DocumentRewriteSessionType.UNRESTRICTED : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
                rewriteSession = (docExt4 == null || docExt4.getActiveRewriteSession() != null) ? null : docExt4.startRewriteSession(rewriteType);
                for (int i = 0; i < cssnodes.size(); i++) {
                    ICSSNode node = (ICSSNode) cssnodes.get(i);
                    CSSSourceFormatter formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) node);
                    StringBuffer buf = formatter.format(node);
                    if (buf != null) {
                        int startOffset = ((IndexedRegion) node).getStartOffset();
                        int endOffset = ((IndexedRegion) node).getEndOffset();
                        if (model == null) {
                            model = node.getOwnerDocument().getModel();
                        }
                        formatUtil.replaceSource(model, startOffset, endOffset - startOffset, buf.toString());
                    }
                }
            } finally {
                // BUG102822 take advantage of IDocumentExtension4
                if (docExt4 != null && rewriteSession != null)
                    docExt4.stopRewriteSession(rewriteSession);
            }
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) CSSSourceFormatter(org.eclipse.wst.css.core.internal.formatter.CSSSourceFormatter) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) DocumentRewriteSessionType(org.eclipse.jface.text.DocumentRewriteSessionType) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) ICSSDocument(org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) CSSFormatUtil(org.eclipse.wst.css.core.internal.formatter.CSSFormatUtil) DocumentRewriteSession(org.eclipse.jface.text.DocumentRewriteSession) Region(org.eclipse.jface.text.Region) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) List(java.util.List) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)

Aggregations

ICSSDocument (org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument)23 ICSSModel (org.eclipse.wst.css.core.internal.provisional.document.ICSSModel)12 CSSRule (org.w3c.dom.css.CSSRule)10 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)9 ICSSStyleSheet (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet)8 CSSRuleList (org.w3c.dom.css.CSSRuleList)8 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)7 IFile (org.eclipse.core.resources.IFile)5 Path (org.eclipse.core.runtime.Path)5 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)4 Iterator (java.util.Iterator)2 List (java.util.List)2 ITextSelection (org.eclipse.jface.text.ITextSelection)2 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)2 CSSFormatUtil (org.eclipse.wst.css.core.internal.formatter.CSSFormatUtil)2 CSSSourceFormatter (org.eclipse.wst.css.core.internal.formatter.CSSSourceFormatter)2 CSSMetaModelUtil (org.eclipse.wst.css.core.internal.metamodel.util.CSSMetaModelUtil)2 ICSSImportRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSImportRule)2 SelectionCollector (org.eclipse.wst.css.core.internal.util.SelectionCollector)2 INodeNotifier (org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)2