Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 2d0605d53285f0d42f8bba01baaad57a52c1bef8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*******************************************************************************
 * Copyright (c) 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.css.core.internal.formatter;



import org.eclipse.jface.text.IRegion;
import org.eclipse.wst.css.core.cleanup.CSSCleanupStrategy;
import org.eclipse.wst.css.core.document.ICSSAttr;
import org.eclipse.wst.css.core.document.ICSSCharsetRule;
import org.eclipse.wst.css.core.document.ICSSNode;
import org.eclipse.wst.css.core.parser.CSSRegionContexts;
import org.eclipse.wst.css.core.preferences.CSSPreferenceHelper;
import org.eclipse.wst.css.core.util.CSSUtil;
import org.eclipse.wst.css.core.util.RegionIterator;
import org.eclipse.wst.sse.core.IndexedRegion;
import org.eclipse.wst.sse.core.text.IStructuredDocument;
import org.eclipse.wst.sse.core.text.IStructuredDocumentRegion;
import org.eclipse.wst.sse.core.text.ITextRegion;


/**
 * 
 */
public class CharsetRuleFormatter extends DefaultCSSSourceFormatter {

	public final static java.lang.String CHARSET = "@charset";//$NON-NLS-1$
	private static CharsetRuleFormatter instance;

	/**
	 * 
	 */
	CharsetRuleFormatter() {
		super();
	}

	/**
	 * 
	 */
	public StringBuffer formatAttrChanged(ICSSNode node, ICSSAttr attr, boolean insert, AttrChangeContext context) {
		StringBuffer buf = new StringBuffer();
		if (node == null || attr == null)
			return buf;

		if (!ICSSCharsetRule.ENCODING.equalsIgnoreCase(attr.getName()))
			return buf;

		// get region to replace
		if (context != null && ((IndexedRegion) node).getEndOffset() > 0) {
			if (attr != null && ((IndexedRegion) attr).getEndOffset() > 0) {
				IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(((IndexedRegion) attr).getStartOffset());
				ITextRegion region = flatNode.getRegionAtCharacterOffset(((IndexedRegion) attr).getStartOffset());
				RegionIterator it = new RegionIterator(flatNode, region);
				it.prev();
				if (it.hasPrev()) {
					ITextRegion prev = it.prev();
					if (prev.getType() == CSSRegionContexts.CSS_S)
						context.start = it.getStructuredDocumentRegion().getStartOffset(prev);
					else
						context.start = it.getStructuredDocumentRegion().getStartOffset(region);
				}
				else
					context.start = it.getStructuredDocumentRegion().getStartOffset(region);
				context.end = it.getStructuredDocumentRegion().getEndOffset(region);
			}
			else {
				IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(((IndexedRegion) node).getEndOffset() - 1);
				ITextRegion region = flatNode.getRegionAtCharacterOffset(((IndexedRegion) node).getEndOffset() - 1);
				if (region.getType() == CSSRegionContexts.CSS_S) {
					context.start = flatNode.getStartOffset(region);
					context.end = flatNode.getStartOffset(region);
				}
				else {
					context.start = flatNode.getEndOffset() + 1;
					context.end = flatNode.getEndOffset();
				}
			}
		}
		// generate text
		if (insert) {
			appendSpaceBefore(node, attr.getValue(), buf);
			buf.append(attr.getValue());
		}
		return buf;
	}

	/**
	 * 
	 */
	protected void formatPre(ICSSNode node, StringBuffer source) {
		int end = ((IndexedRegion) node).getEndOffset();
		if (end > 0) { // format source
			int start = ((IndexedRegion) node).getStartOffset();
			formatPre(node, new FormatRegion(start, end - start), source);
		}
		else { // generate source
			CSSPreferenceHelper mgr = CSSPreferenceHelper.getInstance();
			String quote = mgr.getQuoteString(node.getOwnerDocument().getModel());
			String str = CHARSET;
			if (mgr.isIdentUpperCase())
				str = CHARSET.toUpperCase();
			source.append(str);
			String enc = ((ICSSCharsetRule) node).getEncoding();
			quote = CSSUtil.detectQuote(enc, quote);
			str = quote + ((enc != null) ? enc : "") + quote;//$NON-NLS-1$
			appendSpaceBefore(node, str, source);
			source.append(str);
		}
	}

	/**
	 * 
	 */
	protected void formatPre(ICSSNode node, IRegion region, StringBuffer source) {
		IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
		CSSCleanupStrategy stgy = getCleanupStrategy(node);

		CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, region, stgy);
		CompoundRegion[] outside = getOutsideRegions(structuredDocument, region);
		for (int i = 0; i < regions.length; i++) {
			if (i != 0 || needS(outside[0]))
				appendSpaceBefore(node, regions[i], source);
			source.append(decoratedIdentRegion(regions[i], stgy));
		}
		if (needS(outside[1]) && !isIncludesPreEnd(node, region))
			appendSpaceBefore(node, outside[1], source);
	}

	/**
	 * 
	 */
	public int getAttrInsertPos(ICSSNode node, String attrName) {
		if (node == null || attrName == null || attrName.length() == 0)
			return -1;
		CSSCleanupStrategy stgy = getCleanupStrategy(node);

		if (!ICSSCharsetRule.ENCODING.equalsIgnoreCase(attrName))
			return -1;

		ICSSAttr attr = (ICSSAttr) node.getAttributes().getNamedItem(ICSSCharsetRule.ENCODING);
		if (attr != null && ((IndexedRegion) attr).getEndOffset() > 0)
			return ((IndexedRegion) attr).getStartOffset();
		else {
			IndexedRegion iNode = (IndexedRegion) node;
			if (iNode.getEndOffset() <= 0)
				return -1;

			CompoundRegion regions[] = getRegionsWithoutWhiteSpaces(node.getOwnerDocument().getModel().getStructuredDocument(), new FormatRegion(iNode.getStartOffset(), iNode.getEndOffset() - iNode.getStartOffset()), stgy);
			for (int i = regions.length - 1; i >= 0; i--) {
				if (regions[i].getType() != CSSRegionContexts.CSS_COMMENT)
					return regions[i].getStartOffset();
			}
			return iNode.getEndOffset();
		}
	}

	/**
	 * 
	 */
	public int getChildInsertPos(ICSSNode node) {
		int n = ((IndexedRegion) node).getEndOffset();
		if (n > 0) {
			IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(n - 1);
			if (flatNode.getRegionAtCharacterOffset(n - 1).getType() == CSSRegionContexts.CSS_DELIMITER)
				return n - 1;
			else
				return n;
		}
		return -1;
	}

	/**
	 * 
	 */
	public synchronized static CharsetRuleFormatter getInstance() {
		if (instance == null)
			instance = new CharsetRuleFormatter();
		return instance;
	}
}

Back to the top