use of org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule in project webtools.sourceediting by eclipse.
the class TestCSSDecl method testInsertPropertyInExistingRule.
/**
* To test https://bugs.eclipse.org/298111
*/
public void testInsertPropertyInExistingRule() {
ICSSModel model = FileUtil.createModel();
try {
String originalDocument = "@CHARSET \"ISO-8859-1\";\r\n" + ".test {\r\n" + "}";
IStructuredDocument structuredDocument = model.getStructuredDocument();
structuredDocument.set(originalDocument);
IndexedRegion indexedRegion = model.getIndexedRegion(28);
assertTrue("Indexed region should be an ICSSStyleRule", indexedRegion instanceof ICSSStyleRule);
ICSSStyleRule rule = (ICSSStyleRule) indexedRegion;
ICSSStyleDeclaration declaration = (ICSSStyleDeclaration) rule.getStyle();
declaration.setProperty("color", "#008040", null);
CSSPropertyContext context = new CSSPropertyContext(declaration);
context.applyModified(declaration);
String newDocument = structuredDocument.get();
String expectedNewDocument = "@CHARSET \"ISO-8859-1\";\r\n" + ".test {\r\n" + "\tcolor: #008040\r\n" + "}";
assertEquals("The updated CSS document does not equal the expected", expectedNewDocument, newDocument);
} 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 CSSProposalGeneratorForPseudoSelector method getCandidates.
/**
* getCandidates method comment.
*/
protected Iterator getCandidates() {
List candidates = new ArrayList();
boolean hasLeadingColon = checkLeadingColon();
String textToReplace = fContext.getTextToReplace();
if (!hasLeadingColon && 0 < textToReplace.length() && !textToReplace.equals(fContext.getTextToCompare())) {
// cursor placed midpoint of the region
return candidates.iterator();
}
ITextRegion region = fContext.getTargetRegion();
if (region != null) {
String type = region.getType();
if (type != CSSRegionContexts.CSS_S && !CSSRegionUtil.isSelectorBegginingType(type)) {
return candidates.iterator();
}
}
boolean useUpperCase = CSSCorePlugin.getDefault().getPluginPreferences().getInt(CSSCorePreferenceNames.CASE_IDENTIFIER) == CSSCorePreferenceNames.UPPER;
List tags = getSelectorTags();
Collections.sort(tags, new Comparator() {
/* (non-Javadoc)
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
public int compare(Object o1, Object o2) {
return clean(((CSSMMSelector) o1).getName()).compareTo(clean(((CSSMMSelector) o2).getName()));
}
private String clean(String str) {
int length = str.length();
for (int i = 0; i < length; i++) {
if (str.charAt(i) != ':') {
return str.substring(i);
}
}
return str;
}
});
Iterator i = tags.iterator();
while (i.hasNext()) {
CSSMMSelector selector = (CSSMMSelector) i.next();
String text = selector.getSelectorString();
if (hasLeadingColon && !isMatch(text)) {
continue;
}
text = (useUpperCase) ? text.toUpperCase() : text.toLowerCase();
int cursorPos = 0;
StringBuffer buf = new StringBuffer();
if (!hasLeadingColon)
buf.append(textToReplace);
buf.append(text);
cursorPos += buf.length();
if (0 < buf.length()) {
// Pseudoclass/element takes arguments
if (buf.charAt(buf.length() - 1) == ')') {
--cursorPos;
}
boolean inRule = (fContext.getTargetNode() instanceof ICSSStyleRule || fContext.getTargetNode() instanceof ICSSPageRule);
if (!inRule || (textToReplace.length() == 0 && !hasLeadingColon)) {
// $NON-NLS-1$
buf.append(" ");
cursorPos += 1;
}
if (!inRule) {
StringAndOffset sao = generateBraces();
buf.append(sao.fString);
cursorPos += sao.fOffset;
}
CSSCACandidate item = new CSSCACandidate();
item.setReplacementString(buf.toString());
item.setCursorPosition(cursorPos);
item.setDisplayString(text);
item.setImageType(CSSImageType.SELECTOR_PSEUDO);
item.setMMNode(selector);
candidates.add(item);
}
}
return candidates.iterator();
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule in project webtools.sourceediting by eclipse.
the class CSSProposalArranger method buildProposals.
/**
*/
void buildProposals() {
fProposals.clear();
/*
* String text; ICompletionProposal item; text = "---- Test
* Information ----"; item = new CompletionProposal("",
* fContext.getReplaceBegin(), 0, 0, null, text, null, null);
* fProposals.add(item);
*
* text = "Target: \"" + fContext.getRegionText() + "\"";
*
* item = new CompletionProposal("", fContext.getReplaceBegin(), 0, 0,
* null, text, null, null); fProposals.add(item);
*
* text = fContext.getTargetNode().getClass().toString(); int
* lastPeriodPos = text.lastIndexOf('.'); text = "Node: " +
* text.substring(lastPeriodPos + 1); item = new
* CompletionProposal("", fContext.getReplaceBegin(), 0, 0, null,
* text, null, null); fProposals.add(item);
*/
ICSSNode targetNode = fContext.getTargetNode();
// int targetPos = fContext.getTargetPos();
if (targetNode instanceof ICSSStyleSheet) {
buildProposalsForAnyRule();
} else if ((targetNode instanceof ICSSMediaRule && fContext.isTargetPosAfterOf(CSSRegionContexts.CSS_LBRACE)) || (targetNode instanceof ICSSStyleRule && fContext.isTargetPosBeforeOf(CSSRegionContexts.CSS_LBRACE))) {
buildProposalsForAnyRule();
// buildProposalsForStyleRule();
} else if ((targetNode instanceof ICSSPageRule && fContext.isTargetPosBeforeOf(CSSRegionContexts.CSS_LBRACE))) {
buildProposalsForPageRulePseudoClass();
} else if ((targetNode instanceof ICSSStyleRule || targetNode instanceof CSSFontFaceRule || targetNode instanceof ICSSPageRule || targetNode instanceof ICSSStyleDeclaration) && (targetNode.getOwnerDocument() instanceof ICSSStyleDeclaration || fContext.targetFollows(CSSRegionContexts.CSS_DECLARATION_DELIMITER) || fContext.targetFollows(CSSRegionContexts.CSS_LBRACE))) {
buildProposalsForDeclarationName();
} else if (targetNode instanceof ICSSStyleDeclItem) {
if (fContext.isTargetPosAfterOf(CSSRegionContexts.CSS_DECLARATION_SEPARATOR)) {
buildProposalsForDeclarationValue();
} else {
buildProposalsForDeclarationName();
}
} else if (targetNode instanceof ICSSPrimitiveValue) {
buildProposalsForDeclarationValue();
}
/*
* else if (targetNode instanceof ICSSPrimitiveValue || ((targetNode
* instanceof ICSSStyleRule || targetNode instanceof CSSFontFaceRule ||
* targetNode instanceof ICSSStyleDeclaration || targetNode instanceof
* ICSSStyleDeclItem) &&
* fContext.isTargetPosAfterOf(CSSRegionContexts.COLON))) {
* buildProposalsForDeclarationValue(); }
*/
// for Test
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule in project webtools.sourceediting by eclipse.
the class CSSProposalGeneratorForDeclarationName method getCandidates.
/**
* getCandidates method comment.
*/
protected Iterator getCandidates() {
List candidates = new ArrayList();
Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences();
// $NON-NLS-1$
String preDelim = "";
for (int i = 0; i < preferences.getInt(CSSCorePreferenceNames.FORMAT_PROP_PRE_DELIM); i++) {
// $NON-NLS-1$
preDelim += ' ';
}
// $NON-NLS-1$
String postDelim = "";
for (int i = 0; i < preferences.getInt(CSSCorePreferenceNames.FORMAT_PROP_POST_DELIM); i++) {
// $NON-NLS-1$
postDelim += ' ';
}
ICSSNode targetNode = fContext.getTargetNode();
boolean bFontRule = false;
for (ICSSNode node = targetNode; node != null; node = node.getParentNode()) {
if (node instanceof org.w3c.dom.css.CSSFontFaceRule) {
bFontRule = true;
break;
}
}
List names = new ArrayList();
CSSMetaModelUtil util = new CSSMetaModelUtil(fContext.getMetaModel());
Iterator iNames = util.collectNodesByType((bFontRule) ? CSSMMNode.TYPE_DESCRIPTOR : CSSMMNode.TYPE_PROPERTY);
while (iNames.hasNext()) {
CSSMMNode node = (CSSMMNode) iNames.next();
names.add(node);
}
sortNames(names);
// Collections.sort(names);
boolean bAddColon = true;
if ((targetNode instanceof ICSSStyleRule || targetNode instanceof ICSSStyleDeclItem || targetNode instanceof ICSSStyleDeclaration) && fContext.targetHas(CSSRegionContexts.CSS_DECLARATION_SEPARATOR)) {
bAddColon = false;
}
Iterator i = names.iterator();
while (i.hasNext()) {
CSSMMNode node = (CSSMMNode) i.next();
String text = node.getName();
text = (preferences.getInt(CSSCorePreferenceNames.CASE_PROPERTY_NAME) == CSSCorePreferenceNames.UPPER) ? text.toUpperCase() : text.toLowerCase();
if (!isMatch(text)) {
continue;
}
int cursorPos = 0;
StringBuffer buf = new StringBuffer();
buf.append(text);
buf.append(preDelim);
cursorPos = buf.length();
if (bAddColon) {
// $NON-NLS-1$
buf.append(':');
buf.append(postDelim);
cursorPos += 1 + postDelim.length();
}
// if (! (targetNode instanceof ICSSStyleDeclItem)) {
// buf.append(';');//$NON-NLS-1$
// }
CSSCACandidate item = new CSSCACandidate();
item.setReplacementString(buf.toString());
item.setCursorPosition(cursorPos);
item.setDisplayString(text);
item.setMMNode(node);
item.setImageType(getCategoryImageType(node));
candidates.add(item);
}
return candidates.iterator();
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule in project webtools.sourceediting by eclipse.
the class CSSProposalGeneratorForHTMLTag method getCandidates.
/**
* getCandidates method comment.
*/
protected Iterator getCandidates() {
List candidates = new ArrayList();
if (checkLeadingColon()) {
return candidates.iterator();
}
boolean bLowerCase = CSSCorePlugin.getDefault().getPluginPreferences().getInt(CSSCorePreferenceNames.CASE_SELECTOR) == CSSCorePreferenceNames.LOWER;
// XHTML requires lower case
if (fContext.getModel().getStyleSheetType() == ICSSModel.EMBEDDED) {
Node domNode = fContext.getModel().getOwnerDOMNode();
if (domNode != null && !(domNode instanceof Document)) {
domNode = domNode.getOwnerDocument();
if (domNode instanceof IDOMDocument) {
DocumentTypeAdapter adapter = (DocumentTypeAdapter) ((IDOMDocument) domNode).getAdapterFor(DocumentTypeAdapter.class);
if (adapter != null)
bLowerCase = (adapter.getTagNameCase() == DocumentTypeAdapter.LOWER_CASE);
}
}
}
int length = fHTMLTags.length;
for (int i = 0; i < length; i++) {
String tagText = fHTMLTags[i];
if (bLowerCase) {
tagText = tagText.toLowerCase();
}
if (!isMatch(tagText)) {
continue;
}
int cursorPos = 0;
StringBuffer buf = new StringBuffer();
buf.append(tagText);
cursorPos += tagText.length();
boolean inRule = (fContext.getTargetNode() instanceof ICSSStyleRule);
if (!inRule || fContext.getTextToReplace().length() == 0) {
// $NON-NLS-1$
buf.append(" ");
cursorPos += 1;
}
if (!inRule) {
StringAndOffset sao = generateBraces();
buf.append(sao.fString);
cursorPos += sao.fOffset;
}
CSSCACandidate item = new CSSCACandidate();
item.setReplacementString(buf.toString());
item.setCursorPosition(cursorPos);
item.setDisplayString(tagText);
item.setImageType(CSSImageType.SELECTOR_TAG);
candidates.add(item);
}
return candidates.iterator();
}
Aggregations