Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(423)

Side by Side Diff: Source/devtools/front_end/CSSFormatter.js

Issue 18347003: DevTools: Implement CSS pretty-printing (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Comments addressed Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /**
32 * @constructor
33 * @param {string} content
34 * @param {!CSSFormattedContentBuilder} builder
35 */
36 function CSSFormatter(content, builder)
37 {
38 this._content = content;
39 this._builder = builder;
40 this._lastLine = -1;
41 this._state = {};
42 }
43
44 CSSFormatter.prototype = {
45 format: function()
46 {
47 this._lineEndings = lineEndings(this._content);
48 var tokenize = WebInspector.CodeMirrorUtils.createTokenizer("text/css");
49 var lines = this._content.split("\n");
50
51 for (var i = 0; i < lines.length; ++i) {
52 var line = lines[i];
53 tokenize(line, this._tokenCallback.bind(this, i));
54 }
55 this._builder.flushNewLines(true);
56 },
57
58 _tokenCallback: function(startLine, token, type, startColumn)
59 {
60 if (startLine !== this._lastLine)
61 this._state.eatWhitespace = true;
62 if (/^css-property/.test(type) && !this._state.inPropertyValue)
63 this._state.seenProperty = true;
64 this._lastLine = startLine;
65 var isWhitespace = /^\s+$/.test(token);
66 if (isWhitespace) {
67 if (!this._state.eatWhitespace)
68 this._builder.addSpace();
69 return;
70 }
71 this._state.eatWhitespace = false;
72 if (token === "\n")
73 return;
74
75 if (token !== "}") {
76 if (this._state.afterClosingBrace)
77 this._builder.addNewLine();
78 this._state.afterClosingBrace = false;
79 }
80 var startPosition = (startLine ? this._lineEndings[startLine - 1] : 0) + startColumn;
81 if (token === "}") {
82 if (this._state.inPropertyValue)
83 this._builder.addNewLine();
84 this._builder.decreaseNestingLevel();
85 this._state.afterClosingBrace = true;
86 this._state.inPropertyValue = false;
87 } else if (token === ":" && !this._state.inPropertyValue && this._state. seenProperty) {
88 this._builder.addToken(token, startPosition, startLine, startColumn) ;
89 this._builder.addSpace();
90 this._state.eatWhitespace = true;
91 this._state.inPropertyValue = true;
92 this._state.seenProperty = false;
93 return;
94 } else if (token === "{") {
95 this._builder.addSpace();
96 this._builder.addToken(token, startPosition, startLine, startColumn) ;
97 this._builder.addNewLine();
98 this._builder.increaseNestingLevel();
99 return;
100 }
101
102 this._builder.addToken(token, startPosition, startLine, startColumn);
103
104 if (type === "css-comment" && !this._state.inPropertyValue && !this._sta te.seenProperty)
105 this._builder.addNewLine();
106 if (token === ";" && this._state.inPropertyValue) {
107 this._state.inPropertyValue = false;
108 this._builder.addNewLine();
109 } else if (token === "}") {
110 this._builder.addNewLine();
111 }
112 }
113 }
114
115 /**
116 * @constructor
117 * @param {string} content
118 * @param {{original: Array.<number>, formatted: Array.<number>}} mapping
119 * @param {number} originalOffset
120 * @param {number} formattedOffset
121 * @param {string} indentString
122 */
123 function CSSFormattedContentBuilder(content, mapping, originalOffset, formattedO ffset, indentString)
124 {
125 this._originalContent = content;
126 this._originalOffset = originalOffset;
127 this._lastOriginalPosition = 0;
128
129 this._formattedContent = [];
130 this._formattedContentLength = 0;
131 this._formattedOffset = formattedOffset;
132 this._lastFormattedPosition = 0;
133
134 this._mapping = mapping;
135
136 this._lineNumber = 0;
137 this._nestingLevel = 0;
138 this._needNewLines = 0;
139 this._atLineStart = true;
140 this._indentString = indentString;
141 this._cachedIndents = {};
142 }
143
144 CSSFormattedContentBuilder.prototype = {
145 /**
146 * @param {string} token
147 * @param {number} startPosition
148 * @param {number} startLine
149 * @param {number} startColumn
150 */
151 addToken: function(token, startPosition, startLine, startColumn)
152 {
153 if ((this._isWhitespaceRun || this._atLineStart) && /^\s+$/.test(token))
154 return;
155
156 if (this._isWhitespaceRun && this._lineNumber === startLine && !this._ne edNewLines)
157 this._addText(" ");
158
159 this._isWhitespaceRun = false;
160 this._atLineStart = false;
161
162 while (this._lineNumber < startLine) {
163 this._addText("\n");
164 this._addIndent();
165 this._needNewLines = 0;
166 this._lineNumber += 1;
167 this._atLineStart = true;
168 }
169
170 if (this._needNewLines) {
171 this.flushNewLines();
172 this._addIndent();
173 this._atLineStart = true;
174 }
175
176 this._addMappingIfNeeded(startPosition);
177 this._addText(token);
178 this._lineNumber = startLine;
179 },
180
181 addSpace: function()
182 {
183 if (this._isWhitespaceRun)
184 return;
185 this._isWhitespaceRun = true;
186 },
187
188 addNewLine: function()
189 {
190 ++this._needNewLines;
191 },
192
193 /**
194 * @param {boolean=} atLeastOne
195 */
196 flushNewLines: function(atLeastOne)
197 {
198 var newLineCount = atLeastOne && !this._needNewLines ? 1 : this._needNew Lines;
199 if (newLineCount)
200 this._isWhitespaceRun = false;
201 for (var i = 0; i < newLineCount; ++i)
202 this._addText("\n");
203 this._needNewLines = 0;
204 },
205
206 increaseNestingLevel: function()
207 {
208 this._nestingLevel += 1;
209 },
210
211 /**
212 * @param {boolean=} addNewline
213 */
214 decreaseNestingLevel: function(addNewline)
215 {
216 if (this._nestingLevel)
217 this._nestingLevel -= 1;
218 if (addNewline)
219 this.addNewLine();
220 },
221
222 /**
223 * @return {string}
224 */
225 content: function()
226 {
227 return this._formattedContent.join("");
228 },
229
230 _addIndent: function()
231 {
232 if (this._cachedIndents[this._nestingLevel]) {
233 this._addText(this._cachedIndents[this._nestingLevel]);
234 return;
235 }
236
237 var fullIndent = "";
238 for (var i = 0; i < this._nestingLevel; ++i)
239 fullIndent += this._indentString;
240 this._addText(fullIndent);
241
242 // Cache a maximum of 20 nesting level indents.
243 if (this._nestingLevel <= 20)
244 this._cachedIndents[this._nestingLevel] = fullIndent;
245 },
246
247 /**
248 * @param {string} text
249 */
250 _addText: function(text)
251 {
252 if (!text)
253 return;
254 this._formattedContent.push(text);
255 this._formattedContentLength += text.length;
256 },
257
258 /**
259 * @param {number} originalPosition
260 */
261 _addMappingIfNeeded: function(originalPosition)
262 {
263 if (originalPosition - this._lastOriginalPosition === this._formattedCon tentLength - this._lastFormattedPosition)
264 return;
265 this._mapping.original.push(this._originalOffset + originalPosition);
266 this._lastOriginalPosition = originalPosition;
267 this._mapping.formatted.push(this._formattedOffset + this._formattedCont entLength);
268 this._lastFormattedPosition = this._formattedContentLength;
269 }
270 }
271
272 /**
273 * @param {string} text
274 * @param {string} string
275 */
276 function findAll(text, string)
pfeldman 2013/09/30 15:46:06 Why are these global?
277 {
278 var matches = [];
279 var i = text.indexOf(string);
280 while (i !== -1) {
281 matches.push(i);
282 i = text.indexOf(string, i + string.length);
283 }
284 return matches;
285 }
286
287 /**
288 * @param {string} text
289 */
290 function lineEndings(text)
pfeldman 2013/09/30 15:46:06 Why are these global?
291 {
292 var lineEndings = text.findAll("\n");
293 lineEndings.push(text.length);
294 return lineEndings;
295 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698