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