highlight.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 l10n = require("gcli/l10n");
  6. require("devtools/server/actors/inspector");
  7. const {
  8. BoxModelHighlighter,
  9. HighlighterEnvironment
  10. } = require("devtools/server/actors/highlighters");
  11. const {PluralForm} = require("devtools/shared/plural-form");
  12. const {LocalizationHelper} = require("devtools/shared/l10n");
  13. const L10N = new LocalizationHelper("devtools/shared/locales/gclicommands.properties");
  14. // How many maximum nodes can be highlighted in parallel
  15. const MAX_HIGHLIGHTED_ELEMENTS = 100;
  16. // Store the environment object used to create highlighters so it can be
  17. // destroyed later.
  18. var highlighterEnv;
  19. // Stores the highlighters instances so they can be destroyed later.
  20. // also export them so tests can access those and assert they got created
  21. // correctly.
  22. exports.highlighters = [];
  23. /**
  24. * Destroy all existing highlighters
  25. */
  26. function unhighlightAll() {
  27. for (let highlighter of exports.highlighters) {
  28. highlighter.destroy();
  29. }
  30. exports.highlighters.length = 0;
  31. if (highlighterEnv) {
  32. highlighterEnv.destroy();
  33. highlighterEnv = null;
  34. }
  35. }
  36. exports.items = [
  37. {
  38. item: "command",
  39. runAt: "server",
  40. name: "highlight",
  41. description: l10n.lookup("highlightDesc"),
  42. manual: l10n.lookup("highlightManual"),
  43. params: [
  44. {
  45. name: "selector",
  46. type: "nodelist",
  47. description: l10n.lookup("highlightSelectorDesc"),
  48. manual: l10n.lookup("highlightSelectorManual")
  49. },
  50. {
  51. group: l10n.lookup("highlightOptionsDesc"),
  52. params: [
  53. {
  54. name: "hideguides",
  55. type: "boolean",
  56. description: l10n.lookup("highlightHideGuidesDesc"),
  57. manual: l10n.lookup("highlightHideGuidesManual")
  58. },
  59. {
  60. name: "showinfobar",
  61. type: "boolean",
  62. description: l10n.lookup("highlightShowInfoBarDesc"),
  63. manual: l10n.lookup("highlightShowInfoBarManual")
  64. },
  65. {
  66. name: "showall",
  67. type: "boolean",
  68. description: l10n.lookup("highlightShowAllDesc"),
  69. manual: l10n.lookup("highlightShowAllManual")
  70. },
  71. {
  72. name: "region",
  73. type: {
  74. name: "selection",
  75. data: ["content", "padding", "border", "margin"]
  76. },
  77. description: l10n.lookup("highlightRegionDesc"),
  78. manual: l10n.lookup("highlightRegionManual"),
  79. defaultValue: "border"
  80. },
  81. {
  82. name: "fill",
  83. type: "string",
  84. description: l10n.lookup("highlightFillDesc"),
  85. manual: l10n.lookup("highlightFillManual"),
  86. defaultValue: null
  87. },
  88. {
  89. name: "keep",
  90. type: "boolean",
  91. description: l10n.lookup("highlightKeepDesc"),
  92. manual: l10n.lookup("highlightKeepManual")
  93. }
  94. ]
  95. }
  96. ],
  97. exec: function(args, context) {
  98. // Remove all existing highlighters unless told otherwise
  99. if (!args.keep) {
  100. unhighlightAll();
  101. }
  102. let env = context.environment;
  103. highlighterEnv = new HighlighterEnvironment();
  104. highlighterEnv.initFromWindow(env.window);
  105. // Unhighlight on navigate
  106. highlighterEnv.once("will-navigate", unhighlightAll);
  107. let i = 0;
  108. for (let node of args.selector) {
  109. if (!args.showall && i >= MAX_HIGHLIGHTED_ELEMENTS) {
  110. break;
  111. }
  112. let highlighter = new BoxModelHighlighter(highlighterEnv);
  113. if (args.fill) {
  114. highlighter.regionFill[args.region] = args.fill;
  115. }
  116. highlighter.show(node, {
  117. region: args.region,
  118. hideInfoBar: !args.showinfobar,
  119. hideGuides: args.hideguides,
  120. showOnly: args.region
  121. });
  122. exports.highlighters.push(highlighter);
  123. i++;
  124. }
  125. let highlightText = L10N.getStr("highlightOutputConfirm2");
  126. let output = PluralForm.get(args.selector.length, highlightText)
  127. .replace("%1$S", args.selector.length);
  128. if (args.selector.length > i) {
  129. output = l10n.lookupFormat("highlightOutputMaxReached",
  130. ["" + args.selector.length, "" + i]);
  131. }
  132. return output;
  133. }
  134. },
  135. {
  136. item: "command",
  137. runAt: "server",
  138. name: "unhighlight",
  139. description: l10n.lookup("unhighlightDesc"),
  140. manual: l10n.lookup("unhighlightManual"),
  141. exec: unhighlightAll
  142. }
  143. ];