diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /devtools/server/actors/media-rule.js | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/server/actors/media-rule.js')
-rw-r--r-- | devtools/server/actors/media-rule.js | 82 |
1 files changed, 82 insertions, 0 deletions
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; |