diff --git a/devtools/server/actors/styleeditor.js b/devtools/server/actors/styleeditor.js index c1367fdc1f32..e78b9f1e71f7 100644 --- a/devtools/server/actors/styleeditor.js +++ b/devtools/server/actors/styleeditor.js @@ -315,239 +315,6 @@ var OldStyleSheetActor = protocol.ActorClassWithSpec(oldStyleSheetSpec, { } }); -/** - * Creates a StyleEditorActor. StyleEditorActor provides remote access to the - * stylesheets of a document. - */ -var StyleEditorActor = exports.StyleEditorActor = protocol.ActorClass({ - typeName: "styleeditor", - - /** - * The window we work with, taken from the parent actor. - */ - get window() { - return this.parentActor.window; - }, - - /** - * The current content document of the window we work with. - */ - get document() { - return this.window.document; - }, - - events: { - "document-load" : { - type: "documentLoad", - styleSheets: Arg(0, "array:old-stylesheet") - } - }, - - form: function () - { - return { actor: this.actorID }; - }, - - initialize: function (conn, tabActor) { - protocol.Actor.prototype.initialize.call(this, null); - - this.parentActor = tabActor; - - // keep a map of sheets-to-actors so we don't create two actors for one sheet - this._sheets = new Map(); - }, - - /** - * Destroy the current StyleEditorActor instance. - */ - destroy: function () - { - this._sheets.clear(); - }, - - /** - * Called by client when target navigates to a new document. - * Adds load listeners to document. - */ - newDocument: method(function () { - // delete previous document's actors - this._clearStyleSheetActors(); - - // Note: listening for load won't be necessary once - // https://bugzilla.mozilla.org/show_bug.cgi?id=839103 is fixed - if (this.document.readyState == "complete") { - this._onDocumentLoaded(); - } - else { - this.window.addEventListener("load", this._onDocumentLoaded, false); - } - return {}; - }), - - /** - * Event handler for document loaded event. Add actor for each stylesheet - * and send an event notifying of the load - */ - _onDocumentLoaded: function (event) { - if (event) { - this.window.removeEventListener("load", this._onDocumentLoaded, false); - } - - let documents = [this.document]; - var forms = []; - for (let doc of documents) { - let sheetForms = this._addStyleSheets(doc.styleSheets); - forms = forms.concat(sheetForms); - // Recursively handle style sheets of the documents in iframes. - for (let iframe of doc.getElementsByTagName("iframe")) { - documents.push(iframe.contentDocument); - } - } - - events.emit(this, "document-load", forms); - }, - - /** - * Add all the stylesheets to the map and create an actor for each one - * if not already created. Send event that there are new stylesheets. - * - * @param {[DOMStyleSheet]} styleSheets - * Stylesheets to add - * @return {[object]} - * Array of actors for each StyleSheetActor created - */ - _addStyleSheets: function (styleSheets) - { - let sheets = []; - for (let i = 0; i < styleSheets.length; i++) { - let styleSheet = styleSheets[i]; - sheets.push(styleSheet); - - // Get all sheets, including imported ones - let imports = this._getImported(styleSheet); - sheets = sheets.concat(imports); - } - let actors = sheets.map(this._createStyleSheetActor.bind(this)); - - return actors; - }, - - /** - * Create a new actor for a style sheet, if it hasn't already been created. - * - * @param {DOMStyleSheet} styleSheet - * The style sheet to create an actor for. - * @return {StyleSheetActor} - * The actor for this style sheet - */ - _createStyleSheetActor: function (styleSheet) - { - if (this._sheets.has(styleSheet)) { - return this._sheets.get(styleSheet); - } - let actor = new OldStyleSheetActor(styleSheet, this); - - this.manage(actor); - this._sheets.set(styleSheet, actor); - - return actor; - }, - - /** - * Get all the stylesheets @imported from a stylesheet. - * - * @param {DOMStyleSheet} styleSheet - * Style sheet to search - * @return {array} - * All the imported stylesheets - */ - _getImported: function (styleSheet) { - let imported = []; - - for (let i = 0; i < styleSheet.cssRules.length; i++) { - let rule = styleSheet.cssRules[i]; - if (rule.type == Ci.nsIDOMCSSRule.IMPORT_RULE) { - // Associated styleSheet may be null if it has already been seen due to - // duplicate @imports for the same URL. - if (!rule.styleSheet) { - continue; - } - imported.push(rule.styleSheet); - - // recurse imports in this stylesheet as well - imported = imported.concat(this._getImported(rule.styleSheet)); - } - else if (rule.type != Ci.nsIDOMCSSRule.CHARSET_RULE) { - // @import rules must precede all others except @charset - break; - } - } - return imported; - }, - - /** - * Clear all the current stylesheet actors in map. - */ - _clearStyleSheetActors: function () { - for (let actor in this._sheets) { - this.unmanage(this._sheets[actor]); - } - this._sheets.clear(); - }, - - /** - * Create a new style sheet in the document with the given text. - * Return an actor for it. - * - * @param {object} request - * Debugging protocol request object, with 'text property' - * @return {object} - * Object with 'styelSheet' property for form on new actor. - */ - newStyleSheet: method(function (text) { - let parent = this.document.documentElement; - let style = this.document.createElementNS("http://www.w3.org/1999/xhtml", "style"); - style.setAttribute("type", "text/css"); - - if (text) { - style.appendChild(this.document.createTextNode(text)); - } - parent.appendChild(style); - - let actor = this._createStyleSheetActor(style.sheet); - return actor; - }, { - request: { text: Arg(0, "string") }, - response: { styleSheet: RetVal("old-stylesheet") } - }) -}); - -/** - * The corresponding Front object for the StyleEditorActor. - */ -var StyleEditorFront = protocol.FrontClass(StyleEditorActor, { - initialize: function (client, tabForm) { - protocol.Front.prototype.initialize.call(this, client); - this.actorID = tabForm.styleEditorActor; - this.manage(this); - }, - - getStyleSheets: function () { - let deferred = promise.defer(); - - events.once(this, "document-load", (styleSheets) => { - deferred.resolve(styleSheets); - }); - this.newDocument(); - - return deferred.promise; - }, - - addStyleSheet: function (text) { - return this.newStyleSheet(text); - } -}); - exports.OldStyleSheetActor = OldStyleSheetActor; /**