Search in sources :

Example 1 with CSSCleanupStrategy

use of org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy 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 2 with CSSCleanupStrategy

use of org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy 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 3 with CSSCleanupStrategy

use of org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy in project webtools.sourceediting by eclipse.

the class StyleDeclItemFormatter method formatPost.

/**
 */
protected void formatPost(ICSSNode node, IRegion region, StringBuffer source) {
    CSSCleanupStrategy stgy = getCleanupStrategy(node);
    IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
    CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, region, stgy);
    CompoundRegion[] outside = getOutsideRegions(structuredDocument, region);
    for (int i = 0; i < regions.length; i++) {
        if (i != 0 || needS(outside[0]))
            appendSpaceBefore(node, regions[i], source);
        source.append(decoratedIdentRegion(regions[i], stgy));
    }
}
Also used : IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) CSSCleanupStrategy(org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)

Example 4 with CSSCleanupStrategy

use of org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy in project webtools.sourceediting by eclipse.

the class StyleDeclItemFormatter method formatPre.

/**
 */
protected void formatPre(ICSSNode node, StringBuffer source) {
    int start = ((IndexedRegion) node).getStartOffset();
    int end = (node.getFirstChild() != null && ((IndexedRegion) node.getFirstChild()).getEndOffset() > 0) ? ((IndexedRegion) node.getFirstChild()).getStartOffset() : getChildInsertPos(node);
    Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences();
    if (end > 0) {
        // format source
        CSSCleanupStrategy stgy = getCleanupStrategy(node);
        IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
        CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy);
        for (int i = 0; i < regions.length; i++) {
            if (i != 0)
                appendSpaceBefore(node, regions[i], source);
            source.append(decoratedPropNameRegion(regions[i], stgy));
        }
    } else {
        // generatoe source
        ICSSStyleDeclItem item = (ICSSStyleDeclItem) node;
        if (preferences.getInt(CSSCorePreferenceNames.CASE_PROPERTY_NAME) == CSSCorePreferenceNames.UPPER)
            source.append(item.getPropertyName().toUpperCase());
        else
            source.append(item.getPropertyName());
        int k = preferences.getInt(CSSCorePreferenceNames.FORMAT_PROP_PRE_DELIM);
        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 + k + append > preferences.getInt(CSSCorePreferenceNames.LINE_WIDTH)) {
                source.append(getLineDelimiter(node));
                source.append(getIndent(node));
                source.append(getIndentString());
                // no space is necessary
                k = 0;
            }
        }
        // no delimiter case
        while (k-- > 0) // $NON-NLS-1$
        source.append(" ");
        // $NON-NLS-1$
        source.append(":");
    }
    if (node.getFirstChild() != null && (!isCleanup() || getCleanupStrategy(node).isFormatSource())) {
        appendAfterColonSpace(node, source);
    }
}
Also used : ICSSStyleDeclItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem) 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 5 with CSSCleanupStrategy

use of org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy in project webtools.sourceediting by eclipse.

the class UnknownRuleFormatter method formatPre.

/**
 */
protected void formatPre(ICSSNode node, IRegion region, StringBuffer source) {
    CSSCleanupStrategy stgy = getCleanupStrategy(node);
    IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
    CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, region, stgy);
    CompoundRegion[] outside = getOutsideRegions(structuredDocument, region);
    for (int i = 0; i < regions.length; i++) {
        if (i != 0 || needS(outside[0]))
            appendSpaceBefore(node, regions[i], source);
        source.append(decoratedPropValueRegion(regions[i], stgy));
    }
    if (needS(outside[1]) && !isIncludesPreEnd(node, region))
        appendSpaceBefore(node, outside[1], source);
}
Also used : IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) CSSCleanupStrategy(org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)

Aggregations

CSSCleanupStrategy (org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)50 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)44 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)20 Preferences (org.eclipse.core.runtime.Preferences)6 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)5 ICSSStyleDeclItem (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem)3 MediaList (org.w3c.dom.stylesheets.MediaList)3 ICSSAttr (org.eclipse.wst.css.core.internal.provisional.document.ICSSAttr)2 ICSSModel (org.eclipse.wst.css.core.internal.provisional.document.ICSSModel)2 ICSSPrimitiveValue (org.eclipse.wst.css.core.internal.provisional.document.ICSSPrimitiveValue)2 CSSCleanupStrategyImpl (org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategyImpl)1 CSSRegionContainer (org.eclipse.wst.css.core.internal.document.CSSRegionContainer)1 ICSSDocument (org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument)1 ICSSImportRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSImportRule)1 ICSSStyleRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule)1 ICounter (org.eclipse.wst.css.core.internal.provisional.document.ICounter)1 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)1 CSSUnknownRule (org.w3c.dom.css.CSSUnknownRule)1