summaryrefslogtreecommitdiffstats
path: root/devtools/client/fronts/style-sheet.js
blob: 7f95e09ddbb1ae48af1ea14f84c8412a0a65d4a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* 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 {
  FrontClassWithSpec,
  registerFront,
} = require("devtools/shared/protocol");
const { styleSheetSpec } = require("devtools/shared/specs/style-sheet");
const promise = require("promise");

loader.lazyRequireGetter(
  this,
  ["getIndentationFromPrefs", "getIndentationFromString"],
  "devtools/shared/indentation",
  true
);

/**
 * StyleSheetFront is the client-side counterpart to a StyleSheetActor.
 */
class StyleSheetFront extends FrontClassWithSpec(styleSheetSpec) {
  constructor(conn, targetFront, parentFront) {
    super(conn, targetFront, parentFront);

    this._onPropertyChange = this._onPropertyChange.bind(this);
    this.on("property-change", this._onPropertyChange);
  }

  destroy() {
    this.off("property-change", this._onPropertyChange);
    super.destroy();
  }

  _onPropertyChange(property, value) {
    this[property] = value;
  }

  form(form) {
    this.actorID = form.actor;
    this.href = form.href;
    this.nodeHref = form.nodeHref;
    this.disabled = form.disabled;
    this.title = form.title;
    this.system = form.system;
    this.styleSheetIndex = form.styleSheetIndex;
    this.ruleCount = form.ruleCount;
    this.sourceMapURL = form.sourceMapURL;
    this._sourceMapBaseURL = form.sourceMapBaseURL;
  }

  get isSystem() {
    return this.system;
  }

  get sourceMapBaseURL() {
    // Handle backward-compat for servers that don't return sourceMapBaseURL.
    if (this._sourceMapBaseURL === undefined) {
      return this.href || this.nodeHref;
    }

    return this._sourceMapBaseURL;
  }

  set sourceMapBaseURL(sourceMapBaseURL) {
    this._sourceMapBaseURL = sourceMapBaseURL;
  }

  /**
   * Get the indentation to use for edits to this style sheet.
   *
   * @return {Promise} A promise that will resolve to a string that
   * should be used to indent a block in this style sheet.
   */
  guessIndentation() {
    const prefIndent = getIndentationFromPrefs();
    if (prefIndent) {
      const { indentUnit, indentWithTabs } = prefIndent;
      return promise.resolve(indentWithTabs ? "\t" : " ".repeat(indentUnit));
    }

    return async function() {
      const longStr = await this.getText();
      const source = await longStr.string();

      const { indentUnit, indentWithTabs } = getIndentationFromString(source);

      return indentWithTabs ? "\t" : " ".repeat(indentUnit);
    }.bind(this)();
  }
}

exports.StyleSheetFront = StyleSheetFront;
registerFront(StyleSheetFront);