1/** 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. 5 */ 6 7/** 8 * @constructor 9 * @extends {WebInspector.Object} 10 */ 11WebInspector.CSSParser = function() 12{ 13 this._worker = new Worker("script_formatter_worker/ScriptFormatterWorker.js"); 14 this._worker.onmessage = this._onRuleChunk.bind(this); 15 this._rules = []; 16} 17 18WebInspector.CSSParser.Events = { 19 RulesParsed: "RulesParsed" 20} 21 22WebInspector.CSSParser.prototype = { 23 /** 24 * @param {!WebInspector.CSSStyleSheetHeader} styleSheetHeader 25 * @param {function(!Array.<!WebInspector.CSSParser.Rule>)=} callback 26 */ 27 fetchAndParse: function(styleSheetHeader, callback) 28 { 29 this._lock(); 30 this._finishedCallback = callback; 31 styleSheetHeader.requestContent(this._innerParse.bind(this)); 32 }, 33 34 /** 35 * @param {string} text 36 * @param {function(!Array.<!WebInspector.CSSParser.Rule>)=} callback 37 */ 38 parse: function(text, callback) 39 { 40 this._lock(); 41 this._finishedCallback = callback; 42 this._innerParse(text); 43 }, 44 45 dispose: function() 46 { 47 if (this._worker) { 48 this._worker.terminate(); 49 delete this._worker; 50 } 51 }, 52 53 /** 54 * @return {!Array.<!WebInspector.CSSParser.Rule>} 55 */ 56 rules: function() 57 { 58 return this._rules; 59 }, 60 61 _lock: function() 62 { 63 console.assert(!this._parsingStyleSheet, "Received request to parse stylesheet before previous was completed."); 64 this._parsingStyleSheet = true; 65 }, 66 67 _unlock: function() 68 { 69 delete this._parsingStyleSheet; 70 }, 71 72 /** 73 * @param {?string} text 74 */ 75 _innerParse: function(text) 76 { 77 this._rules = []; 78 this._worker.postMessage({ method: "parseCSS", params: { content: text } }); 79 }, 80 81 /** 82 * @param {!MessageEvent} event 83 */ 84 _onRuleChunk: function(event) 85 { 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]); 90 91 if (data.isLastChunk) 92 this._onFinishedParsing(); 93 this.dispatchEventToListeners(WebInspector.CSSParser.Events.RulesParsed); 94 }, 95 96 _onFinishedParsing: function() 97 { 98 this._unlock(); 99 if (this._finishedCallback) 100 this._finishedCallback(this._rules); 101 }, 102 103 __proto__: WebInspector.Object.prototype, 104} 105 106/** 107 * @typedef {{isLastChunk: boolean, chunk: !Array.<!WebInspector.CSSParser.Rule>}} 108 */ 109WebInspector.CSSParser.DataChunk; 110 111/** 112 * @typedef {{selectorText: string, lineNumber: number, columnNumber: number, properties: !Array.<!WebInspector.CSSParser.Property>}} 113 */ 114WebInspector.CSSParser.StyleRule; 115 116/** 117 * @typedef {{atRule: string, lineNumber: number, columnNumber: number}} 118 */ 119WebInspector.CSSParser.AtRule; 120 121/** 122 * @typedef {(WebInspector.CSSParser.StyleRule|WebInspector.CSSParser.AtRule)} 123 */ 124WebInspector.CSSParser.Rule; 125 126/** 127 * @typedef {{name: string, value: string}} 128 */ 129WebInspector.CSSParser.Property; 130