original-source.js (2948B)
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 /** 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. 11 */ 12 class OriginalSource { 13 /** 14 * @param {string} url 15 * The URL of the original source. 16 * @param {string} sourceID 17 * The source ID of the original source, as used by the source 18 * map service. 19 * @param {SourceMapLoader} sourceMapLoader 20 * The source map loader; @see Toolbox.sourceMapLoader 21 */ 22 constructor(url, sourceId, sourceMapLoader) { 23 this.isOriginalSource = true; 24 25 this._url = url; 26 this._sourceId = sourceId; 27 this._sourceMapLoader = sourceMapLoader; 28 } 29 get sourceId() { 30 return this._sourceId; 31 } 32 33 /** Get the original source's URL. */ 34 get url() { 35 return this._url; 36 } 37 38 /** Get the original source's URL. */ 39 get href() { 40 return this._url; 41 } 42 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. 49 */ 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, 58 }; 59 }); 60 } 61 return this._sourcePromise; 62 } 63 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. 68 * 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. 79 */ 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, 89 }) 90 .then(location => { 91 // Add some properties for the style editor. 92 location.source = location.sourceUrl; 93 location.styleSheet = relatedSheet; 94 return location; 95 }); 96 } 97 98 // Dummy implementations, as we never emit an event. 99 on() {} 100 off() {} 101 } 102 103 exports.OriginalSource = OriginalSource;