Bug 1946184 - Fix computing the CSD margin right after calling HideWindowChrome(...
[gecko.git] / devtools / client / styleeditor / original-source.js
blob1c01ae0355fbdb72afa758ecbd5b12a605ea91b0
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/. */
5 "use strict";
7 /**
8 * An object of this type represents an original source for the style
9 * editor. An "original" source is one that is mentioned in a source
10 * map.
12 * @param {String} url
13 * The URL of the original source.
14 * @param {String} sourceID
15 * The source ID of the original source, as used by the source
16 * map service.
17 * @param {SourceMapLoader} sourceMapLoader
18 * The source map loader; @see Toolbox.sourceMapLoader
20 function OriginalSource(url, sourceId, sourceMapLoader) {
21 this.isOriginalSource = true;
23 this._url = url;
24 this._sourceId = sourceId;
25 this._sourceMapLoader = sourceMapLoader;
28 OriginalSource.prototype = {
29 get sourceId() {
30 return this._sourceId;
33 /** Get the original source's URL. */
34 get url() {
35 return this._url;
38 /** Get the original source's URL. */
39 get href() {
40 return this._url;
43 /**
44 * Return a promise that will resolve to the original source's full
45 * text. The return result is actually an object with a single
46 * `string` method; this method will return the source text as a
47 * string. This is done because the style editor elsewhere expects
48 * a long string actor.
50 getText() {
51 if (!this._sourcePromise) {
52 this._sourcePromise = this._sourceMapLoader
53 .getOriginalSourceText(this._sourceId)
54 .then(contents => {
55 // Make it look like a long string actor.
56 return {
57 string: () => contents.text,
59 });
61 return this._sourcePromise;
64 /**
65 * Given a source-mapped, generated style sheet, a line, and a
66 * column, return the corresponding original location in this style
67 * sheet.
69 * @param {StyleSheetResource} relatedSheet
70 * The generated style sheet's resource
71 * @param {Number} line
72 * Line number.
73 * @param {Number} column
74 * Column number.
75 * @return {Location}
76 * The original location, an object with at least
77 * `sourceUrl`, `source`, `styleSheet`, `line`, and `column`
78 * properties.
80 getOriginalLocation(relatedSheet, line, column) {
81 const { href, nodeHref, resourceId: sourceId } = relatedSheet;
82 const sourceUrl = href || nodeHref;
83 return this._sourceMapLoader
84 .getOriginalLocation({
85 sourceId,
86 line,
87 column,
88 sourceUrl,
90 .then(location => {
91 // Add some properties for the style editor.
92 location.source = location.sourceUrl;
93 location.styleSheet = relatedSheet;
94 return location;
95 });
98 // Dummy implementations, as we never emit an event.
99 on() {},
100 off() {},
103 exports.OriginalSource = OriginalSource;