From 2aa4a82499d4becd2284cdb482213d541b8804dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 16:29:10 +0200 Subject: Adding upstream version 86.0.1. Signed-off-by: Daniel Baumann --- devtools/server/actors/media-rule.js | 82 ++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 devtools/server/actors/media-rule.js (limited to 'devtools/server/actors/media-rule.js') diff --git a/devtools/server/actors/media-rule.js b/devtools/server/actors/media-rule.js new file mode 100644 index 0000000000..3c9d662ef3 --- /dev/null +++ b/devtools/server/actors/media-rule.js @@ -0,0 +1,82 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +const { Cu } = require("chrome"); +const protocol = require("devtools/shared/protocol"); +const { mediaRuleSpec } = require("devtools/shared/specs/media-rule"); +const InspectorUtils = require("InspectorUtils"); + +/** + * A MediaRuleActor lives on the server and provides access to properties + * of a DOM @media rule and emits events when it changes. + */ +var MediaRuleActor = protocol.ActorClassWithSpec(mediaRuleSpec, { + get window() { + return this.parentActor.window; + }, + + get document() { + return this.window.document; + }, + + get matches() { + return this.mql ? this.mql.matches : null; + }, + + initialize: function(mediaRule, parentActor) { + protocol.Actor.prototype.initialize.call(this, parentActor.conn); + + this.rawRule = mediaRule; + this.parentActor = parentActor; + this.conn = this.parentActor.conn; + + this._matchesChange = this._matchesChange.bind(this); + + this.line = InspectorUtils.getRuleLine(mediaRule); + this.column = InspectorUtils.getRuleColumn(mediaRule); + + try { + this.mql = this.window.matchMedia(mediaRule.media.mediaText); + } catch (e) { + // Ignored + } + + if (this.mql) { + this.mql.addListener(this._matchesChange); + } + }, + + destroy: function() { + if (this.mql) { + // The content page may already be destroyed and mql be the dead wrapper. + if (!Cu.isDeadWrapper(this.mql)) { + this.mql.removeListener(this._matchesChange); + } + this.mql = null; + } + + protocol.Actor.prototype.destroy.call(this); + }, + + form: function() { + const form = { + actor: this.actorID, // actorID is set when this is added to a pool + mediaText: this.rawRule.media.mediaText, + conditionText: this.rawRule.conditionText, + matches: this.matches, + line: this.line, + column: this.column, + parentStyleSheet: this.parentActor.actorID, + }; + + return form; + }, + + _matchesChange: function() { + this.emit("matches-change", this.matches); + }, +}); +exports.MediaRuleActor = MediaRuleActor; -- cgit v1.2.3