stylesheets.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. "use strict";
  5. const { Front, FrontClassWithSpec } = require("devtools/shared/protocol");
  6. const {
  7. getIndentationFromPrefs,
  8. getIndentationFromString
  9. } = require("devtools/shared/indentation");
  10. const {
  11. originalSourceSpec,
  12. mediaRuleSpec,
  13. styleSheetSpec,
  14. styleSheetsSpec
  15. } = require("devtools/shared/specs/stylesheets");
  16. const promise = require("promise");
  17. const { Task } = require("devtools/shared/task");
  18. const events = require("sdk/event/core");
  19. /**
  20. * The client-side counterpart for an OriginalSourceActor.
  21. */
  22. const OriginalSourceFront = FrontClassWithSpec(originalSourceSpec, {
  23. initialize: function (client, form) {
  24. Front.prototype.initialize.call(this, client, form);
  25. this.isOriginalSource = true;
  26. },
  27. form: function (form, detail) {
  28. if (detail === "actorid") {
  29. this.actorID = form;
  30. return;
  31. }
  32. this.actorID = form.actor;
  33. this._form = form;
  34. },
  35. get href() {
  36. return this._form.url;
  37. },
  38. get url() {
  39. return this._form.url;
  40. }
  41. });
  42. exports.OriginalSourceFront = OriginalSourceFront;
  43. /**
  44. * Corresponding client-side front for a MediaRuleActor.
  45. */
  46. const MediaRuleFront = FrontClassWithSpec(mediaRuleSpec, {
  47. initialize: function (client, form) {
  48. Front.prototype.initialize.call(this, client, form);
  49. this._onMatchesChange = this._onMatchesChange.bind(this);
  50. events.on(this, "matches-change", this._onMatchesChange);
  51. },
  52. _onMatchesChange: function (matches) {
  53. this._form.matches = matches;
  54. },
  55. form: function (form, detail) {
  56. if (detail === "actorid") {
  57. this.actorID = form;
  58. return;
  59. }
  60. this.actorID = form.actor;
  61. this._form = form;
  62. },
  63. get mediaText() {
  64. return this._form.mediaText;
  65. },
  66. get conditionText() {
  67. return this._form.conditionText;
  68. },
  69. get matches() {
  70. return this._form.matches;
  71. },
  72. get line() {
  73. return this._form.line || -1;
  74. },
  75. get column() {
  76. return this._form.column || -1;
  77. },
  78. get parentStyleSheet() {
  79. return this.conn.getActor(this._form.parentStyleSheet);
  80. }
  81. });
  82. exports.MediaRuleFront = MediaRuleFront;
  83. /**
  84. * StyleSheetFront is the client-side counterpart to a StyleSheetActor.
  85. */
  86. const StyleSheetFront = FrontClassWithSpec(styleSheetSpec, {
  87. initialize: function (conn, form) {
  88. Front.prototype.initialize.call(this, conn, form);
  89. this._onPropertyChange = this._onPropertyChange.bind(this);
  90. events.on(this, "property-change", this._onPropertyChange);
  91. },
  92. destroy: function () {
  93. events.off(this, "property-change", this._onPropertyChange);
  94. Front.prototype.destroy.call(this);
  95. },
  96. _onPropertyChange: function (property, value) {
  97. this._form[property] = value;
  98. },
  99. form: function (form, detail) {
  100. if (detail === "actorid") {
  101. this.actorID = form;
  102. return;
  103. }
  104. this.actorID = form.actor;
  105. this._form = form;
  106. },
  107. get href() {
  108. return this._form.href;
  109. },
  110. get nodeHref() {
  111. return this._form.nodeHref;
  112. },
  113. get disabled() {
  114. return !!this._form.disabled;
  115. },
  116. get title() {
  117. return this._form.title;
  118. },
  119. get isSystem() {
  120. return this._form.system;
  121. },
  122. get styleSheetIndex() {
  123. return this._form.styleSheetIndex;
  124. },
  125. get ruleCount() {
  126. return this._form.ruleCount;
  127. },
  128. /**
  129. * Get the indentation to use for edits to this style sheet.
  130. *
  131. * @return {Promise} A promise that will resolve to a string that
  132. * should be used to indent a block in this style sheet.
  133. */
  134. guessIndentation: function () {
  135. let prefIndent = getIndentationFromPrefs();
  136. if (prefIndent) {
  137. let {indentUnit, indentWithTabs} = prefIndent;
  138. return promise.resolve(indentWithTabs ? "\t" : " ".repeat(indentUnit));
  139. }
  140. return Task.spawn(function* () {
  141. let longStr = yield this.getText();
  142. let source = yield longStr.string();
  143. let {indentUnit, indentWithTabs} = getIndentationFromString(source);
  144. return indentWithTabs ? "\t" : " ".repeat(indentUnit);
  145. }.bind(this));
  146. }
  147. });
  148. exports.StyleSheetFront = StyleSheetFront;
  149. /**
  150. * The corresponding Front object for the StyleSheetsActor.
  151. */
  152. const StyleSheetsFront = FrontClassWithSpec(styleSheetsSpec, {
  153. initialize: function (client, tabForm) {
  154. Front.prototype.initialize.call(this, client);
  155. this.actorID = tabForm.styleSheetsActor;
  156. this.manage(this);
  157. }
  158. });
  159. exports.StyleSheetsFront = StyleSheetsFront;