summaryrefslogtreecommitdiffstats
path: root/devtools/client/styleeditor/test/browser_styleeditor_highlight-selector.js
blob: a5ee01e4384f1a3360f08024b0bb0e2e0b4be2d2 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test that hovering over a simple selector in the style-editor requests the
// highlighting of the corresponding nodes, even in remote iframes.

const REMOTE_IFRAME_URL = `https://example.org/document-builder.sjs?html=
    <style>h2{color:cyan}</style>
    <h2>highlighter test</h2>`;
const TOP_LEVEL_URL = `https://example.com/document-builder.sjs?html=
    <style>h1{color:red}</style>
    <h1>highlighter test</h1>
    <iframe src='${REMOTE_IFRAME_URL}'></iframe>`;

add_task(async function () {
  const { ui } = await openStyleEditorForURL(TOP_LEVEL_URL);

  info(
    "Wait until both stylesheet are loaded and ready to handle mouse events"
  );
  await waitFor(() => ui.editors.length == 2);
  const topLevelStylesheetEditor = ui.editors.find(e =>
    e._resource.nodeHref.startsWith("https://example.com")
  );
  const iframeStylesheetEditor = ui.editors.find(e =>
    e._resource.nodeHref.startsWith("https://example.org")
  );

  await ui.selectStyleSheet(topLevelStylesheetEditor.styleSheet);
  await waitFor(() => topLevelStylesheetEditor.highlighter);

  info("Check that highlighting works on the top-level document");
  const topLevelHighlighterTestFront =
    await topLevelStylesheetEditor._resource.targetFront.getFront(
      "highlighterTest"
    );
  topLevelHighlighterTestFront.highlighter =
    topLevelStylesheetEditor.highlighter;

  info("Expecting a node-highlighted event");
  let onHighlighted = topLevelStylesheetEditor.once("node-highlighted");

  info("Simulate a mousemove event on the h1 selector");
  // mousemove event listeners is set on editor.sourceEditor, which is not defined right away.
  await waitFor(() => !!topLevelStylesheetEditor.sourceEditor);
  let selectorEl = querySelectorCodeMirrorCssRuleSelectorToken(
    topLevelStylesheetEditor
  );
  EventUtils.synthesizeMouseAtCenter(
    selectorEl,
    { type: "mousemove" },
    selectorEl.ownerDocument.defaultView
  );
  await onHighlighted;
  ok(
    await topLevelHighlighterTestFront.isNodeRectHighlighted(
      await getElementNodeRectWithinTarget(["h1"])
    ),
    "The highlighter's outline corresponds to the h1 node"
  );

  info(
    "Simulate a mousemove event on the property name to hide the highlighter"
  );
  EventUtils.synthesizeMouseAtCenter(
    querySelectorCodeMirrorCssPropertyNameToken(topLevelStylesheetEditor),
    { type: "mousemove" },
    selectorEl.ownerDocument.defaultView
  );

  await waitFor(async () => !topLevelStylesheetEditor.highlighter.isShown());
  let isVisible = await topLevelHighlighterTestFront.isHighlighting();
  is(isVisible, false, "The highlighter is now hidden");

  // It looks like we need to show the same highlighter again to trigger Bug 1847747
  info("Show and hide the highlighter again");
  onHighlighted = topLevelStylesheetEditor.once("node-highlighted");
  EventUtils.synthesizeMouseAtCenter(
    selectorEl,
    { type: "mousemove" },
    selectorEl.ownerDocument.defaultView
  );
  await onHighlighted;
  EventUtils.synthesizeMouseAtCenter(
    querySelectorCodeMirrorCssPropertyNameToken(topLevelStylesheetEditor),
    { type: "mousemove" },
    selectorEl.ownerDocument.defaultView
  );

  await waitFor(async () => !topLevelStylesheetEditor.highlighter.isShown());
  // wait for a bit so the style editor would have had the time to process
  // any unwanted stylesheets
  await wait(1000);
  ok(
    !ui.editors.find(e => e._resource.href?.includes("highlighters.css")),
    "highlighters.css isn't displayed in StyleEditor"
  );
  is(ui.editors.length, 2, "No other stylesheet was displayed");

  info("Check that highlighting works on the iframe document");
  await ui.selectStyleSheet(iframeStylesheetEditor.styleSheet);
  await waitFor(() => iframeStylesheetEditor.highlighter);

  const iframeHighlighterTestFront =
    await iframeStylesheetEditor._resource.targetFront.getFront(
      "highlighterTest"
    );
  iframeHighlighterTestFront.highlighter = iframeStylesheetEditor.highlighter;

  info("Expecting a node-highlighted event");
  onHighlighted = iframeStylesheetEditor.once("node-highlighted");

  info("Simulate a mousemove event on the h2 selector");
  // mousemove event listeners is set on editor.sourceEditor, which is not defined right away.
  await waitFor(() => !!iframeStylesheetEditor.sourceEditor);
  selectorEl = querySelectorCodeMirrorCssRuleSelectorToken(
    iframeStylesheetEditor
  );
  EventUtils.synthesizeMouseAtCenter(
    selectorEl,
    { type: "mousemove" },
    selectorEl.ownerDocument.defaultView
  );
  await onHighlighted;

  isVisible = await iframeHighlighterTestFront.isHighlighting();
  ok(isVisible, "The highlighter is shown");
  ok(
    await iframeHighlighterTestFront.isNodeRectHighlighted(
      await getElementNodeRectWithinTarget(["iframe", "h2"])
    ),
    "The highlighter's outline corresponds to the h2 node"
  );

  info("Simulate a mousemove event elsewhere in the editor");
  EventUtils.synthesizeMouseAtCenter(
    querySelectorCodeMirrorCssPropertyNameToken(iframeStylesheetEditor),
    { type: "mousemove" },
    selectorEl.ownerDocument.defaultView
  );

  await waitFor(async () => !topLevelStylesheetEditor.highlighter.isShown());

  isVisible = await iframeHighlighterTestFront.isHighlighting();
  is(isVisible, false, "The highlighter is now hidden");
});

function querySelectorCodeMirrorCssRuleSelectorToken(stylesheetEditor) {
  // CSS Rules selector (e.g. `h1`) are displayed in a .cm-tag span
  return querySelectorCodeMirror(stylesheetEditor, ".cm-tag");
}

function querySelectorCodeMirrorCssPropertyNameToken(stylesheetEditor) {
  // properties name (e.g. `color`) are displayed in a .cm-property span
  return querySelectorCodeMirror(stylesheetEditor, ".cm-property");
}

function querySelectorCodeMirror(stylesheetEditor, selector) {
  return stylesheetEditor.sourceEditor.codeMirror
    .getWrapperElement()
    .querySelector(selector);
}

/**
 * Return the bounds of the element matching the selector, relatively to the target bounds
 * (e.g. if Fission is enabled, it's related to the iframe bound, if Fission is disabled,
 * it's related to the top level document).
 *
 * @param {Array<string>} selectors: Arrays of CSS selectors from the root document to the node.
 *        The last CSS selector of the array is for the node in its frame doc.
 *        The before-last CSS selector is for the frame in its parent frame, etc...
 *        Ex: ["frame.first-frame", ..., "frame.last-frame", ".target-node"]
 * @returns {Object} with left/top/width/height properties representing the node bounds
 */
async function getElementNodeRectWithinTarget(selectors) {
  // Retrieve the browsing context in which the element is
  const inBCSelector = selectors.pop();
  const frameSelectors = selectors;
  const bc = frameSelectors.length
    ? await getBrowsingContextInFrames(
        gBrowser.selectedBrowser.browsingContext,
        frameSelectors
      )
    : gBrowser.selectedBrowser.browsingContext;

  // Get the element bounds within the Firefox window
  const elementBounds = await SpecialPowers.spawn(
    bc,
    [inBCSelector],
    _selector => {
      const el = content.document.querySelector(_selector);
      const { left, top, width, height } = el
        .getBoxQuadsFromWindowOrigin()[0]
        .getBounds();
      return { left, top, width, height };
    }
  );

  // Then we need to offset the element bounds from a frame bounds
  // When fission/EFT is enabled, the highlighter is only shown within the iframe bounds.
  // So we only need to retrieve the element bounds within the iframe.
  // Otherwise, we retrieve the top frame bounds
  const relativeBrowsingContext =
    isFissionEnabled() || isEveryFrameTargetEnabled()
      ? bc
      : gBrowser.selectedBrowser.browsingContext;
  const relativeDocumentBounds = await SpecialPowers.spawn(
    relativeBrowsingContext,
    [],
    () =>
      content.document.documentElement
        .getBoxQuadsFromWindowOrigin()[0]
        .getBounds()
  );

  // Adjust the element bounds based on the relative document bounds
  elementBounds.left = elementBounds.left - relativeDocumentBounds.left;
  elementBounds.top = elementBounds.top - relativeDocumentBounds.top;

  return elementBounds;
}