Search in sources :

Example 66 with ICSSNode

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

the class AbstractCSSSourceFormatter method formatChildren.

/**
 */
protected final void formatChildren(ICSSNode node, StringBuffer source) {
    ICSSNode child = node.getFirstChild();
    ICSSNode last = null;
    boolean first = true;
    while (child != null) {
        // append child
        CSSSourceFormatter formatter = (CSSSourceFormatter) ((INodeNotifier) child).getAdapterFor(CSSSourceFormatter.class);
        if (formatter == null) {
            formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) child);
        }
        StringBuffer childSource = ((AbstractCSSSourceFormatter) formatter).formatProc(child);
        if (!first) {
            formatBefore(node, child, new String(childSource), source, null);
        }
        source.append(childSource);
        last = child;
        // append between children
        child = child.getNextSibling();
        first = false;
    }
    // This handles the case where the last child doesn't align with the end of the parent node, likely malformed content
    if (node instanceof IndexedRegion && last instanceof IndexedRegion && (node.getOwnerDocument().getNodeType() == ICSSNode.STYLEDECLARATION_NODE)) {
        IndexedRegion parent = (IndexedRegion) node;
        IndexedRegion lastChild = (IndexedRegion) last;
        if (lastChild.getEndOffset() < parent.getEndOffset()) {
            // Find the region at the end offset of the last child
            IStructuredDocumentRegion region = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(lastChild.getEndOffset());
            ITextRegionList regions = region != null ? region.getRegions() : null;
            if (regions != null) {
                Iterator it = regions.iterator();
                while (it.hasNext()) {
                    ITextRegion token = (ITextRegion) it.next();
                    if (token.getType() == CSSRegionContexts.CSS_UNKNOWN) {
                        // Found something that won't be consumed. Append the regions that remain in the node to the source
                        do {
                            source.append(region.getFullText());
                        } while ((region = region.getNext()) != null && region.getEndOffset() <= parent.getEndOffset());
                        break;
                    }
                }
            }
        }
    }
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) 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) Iterator(java.util.Iterator) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)

Example 67 with ICSSNode

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

the class CSSStyleSheetImpl method getRules.

private void getRules(CSSRuleListImpl list, ICSSStyleSheet sheet, Stack refs) {
    String href = sheet.getHref();
    if (href != null) {
        // Avoid circular @imports
        if (refs.contains(href))
            return;
        refs.push(href);
    }
    boolean acceptImports = true;
    for (ICSSNode node = sheet.getFirstChild(); node != null; node = node.getNextSibling()) {
        // @import rules must precede all other rules, according to the spec
        if (node.getNodeType() == ICSSNode.IMPORTRULE_NODE && acceptImports) {
            CSSStyleSheet importSheet = ((ICSSImportRule) node).getStyleSheet();
            if (importSheet instanceof ICSSStyleSheet)
                getRules(list, (ICSSStyleSheet) importSheet, refs);
            else
                list.appendNode(node);
        } else // Add the rule to the list
        if (node instanceof CSSRule) {
            list.appendNode(node);
            if (node.getNodeType() != ICSSNode.CHARSETRULE_NODE)
                acceptImports = false;
        }
    }
    if (href != null)
        refs.pop();
}
Also used : CSSRule(org.w3c.dom.css.CSSRule) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet) CSSStyleSheet(org.w3c.dom.css.CSSStyleSheet) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet) ICSSImportRule(org.eclipse.wst.css.core.internal.provisional.document.ICSSImportRule)

Example 68 with ICSSNode

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

the class CSSModelUpdater method defaultInserted.

/**
 */
private StructuredDocumentEvent defaultInserted(CSSNodeImpl parentNode, CSSNodeImpl node) {
    int insertPos = -1;
    ICSSNode sibling;
    // $NON-NLS-2$//$NON-NLS-1$
    String preSpace = "", postSpace = "";
    int length = 0;
    if (insertPos < 0) {
        if ((sibling = node.getPreviousSibling()) != null) {
            // after previous child
            insertPos = getTextEnd(sibling);
        }
    }
    if (insertPos < 0) {
        if ((sibling = node.getNextSibling()) != null) {
            // before next child
            insertPos = getTextStart(sibling);
        }
    }
    if (insertPos < 0) {
        // position of parent
        insertPos = getChildInsertPos(parentNode);
    }
    if (insertPos < 0) {
        // firsttime
        insertPos = 0;
    }
    // format previous spaces
    length = nearestSpaceLengthBefore(parentNode, insertPos);
    insertPos -= length;
    preSpace = getSpaceBefore(parentNode, node, null);
    // format post spaces
    length += nearestSpaceLengthAfter(parentNode, insertPos + length);
    postSpace = getSpaceBefore(parentNode, node.getNextSibling(), null);
    // set text
    String text = preSpace + node.generateSource().trim() + postSpace;
    return insertText(insertPos, length, text);
}
Also used : ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)

Example 69 with ICSSNode

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

the class FormatProcessorCSS method formatModel.

public void formatModel(IStructuredModel structuredModel, int start, int length) {
    CSSFormatUtil formatUtil = CSSFormatUtil.getInstance();
    if (structuredModel instanceof ICSSModel) {
        // BUG102822 take advantage of IDocumentExtension4
        IDocumentExtension4 docExt4 = null;
        if (structuredModel.getStructuredDocument() instanceof IDocumentExtension4) {
            docExt4 = (IDocumentExtension4) structuredModel.getStructuredDocument();
        }
        DocumentRewriteSession rewriteSession = null;
        try {
            DocumentRewriteSessionType rewriteType = (length > MAX_SMALL_FORMAT_SIZE) ? DocumentRewriteSessionType.UNRESTRICTED : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
            rewriteSession = (docExt4 == null || docExt4.getActiveRewriteSession() != null) ? null : docExt4.startRewriteSession(rewriteType);
            ICSSDocument doc = ((ICSSModel) structuredModel).getDocument();
            IStructuredDocumentRegion startRegion = structuredModel.getStructuredDocument().getRegionAtCharacterOffset(start);
            IStructuredDocumentRegion endRegion = structuredModel.getStructuredDocument().getRegionAtCharacterOffset(start + length);
            if (startRegion != null && endRegion != null) {
                start = startRegion.getStart();
                CSSSourceFormatter formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) doc);
                StringBuffer buf = formatter.format(doc, new Region(start, (endRegion.getEnd() - start)));
                if (buf != null) {
                    formatUtil.replaceSource(doc.getModel(), start, endRegion.getEnd() - start, buf.toString());
                }
            }
        } finally {
            // BUG102822 take advantage of IDocumentExtension4
            if (docExt4 != null && rewriteSession != null)
                docExt4.stopRewriteSession(rewriteSession);
        }
    } else if (structuredModel instanceof IDOMModel) {
        List cssnodes = formatUtil.collectCSSNodes(structuredModel, start, length);
        if (cssnodes != null && !cssnodes.isEmpty()) {
            ICSSModel model = null;
            // BUG102822 take advantage of IDocumentExtension4
            IDocumentExtension4 docExt4 = null;
            if (structuredModel.getStructuredDocument() instanceof IDocumentExtension4) {
                docExt4 = (IDocumentExtension4) structuredModel.getStructuredDocument();
            }
            DocumentRewriteSession rewriteSession = null;
            try {
                DocumentRewriteSessionType rewriteType = (length > MAX_SMALL_FORMAT_SIZE) ? DocumentRewriteSessionType.UNRESTRICTED : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
                rewriteSession = (docExt4 == null || docExt4.getActiveRewriteSession() != null) ? null : docExt4.startRewriteSession(rewriteType);
                for (int i = 0; i < cssnodes.size(); i++) {
                    ICSSNode node = (ICSSNode) cssnodes.get(i);
                    CSSSourceFormatter formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) node);
                    StringBuffer buf = formatter.format(node);
                    if (buf != null) {
                        int startOffset = ((IndexedRegion) node).getStartOffset();
                        int endOffset = ((IndexedRegion) node).getEndOffset();
                        if (model == null) {
                            model = node.getOwnerDocument().getModel();
                        }
                        formatUtil.replaceSource(model, startOffset, endOffset - startOffset, buf.toString());
                    }
                }
            } finally {
                // BUG102822 take advantage of IDocumentExtension4
                if (docExt4 != null && rewriteSession != null)
                    docExt4.stopRewriteSession(rewriteSession);
            }
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) CSSSourceFormatter(org.eclipse.wst.css.core.internal.formatter.CSSSourceFormatter) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) DocumentRewriteSessionType(org.eclipse.jface.text.DocumentRewriteSessionType) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) ICSSDocument(org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) CSSFormatUtil(org.eclipse.wst.css.core.internal.formatter.CSSFormatUtil) DocumentRewriteSession(org.eclipse.jface.text.DocumentRewriteSession) Region(org.eclipse.jface.text.Region) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) List(java.util.List) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)

Example 70 with ICSSNode

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

the class CSSStyleDeclarationFactoryContext method cloneChildNodes.

/**
 */
protected void cloneChildNodes(ICSSNode src, ICSSNode dest) {
    if (fOwnerDocument == null) {
        return;
    }
    if (!(dest instanceof CSSNodeImpl)) {
        return;
    }
    CSSNodeImpl container = (CSSNodeImpl) dest;
    container.removeChildNodes();
    for (ICSSNode child = src.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child instanceof CSSStyleDeclItemImpl) {
            // extract shorthand property ..
            // --> Temp
            ICSSNode cloned = child.cloneNode(false);
            if (cloned instanceof CSSNodeImpl) {
                ((CSSNodeImpl) cloned).setOwnerDocument(fOwnerDocument);
                container.appendChild((CSSNodeImpl) cloned);
                cloneChildNodes(child, cloned);
            }
        // <-- Temp
        } else {
            ICSSNode cloned = child.cloneNode(false);
            if (cloned instanceof CSSNodeImpl) {
                ((CSSNodeImpl) cloned).setOwnerDocument(fOwnerDocument);
                container.appendChild((CSSNodeImpl) cloned);
                cloneChildNodes(child, cloned);
            }
        }
    }
}
Also used : ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)

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