Search in sources :

Example 6 with MediaList

use of org.w3c.dom.stylesheets.MediaList 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)

Aggregations

MediaList (org.w3c.dom.stylesheets.MediaList)6 CSSCleanupStrategy (org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)3 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)3 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)3 ICSSDocument (org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument)2 ICSSStyleSheet (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet)2 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)2 CSSRule (org.w3c.dom.css.CSSRule)2 CSSRuleList (org.w3c.dom.css.CSSRuleList)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ICSSImportRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSImportRule)1 ICSSMediaRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSMediaRule)1 ICSSPrimitiveValue (org.eclipse.wst.css.core.internal.provisional.document.ICSSPrimitiveValue)1 ICSSStyleDeclaration (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration)1 INodeNotifier (org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)1 CSSImportRule (org.w3c.dom.css.CSSImportRule)1 CSSMediaRule (org.w3c.dom.css.CSSMediaRule)1