Search in sources :

Example 26 with CSSCleanupStrategy

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

the class MediaRuleFormatter method formatBefore.

/**
 */
protected void formatBefore(ICSSNode node, ICSSNode child, String toAppend, StringBuffer source, IRegion exceptFor) {
    ICSSNode prev = (child != null) ? child.getPreviousSibling() : node.getLastChild();
    int start = (prev != null) ? ((IndexedRegion) prev).getEndOffset() : 0;
    int end = (child != null) ? ((IndexedRegion) child).getStartOffset() : 0;
    if (start > 0 && start < end) {
        // source formatting
        CSSCleanupStrategy stgy = getCleanupStrategy(node);
        IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
        // get meaning regions
        CompoundRegion[] regions = null;
        if (exceptFor == null)
            regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy);
        else {
            String pickupType = null;
            if ((prev != null && prev.getNodeType() == ICSSNode.MEDIALIST_NODE) || (prev == null && (child == null || child.getNodeType() != ICSSNode.MEDIALIST_NODE))) {
                pickupType = CSSRegionContexts.CSS_LBRACE;
            }
            regions = getRegions(structuredDocument, new FormatRegion(start, end - start), exceptFor, pickupType);
        }
        // extract source
        if (child != null && child.getNodeType() == ICSSNode.MEDIALIST_NODE && ((MediaList) child).getLength() > 0) {
            // "@media" and mediatype
            for (int i = 0; i < regions.length; i++) {
                appendSpaceBefore(node, regions[i], source);
                // must
                source.append(decoratedRegion(regions[i], 0, stgy));
            // be comments
            }
            appendSpaceBefore(node, toAppend, source);
        } else if (child != null && (child.getPreviousSibling() == null || child.getPreviousSibling().getNodeType() == ICSSNode.MEDIALIST_NODE)) {
            // mediatype and the first style rule
            for (int i = 0; i < regions.length; i++) {
                appendSpaceBefore(node, regions[i], source);
                // must
                source.append(decoratedRegion(regions[i], 0, stgy));
            // be comments
            }
            appendDelimBefore(node, null, source);
        } else {
            // between styles
            for (int i = 0; i < regions.length; i++) {
                appendDelimBefore(node, regions[i], source);
                // must
                source.append(decoratedRegion(regions[i], 0, stgy));
            // be comments
            }
            appendDelimBefore(node, null, source);
        }
    } else {
        // source generation
        if (child == null && prev != null && prev.getNodeType() != ICSSNode.MEDIALIST_NODE) {
            // after
            // the last style rule
            appendDelimBefore(node.getParentNode(), null, source);
        } else if (child != null && child.getNodeType() == ICSSNode.MEDIALIST_NODE && ((MediaList) child).getLength() > 0) {
            // between
            // "@media" and mediatype
            appendSpaceBefore(node, toAppend, source);
        } else if (prev != null && prev.getNodeType() == ICSSNode.MEDIALIST_NODE && ((MediaList) prev).getLength() > 0) {
            // between
            // mediatype and the first style rule
            // $NON-NLS-1$
            appendSpaceBefore(node, "{", source);
            // $NON-NLS-1$
            source.append("{");
            if (child != null)
                appendDelimBefore(node, null, source);
            else
                appendDelimBefore(node.getParentNode(), null, source);
        } else {
            // normal case
            appendDelimBefore(node, null, source);
        }
    }
}
Also used : MediaList(org.w3c.dom.stylesheets.MediaList) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) CSSCleanupStrategy(org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)

Example 27 with CSSCleanupStrategy

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

the class MediaRuleFormatter 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(decoratedIdentRegion(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)

Example 28 with CSSCleanupStrategy

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

the class PrimitiveValueFormatter method formatPre.

/**
 */
protected void formatPre(ICSSNode node, StringBuffer source) {
    int start = ((IndexedRegion) node).getStartOffset();
    int end = ((IndexedRegion) node).getEndOffset();
    Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences();
    CSSCleanupStrategy stgy = getCleanupStrategy(node);
    if (end > 0) {
        // format source
        IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
        CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy);
        boolean appendQuote = regions.length > 1 && node.getParentNode() instanceof ICSSStyleDeclItem && isCleanup() && getCleanupStrategy(node).isQuoteValues() && (((ICSSStyleDeclItem) node.getParentNode()).getPropertyName().equals(PropCMProperty.P_FONT) || ((ICSSStyleDeclItem) node.getParentNode()).getPropertyName().equals(PropCMProperty.P_FONT_FAMILY) || ((ICSSStyleDeclItem) node.getParentNode()).getPropertyName().equals(PropCMProperty.P_VOICE_FAMILY));
        String quote = preferences.getString(CSSCorePreferenceNames.FORMAT_QUOTE);
        StringBuffer strBuf = new StringBuffer();
        boolean skipSpace = false;
        for (int i = 0; i < regions.length; i++) {
            if (i != 0 && !skipSpace)
                appendSpaceBefore(node, regions[i], strBuf);
            skipSpace = false;
            strBuf.append(decoratedPropValueRegion(regions[i], stgy));
            if (regions[i].getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION)
                skipSpace = true;
        }
        if (appendQuote) {
            source.append(quote);
            String str = strBuf.toString();
            str = str.replace('\'', ' ');
            str = str.replace('\"', ' ');
            str = str.trim();
            source.append(str);
            source.append(quote);
        } else {
            source.append(strBuf);
        }
    } else {
        // generate source
        ICSSPrimitiveValue value = (ICSSPrimitiveValue) node;
        short type = value.getPrimitiveType();
        String quote = preferences.getString(CSSCorePreferenceNames.FORMAT_QUOTE);
        String str = null;
        switch(type) {
            case CSSPrimitiveValue.CSS_NUMBER:
            case CSSPrimitiveValue.CSS_PERCENTAGE:
            case CSSPrimitiveValue.CSS_EMS:
            case CSSPrimitiveValue.CSS_EXS:
            case CSSPrimitiveValue.CSS_PX:
            case CSSPrimitiveValue.CSS_CM:
            case CSSPrimitiveValue.CSS_MM:
            case CSSPrimitiveValue.CSS_IN:
            case CSSPrimitiveValue.CSS_PT:
            case CSSPrimitiveValue.CSS_PC:
            case CSSPrimitiveValue.CSS_DEG:
            case CSSPrimitiveValue.CSS_RAD:
            case CSSPrimitiveValue.CSS_GRAD:
            case CSSPrimitiveValue.CSS_MS:
            case CSSPrimitiveValue.CSS_S:
            case CSSPrimitiveValue.CSS_HZ:
            case CSSPrimitiveValue.CSS_KHZ:
            case CSSPrimitiveValue.CSS_DIMENSION:
            case ICSSPrimitiveValue.CSS_INTEGER:
                if (value.getFloatValue(type) == ((int) value.getFloatValue(type))) {
                    str = Integer.toString((int) value.getFloatValue(type));
                } else {
                    str = Float.toString(value.getFloatValue(type));
                }
                break;
            case CSSPrimitiveValue.CSS_IDENT:
            case ICSSPrimitiveValue.CSS_HASH:
            case ICSSPrimitiveValue.CSS_INHERIT_PRIMITIVE:
                str = value.getStringValue();
                if (str != null) {
                    if (preferences.getInt(CSSCorePreferenceNames.CASE_PROPERTY_VALUE) == CSSCorePreferenceNames.UPPER)
                        str = str.toUpperCase();
                    else
                        str.toLowerCase();
                }
                break;
            case CSSPrimitiveValue.CSS_STRING:
            case CSSPrimitiveValue.CSS_URI:
            case CSSPrimitiveValue.CSS_ATTR:
            case ICSSPrimitiveValue.CSS_URANGE:
            case ICSSPrimitiveValue.CSS_FORMAT:
            case ICSSPrimitiveValue.CSS_LOCAL:
            case ICSSPrimitiveValue.CSS_COMMA:
            case ICSSPrimitiveValue.CSS_SLASH:
                str = value.getStringValue();
        }
        String preStr = null, postStr = null;
        switch(type) {
            case CSSPrimitiveValue.CSS_ATTR:
                // $NON-NLS-1$
                preStr = "attr(";
                // $NON-NLS-1$
                postStr = ")";
                break;
            case CSSPrimitiveValue.CSS_CM:
                // $NON-NLS-1$
                postStr = "cm";
                break;
            // case ICSSPrimitiveValue.CSS_COUNTER:
            case CSSPrimitiveValue.CSS_DEG:
                // $NON-NLS-1$
                postStr = "deg";
                break;
            case CSSPrimitiveValue.CSS_EMS:
                // $NON-NLS-1$
                postStr = "em";
                break;
            case CSSPrimitiveValue.CSS_EXS:
                // $NON-NLS-1$
                postStr = "ex";
                break;
            case CSSPrimitiveValue.CSS_GRAD:
                // $NON-NLS-1$
                postStr = "grad";
                break;
            case CSSPrimitiveValue.CSS_HZ:
                // $NON-NLS-1$
                postStr = "Hz";
                break;
            case CSSPrimitiveValue.CSS_IN:
                // $NON-NLS-1$
                postStr = "in";
                break;
            case CSSPrimitiveValue.CSS_KHZ:
                // $NON-NLS-1$
                postStr = "kHz";
                break;
            case CSSPrimitiveValue.CSS_MM:
                // $NON-NLS-1$
                postStr = "mm";
                break;
            case CSSPrimitiveValue.CSS_MS:
                // $NON-NLS-1$
                postStr = "ms";
                break;
            case CSSPrimitiveValue.CSS_PC:
                // $NON-NLS-1$
                postStr = "pc";
                break;
            case CSSPrimitiveValue.CSS_PERCENTAGE:
                // $NON-NLS-1$
                postStr = "%";
                break;
            case CSSPrimitiveValue.CSS_PT:
                // $NON-NLS-1$
                postStr = "pt";
                break;
            case CSSPrimitiveValue.CSS_PX:
                // $NON-NLS-1$
                postStr = "px";
                break;
            case CSSPrimitiveValue.CSS_RAD:
                // $NON-NLS-1$
                postStr = "rad";
                break;
            // case ICSSPrimitiveValue.CSS_RGBCOLOR:
            case CSSPrimitiveValue.CSS_S:
                // $NON-NLS-1$
                postStr = "s";
                break;
            case CSSPrimitiveValue.CSS_STRING:
                quote = CSSUtil.detectQuote(str, quote);
                preStr = quote;
                postStr = quote;
                break;
            case CSSPrimitiveValue.CSS_URI:
                quote = CSSUtil.detectQuote(str, quote);
                // $NON-NLS-1$
                preStr = "url(" + quote;
                // $NON-NLS-1$
                postStr = quote + ")";
                break;
            // the followings are original primitive values
            case CSSPrimitiveValue.CSS_IDENT:
            case CSSPrimitiveValue.CSS_UNKNOWN:
            // use str
            case ICSSPrimitiveValue.CSS_INTEGER:
            case ICSSPrimitiveValue.CSS_HASH:
            case ICSSPrimitiveValue.CSS_SLASH:
            case ICSSPrimitiveValue.CSS_COMMA:
            case CSSPrimitiveValue.CSS_DIMENSION:
            case CSSPrimitiveValue.CSS_NUMBER:
            case ICSSPrimitiveValue.CSS_INHERIT_PRIMITIVE:
                break;
            case ICSSPrimitiveValue.CSS_URANGE:
                // $NON-NLS-1$
                preStr = "U+";
                break;
            case ICSSPrimitiveValue.CSS_FORMAT:
                quote = CSSUtil.detectQuote(str, quote);
                // $NON-NLS-1$
                preStr = "format(" + quote;
                // $NON-NLS-1$
                postStr = quote + ")";
                break;
            case ICSSPrimitiveValue.CSS_LOCAL:
                quote = CSSUtil.detectQuote(str, quote);
                // $NON-NLS-1$
                preStr = "local(" + quote;
                // $NON-NLS-1$
                postStr = quote + ")";
                break;
        }
        if (preferences.getInt(CSSCorePreferenceNames.CASE_PROPERTY_VALUE) == CSSCorePreferenceNames.UPPER) {
            if (preStr != null)
                preStr = preStr.toUpperCase();
            if (postStr != null)
                postStr = postStr.toUpperCase();
        }
        if (preStr != null)
            source.append(preStr);
        if (str != null)
            source.append(str);
        if (postStr != null)
            source.append(postStr);
    }
}
Also used : ICSSPrimitiveValue(org.eclipse.wst.css.core.internal.provisional.document.ICSSPrimitiveValue) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) CSSCleanupStrategy(org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy) 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)

Example 29 with CSSCleanupStrategy

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

the class StyleSheetFormatter method formatPost.

/**
 */
protected void formatPost(ICSSNode node, StringBuffer source) {
    int end = ((IndexedRegion) node).getEndOffset();
    int start = (node.getLastChild() != null && ((IndexedRegion) node.getLastChild()).getEndOffset() > 0) ? ((IndexedRegion) node.getLastChild()).getEndOffset() : getChildInsertPos(node);
    if (end > 0 && start < end) {
        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++) {
            appendDelimBefore(node, regions[i], source);
            source.append(decoratedRegion(regions[i], 0, stgy));
        }
        if ((regions != null && regions.length > 0 && regions[regions.length - 1].getType() == CSSRegionContexts.CSS_CDC) || node.getOwnerDocument().getModel().getStyleSheetType() == ICSSModel.EMBEDDED)
            appendDelimBefore(node, null, source);
    } else {
        // source generation
        if (node.getOwnerDocument().getModel() != null && node.getOwnerDocument().getModel().getStyleSheetType() == ICSSModel.EMBEDDED)
            appendDelimBefore(node, null, source);
        // nothing
        return;
    }
}
Also used : IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) CSSCleanupStrategy(org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)

Example 30 with CSSCleanupStrategy

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

the class StyleSheetFormatter method formatBefore.

/**
 */
protected void formatBefore(ICSSNode node, ICSSNode child, IRegion region, String toAppend, 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]))
            appendDelimBefore(node, regions[i], source);
        // must be
        source.append(decoratedRegion(regions[i], 0, stgy));
    // comments
    }
    if (needS(outside[1])) {
        if (((IndexedRegion) child).getStartOffset() == region.getOffset() + region.getLength())
            appendDelimBefore(node, null, 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