style-sheets.js (3303B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { Actor } = require("resource://devtools/shared/protocol.js"); 8 const { 9 styleSheetsSpec, 10 } = require("resource://devtools/shared/specs/style-sheets.js"); 11 12 const { 13 LongStringActor, 14 } = require("resource://devtools/server/actors/string.js"); 15 16 loader.lazyRequireGetter( 17 this, 18 "UPDATE_GENERAL", 19 "resource://devtools/server/actors/utils/stylesheets-manager.js", 20 true 21 ); 22 loader.lazyRequireGetter( 23 this, 24 "getIndentationFromString", 25 "resource://devtools/shared/indentation.js", 26 true 27 ); 28 29 /** 30 * Creates a StyleSheetsActor. StyleSheetsActor provides remote access to the 31 * stylesheets of a document. 32 */ 33 class StyleSheetsActor extends Actor { 34 constructor(conn, targetActor) { 35 super(conn, styleSheetsSpec); 36 37 this.targetActor = targetActor; 38 } 39 40 /** 41 * The window we work with, taken from the parent actor. 42 */ 43 get window() { 44 return this.targetActor.window; 45 } 46 47 /** 48 * The current content document of the window we work with. 49 */ 50 get document() { 51 return this.window.document; 52 } 53 54 getTraits() { 55 return { 56 traits: {}, 57 }; 58 } 59 60 destroy() { 61 for (const win of this.targetActor.windows) { 62 // This flag only exists for devtools, so we are free to clear 63 // it when we're done. 64 win.document.styleSheetChangeEventsEnabled = false; 65 } 66 67 super.destroy(); 68 } 69 70 /** 71 * Create a new style sheet in the document with the given text. 72 * Return an actor for it. 73 * 74 * @param {object} request 75 * Debugging protocol request object, with 'text property' 76 * @param {string} fileName 77 * If the stylesheet adding is from file, `fileName` indicates the path. 78 * @return {object} 79 * Object with 'styelSheet' property for form on new actor. 80 */ 81 async addStyleSheet(text, fileName = null) { 82 const styleSheetsManager = this._getStyleSheetsManager(); 83 await styleSheetsManager.addStyleSheet( 84 this.document, 85 this.document.documentElement, 86 text, 87 fileName 88 ); 89 } 90 91 _getStyleSheetsManager() { 92 return this.targetActor.getStyleSheetsManager(); 93 } 94 95 toggleDisabled(resourceId) { 96 const styleSheetsManager = this._getStyleSheetsManager(); 97 return styleSheetsManager.toggleDisabled(resourceId); 98 } 99 100 async getText(resourceId) { 101 const styleSheetsManager = this._getStyleSheetsManager(); 102 const text = await styleSheetsManager.getText(resourceId); 103 return new LongStringActor(this.conn, text || ""); 104 } 105 106 async getStyleSheetIndentation(resourceId) { 107 const styleSheetsManager = this._getStyleSheetsManager(); 108 const text = await styleSheetsManager.getText(resourceId); 109 110 const { indentUnit, indentWithTabs } = getIndentationFromString(text); 111 return indentWithTabs ? "\t" : " ".repeat(indentUnit); 112 } 113 114 update(resourceId, text, transition, cause = "") { 115 const styleSheetsManager = this._getStyleSheetsManager(); 116 return styleSheetsManager.setStyleSheetText(resourceId, text, { 117 transition, 118 kind: UPDATE_GENERAL, 119 cause, 120 }); 121 } 122 } 123 124 exports.StyleSheetsActor = StyleSheetsActor;