Search in sources :

Example 1 with ICSSAttr

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

the class AbstractCSSSourceFormatter method getIndent.

/**
 */
protected String getIndent(ICSSNode node) {
    if (node == null)
        // $NON-NLS-1$
        return "";
    ICSSNode parent = node.getParentNode();
    if (node instanceof ICSSAttr)
        parent = ((ICSSAttr) node).getOwnerCSSNode();
    if (parent == null)
        // $NON-NLS-1$
        return "";
    if (node instanceof org.w3c.dom.css.CSSStyleDeclaration)
        parent = parent.getParentNode();
    if (parent == null)
        // $NON-NLS-1$
        return "";
    String parentIndent = getIndent(parent);
    if (parent instanceof org.w3c.dom.css.CSSRule)
        return parentIndent + getIndentString();
    if (node.getParentNode() instanceof ICSSStyleDeclaration)
        return parentIndent + getIndentString();
    return parentIndent;
}
Also used : ICSSAttr(org.eclipse.wst.css.core.internal.provisional.document.ICSSAttr) ICSSStyleDeclaration(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSStyleDeclaration(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration)

Example 2 with ICSSAttr

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

the class AttrFormatter method formatPre.

/**
 */
protected void formatPre(ICSSNode node, StringBuffer source) {
    ICSSAttr attr = (ICSSAttr) node;
    CSSCleanupStrategy stgy = getCleanupStrategy(node);
    int end = ((IndexedRegion) node).getEndOffset();
    if (end > 0) {
        // format source
        int start = ((IndexedRegion) node).getStartOffset();
        IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
        CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy);
        if (attr.getName().equals(ICSSStyleRule.SELECTOR) || attr.getName().equals(ICSSPageRule.SELECTOR)) {
            for (int i = 0; i < regions.length; i++) {
                if (i != 0)
                    appendSpaceBetween(node, regions[i - 1], regions[i], source);
                source.append(decoratedRegion(regions[i], 0, stgy));
            }
        } else if (attr.getName().equals(ICSSImportRule.HREF)) {
            for (int i = 0; i < regions.length; i++) {
                if (i != 0)
                    appendSpaceBefore(node, regions[i], source);
                source.append(decoratedPropValueRegion(regions[i], stgy));
            }
        } else if (attr.getName().equals(ICSSCharsetRule.ENCODING)) {
            for (int i = 0; i < regions.length; i++) {
                if (i != 0)
                    appendSpaceBefore(node, regions[i], source);
                source.append(decoratedIdentRegion(regions[i], stgy));
            }
        } else if (attr.getName().equals(ICSSStyleDeclItem.IMPORTANT)) {
            for (int i = 0; i < regions.length; i++) {
                if (i != 0)
                    appendSpaceBefore(node, regions[i], source);
                source.append(decoratedPropValueRegion(regions[i], stgy));
            }
        } else if (attr.getName() == null || attr.getName().length() == 0) {
            for (int i = 0; i < regions.length; i++) {
                if (i != 0)
                    appendSpaceBefore(node, regions[i], source);
                source.append(decoratedIdentRegion(regions[i], stgy));
            }
        } else {
            // counter attributes
            for (int i = 0; i < regions.length; i++) {
                if (i != 0)
                    appendSpaceBefore(node, regions[i], source);
                source.append(decoratedPropValueRegion(regions[i], stgy));
            }
        }
    } else {
        // generate source
        Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences();
        String value = attr.getValue();
        if (value == null)
            // $NON-NLS-1$
            value = "";
        if (attr.getName().equals(ICSSStyleRule.SELECTOR) || attr.getName().equals(ICSSPageRule.SELECTOR)) {
        } else if (attr.getName().equals(ICSSImportRule.HREF)) {
            String uri = org.eclipse.wst.css.core.internal.util.CSSLinkConverter.stripFunc(value);
            // $NON-NLS-2$//$NON-NLS-1$
            String func = preferences.getInt(CSSCorePreferenceNames.CASE_PROPERTY_VALUE) == CSSCorePreferenceNames.UPPER ? "URL(" : "url(";
            if (preferences.getBoolean(CSSCorePreferenceNames.FORMAT_QUOTE_IN_URI)) {
                String quote = preferences.getString(CSSCorePreferenceNames.FORMAT_QUOTE);
                quote = CSSUtil.detectQuote(uri, quote);
                // $NON-NLS-1$
                value = func + quote + uri + quote + ")";
            } else {
                // $NON-NLS-1$
                value = func + uri + ")";
            }
        } else if (attr.getName().equals(ICSSCharsetRule.ENCODING)) {
            String quote = preferences.getString(CSSCorePreferenceNames.FORMAT_QUOTE);
            if (// $NON-NLS-2$//$NON-NLS-1$
            !value.startsWith("\"") && !value.startsWith("\'"))
                value = quote + value;
            if (// $NON-NLS-2$//$NON-NLS-1$
            !value.endsWith("\"") && !value.endsWith("\'"))
                value = value + quote;
        } else if (attr.getName().equals(ICSSStyleDeclItem.IMPORTANT)) {
            if (preferences.getInt(CSSCorePreferenceNames.CASE_PROPERTY_VALUE) == CSSCorePreferenceNames.UPPER)
                value = value.toUpperCase();
            else
                value = value.toLowerCase();
        } else if (attr.getName() == null || attr.getName().length() == 0) {
            if (CSSCorePlugin.getDefault().getPluginPreferences().getInt(CSSCorePreferenceNames.CASE_IDENTIFIER) == CSSCorePreferenceNames.UPPER)
                value = value.toUpperCase();
            else
                value = value.toLowerCase();
        } else {
            if (preferences.getInt(CSSCorePreferenceNames.CASE_PROPERTY_VALUE) == CSSCorePreferenceNames.UPPER)
                value = value.toUpperCase();
            else
                value = value.toLowerCase();
        }
        source.append(value);
    }
}
Also used : ICSSAttr(org.eclipse.wst.css.core.internal.provisional.document.ICSSAttr) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) Preferences(org.eclipse.core.runtime.Preferences) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) CSSCleanupStrategy(org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)

Example 3 with ICSSAttr

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

the class CharsetRuleFormatter method getAttrInsertPos.

/**
 */
public int getAttrInsertPos(ICSSNode node, String attrName) {
    if (node == null || attrName == null || attrName.length() == 0)
        return -1;
    CSSCleanupStrategy stgy = getCleanupStrategy(node);
    if (!ICSSCharsetRule.ENCODING.equalsIgnoreCase(attrName))
        return -1;
    ICSSAttr attr = (ICSSAttr) node.getAttributes().getNamedItem(ICSSCharsetRule.ENCODING);
    if (attr != null && ((IndexedRegion) attr).getEndOffset() > 0)
        return ((IndexedRegion) attr).getStartOffset();
    IndexedRegion iNode = (IndexedRegion) node;
    if (iNode.getEndOffset() <= 0)
        return -1;
    CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(node.getOwnerDocument().getModel().getStructuredDocument(), new FormatRegion(iNode.getStartOffset(), iNode.getEndOffset() - iNode.getStartOffset()), stgy);
    for (int i = regions.length - 1; i >= 0; i--) {
        if (regions[i].getType() != CSSRegionContexts.CSS_COMMENT)
            return regions[i].getStartOffset();
    }
    return iNode.getEndOffset();
}
Also used : ICSSAttr(org.eclipse.wst.css.core.internal.provisional.document.ICSSAttr) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) CSSCleanupStrategy(org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)

Example 4 with ICSSAttr

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

the class CounterFormatter method getAttrInsertPos.

/**
 */
public int getAttrInsertPos(ICSSNode node, String attrName) {
    if (node == null || attrName == null || attrName.length() == 0)
        return -1;
    IndexedRegion iNode = (IndexedRegion) node;
    if (ICounter.IDENTIFIER.equalsIgnoreCase(attrName)) {
        ICSSAttr attr = (ICSSAttr) node.getAttributes().getNamedItem(ICounter.IDENTIFIER);
        if (attr != null && ((IndexedRegion) attr).getEndOffset() > 0)
            return ((IndexedRegion) attr).getStartOffset();
        if (iNode.getEndOffset() <= 0)
            return -1;
        IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(iNode.getEndOffset() - 1);
        RegionIterator it = new RegionIterator(flatNode, flatNode.getRegionAtCharacterOffset(iNode.getEndOffset() - 1));
        while (it.hasPrev()) {
            ITextRegion region = it.prev();
            if (region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION)
                return it.getStructuredDocumentRegion().getEndOffset(region);
        }
        return ((IndexedRegion) node).getEndOffset();
    } else if (ICounter.LISTSTYLE.equalsIgnoreCase(attrName)) {
        ICSSAttr attr = (ICSSAttr) node.getAttributes().getNamedItem(ICounter.LISTSTYLE);
        if (attr != null && ((IndexedRegion) attr).getEndOffset() > 0)
            return ((IndexedRegion) attr).getStartOffset();
        IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(iNode.getEndOffset() - 1);
        RegionIterator it = new RegionIterator(flatNode, flatNode.getRegionAtCharacterOffset(iNode.getEndOffset() - 1));
        while (it.hasPrev()) {
            ITextRegion region = it.prev();
            if (region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_OPERATOR)
                return it.getStructuredDocumentRegion().getEndOffset(region);
            else if (region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION)
                return it.getStructuredDocumentRegion().getEndOffset(region);
        }
        return ((IndexedRegion) node).getEndOffset();
    } else if (ICounter.SEPARATOR.equalsIgnoreCase(attrName)) {
        ICSSAttr attr = (ICSSAttr) node.getAttributes().getNamedItem(ICounter.SEPARATOR);
        if (attr != null && ((IndexedRegion) attr).getEndOffset() > 0)
            return ((IndexedRegion) attr).getStartOffset();
        IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(iNode.getEndOffset() - 1);
        RegionIterator it = new RegionIterator(flatNode, flatNode.getRegionAtCharacterOffset(iNode.getEndOffset() - 1));
        boolean hasComma = false;
        while (it.hasPrev()) {
            ITextRegion region = it.prev();
            if (region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_OPERATOR) {
                if (!hasComma)
                    hasComma = true;
                else
                    return it.getStructuredDocumentRegion().getEndOffset(region);
            } else if (region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION)
                return it.getStructuredDocumentRegion().getEndOffset(region);
        }
        return ((IndexedRegion) node).getEndOffset();
    } else
        return -1;
}
Also used : ICSSAttr(org.eclipse.wst.css.core.internal.provisional.document.ICSSAttr) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) RegionIterator(org.eclipse.wst.css.core.internal.util.RegionIterator) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 5 with ICSSAttr

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

the class StyleDeclItemFormatter method getAttrInsertPos.

/**
 */
public int getAttrInsertPos(ICSSNode node, String attrName) {
    if (node == null || attrName == null || attrName.length() == 0)
        return -1;
    if (!ICSSStyleDeclItem.IMPORTANT.equalsIgnoreCase(attrName))
        return -1;
    ICSSAttr attr = (ICSSAttr) node.getAttributes().getNamedItem(ICSSStyleDeclItem.IMPORTANT);
    if (attr != null && ((IndexedRegion) attr).getEndOffset() > 0)
        return ((IndexedRegion) attr).getStartOffset();
    IndexedRegion iNode = (IndexedRegion) node;
    if (iNode.getEndOffset() <= 0)
        return -1;
    CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(node.getOwnerDocument().getModel().getStructuredDocument(), new FormatRegion(iNode.getStartOffset(), iNode.getEndOffset() - iNode.getStartOffset()), getCleanupStrategy(node));
    for (int i = regions.length - 1; i >= 0; i--) {
        if (regions[i].getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_IMPORTANT)
            return regions[i].getStartOffset();
    }
    return iNode.getEndOffset();
}
Also used : ICSSAttr(org.eclipse.wst.css.core.internal.provisional.document.ICSSAttr) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Aggregations

ICSSAttr (org.eclipse.wst.css.core.internal.provisional.document.ICSSAttr)9 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)6 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)3 CSSCleanupStrategy (org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)2 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)2 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)2 Iterator (java.util.Iterator)1 Vector (java.util.Vector)1 Preferences (org.eclipse.core.runtime.Preferences)1 CSSSourceGenerator (org.eclipse.wst.css.core.internal.formatter.CSSSourceGenerator)1 ICSSNodeList (org.eclipse.wst.css.core.internal.provisional.document.ICSSNodeList)1 ICSSStyleDeclaration (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration)1 RegionIterator (org.eclipse.wst.css.core.internal.util.RegionIterator)1 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)1