michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = ["StyleSheetEditor"]; michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: michael@0: const require = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}).devtools.require; michael@0: const Editor = require("devtools/sourceeditor/editor"); michael@0: const {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm", {}); michael@0: const {CssLogic} = require("devtools/styleinspector/css-logic"); michael@0: const AutoCompleter = require("devtools/sourceeditor/autocomplete"); michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/FileUtils.jsm"); michael@0: Cu.import("resource://gre/modules/NetUtil.jsm"); michael@0: Cu.import("resource://gre/modules/osfile.jsm"); michael@0: Cu.import("resource://gre/modules/devtools/event-emitter.js"); michael@0: Cu.import("resource:///modules/devtools/StyleEditorUtil.jsm"); michael@0: michael@0: const LOAD_ERROR = "error-load"; michael@0: const SAVE_ERROR = "error-save"; michael@0: michael@0: // max update frequency in ms (avoid potential typing lag and/or flicker) michael@0: // @see StyleEditor.updateStylesheet michael@0: const UPDATE_STYLESHEET_THROTTLE_DELAY = 500; michael@0: michael@0: // Pref which decides if CSS autocompletion is enabled in Style Editor or not. michael@0: const AUTOCOMPLETION_PREF = "devtools.styleeditor.autocompletion-enabled"; michael@0: michael@0: // How long to wait to update linked CSS file after original source was saved michael@0: // to disk. Time in ms. michael@0: const CHECK_LINKED_SHEET_DELAY=500; michael@0: michael@0: // How many times to check for linked file changes michael@0: const MAX_CHECK_COUNT=10; michael@0: michael@0: /** michael@0: * StyleSheetEditor controls the editor linked to a particular StyleSheet michael@0: * object. michael@0: * michael@0: * Emits events: michael@0: * 'property-change': A property on the underlying stylesheet has changed michael@0: * 'source-editor-load': The source editor for this editor has been loaded michael@0: * 'error': An error has occured michael@0: * michael@0: * @param {StyleSheet|OriginalSource} styleSheet michael@0: * Stylesheet or original source to show michael@0: * @param {DOMWindow} win michael@0: * panel window for style editor michael@0: * @param {nsIFile} file michael@0: * Optional file that the sheet was imported from michael@0: * @param {boolean} isNew michael@0: * Optional whether the sheet was created by the user michael@0: * @param {Walker} walker michael@0: * Optional walker used for selectors autocompletion michael@0: */ michael@0: function StyleSheetEditor(styleSheet, win, file, isNew, walker) { michael@0: EventEmitter.decorate(this); michael@0: michael@0: this.styleSheet = styleSheet; michael@0: this._inputElement = null; michael@0: this.sourceEditor = null; michael@0: this._window = win; michael@0: this._isNew = isNew; michael@0: this.walker = walker; michael@0: michael@0: this._state = { // state to use when inputElement attaches michael@0: text: "", michael@0: selection: { michael@0: start: {line: 0, ch: 0}, michael@0: end: {line: 0, ch: 0} michael@0: }, michael@0: topIndex: 0 // the first visible line michael@0: }; michael@0: michael@0: this._styleSheetFilePath = null; michael@0: if (styleSheet.href && michael@0: Services.io.extractScheme(this.styleSheet.href) == "file") { michael@0: this._styleSheetFilePath = this.styleSheet.href; michael@0: } michael@0: michael@0: this._onPropertyChange = this._onPropertyChange.bind(this); michael@0: this._onError = this._onError.bind(this); michael@0: this.checkLinkedFileForChanges = this.checkLinkedFileForChanges.bind(this); michael@0: this.markLinkedFileBroken = this.markLinkedFileBroken.bind(this); michael@0: michael@0: this._focusOnSourceEditorReady = false; michael@0: michael@0: let relatedSheet = this.styleSheet.relatedStyleSheet; michael@0: if (relatedSheet) { michael@0: relatedSheet.on("property-change", this._onPropertyChange); michael@0: } michael@0: this.styleSheet.on("property-change", this._onPropertyChange); michael@0: this.styleSheet.on("error", this._onError); michael@0: michael@0: this.savedFile = file; michael@0: this.linkCSSFile(); michael@0: } michael@0: michael@0: StyleSheetEditor.prototype = { michael@0: /** michael@0: * Whether there are unsaved changes in the editor michael@0: */ michael@0: get unsaved() { michael@0: return this.sourceEditor && !this.sourceEditor.isClean(); michael@0: }, michael@0: michael@0: /** michael@0: * Whether the editor is for a stylesheet created by the user michael@0: * through the style editor UI. michael@0: */ michael@0: get isNew() { michael@0: return this._isNew; michael@0: }, michael@0: michael@0: get savedFile() { michael@0: return this._savedFile; michael@0: }, michael@0: michael@0: set savedFile(name) { michael@0: this._savedFile = name; michael@0: michael@0: this.linkCSSFile(); michael@0: }, michael@0: michael@0: /** michael@0: * Get a user-friendly name for the style sheet. michael@0: * michael@0: * @return string michael@0: */ michael@0: get friendlyName() { michael@0: if (this.savedFile) { michael@0: return this.savedFile.leafName; michael@0: } michael@0: michael@0: if (this._isNew) { michael@0: let index = this.styleSheet.styleSheetIndex + 1; michael@0: return _("newStyleSheet", index); michael@0: } michael@0: michael@0: if (!this.styleSheet.href) { michael@0: let index = this.styleSheet.styleSheetIndex + 1; michael@0: return _("inlineStyleSheet", index); michael@0: } michael@0: michael@0: if (!this._friendlyName) { michael@0: let sheetURI = this.styleSheet.href; michael@0: this._friendlyName = CssLogic.shortSource({ href: sheetURI }); michael@0: try { michael@0: this._friendlyName = decodeURI(this._friendlyName); michael@0: } catch (ex) { michael@0: } michael@0: } michael@0: return this._friendlyName; michael@0: }, michael@0: michael@0: /** michael@0: * If this is an original source, get the path of the CSS file it generated. michael@0: */ michael@0: linkCSSFile: function() { michael@0: if (!this.styleSheet.isOriginalSource) { michael@0: return; michael@0: } michael@0: michael@0: let relatedSheet = this.styleSheet.relatedStyleSheet; michael@0: michael@0: let path; michael@0: let href = removeQuery(relatedSheet.href); michael@0: let uri = NetUtil.newURI(href); michael@0: michael@0: if (uri.scheme == "file") { michael@0: let file = uri.QueryInterface(Ci.nsIFileURL).file; michael@0: path = file.path; michael@0: } michael@0: else if (this.savedFile) { michael@0: let origHref = removeQuery(this.styleSheet.href); michael@0: let origUri = NetUtil.newURI(origHref); michael@0: path = findLinkedFilePath(uri, origUri, this.savedFile); michael@0: } michael@0: else { michael@0: // we can't determine path to generated file on disk michael@0: return; michael@0: } michael@0: michael@0: if (this.linkedCSSFile == path) { michael@0: return; michael@0: } michael@0: michael@0: this.linkedCSSFile = path; michael@0: michael@0: this.linkedCSSFileError = null; michael@0: michael@0: // save last file change time so we can compare when we check for changes. michael@0: OS.File.stat(path).then((info) => { michael@0: this._fileModDate = info.lastModificationDate.getTime(); michael@0: }, this.markLinkedFileBroken); michael@0: michael@0: this.emit("linked-css-file"); michael@0: }, michael@0: michael@0: /** michael@0: * Start fetching the full text source for this editor's sheet. michael@0: */ michael@0: fetchSource: function(callback) { michael@0: this.styleSheet.getText().then((longStr) => { michael@0: longStr.string().then((source) => { michael@0: let ruleCount = this.styleSheet.ruleCount; michael@0: this._state.text = prettifyCSS(source, ruleCount); michael@0: this.sourceLoaded = true; michael@0: michael@0: callback(source); michael@0: }); michael@0: }, e => { michael@0: this.emit("error", LOAD_ERROR, this.styleSheet.href); michael@0: }) michael@0: }, michael@0: michael@0: /** michael@0: * Forward property-change event from stylesheet. michael@0: * michael@0: * @param {string} event michael@0: * Event type michael@0: * @param {string} property michael@0: * Property that has changed on sheet michael@0: */ michael@0: _onPropertyChange: function(property, value) { michael@0: this.emit("property-change", property, value); michael@0: }, michael@0: michael@0: /** michael@0: * Forward error event from stylesheet. michael@0: * michael@0: * @param {string} event michael@0: * Event type michael@0: * @param {string} errorCode michael@0: */ michael@0: _onError: function(event, errorCode) { michael@0: this.emit("error", errorCode); michael@0: }, michael@0: michael@0: /** michael@0: * Create source editor and load state into it. michael@0: * @param {DOMElement} inputElement michael@0: * Element to load source editor in michael@0: * michael@0: * @return {Promise} michael@0: * Promise that will resolve when the style editor is loaded. michael@0: */ michael@0: load: function(inputElement) { michael@0: this._inputElement = inputElement; michael@0: michael@0: let config = { michael@0: value: this._state.text, michael@0: lineNumbers: true, michael@0: mode: Editor.modes.css, michael@0: readOnly: false, michael@0: autoCloseBrackets: "{}()[]", michael@0: extraKeys: this._getKeyBindings(), michael@0: contextMenu: "sourceEditorContextMenu" michael@0: }; michael@0: let sourceEditor = new Editor(config); michael@0: michael@0: sourceEditor.on("dirty-change", this._onPropertyChange); michael@0: michael@0: return sourceEditor.appendTo(inputElement).then(() => { michael@0: if (Services.prefs.getBoolPref(AUTOCOMPLETION_PREF)) { michael@0: sourceEditor.extend(AutoCompleter); michael@0: sourceEditor.setupAutoCompletion(this.walker); michael@0: } michael@0: sourceEditor.on("save", () => { michael@0: this.saveToFile(); michael@0: }); michael@0: michael@0: if (this.styleSheet.update) { michael@0: sourceEditor.on("change", () => { michael@0: this.updateStyleSheet(); michael@0: }); michael@0: } michael@0: michael@0: this.sourceEditor = sourceEditor; michael@0: michael@0: if (this._focusOnSourceEditorReady) { michael@0: this._focusOnSourceEditorReady = false; michael@0: sourceEditor.focus(); michael@0: } michael@0: michael@0: sourceEditor.setFirstVisibleLine(this._state.topIndex); michael@0: sourceEditor.setSelection(this._state.selection.start, michael@0: this._state.selection.end); michael@0: michael@0: this.emit("source-editor-load"); michael@0: }); michael@0: }, michael@0: michael@0: /** michael@0: * Get the source editor for this editor. michael@0: * michael@0: * @return {Promise} michael@0: * Promise that will resolve with the editor. michael@0: */ michael@0: getSourceEditor: function() { michael@0: let deferred = promise.defer(); michael@0: michael@0: if (this.sourceEditor) { michael@0: return promise.resolve(this); michael@0: } michael@0: this.on("source-editor-load", () => { michael@0: deferred.resolve(this); michael@0: }); michael@0: return deferred.promise; michael@0: }, michael@0: michael@0: /** michael@0: * Focus the Style Editor input. michael@0: */ michael@0: focus: function() { michael@0: if (this.sourceEditor) { michael@0: this.sourceEditor.focus(); michael@0: } else { michael@0: this._focusOnSourceEditorReady = true; michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Event handler for when the editor is shown. michael@0: */ michael@0: onShow: function() { michael@0: if (this.sourceEditor) { michael@0: this.sourceEditor.setFirstVisibleLine(this._state.topIndex); michael@0: } michael@0: this.focus(); michael@0: }, michael@0: michael@0: /** michael@0: * Toggled the disabled state of the underlying stylesheet. michael@0: */ michael@0: toggleDisabled: function() { michael@0: this.styleSheet.toggleDisabled(); michael@0: }, michael@0: michael@0: /** michael@0: * Queue a throttled task to update the live style sheet. michael@0: * michael@0: * @param boolean immediate michael@0: * Optional. If true the update is performed immediately. michael@0: */ michael@0: updateStyleSheet: function(immediate) { michael@0: if (this._updateTask) { michael@0: // cancel previous queued task not executed within throttle delay michael@0: this._window.clearTimeout(this._updateTask); michael@0: } michael@0: michael@0: if (immediate) { michael@0: this._updateStyleSheet(); michael@0: } else { michael@0: this._updateTask = this._window.setTimeout(this._updateStyleSheet.bind(this), michael@0: UPDATE_STYLESHEET_THROTTLE_DELAY); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Update live style sheet according to modifications. michael@0: */ michael@0: _updateStyleSheet: function() { michael@0: if (this.styleSheet.disabled) { michael@0: return; // TODO: do we want to do this? michael@0: } michael@0: michael@0: this._updateTask = null; // reset only if we actually perform an update michael@0: // (stylesheet is enabled) so that 'missed' updates michael@0: // while the stylesheet is disabled can be performed michael@0: // when it is enabled back. @see enableStylesheet michael@0: michael@0: if (this.sourceEditor) { michael@0: this._state.text = this.sourceEditor.getText(); michael@0: } michael@0: michael@0: this.styleSheet.update(this._state.text, true); michael@0: }, michael@0: michael@0: /** michael@0: * Save the editor contents into a file and set savedFile property. michael@0: * A file picker UI will open if file is not set and editor is not headless. michael@0: * michael@0: * @param mixed file michael@0: * Optional nsIFile or string representing the filename to save in the michael@0: * background, no UI will be displayed. michael@0: * If not specified, the original style sheet URI is used. michael@0: * To implement 'Save' instead of 'Save as', you can pass savedFile here. michael@0: * @param function(nsIFile aFile) callback michael@0: * Optional callback called when the operation has finished. michael@0: * aFile has the nsIFile object for saved file or null if the operation michael@0: * has failed or has been canceled by the user. michael@0: * @see savedFile michael@0: */ michael@0: saveToFile: function(file, callback) { michael@0: let onFile = (returnFile) => { michael@0: if (!returnFile) { michael@0: if (callback) { michael@0: callback(null); michael@0: } michael@0: return; michael@0: } michael@0: michael@0: if (this.sourceEditor) { michael@0: this._state.text = this.sourceEditor.getText(); michael@0: } michael@0: michael@0: let ostream = FileUtils.openSafeFileOutputStream(returnFile); michael@0: let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"] michael@0: .createInstance(Ci.nsIScriptableUnicodeConverter); michael@0: converter.charset = "UTF-8"; michael@0: let istream = converter.convertToInputStream(this._state.text); michael@0: michael@0: NetUtil.asyncCopy(istream, ostream, function onStreamCopied(status) { michael@0: if (!Components.isSuccessCode(status)) { michael@0: if (callback) { michael@0: callback(null); michael@0: } michael@0: this.emit("error", SAVE_ERROR); michael@0: return; michael@0: } michael@0: FileUtils.closeSafeFileOutputStream(ostream); michael@0: michael@0: this.onFileSaved(returnFile); michael@0: michael@0: if (callback) { michael@0: callback(returnFile); michael@0: } michael@0: }.bind(this)); michael@0: }; michael@0: michael@0: let defaultName; michael@0: if (this._friendlyName) { michael@0: defaultName = OS.Path.basename(this._friendlyName); michael@0: } michael@0: showFilePicker(file || this._styleSheetFilePath, true, this._window, michael@0: onFile, defaultName); michael@0: }, michael@0: michael@0: /** michael@0: * Called when this source has been successfully saved to disk. michael@0: */ michael@0: onFileSaved: function(returnFile) { michael@0: this._friendlyName = null; michael@0: this.savedFile = returnFile; michael@0: michael@0: this.sourceEditor.setClean(); michael@0: michael@0: this.emit("property-change"); michael@0: michael@0: // TODO: replace with file watching michael@0: this._modCheckCount = 0; michael@0: this._window.clearTimeout(this._timeout); michael@0: michael@0: if (this.linkedCSSFile && !this.linkedCSSFileError) { michael@0: this._timeout = this._window.setTimeout(this.checkLinkedFileForChanges, michael@0: CHECK_LINKED_SHEET_DELAY); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Check to see if our linked CSS file has changed on disk, and michael@0: * if so, update the live style sheet. michael@0: */ michael@0: checkLinkedFileForChanges: function() { michael@0: OS.File.stat(this.linkedCSSFile).then((info) => { michael@0: let lastChange = info.lastModificationDate.getTime(); michael@0: michael@0: if (this._fileModDate && lastChange != this._fileModDate) { michael@0: this._fileModDate = lastChange; michael@0: this._modCheckCount = 0; michael@0: michael@0: this.updateLinkedStyleSheet(); michael@0: return; michael@0: } michael@0: michael@0: if (++this._modCheckCount > MAX_CHECK_COUNT) { michael@0: this.updateLinkedStyleSheet(); michael@0: return; michael@0: } michael@0: michael@0: // try again in a bit michael@0: this._timeout = this._window.setTimeout(this.checkLinkedFileForChanges, michael@0: CHECK_LINKED_SHEET_DELAY); michael@0: }, this.markLinkedFileBroken); michael@0: }, michael@0: michael@0: /** michael@0: * Notify that the linked CSS file (if this is an original source) michael@0: * doesn't exist on disk in the place we think it does. michael@0: * michael@0: * @param string error michael@0: * The error we got when trying to access the file. michael@0: */ michael@0: markLinkedFileBroken: function(error) { michael@0: this.linkedCSSFileError = error || true; michael@0: this.emit("linked-css-file-error"); michael@0: michael@0: error += " querying " + this.linkedCSSFile + michael@0: " original source location: " + this.savedFile.path michael@0: Cu.reportError(error); michael@0: }, michael@0: michael@0: /** michael@0: * For original sources (e.g. Sass files). Fetch contents of linked CSS michael@0: * file from disk and live update the stylesheet object with the contents. michael@0: */ michael@0: updateLinkedStyleSheet: function() { michael@0: OS.File.read(this.linkedCSSFile).then((array) => { michael@0: let decoder = new TextDecoder(); michael@0: let text = decoder.decode(array); michael@0: michael@0: let relatedSheet = this.styleSheet.relatedStyleSheet; michael@0: relatedSheet.update(text, true); michael@0: }, this.markLinkedFileBroken); michael@0: }, michael@0: michael@0: /** michael@0: * Retrieve custom key bindings objects as expected by Editor. michael@0: * Editor action names are not displayed to the user. michael@0: * michael@0: * @return {array} key binding objects for the source editor michael@0: */ michael@0: _getKeyBindings: function() { michael@0: let bindings = {}; michael@0: michael@0: bindings[Editor.accel(_("saveStyleSheet.commandkey"))] = () => { michael@0: this.saveToFile(this.savedFile); michael@0: }; michael@0: michael@0: bindings["Shift-" + Editor.accel(_("saveStyleSheet.commandkey"))] = () => { michael@0: this.saveToFile(); michael@0: }; michael@0: michael@0: return bindings; michael@0: }, michael@0: michael@0: /** michael@0: * Clean up for this editor. michael@0: */ michael@0: destroy: function() { michael@0: if (this.sourceEditor) { michael@0: this.sourceEditor.destroy(); michael@0: } michael@0: this.styleSheet.off("property-change", this._onPropertyChange); michael@0: this.styleSheet.off("error", this._onError); michael@0: } michael@0: } michael@0: michael@0: michael@0: const TAB_CHARS = "\t"; michael@0: michael@0: const CURRENT_OS = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime).OS; michael@0: const LINE_SEPARATOR = CURRENT_OS === "WINNT" ? "\r\n" : "\n"; michael@0: michael@0: /** michael@0: * Prettify minified CSS text. michael@0: * This prettifies CSS code where there is no indentation in usual places while michael@0: * keeping original indentation as-is elsewhere. michael@0: * michael@0: * @param string text michael@0: * The CSS source to prettify. michael@0: * @return string michael@0: * Prettified CSS source michael@0: */ michael@0: function prettifyCSS(text, ruleCount) michael@0: { michael@0: // remove initial and terminating HTML comments and surrounding whitespace michael@0: text = text.replace(/(?:^\s*\s*$)/g, ""); michael@0: michael@0: // don't attempt to prettify if there's more than one line per rule. michael@0: let lineCount = text.split("\n").length - 1; michael@0: if (ruleCount !== null && lineCount >= ruleCount) { michael@0: return text; michael@0: } michael@0: michael@0: let parts = []; // indented parts michael@0: let partStart = 0; // start offset of currently parsed part michael@0: let indent = ""; michael@0: let indentLevel = 0; michael@0: michael@0: for (let i = 0; i < text.length; i++) { michael@0: let c = text[i]; michael@0: let shouldIndent = false; michael@0: michael@0: switch (c) { michael@0: case "}": michael@0: if (i - partStart > 1) { michael@0: // there's more than just } on the line, add line michael@0: parts.push(indent + text.substring(partStart, i)); michael@0: partStart = i; michael@0: } michael@0: indent = TAB_CHARS.repeat(--indentLevel); michael@0: /* fallthrough */ michael@0: case ";": michael@0: case "{": michael@0: shouldIndent = true; michael@0: break; michael@0: } michael@0: michael@0: if (shouldIndent) { michael@0: let la = text[i+1]; // one-character lookahead michael@0: if (!/\n/.test(la) || /^\s+$/.test(text.substring(i+1, text.length))) { michael@0: // following character should be a new line, but isn't, michael@0: // or it's whitespace at the end of the file michael@0: parts.push(indent + text.substring(partStart, i + 1)); michael@0: if (c == "}") { michael@0: parts.push(""); // for extra line separator michael@0: } michael@0: partStart = i + 1; michael@0: } else { michael@0: return text; // assume it is not minified, early exit michael@0: } michael@0: } michael@0: michael@0: if (c == "{") { michael@0: indent = TAB_CHARS.repeat(++indentLevel); michael@0: } michael@0: } michael@0: return parts.join(LINE_SEPARATOR); michael@0: } michael@0: michael@0: /** michael@0: * Find a path on disk for a file given it's hosted uri, the uri of the michael@0: * original resource that generated it (e.g. Sass file), and the location of the michael@0: * local file for that source. michael@0: * michael@0: * @param {nsIURI} uri michael@0: * The uri of the resource michael@0: * @param {nsIURI} origUri michael@0: * The uri of the original source for the resource michael@0: * @param {nsIFile} file michael@0: * The local file for the resource on disk michael@0: * michael@0: * @return {string} michael@0: * The path of original file on disk michael@0: */ michael@0: function findLinkedFilePath(uri, origUri, file) { michael@0: let { origBranch, branch } = findUnsharedBranches(origUri, uri); michael@0: let project = findProjectPath(file, origBranch); michael@0: michael@0: let parts = project.concat(branch); michael@0: let path = OS.Path.join.apply(this, parts); michael@0: michael@0: return path; michael@0: } michael@0: michael@0: /** michael@0: * Find the path of a project given a file in the project and its branch michael@0: * off the root. e.g.: michael@0: * /Users/moz/proj/src/a.css" and "src/a.css" michael@0: * would yield ["Users", "moz", "proj"] michael@0: * michael@0: * @param {nsIFile} file michael@0: * file for that resource on disk michael@0: * @param {array} branch michael@0: * path parts for branch to chop off file path. michael@0: * @return {array} michael@0: * array of path parts michael@0: */ michael@0: function findProjectPath(file, branch) { michael@0: let path = OS.Path.split(file.path).components; michael@0: michael@0: for (let i = 2; i <= branch.length; i++) { michael@0: // work backwards until we find a differing directory name michael@0: if (path[path.length - i] != branch[branch.length - i]) { michael@0: return path.slice(0, path.length - i + 1); michael@0: } michael@0: } michael@0: michael@0: // if we don't find a differing directory, just chop off the branch michael@0: return path.slice(0, path.length - branch.length); michael@0: } michael@0: michael@0: /** michael@0: * Find the parts of a uri past the root it shares with another uri. e.g: michael@0: * "http://localhost/built/a.scss" and "http://localhost/src/a.css" michael@0: * would yield ["built", "a.scss"] and ["src", "a.css"] michael@0: * michael@0: * @param {nsIURI} origUri michael@0: * uri to find unshared branch of. Usually is uri for original source. michael@0: * @param {nsIURI} uri michael@0: * uri to compare against to get a shared root michael@0: * @return {object} michael@0: * object with 'branch' and 'origBranch' array of path parts for branch michael@0: */ michael@0: function findUnsharedBranches(origUri, uri) { michael@0: origUri = OS.Path.split(origUri.path).components; michael@0: uri = OS.Path.split(uri.path).components; michael@0: michael@0: for (let i = 0; i < uri.length - 1; i++) { michael@0: if (uri[i] != origUri[i]) { michael@0: return { michael@0: branch: uri.slice(i), michael@0: origBranch: origUri.slice(i) michael@0: }; michael@0: } michael@0: } michael@0: return { michael@0: branch: uri, michael@0: origBranch: origUri michael@0: }; michael@0: } michael@0: michael@0: /** michael@0: * Remove the query string from a url. michael@0: * michael@0: * @param {string} href michael@0: * Url to remove query string from michael@0: * @return {string} michael@0: * Url without query string michael@0: */ michael@0: function removeQuery(href) { michael@0: return href.replace(/\?.*/, ""); michael@0: }