use of org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule in project webtools.sourceediting by eclipse.
the class TestForNPEInCSSCreation method testCSSModel.
public void testCSSModel() {
IDOMModel model = FileUtil.createHTMLModel();
try {
IStructuredDocument structuredDocument = model.getStructuredDocument();
structuredDocument.set(getHTMLDocumentText());
IDOMDocument doc = model.getDocument();
// get head tag
NodeList list = doc.getElementsByTagName(HTML40Namespace.ElementName.HEAD);
Element head = (Element) list.item(0);
// create and append style element
Element ele = doc.createElement(HTML40Namespace.ElementName.STYLE);
ele.setAttribute(HTML40Namespace.ATTR_NAME_TYPE, "text/css");
String delim = model.getStructuredDocument().getLineDelimiter();
if (delim == null)
// $NON-NLS-1$
delim = "\n";
StringBuffer buffer = new StringBuffer(delim);
// $NON-NLS-1$
buffer.append("<!--");
buffer.append(delim);
// $NON-NLS-1$
buffer.append("-->");
buffer.append(delim);
Text text = doc.createTextNode(buffer.toString());
ele.appendChild(text);
head.insertBefore(ele, null);
// get adapter for StyleSheet
ICSSStyleSheet sheet = (ICSSStyleSheet) ((IStyleSheetAdapter) ((INodeNotifier) ele).getAdapterFor(IStyleSheetAdapter.class)).getSheet();
// create style declaration
ICSSStyleRule rule = sheet.createCSSStyleRule();
rule.getStyle().setProperty("background-color", "lime", "");
rule.getStyle().setProperty("background-color", "blue", "");
rule.getStyle().setProperty("background-color", "#0080ff", "");
// model.save();
} finally {
if (model != null) {
model.releaseFromEdit();
}
}
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule in project webtools.sourceediting by eclipse.
the class StyleRuleFormatter method formatPre.
/**
*/
protected void formatPre(ICSSNode node, StringBuffer source) {
CSSCleanupStrategy stgy = getCleanupStrategy(node);
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
IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy);
for (int i = 0; i < regions.length; i++) {
if (i != 0)
appendSpaceBetween(node, regions[i - 1], regions[i], source);
source.append(decoratedSelectorRegion(regions[i], (i != 0) ? regions[i - 1] : null, stgy));
}
} else {
// generate source
String str = ((ICSSStyleRule) node).getSelectorText();
// appendSpaceBefore(node,str,source);
source.append(str);
// $NON-NLS-1$
appendSpaceBefore(node, "{", source);
// $NON-NLS-1$
source.append("{");
}
appendDelimBefore(node, null, source);
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule in project webtools.sourceediting by eclipse.
the class CSSModelParser method insertBraceClose.
/**
*/
private CSSNodeImpl insertBraceClose(IStructuredDocumentRegion region) {
ICSSNode target = CSSModelUtil.findBraceContainer(fCreationContext.getTargetNode());
if (!(target instanceof CSSStructuredDocumentRegionContainer)) {
return null;
}
CSSStructuredDocumentRegionContainer parent = (CSSStructuredDocumentRegionContainer) target;
if (CSSModelUtil.isInterruption(parent, region)) {
ICSSNode node = parent.getParentNode();
if (node instanceof CSSNodeImpl) {
fCreationContext.setReparseStart(parent.getStartOffset());
fCreationContext.setReparseEnd(parent.getEndOffset());
((CSSNodeImpl) node).removeChild(parent);
}
return null;
}
if (parent instanceof ICSSPageRule || parent instanceof ICSSMediaRule || parent instanceof CSSFontFaceRule || parent instanceof ICSSStyleRule) {
CSSModelUtil.expandStructuredDocumentRegionContainer(parent, region);
if (!CSSModelUtil.isBraceClosed(target)) {
fCreationContext.setTargetNode(parent.getParentNode());
fCreationContext.setNextNode(parent.getNextSibling());
return parent;
}
}
return null;
}
Aggregations