Search in sources :

Example 6 with ICSSStyleRule

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

the class TestCSSDecl method testInsertPropertyInExistingRule.

/**
 * To test https://bugs.eclipse.org/298111
 */
public void testInsertPropertyInExistingRule() {
    ICSSModel model = FileUtil.createModel();
    try {
        String originalDocument = "@CHARSET \"ISO-8859-1\";\r\n" + ".test {\r\n" + "}";
        IStructuredDocument structuredDocument = model.getStructuredDocument();
        structuredDocument.set(originalDocument);
        IndexedRegion indexedRegion = model.getIndexedRegion(28);
        assertTrue("Indexed region should be an ICSSStyleRule", indexedRegion instanceof ICSSStyleRule);
        ICSSStyleRule rule = (ICSSStyleRule) indexedRegion;
        ICSSStyleDeclaration declaration = (ICSSStyleDeclaration) rule.getStyle();
        declaration.setProperty("color", "#008040", null);
        CSSPropertyContext context = new CSSPropertyContext(declaration);
        context.applyModified(declaration);
        String newDocument = structuredDocument.get();
        String expectedNewDocument = "@CHARSET \"ISO-8859-1\";\r\n" + ".test {\r\n" + "\tcolor: #008040\r\n" + "}";
        assertEquals("The updated CSS document does not equal the expected", expectedNewDocument, newDocument);
    } finally {
        if (model != null) {
            model.releaseFromEdit();
        }
    }
}
Also used : ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) ICSSStyleDeclaration(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) ICSSStyleRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule) CSSPropertyContext(org.eclipse.wst.css.core.internal.util.declaration.CSSPropertyContext)

Example 7 with ICSSStyleRule

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

the class CSSProposalGeneratorForPseudoSelector method getCandidates.

/**
 * getCandidates method comment.
 */
protected Iterator getCandidates() {
    List candidates = new ArrayList();
    boolean hasLeadingColon = checkLeadingColon();
    String textToReplace = fContext.getTextToReplace();
    if (!hasLeadingColon && 0 < textToReplace.length() && !textToReplace.equals(fContext.getTextToCompare())) {
        // cursor placed midpoint of the region
        return candidates.iterator();
    }
    ITextRegion region = fContext.getTargetRegion();
    if (region != null) {
        String type = region.getType();
        if (type != CSSRegionContexts.CSS_S && !CSSRegionUtil.isSelectorBegginingType(type)) {
            return candidates.iterator();
        }
    }
    boolean useUpperCase = CSSCorePlugin.getDefault().getPluginPreferences().getInt(CSSCorePreferenceNames.CASE_IDENTIFIER) == CSSCorePreferenceNames.UPPER;
    List tags = getSelectorTags();
    Collections.sort(tags, new Comparator() {

        /* (non-Javadoc)
			 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
			 */
        public int compare(Object o1, Object o2) {
            return clean(((CSSMMSelector) o1).getName()).compareTo(clean(((CSSMMSelector) o2).getName()));
        }

        private String clean(String str) {
            int length = str.length();
            for (int i = 0; i < length; i++) {
                if (str.charAt(i) != ':') {
                    return str.substring(i);
                }
            }
            return str;
        }
    });
    Iterator i = tags.iterator();
    while (i.hasNext()) {
        CSSMMSelector selector = (CSSMMSelector) i.next();
        String text = selector.getSelectorString();
        if (hasLeadingColon && !isMatch(text)) {
            continue;
        }
        text = (useUpperCase) ? text.toUpperCase() : text.toLowerCase();
        int cursorPos = 0;
        StringBuffer buf = new StringBuffer();
        if (!hasLeadingColon)
            buf.append(textToReplace);
        buf.append(text);
        cursorPos += buf.length();
        if (0 < buf.length()) {
            // Pseudoclass/element takes arguments
            if (buf.charAt(buf.length() - 1) == ')') {
                --cursorPos;
            }
            boolean inRule = (fContext.getTargetNode() instanceof ICSSStyleRule || fContext.getTargetNode() instanceof ICSSPageRule);
            if (!inRule || (textToReplace.length() == 0 && !hasLeadingColon)) {
                // $NON-NLS-1$
                buf.append(" ");
                cursorPos += 1;
            }
            if (!inRule) {
                StringAndOffset sao = generateBraces();
                buf.append(sao.fString);
                cursorPos += sao.fOffset;
            }
            CSSCACandidate item = new CSSCACandidate();
            item.setReplacementString(buf.toString());
            item.setCursorPosition(cursorPos);
            item.setDisplayString(text);
            item.setImageType(CSSImageType.SELECTOR_PSEUDO);
            item.setMMNode(selector);
            candidates.add(item);
        }
    }
    return candidates.iterator();
}
Also used : ArrayList(java.util.ArrayList) Comparator(java.util.Comparator) ICSSStyleRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule) ICSSPageRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSPageRule) CSSMMSelector(org.eclipse.wst.css.core.internal.metamodel.CSSMMSelector) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with ICSSStyleRule

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

the class CSSProposalArranger method buildProposals.

/**
 */
void buildProposals() {
    fProposals.clear();
    /*
		 * String text; ICompletionProposal item; text = "---- Test
		 * Information ----"; item = new CompletionProposal("",
		 * fContext.getReplaceBegin(), 0, 0, null, text, null, null);
		 * fProposals.add(item);
		 * 
		 * text = "Target: \"" + fContext.getRegionText() + "\"";
		 * 
		 * item = new CompletionProposal("", fContext.getReplaceBegin(), 0, 0,
		 * null, text, null, null); fProposals.add(item);
		 * 
		 * text = fContext.getTargetNode().getClass().toString(); int
		 * lastPeriodPos = text.lastIndexOf('.'); text = "Node: " +
		 * text.substring(lastPeriodPos + 1); item = new
		 * CompletionProposal("", fContext.getReplaceBegin(), 0, 0, null,
		 * text, null, null); fProposals.add(item);
		 */
    ICSSNode targetNode = fContext.getTargetNode();
    // int targetPos = fContext.getTargetPos();
    if (targetNode instanceof ICSSStyleSheet) {
        buildProposalsForAnyRule();
    } else if ((targetNode instanceof ICSSMediaRule && fContext.isTargetPosAfterOf(CSSRegionContexts.CSS_LBRACE)) || (targetNode instanceof ICSSStyleRule && fContext.isTargetPosBeforeOf(CSSRegionContexts.CSS_LBRACE))) {
        buildProposalsForAnyRule();
    // buildProposalsForStyleRule();
    } else if ((targetNode instanceof ICSSPageRule && fContext.isTargetPosBeforeOf(CSSRegionContexts.CSS_LBRACE))) {
        buildProposalsForPageRulePseudoClass();
    } else if ((targetNode instanceof ICSSStyleRule || targetNode instanceof CSSFontFaceRule || targetNode instanceof ICSSPageRule || targetNode instanceof ICSSStyleDeclaration) && (targetNode.getOwnerDocument() instanceof ICSSStyleDeclaration || fContext.targetFollows(CSSRegionContexts.CSS_DECLARATION_DELIMITER) || fContext.targetFollows(CSSRegionContexts.CSS_LBRACE))) {
        buildProposalsForDeclarationName();
    } else if (targetNode instanceof ICSSStyleDeclItem) {
        if (fContext.isTargetPosAfterOf(CSSRegionContexts.CSS_DECLARATION_SEPARATOR)) {
            buildProposalsForDeclarationValue();
        } else {
            buildProposalsForDeclarationName();
        }
    } else if (targetNode instanceof ICSSPrimitiveValue) {
        buildProposalsForDeclarationValue();
    }
/*
		 * else if (targetNode instanceof ICSSPrimitiveValue || ((targetNode
		 * instanceof ICSSStyleRule || targetNode instanceof CSSFontFaceRule ||
		 * targetNode instanceof ICSSStyleDeclaration || targetNode instanceof
		 * ICSSStyleDeclItem) &&
		 * fContext.isTargetPosAfterOf(CSSRegionContexts.COLON))) {
		 * buildProposalsForDeclarationValue(); }
		 */
// for Test
}
Also used : ICSSMediaRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSMediaRule) ICSSPageRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSPageRule) ICSSStyleDeclaration(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration) ICSSStyleDeclItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem) ICSSPrimitiveValue(org.eclipse.wst.css.core.internal.provisional.document.ICSSPrimitiveValue) CSSFontFaceRule(org.w3c.dom.css.CSSFontFaceRule) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet) ICSSStyleRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule)

Example 9 with ICSSStyleRule

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

the class CSSProposalGeneratorForDeclarationName method getCandidates.

/**
 * getCandidates method comment.
 */
protected Iterator getCandidates() {
    List candidates = new ArrayList();
    Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences();
    // $NON-NLS-1$
    String preDelim = "";
    for (int i = 0; i < preferences.getInt(CSSCorePreferenceNames.FORMAT_PROP_PRE_DELIM); i++) {
        // $NON-NLS-1$
        preDelim += ' ';
    }
    // $NON-NLS-1$
    String postDelim = "";
    for (int i = 0; i < preferences.getInt(CSSCorePreferenceNames.FORMAT_PROP_POST_DELIM); i++) {
        // $NON-NLS-1$
        postDelim += ' ';
    }
    ICSSNode targetNode = fContext.getTargetNode();
    boolean bFontRule = false;
    for (ICSSNode node = targetNode; node != null; node = node.getParentNode()) {
        if (node instanceof org.w3c.dom.css.CSSFontFaceRule) {
            bFontRule = true;
            break;
        }
    }
    List names = new ArrayList();
    CSSMetaModelUtil util = new CSSMetaModelUtil(fContext.getMetaModel());
    Iterator iNames = util.collectNodesByType((bFontRule) ? CSSMMNode.TYPE_DESCRIPTOR : CSSMMNode.TYPE_PROPERTY);
    while (iNames.hasNext()) {
        CSSMMNode node = (CSSMMNode) iNames.next();
        names.add(node);
    }
    sortNames(names);
    // Collections.sort(names);
    boolean bAddColon = true;
    if ((targetNode instanceof ICSSStyleRule || targetNode instanceof ICSSStyleDeclItem || targetNode instanceof ICSSStyleDeclaration) && fContext.targetHas(CSSRegionContexts.CSS_DECLARATION_SEPARATOR)) {
        bAddColon = false;
    }
    Iterator i = names.iterator();
    while (i.hasNext()) {
        CSSMMNode node = (CSSMMNode) i.next();
        String text = node.getName();
        text = (preferences.getInt(CSSCorePreferenceNames.CASE_PROPERTY_NAME) == CSSCorePreferenceNames.UPPER) ? text.toUpperCase() : text.toLowerCase();
        if (!isMatch(text)) {
            continue;
        }
        int cursorPos = 0;
        StringBuffer buf = new StringBuffer();
        buf.append(text);
        buf.append(preDelim);
        cursorPos = buf.length();
        if (bAddColon) {
            // $NON-NLS-1$
            buf.append(':');
            buf.append(postDelim);
            cursorPos += 1 + postDelim.length();
        }
        // if (! (targetNode instanceof ICSSStyleDeclItem)) {
        // buf.append(';');//$NON-NLS-1$
        // }
        CSSCACandidate item = new CSSCACandidate();
        item.setReplacementString(buf.toString());
        item.setCursorPosition(cursorPos);
        item.setDisplayString(text);
        item.setMMNode(node);
        item.setImageType(getCategoryImageType(node));
        candidates.add(item);
    }
    return candidates.iterator();
}
Also used : CSSMetaModelUtil(org.eclipse.wst.css.core.internal.metamodel.util.CSSMetaModelUtil) ICSSStyleDeclaration(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration) ArrayList(java.util.ArrayList) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSStyleRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule) CSSMMNode(org.eclipse.wst.css.core.internal.metamodel.CSSMMNode) ICSSStyleDeclItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Preferences(org.eclipse.core.runtime.Preferences)

Example 10 with ICSSStyleRule

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

the class CSSProposalGeneratorForHTMLTag method getCandidates.

/**
 * getCandidates method comment.
 */
protected Iterator getCandidates() {
    List candidates = new ArrayList();
    if (checkLeadingColon()) {
        return candidates.iterator();
    }
    boolean bLowerCase = CSSCorePlugin.getDefault().getPluginPreferences().getInt(CSSCorePreferenceNames.CASE_SELECTOR) == CSSCorePreferenceNames.LOWER;
    // XHTML requires lower case
    if (fContext.getModel().getStyleSheetType() == ICSSModel.EMBEDDED) {
        Node domNode = fContext.getModel().getOwnerDOMNode();
        if (domNode != null && !(domNode instanceof Document)) {
            domNode = domNode.getOwnerDocument();
            if (domNode instanceof IDOMDocument) {
                DocumentTypeAdapter adapter = (DocumentTypeAdapter) ((IDOMDocument) domNode).getAdapterFor(DocumentTypeAdapter.class);
                if (adapter != null)
                    bLowerCase = (adapter.getTagNameCase() == DocumentTypeAdapter.LOWER_CASE);
            }
        }
    }
    int length = fHTMLTags.length;
    for (int i = 0; i < length; i++) {
        String tagText = fHTMLTags[i];
        if (bLowerCase) {
            tagText = tagText.toLowerCase();
        }
        if (!isMatch(tagText)) {
            continue;
        }
        int cursorPos = 0;
        StringBuffer buf = new StringBuffer();
        buf.append(tagText);
        cursorPos += tagText.length();
        boolean inRule = (fContext.getTargetNode() instanceof ICSSStyleRule);
        if (!inRule || fContext.getTextToReplace().length() == 0) {
            // $NON-NLS-1$
            buf.append(" ");
            cursorPos += 1;
        }
        if (!inRule) {
            StringAndOffset sao = generateBraces();
            buf.append(sao.fString);
            cursorPos += sao.fOffset;
        }
        CSSCACandidate item = new CSSCACandidate();
        item.setReplacementString(buf.toString());
        item.setCursorPosition(cursorPos);
        item.setDisplayString(tagText);
        item.setImageType(CSSImageType.SELECTOR_TAG);
        candidates.add(item);
    }
    return candidates.iterator();
}
Also used : Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) Document(org.w3c.dom.Document) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) ICSSStyleRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule) ArrayList(java.util.ArrayList) List(java.util.List) DocumentTypeAdapter(org.eclipse.wst.xml.core.internal.document.DocumentTypeAdapter)

Aggregations

ICSSStyleRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule)13 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)6 Iterator (java.util.Iterator)5 ICSSSelectorList (org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorList)4 ICSSStyleDeclaration (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ICSSPageRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSPageRule)3 ICSSSelector (org.eclipse.wst.css.core.internal.provisional.document.ICSSSelector)3 ICSSStyleDeclItem (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem)3 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)3 CSSFontFaceRule (org.w3c.dom.css.CSSFontFaceRule)3 CSSMMNode (org.eclipse.wst.css.core.internal.metamodel.CSSMMNode)2 ICSSMediaRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSMediaRule)2 ICSSStyleSheet (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet)2 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)2 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)2 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 Preferences (org.eclipse.core.runtime.Preferences)1