| OLD | NEW |
| 1 /** | 1 /** |
| 2 * Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * @unrestricted | 8 * @unrestricted |
| 9 */ | 9 */ |
| 10 SDK.CSSParser = class extends Common.Object { | 10 SDK.CSSParser = class extends Common.Object { |
| 11 constructor() { | 11 constructor() { |
| 12 super(); | 12 super(); |
| 13 this._rules = []; | 13 this._rules = []; |
| 14 this._terminated = false; | 14 this._terminated = false; |
| 15 } | 15 } |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * @param {!SDK.CSSStyleSheetHeader} styleSheetHeader | 18 * @param {!SDK.CSSStyleSheetHeader} styleSheetHeader |
| 19 * @param {function(!Array.<!SDK.CSSParser.Rule>)=} callback | 19 * @param {function(!Array.<!Common.FormatterWorkerPool.CSSRule>)=} callback |
| 20 */ | 20 */ |
| 21 fetchAndParse(styleSheetHeader, callback) { | 21 fetchAndParse(styleSheetHeader, callback) { |
| 22 this._lock(); | 22 this._lock(); |
| 23 this._finishedCallback = callback; | 23 this._finishedCallback = callback; |
| 24 styleSheetHeader.requestContent().then(this._innerParse.bind(this)); | 24 styleSheetHeader.requestContent().then(this._innerParse.bind(this)); |
| 25 } | 25 } |
| 26 | 26 |
| 27 /** | 27 /** |
| 28 * @param {string} text | 28 * @param {string} text |
| 29 * @param {function(!Array.<!SDK.CSSParser.Rule>)=} callback | 29 * @param {function(!Array.<!Common.FormatterWorkerPool.CSSRule>)=} callback |
| 30 */ | 30 */ |
| 31 parse(text, callback) { | 31 parse(text, callback) { |
| 32 this._lock(); | 32 this._lock(); |
| 33 this._finishedCallback = callback; | 33 this._finishedCallback = callback; |
| 34 this._innerParse(text); | 34 this._innerParse(text); |
| 35 } | 35 } |
| 36 | 36 |
| 37 /** | 37 /** |
| 38 * @param {string} text | 38 * @param {string} text |
| 39 * @return {!Promise<!Array.<!SDK.CSSParser.Rule>>} | 39 * @return {!Promise<!Array.<!Common.FormatterWorkerPool.CSSRule>>} |
| 40 */ | 40 */ |
| 41 parsePromise(text) { | 41 parsePromise(text) { |
| 42 return new Promise(promiseConstructor.bind(this)); | 42 return new Promise(promiseConstructor.bind(this)); |
| 43 | 43 |
| 44 /** | 44 /** |
| 45 * @param {function()} succ | 45 * @param {function()} succ |
| 46 * @param {function()} fail | 46 * @param {function()} fail |
| 47 * @this {SDK.CSSParser} | 47 * @this {SDK.CSSParser} |
| 48 */ | 48 */ |
| 49 function promiseConstructor(succ, fail) { | 49 function promiseConstructor(succ, fail) { |
| 50 this.parse(text, succ); | 50 this.parse(text, succ); |
| 51 } | 51 } |
| 52 } | 52 } |
| 53 | 53 |
| 54 dispose() { | 54 dispose() { |
| 55 if (this._terminated) | 55 if (this._terminated) |
| 56 return; | 56 return; |
| 57 this._terminated = true; | 57 this._terminated = true; |
| 58 this._runFinishedCallback([]); | 58 this._runFinishedCallback([]); |
| 59 } | 59 } |
| 60 | 60 |
| 61 /** | 61 /** |
| 62 * @return {!Array.<!SDK.CSSParser.Rule>} | 62 * @return {!Array.<!Common.FormatterWorkerPool.CSSRule>} |
| 63 */ | 63 */ |
| 64 rules() { | 64 rules() { |
| 65 return this._rules; | 65 return this._rules; |
| 66 } | 66 } |
| 67 | 67 |
| 68 _lock() { | 68 _lock() { |
| 69 console.assert(!this._parsingStyleSheet, 'Received request to parse styleshe
et before previous was completed.'); | 69 console.assert(!this._parsingStyleSheet, 'Received request to parse styleshe
et before previous was completed.'); |
| 70 this._parsingStyleSheet = true; | 70 this._parsingStyleSheet = true; |
| 71 } | 71 } |
| 72 | 72 |
| 73 _unlock() { | 73 _unlock() { |
| 74 delete this._parsingStyleSheet; | 74 delete this._parsingStyleSheet; |
| 75 } | 75 } |
| 76 | 76 |
| 77 /** | 77 /** |
| 78 * @param {?string} text | 78 * @param {?string} text |
| 79 */ | 79 */ |
| 80 _innerParse(text) { | 80 _innerParse(text) { |
| 81 this._rules = []; | 81 this._rules = []; |
| 82 var params = {content: text}; | 82 Common.formatterWorkerPool.parseCSS(text || '', this._onRuleChunk.bind(this)
); |
| 83 Common.formatterWorkerPool.runChunkedTask('parseCSS', params, this._onRuleCh
unk.bind(this)); | |
| 84 } | 83 } |
| 85 | 84 |
| 86 /** | 85 /** |
| 87 * @param {?MessageEvent} event | 86 * @param {boolean} isLastChunk |
| 87 * @param {!Array.<!Common.FormatterWorkerPool.CSSRule>} rules |
| 88 */ | 88 */ |
| 89 _onRuleChunk(event) { | 89 _onRuleChunk(isLastChunk, rules) { |
| 90 if (this._terminated) | 90 if (this._terminated) |
| 91 return; | 91 return; |
| 92 if (!event) { | 92 this._rules = this._rules.concat(rules); |
| 93 this._onFinishedParsing(); | 93 if (isLastChunk) |
| 94 this.dispatchEventToListeners(SDK.CSSParser.Events.RulesParsed); | |
| 95 return; | |
| 96 } | |
| 97 var data = /** @type {!SDK.CSSParser.DataChunk} */ (event.data); | |
| 98 var chunk = data.chunk; | |
| 99 for (var i = 0; i < chunk.length; ++i) | |
| 100 this._rules.push(chunk[i]); | |
| 101 | |
| 102 if (data.isLastChunk) | |
| 103 this._onFinishedParsing(); | 94 this._onFinishedParsing(); |
| 104 this.dispatchEventToListeners(SDK.CSSParser.Events.RulesParsed); | 95 this.dispatchEventToListeners(SDK.CSSParser.Events.RulesParsed); |
| 105 } | 96 } |
| 106 | 97 |
| 107 _onFinishedParsing() { | 98 _onFinishedParsing() { |
| 108 this._unlock(); | 99 this._unlock(); |
| 109 this._runFinishedCallback(this._rules); | 100 this._runFinishedCallback(this._rules); |
| 110 } | 101 } |
| 111 | 102 |
| 112 /** | 103 /** |
| 113 * @param {!Array<!SDK.CSSRule>} rules | 104 * @param {!Array<!SDK.CSSRule>} rules |
| 114 */ | 105 */ |
| 115 _runFinishedCallback(rules) { | 106 _runFinishedCallback(rules) { |
| 116 var callback = this._finishedCallback; | 107 var callback = this._finishedCallback; |
| 117 delete this._finishedCallback; | 108 delete this._finishedCallback; |
| 118 if (callback) | 109 if (callback) |
| 119 callback.call(null, rules); | 110 callback.call(null, rules); |
| 120 } | 111 } |
| 121 }; | 112 }; |
| 122 | 113 |
| 123 /** @enum {symbol} */ | 114 /** @enum {symbol} */ |
| 124 SDK.CSSParser.Events = { | 115 SDK.CSSParser.Events = { |
| 125 RulesParsed: Symbol('RulesParsed') | 116 RulesParsed: Symbol('RulesParsed') |
| 126 }; | 117 }; |
| 127 | |
| 128 /** | |
| 129 * @typedef {{isLastChunk: boolean, chunk: !Array.<!SDK.CSSParser.Rule>}} | |
| 130 */ | |
| 131 SDK.CSSParser.DataChunk; | |
| 132 | |
| 133 /** | |
| 134 * @unrestricted | |
| 135 */ | |
| 136 SDK.CSSParser.StyleRule = class { | |
| 137 constructor() { | |
| 138 /** @type {string} */ | |
| 139 this.selectorText; | |
| 140 /** @type {!SDK.CSSParser.Range} */ | |
| 141 this.styleRange; | |
| 142 /** @type {number} */ | |
| 143 this.lineNumber; | |
| 144 /** @type {number} */ | |
| 145 this.columnNumber; | |
| 146 /** @type {!Array.<!SDK.CSSParser.Property>} */ | |
| 147 this.properties; | |
| 148 } | |
| 149 }; | |
| 150 | |
| 151 /** | |
| 152 * @typedef {{atRule: string, lineNumber: number, columnNumber: number}} | |
| 153 */ | |
| 154 SDK.CSSParser.AtRule; | |
| 155 | |
| 156 /** | |
| 157 * @typedef {(SDK.CSSParser.StyleRule|SDK.CSSParser.AtRule)} | |
| 158 */ | |
| 159 SDK.CSSParser.Rule; | |
| 160 | |
| 161 /** | |
| 162 * @typedef {{startLine: number, startColumn: number, endLine: number, endColumn
: number}} | |
| 163 */ | |
| 164 SDK.CSSParser.Range; | |
| 165 | |
| 166 /** | |
| 167 * @unrestricted | |
| 168 */ | |
| 169 SDK.CSSParser.Property = class { | |
| 170 constructor() { | |
| 171 /** @type {string} */ | |
| 172 this.name; | |
| 173 /** @type {!SDK.CSSParser.Range} */ | |
| 174 this.nameRange; | |
| 175 /** @type {string} */ | |
| 176 this.value; | |
| 177 /** @type {!SDK.CSSParser.Range} */ | |
| 178 this.valueRange; | |
| 179 /** @type {!SDK.CSSParser.Range} */ | |
| 180 this.range; | |
| 181 /** @type {(boolean|undefined)} */ | |
| 182 this.disabled; | |
| 183 } | |
| 184 }; | |
| OLD | NEW |