use of org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet in project webtools.sourceediting by eclipse.
the class AbstractCSSSourceFormatter method appendSpaceBefore.
/**
*/
protected void appendSpaceBefore(ICSSNode node, CompoundRegion toAppend, StringBuffer source) {
if (node == null || toAppend == null || source == null)
return;
if (isCleanup() && !getCleanupStrategy(node).isFormatSource())
// for not formatting case on cleanup action
return;
String type = toAppend.getType();
Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences();
boolean needIndent = !(node instanceof ICSSStyleSheet);
if (type == CSSRegionContexts.CSS_COMMENT) {
// check whether previous region is 'S' and has CR-LF
String delim = getLineDelimiter(node);
RegionIterator it = new RegionIterator(toAppend.getDocumentRegion(), toAppend.getTextRegion());
it.prev();
ITextRegion prev = it.prev();
// bug390904
if (prev.getType() == CSSRegionContexts.CSS_LBRACE && TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getFullText(prev), 0)[0] > 0) {
source.append(delim);
source.append(getIndent(node));
source.append(getIndentString());
} else if (prev.getType() == CSSRegionContexts.CSS_S && TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getText(prev), 0)[0] >= 0) {
source.append(delim);
source.append(getIndent(node));
if (needIndent)
source.append(getIndentString());
} else {
appendSpaceBefore(node, toAppend.getText(), source);
}
} else if (type == CSSRegionContexts.CSS_LBRACE && preferences.getBoolean(CSSCorePreferenceNames.WRAPPING_NEWLINE_ON_OPEN_BRACE)) {
String delim = getLineDelimiter(node);
source.append(delim);
source.append(getIndent(node));
// } else if (type == CSSRegionContexts.CSS_CURLY_BRACE_CLOSE) {
// } else if (type == CSSRegionContexts.CSS_INCLUDES || type ==
// CSSRegionContexts.CSS_DASHMATCH) {
} else if (type == CSSRegionContexts.CSS_DECLARATION_SEPARATOR && node instanceof ICSSStyleDeclItem) {
int n = preferences.getInt(CSSCorePreferenceNames.FORMAT_PROP_PRE_DELIM);
// no delimiter case
while (n-- > 0) // $NON-NLS-1$
source.append(" ");
} else if (type == CSSRegionContexts.CSS_DECLARATION_VALUE_OPERATOR || type == CSSRegionContexts.CSS_DECLARATION_VALUE_PARENTHESIS_CLOSE) {
if (preferences.getInt(CSSCorePreferenceNames.LINE_WIDTH) > 0 && (!preferences.getBoolean(CSSCorePreferenceNames.WRAPPING_PROHIBIT_WRAP_ON_ATTR) || node.getOwnerDocument().getNodeType() != ICSSNode.STYLEDECLARATION_NODE)) {
int length = getLastLineLength(node, source);
int append = 1;
if (length + append > preferences.getInt(CSSCorePreferenceNames.LINE_WIDTH)) {
source.append(getLineDelimiter(node));
source.append(getIndent(node));
if (needIndent)
source.append(getIndentString());
}
}
} else if (CSSRegionContexts.CSS_FOREIGN_ELEMENT == type || CSSRegionContexts.CSS_DECLARATION_DELIMITER == type) {
return;
} else
appendSpaceBefore(node, toAppend.getText(), source);
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet in project webtools.sourceediting by eclipse.
the class CSSNodeAdapter method internalActionPerformed.
/**
* Insert the method's description here.
*/
protected void internalActionPerformed() {
if (notifyQueue == null) {
return;
}
boolean refresh_all = false;
boolean refresh_rule = false;
int pos_all = 0;
List targets = new ArrayList();
for (int i = 0; i < notifyQueue.size(); i++) {
NotifyContext context = (NotifyContext) notifyQueue.get(i);
if (context.notifier instanceof ICSSStyleSheet) {
refresh_all = true;
pos_all = i;
}
if (context.notifier instanceof ICSSStyleDeclaration) {
refresh_rule = true;
targets.add(context);
// pos_rule = i;
}
// ((NotifyContext) notifyQueue.get(i)).fire();
}
if (refresh_all) {
((NotifyContext) notifyQueue.get(pos_all)).fire();
} else if (refresh_rule) {
Iterator i = targets.iterator();
while (i.hasNext()) {
((NotifyContext) i.next()).fire();
}
// else if (refresh_rule) internalRefreshAll();
} else {
for (int i = 0; i < notifyQueue.size(); i++) {
((NotifyContext) notifyQueue.get(i)).fire();
}
}
notifyQueue.clear();
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet in project webtools.sourceediting by eclipse.
the class CSSMediaRuleTest method testCreateRule.
public void testCreateRule() {
ICSSStyleSheet sheet = getStyleSheet();
ICSSDocument doc = sheet;
ICSSMediaRule newRule = doc.createCSSMediaRule();
MediaList newList = newRule.getMedia();
newList.appendMedium("media1");
newList.appendMedium("media2");
sheet.insertRuleBefore(newRule, null);
CSSRuleList ruleList = sheet.getCssRules();
CSSRule rule = ruleList.item(0);
assertTrue(rule instanceof CSSMediaRule);
CSSMediaRule mediaRule = (CSSMediaRule) rule;
MediaList mediaList = mediaRule.getMedia();
assertEquals(2, mediaList.getLength());
assertEquals("media1", mediaList.item(0));
assertEquals("media2", mediaList.item(1));
assertEquals("@media media1, media2 {" + FileUtil.commonEOL + "}", mediaRule.getCssText());
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet in project webtools.sourceediting by eclipse.
the class StyleSheetTest method testImportFromParentDirectory.
/**
* Tests for the import of a stylesheet in a parent directory
* @throws Exception
*/
public void testImportFromParentDirectory() throws Exception {
String filePath = "/" + PROJECT_NAME + "/WebContent/style/sub.css";
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(filePath));
assertTrue("File doesn't exist: " + filePath, file.exists());
IStructuredModel model = null;
try {
model = StructuredModelManager.getModelManager().getModelForRead(file);
if (model instanceof ICSSModel) {
ICSSDocument document = ((ICSSModel) model).getDocument();
if (document instanceof ICSSStyleSheet) {
CSSRuleList list = ((ICSSStyleSheet) document).getCssRules(true);
assertTrue("There should be a total of 1 rule.", list.getLength() == 1);
CSSRule rule = list.item(0);
assertTrue("Rule should be a style rule. rule.getType() == " + rule.getType(), rule.getType() == ICSSNode.STYLERULE_NODE);
assertEquals("Stylesheet reference is different than expected.", ((CSSStyleRule) rule).getSelectorText(), "#content");
}
}
} finally {
if (model != null)
model.releaseFromRead();
}
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet in project webtools.sourceediting by eclipse.
the class StyleSheetTest method testImportCycle.
/**
* Tests for cycles in imports. If a cycle is encountered, we should not get stuck
* in a loop or re-import the stylesheet.
* @throws Exception
*/
public void testImportCycle() throws Exception {
String filePath = "/" + PROJECT_NAME + "/WebContent/cycle0.css";
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(filePath));
assertTrue("File doesn't exist: " + filePath, file.exists());
IStructuredModel model = null;
try {
model = StructuredModelManager.getModelManager().getModelForRead(file);
if (model instanceof ICSSModel) {
ICSSDocument document = ((ICSSModel) model).getDocument();
if (document instanceof ICSSStyleSheet) {
CSSRuleList list = ((ICSSStyleSheet) document).getCssRules(true);
assertTrue("There should be a total of 3 rules supplied stylesheet.", list.getLength() == 3);
for (int i = 0; i < list.getLength(); i++) {
CSSRule rule = list.item(i);
assertTrue("Rule should be a style rule. rule.getType() == " + rule.getType(), rule.getType() == ICSSNode.STYLERULE_NODE);
// Check that the rules are imported, it would start with the last imported stylesheet's rules and move up
assertEquals("Style rules are not equal.", ((CSSStyleRule) rule).getSelectorText(), "#cycle" + (2 - i));
}
}
}
} finally {
if (model != null)
model.releaseFromRead();
}
}
Aggregations