Search in sources :

Example 71 with ICSSNode

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

the class StyleDeclItemFormatter 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) {
        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 {
            regions = getRegions(structuredDocument, new FormatRegion(start, end - start), exceptFor, null);
        }
        // generate source
        for (int i = 0; i < regions.length; i++) {
            appendSpaceBefore(node, regions[i], source);
            // must
            source.append(decoratedRegion(regions[i], 2, stgy));
        // be
        // comments
        }
    }
    if (child != null) {
        boolean append = true;
        if (child instanceof ICSSPrimitiveValue) {
            if (((ICSSPrimitiveValue) child).getPrimitiveType() == ICSSPrimitiveValue.CSS_COMMA)
                // $NON-NLS-1$
                toAppend = ",";
            else if (((ICSSPrimitiveValue) child).getPrimitiveType() == ICSSPrimitiveValue.CSS_SLASH)
                // $NON-NLS-1$
                toAppend = "/";
            ICSSNode prevSibling = child.getPreviousSibling();
            if (prevSibling instanceof ICSSPrimitiveValue) {
                if (((ICSSPrimitiveValue) prevSibling).getPrimitiveType() == ICSSPrimitiveValue.CSS_SLASH)
                    append = false;
                if (prevSibling instanceof CSSRegionContainer) {
                    // If the previous region was unknown, don't append whitespace
                    final ITextRegion region = ((CSSRegionContainer) prevSibling).getFirstRegion();
                    if (region != null && CSSRegionContexts.CSS_UNKNOWN.equals(region.getType()))
                        append = false;
                }
            }
        }
        if (child instanceof CSSRegionContainer) {
            final ITextRegion region = ((CSSRegionContainer) child).getFirstRegion();
            // If the current region is unknown, don't append whitespace
            if (region != null && CSSRegionContexts.CSS_UNKNOWN.equals(region.getType()))
                append = false;
        }
        if (toAppend != null && !toAppend.equals(",") && !toAppend.equals("/") && !toAppend.equals(")") && append) {
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            appendSpaceBefore(node, toAppend, source);
        }
    }
}
Also used : ICSSPrimitiveValue(org.eclipse.wst.css.core.internal.provisional.document.ICSSPrimitiveValue) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) CSSRegionContainer(org.eclipse.wst.css.core.internal.document.CSSRegionContainer) CSSCleanupStrategy(org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)

Example 72 with ICSSNode

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

the class ImportRuleFormatter 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);
    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++) {
            String str = regions[i].getText();
            if (regions[i].getType() == CSSRegionContexts.CSS_IMPORT)
                str = decoratedIdentRegion(regions[i], stgy);
            else
                str = decoratedPropValueRegion(regions[i], stgy);
            if (i != 0)
                appendSpaceBefore(node, regions[i], source);
            source.append(str);
        }
    } else {
        // generate source
        Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences();
        String str = IMPORT;
        if (preferences.getInt(CSSCorePreferenceNames.CASE_IDENTIFIER) == CSSCorePreferenceNames.UPPER)
            str = IMPORT.toUpperCase();
        String quote = preferences.getString(CSSCorePreferenceNames.FORMAT_QUOTE);
        source.append(str);
        // $NON-NLS-1$
        str = "url(";
        if (preferences.getInt(CSSCorePreferenceNames.CASE_PROPERTY_VALUE) == CSSCorePreferenceNames.UPPER)
            str = str.toUpperCase();
        String href = ((ICSSImportRule) node).getHref();
        quote = CSSUtil.detectQuote(href, quote);
        // $NON-NLS-1$
        str = str + quote + href + quote + ")";
        appendSpaceBefore(node, str, source);
        source.append(str);
    }
    ICSSNode child = node.getFirstChild();
    if (child != null && (child instanceof org.w3c.dom.stylesheets.MediaList) && ((org.w3c.dom.stylesheets.MediaList) child).getLength() > 0) {
        // $NON-NLS-1$
        appendSpaceBefore(node, "", source);
    }
}
Also used : ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) CSSCleanupStrategy(org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) Preferences(org.eclipse.core.runtime.Preferences) ICSSImportRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSImportRule)

Example 73 with ICSSNode

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

the class HTMLElementFormatter method getCSSValue.

/**
 */
private String getCSSValue(Attr attr) {
    ICSSModel model = getCSSModel(attr);
    if (model == null)
        return null;
    ICSSNode document = model.getDocument();
    if (document == null)
        return null;
    INodeNotifier notifier = (INodeNotifier) document;
    CSSSourceFormatter formatter = (CSSSourceFormatter) notifier.getAdapterFor(CSSSourceFormatter.class);
    // try another way to get formatter
    if (formatter == null)
        formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter(notifier);
    if (formatter == null)
        return null;
    StringBuffer buffer = formatter.format(document);
    if (buffer == null)
        return null;
    return buffer.toString();
}
Also used : CSSSourceFormatter(org.eclipse.wst.css.core.internal.formatter.CSSSourceFormatter) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)

Example 74 with ICSSNode

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

the class CSSModelParser method removeStructuredDocumentRegion.

/**
 */
private void removeStructuredDocumentRegion(IStructuredDocumentRegion flatNode) {
    String type = CSSUtil.getStructuredDocumentRegionType(flatNode);
    if (type == CSSRegionContexts.CSS_DELIMITER || type == CSSRegionContexts.CSS_DECLARATION_DELIMITER) {
        do {
            flatNode = fStructuredDocumentWalker.getPrevNode(flatNode);
            type = (flatNode != null) ? CSSUtil.getStructuredDocumentRegionType(flatNode) : null;
        } while (type != null && (type == CSSRegionContexts.CSS_S || type == CSSRegionContexts.CSS_COMMENT));
    }
    if (flatNode == null) {
        return;
    }
    // if (fDeletionContext.isInRemovedRange(flatNode)) { // already
    // removed
    // return;
    // }
    CSSStructuredDocumentRegionContainer node = fDeletionContext.findDeletionTarget((CSSNodeImpl) fDocument, flatNode);
    if (node == null || node == fDocument) {
        // not attached with any treeNode
        return;
    }
    if (node instanceof CSSStyleDeclarationImpl) {
        ICSSNode rule = node.getParentNode();
        if (rule instanceof CSSStyleRuleImpl) {
            node = (CSSStructuredDocumentRegionContainer) rule;
        } else {
            return;
        }
    }
    if (fDeletionContext.addNodeToBeRemoved(node)) {
        IStructuredDocumentRegion firstNode = node.getFirstStructuredDocumentRegion();
        if (firstNode != null) {
            fDeletionContext.expandRemovedRangeBegin(firstNode);
        }
        IStructuredDocumentRegion lastNode = node.getLastStructuredDocumentRegion();
        if (lastNode != null) {
            fDeletionContext.expandRemovedRangeEnd(lastNode);
        }
    }
// shrinkContainer(node);
// parent.removeChild(node);
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)

Example 75 with ICSSNode

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

the class CSSModelParser method insertStructuredDocumentRegion.

/**
 * currently public but may be made default access protected in future.
 */
protected CSSNodeImpl insertStructuredDocumentRegion(IStructuredDocumentRegion region) {
    if (fCreationContext == null || region == null) {
        return null;
    }
    String type = ((BasicStructuredDocumentRegion) region).getType();
    CSSNodeImpl modified = null;
    ICSSNode target = fCreationContext.getTargetNode();
    if ((fParseFloating && target == null) || target instanceof ICSSRuleContainer) {
        if (type == CSSRegionContexts.CSS_DELIMITER) {
            modified = insertSemiColonForRule(region);
        } else if (type == CSSRegionContexts.CSS_LBRACE) {
            modified = insertBraceOpen(region);
        } else if (type == CSSRegionContexts.CSS_RBRACE) {
            modified = insertBraceClose(region);
        } else if (type == CSSRegionContexts.CSS_MEDIA || type == CSSRegionContexts.CSS_PAGE || type == CSSRegionContexts.CSS_FONT_FACE || CSSUtil.isSelectorText(region)) {
            checkNextNode(region, CSSRegionContexts.CSS_LBRACE);
        } else if (type == CSSRegionContexts.CSS_IMPORT || type == CSSRegionContexts.CSS_CHARSET) {
            checkNextNode(region, CSSRegionContexts.CSS_DELIMITER);
        }
    } else if ((target instanceof CSSRuleDeclContainer || target instanceof CSSStyleDeclaration) && type == CSSRegionContexts.CSS_DECLARATION_PROPERTY) {
        modified = insertStyleDeclarationItem(region);
    } else if (target instanceof ICSSStyleDeclItem && type == CSSRegionContexts.CSS_DECLARATION_DELIMITER) {
        modified = insertSemiColonForStyleDeclarationItem(region);
    } else if (type == CSSRegionContexts.CSS_RBRACE) {
        modified = insertBraceClose(region);
    }
    // post process
    if (modified != null) {
        if (modified instanceof CSSStructuredDocumentRegionContainer) {
            ((CSSStructuredDocumentRegionContainer) modified).propagateRangeStructuredDocumentRegion();
        }
    }
    return modified;
}
Also used : ICSSRuleContainer(org.eclipse.wst.css.core.internal.provisional.document.ICSSRuleContainer) BasicStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.text.BasicStructuredDocumentRegion) ICSSStyleDeclItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) CSSStyleDeclaration(org.w3c.dom.css.CSSStyleDeclaration)

Aggregations

ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)95 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)21 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)13 INodeNotifier (org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)12 DOMException (org.w3c.dom.DOMException)12 ICSSModel (org.eclipse.wst.css.core.internal.provisional.document.ICSSModel)11 ICSSStyleDeclItem (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem)11 Iterator (java.util.Iterator)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 ICSSDocument (org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument)9 Region (org.eclipse.jface.text.Region)8 CSSSourceFormatter (org.eclipse.wst.css.core.internal.formatter.CSSSourceFormatter)6 ICSSPrimitiveValue (org.eclipse.wst.css.core.internal.provisional.document.ICSSPrimitiveValue)6 ICSSStyleRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule)6 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)6 CSSCleanupStrategy (org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)5 ICSSImportRule (org.eclipse.wst.css.core.internal.provisional.document.ICSSImportRule)5 ICSSStyleDeclaration (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration)5 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)5