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;
}
}
}
}
}
}
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();
}
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);
}
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);
}
}
}
}
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);
}
}
}
}
Aggregations