| OLD | NEW |
| (Empty) |
| 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 * @unrestricted | |
| 9 */ | |
| 10 SDK.CSSParser = class extends Common.Object { | |
| 11 constructor() { | |
| 12 super(); | |
| 13 this._rules = []; | |
| 14 this._terminated = false; | |
| 15 } | |
| 16 | |
| 17 /** | |
| 18 * @param {!SDK.CSSStyleSheetHeader} styleSheetHeader | |
| 19 * @param {function(!Array.<!Common.FormatterWorkerPool.CSSRule>)=} callback | |
| 20 */ | |
| 21 fetchAndParse(styleSheetHeader, callback) { | |
| 22 this._lock(); | |
| 23 this._finishedCallback = callback; | |
| 24 styleSheetHeader.requestContent().then(this._innerParse.bind(this)); | |
| 25 } | |
| 26 | |
| 27 /** | |
| 28 * @param {string} text | |
| 29 * @param {function(!Array.<!Common.FormatterWorkerPool.CSSRule>)=} callback | |
| 30 */ | |
| 31 parse(text, callback) { | |
| 32 this._lock(); | |
| 33 this._finishedCallback = callback; | |
| 34 this._innerParse(text); | |
| 35 } | |
| 36 | |
| 37 /** | |
| 38 * @param {string} text | |
| 39 * @return {!Promise<!Array.<!Common.FormatterWorkerPool.CSSRule>>} | |
| 40 */ | |
| 41 parsePromise(text) { | |
| 42 return new Promise(promiseConstructor.bind(this)); | |
| 43 | |
| 44 /** | |
| 45 * @param {function()} succ | |
| 46 * @param {function()} fail | |
| 47 * @this {SDK.CSSParser} | |
| 48 */ | |
| 49 function promiseConstructor(succ, fail) { | |
| 50 this.parse(text, succ); | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 dispose() { | |
| 55 if (this._terminated) | |
| 56 return; | |
| 57 this._terminated = true; | |
| 58 this._runFinishedCallback([]); | |
| 59 } | |
| 60 | |
| 61 /** | |
| 62 * @return {!Array.<!Common.FormatterWorkerPool.CSSRule>} | |
| 63 */ | |
| 64 rules() { | |
| 65 return this._rules; | |
| 66 } | |
| 67 | |
| 68 _lock() { | |
| 69 console.assert(!this._parsingStyleSheet, 'Received request to parse styleshe
et before previous was completed.'); | |
| 70 this._parsingStyleSheet = true; | |
| 71 } | |
| 72 | |
| 73 _unlock() { | |
| 74 delete this._parsingStyleSheet; | |
| 75 } | |
| 76 | |
| 77 /** | |
| 78 * @param {?string} text | |
| 79 */ | |
| 80 _innerParse(text) { | |
| 81 this._rules = []; | |
| 82 Common.formatterWorkerPool.parseCSS(text || '', this._onRuleChunk.bind(this)
); | |
| 83 } | |
| 84 | |
| 85 /** | |
| 86 * @param {boolean} isLastChunk | |
| 87 * @param {!Array.<!Common.FormatterWorkerPool.CSSRule>} rules | |
| 88 */ | |
| 89 _onRuleChunk(isLastChunk, rules) { | |
| 90 if (this._terminated) | |
| 91 return; | |
| 92 this._rules = this._rules.concat(rules); | |
| 93 if (isLastChunk) | |
| 94 this._onFinishedParsing(); | |
| 95 this.dispatchEventToListeners(SDK.CSSParser.Events.RulesParsed); | |
| 96 } | |
| 97 | |
| 98 _onFinishedParsing() { | |
| 99 this._unlock(); | |
| 100 this._runFinishedCallback(this._rules); | |
| 101 } | |
| 102 | |
| 103 /** | |
| 104 * @param {!Array<!SDK.CSSRule>} rules | |
| 105 */ | |
| 106 _runFinishedCallback(rules) { | |
| 107 var callback = this._finishedCallback; | |
| 108 delete this._finishedCallback; | |
| 109 if (callback) | |
| 110 callback.call(null, rules); | |
| 111 } | |
| 112 }; | |
| 113 | |
| 114 /** @enum {symbol} */ | |
| 115 SDK.CSSParser.Events = { | |
| 116 RulesParsed: Symbol('RulesParsed') | |
| 117 }; | |
| OLD | NEW |