2 * Copyright 2014 The Chromium Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
9 * @extends {WebInspector.Object}
11 WebInspector.CSSParser = function()
13 this._worker = Runtime.startWorker("script_formatter_worker");
14 this._worker.onmessage = this._onRuleChunk.bind(this);
18 WebInspector.CSSParser.Events = {
19 RulesParsed: "RulesParsed"
22 WebInspector.CSSParser.prototype = {
24 * @param {!WebInspector.CSSStyleSheetHeader} styleSheetHeader
25 * @param {function(!Array.<!WebInspector.CSSParser.Rule>)=} callback
27 fetchAndParse: function(styleSheetHeader, callback)
30 this._finishedCallback = callback;
31 styleSheetHeader.requestContent(this._innerParse.bind(this));
35 * @param {string} text
36 * @param {function(!Array.<!WebInspector.CSSParser.Rule>)=} callback
38 parse: function(text, callback)
41 this._finishedCallback = callback;
42 this._innerParse(text);
48 this._worker.terminate();
54 * @return {!Array.<!WebInspector.CSSParser.Rule>}
63 console.assert(!this._parsingStyleSheet, "Received request to parse stylesheet before previous was completed.");
64 this._parsingStyleSheet = true;
69 delete this._parsingStyleSheet;
73 * @param {?string} text
75 _innerParse: function(text)
78 this._worker.postMessage({ method: "parseCSS", params: { content: text } });
82 * @param {!MessageEvent} event
84 _onRuleChunk: function(event)
86 var data = /** @type {!WebInspector.CSSParser.DataChunk} */ (event.data);
87 var chunk = data.chunk;
88 for (var i = 0; i < chunk.length; ++i)
89 this._rules.push(chunk[i]);
92 this._onFinishedParsing();
93 this.dispatchEventToListeners(WebInspector.CSSParser.Events.RulesParsed);
96 _onFinishedParsing: function()
99 if (this._finishedCallback)
100 this._finishedCallback(this._rules);
103 __proto__: WebInspector.Object.prototype,
107 * @typedef {{isLastChunk: boolean, chunk: !Array.<!WebInspector.CSSParser.Rule>}}
109 WebInspector.CSSParser.DataChunk;
112 * @typedef {{selectorText: string, lineNumber: number, columnNumber: number, properties: !Array.<!WebInspector.CSSParser.Property>}}
114 WebInspector.CSSParser.StyleRule;
117 * @typedef {{atRule: string, lineNumber: number, columnNumber: number}}
119 WebInspector.CSSParser.AtRule;
122 * @typedef {(WebInspector.CSSParser.StyleRule|WebInspector.CSSParser.AtRule)}
124 WebInspector.CSSParser.Rule;
127 * @typedef {{name: string, value: string}}
129 WebInspector.CSSParser.Property;