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 * @constructor | 8 * @constructor |
9 * @extends {WebInspector.Object} | 9 * @extends {WebInspector.Object} |
10 */ | 10 */ |
11 WebInspector.CSSParser = function() | 11 WebInspector.CSSParser = function() |
12 { | 12 { |
13 this._worker = Runtime.startWorker("script_formatter_worker"); | |
14 this._worker.onmessage = this._onRuleChunk.bind(this); | |
15 this._rules = []; | 13 this._rules = []; |
14 Runtime.startWorker("script_formatter_worker").then(this._init.bind(this)); | |
16 } | 15 } |
17 | 16 |
18 WebInspector.CSSParser.Events = { | 17 WebInspector.CSSParser.Events = { |
19 RulesParsed: "RulesParsed" | 18 RulesParsed: "RulesParsed" |
20 } | 19 } |
21 | 20 |
22 WebInspector.CSSParser.prototype = { | 21 WebInspector.CSSParser.prototype = { |
22 _init: function(worker) | |
pfeldman
2014/11/07 14:51:17
Here and in other _inits, please annotate worker.
apavlov
2014/11/10 07:44:11
Done.
| |
23 { | |
24 this._worker = worker; | |
25 if (this._disposed) { | |
26 this.dispose(); | |
27 return; | |
28 } | |
29 this._worker.onmessage = this._onRuleChunk.bind(this); | |
30 this._parseStoredText(); | |
31 }, | |
32 | |
23 /** | 33 /** |
24 * @param {!WebInspector.CSSStyleSheetHeader} styleSheetHeader | 34 * @param {!WebInspector.CSSStyleSheetHeader} styleSheetHeader |
25 * @param {function(!Array.<!WebInspector.CSSParser.Rule>)=} callback | 35 * @param {function(!Array.<!WebInspector.CSSParser.Rule>)=} callback |
26 */ | 36 */ |
27 fetchAndParse: function(styleSheetHeader, callback) | 37 fetchAndParse: function(styleSheetHeader, callback) |
28 { | 38 { |
29 this._lock(); | 39 this._lock(); |
30 this._finishedCallback = callback; | 40 this._finishedCallback = callback; |
31 styleSheetHeader.requestContent(this._innerParse.bind(this)); | 41 styleSheetHeader.requestContent(this._innerParse.bind(this)); |
32 }, | 42 }, |
33 | 43 |
34 /** | 44 /** |
35 * @param {string} text | 45 * @param {string} text |
36 * @param {function(!Array.<!WebInspector.CSSParser.Rule>)=} callback | 46 * @param {function(!Array.<!WebInspector.CSSParser.Rule>)=} callback |
37 */ | 47 */ |
38 parse: function(text, callback) | 48 parse: function(text, callback) |
pfeldman
2014/11/07 14:51:17
Since this is async, you should promisify it as we
apavlov
2014/11/10 07:44:11
Done.
| |
39 { | 49 { |
40 this._lock(); | 50 this._lock(); |
41 this._finishedCallback = callback; | 51 this._finishedCallback = callback; |
42 this._innerParse(text); | 52 this._innerParse(text); |
43 }, | 53 }, |
44 | 54 |
45 dispose: function() | 55 dispose: function() |
46 { | 56 { |
47 if (this._worker) { | 57 if (this._worker) { |
48 this._worker.terminate(); | 58 this._worker.terminate(); |
49 delete this._worker; | 59 delete this._worker; |
60 } else { | |
61 this._disposed = true; | |
50 } | 62 } |
51 }, | 63 }, |
52 | 64 |
53 /** | 65 /** |
54 * @return {!Array.<!WebInspector.CSSParser.Rule>} | 66 * @return {!Array.<!WebInspector.CSSParser.Rule>} |
55 */ | 67 */ |
56 rules: function() | 68 rules: function() |
57 { | 69 { |
58 return this._rules; | 70 return this._rules; |
59 }, | 71 }, |
60 | 72 |
61 _lock: function() | 73 _lock: function() |
62 { | 74 { |
63 console.assert(!this._parsingStyleSheet, "Received request to parse styl esheet before previous was completed."); | 75 console.assert(!this._parsingStyleSheet, "Received request to parse styl esheet before previous was completed."); |
64 this._parsingStyleSheet = true; | 76 this._parsingStyleSheet = true; |
65 }, | 77 }, |
66 | 78 |
67 _unlock: function() | 79 _unlock: function() |
68 { | 80 { |
69 delete this._parsingStyleSheet; | 81 delete this._parsingStyleSheet; |
70 }, | 82 }, |
71 | 83 |
72 /** | 84 /** |
73 * @param {?string} text | 85 * @param {?string} text |
74 */ | 86 */ |
75 _innerParse: function(text) | 87 _innerParse: function(text) |
76 { | 88 { |
89 this._text = text; | |
90 if (this._worker) | |
91 this._parseStoredText(); | |
92 }, | |
93 | |
94 _parseStoredText: function() | |
95 { | |
96 if (this._text === undefined) | |
97 return; | |
77 this._rules = []; | 98 this._rules = []; |
78 this._worker.postMessage({ method: "parseCSS", params: { content: text } }); | 99 this._worker.postMessage({ method: "parseCSS", params: { content: this._ text } }); |
100 delete this._text; | |
79 }, | 101 }, |
80 | 102 |
81 /** | 103 /** |
82 * @param {!MessageEvent} event | 104 * @param {!MessageEvent} event |
83 */ | 105 */ |
84 _onRuleChunk: function(event) | 106 _onRuleChunk: function(event) |
85 { | 107 { |
86 var data = /** @type {!WebInspector.CSSParser.DataChunk} */ (event.data) ; | 108 var data = /** @type {!WebInspector.CSSParser.DataChunk} */ (event.data) ; |
87 var chunk = data.chunk; | 109 var chunk = data.chunk; |
88 for (var i = 0; i < chunk.length; ++i) | 110 for (var i = 0; i < chunk.length; ++i) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
120 | 142 |
121 /** | 143 /** |
122 * @typedef {(WebInspector.CSSParser.StyleRule|WebInspector.CSSParser.AtRule)} | 144 * @typedef {(WebInspector.CSSParser.StyleRule|WebInspector.CSSParser.AtRule)} |
123 */ | 145 */ |
124 WebInspector.CSSParser.Rule; | 146 WebInspector.CSSParser.Rule; |
125 | 147 |
126 /** | 148 /** |
127 * @typedef {{name: string, value: string}} | 149 * @typedef {{name: string, value: string}} |
128 */ | 150 */ |
129 WebInspector.CSSParser.Property; | 151 WebInspector.CSSParser.Property; |
OLD | NEW |