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