-
Notifications
You must be signed in to change notification settings - Fork 0
/
1090871-focus-attribute-after-tag-edit.patch
385 lines (353 loc) · 15.7 KB
/
1090871-focus-attribute-after-tag-edit.patch
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# HG changeset patch
# User Grisha Pushkov <[email protected]>
# Parent f95c614295a0971529da8c0c93d28718e7c4c5bd
Bug 1090871 - Focus next editable field after pressing tab in the tagname editable field
diff --git a/devtools/client/markupview/markup-view.js b/devtools/client/markupview/markup-view.js
--- a/devtools/client/markupview/markup-view.js
+++ b/devtools/client/markupview/markup-view.js
@@ -14,17 +14,17 @@ const COLLAPSE_DATA_URL_REGEX = /^data.+
const COLLAPSE_DATA_URL_LENGTH = 60;
const NEW_SELECTION_HIGHLIGHTER_TIMER = 1000;
const DRAG_DROP_AUTOSCROLL_EDGE_DISTANCE = 50;
const DRAG_DROP_MIN_AUTOSCROLL_SPEED = 5;
const DRAG_DROP_MAX_AUTOSCROLL_SPEED = 15;
const AUTOCOMPLETE_POPUP_PANEL_ID = "markupview_autoCompletePopup";
const {UndoStack} = require("devtools/client/shared/undo");
-const {editableField, InplaceEditor} = require("devtools/client/shared/inplace-editor");
+const {editableField, InplaceEditor, moveFocus} = require("devtools/client/shared/inplace-editor");
const {gDevTools} = Cu.import("resource://devtools/client/framework/gDevTools.jsm", {});
const {HTMLEditor} = require("devtools/client/markupview/html-editor");
const promise = require("promise");
const {Tooltip} = require("devtools/client/shared/widgets/Tooltip");
const EventEmitter = require("devtools/shared/event-emitter");
const Heritage = require("sdk/core/heritage");
const {setTimeout, clearTimeout, setInterval, clearInterval} = require("sdk/timers");
const {parseAttribute} = require("devtools/client/shared/node-attribute-parser");
@@ -1108,24 +1108,26 @@ MarkupView.prototype = {
}
this._inspector.off("markupmutation", onMutations);
this._removedNodeObserver = null;
// Don't select the new node if the user has already changed the current
// selection.
if (this._inspector.selection.nodeFront === parentContainer.node ||
- (this._inspector.selection.nodeFront === removedNode && isHTMLTag)) {
+ (this._inspector.selection.nodeFront === removedNode && isHTMLTag)) {
let childContainers = parentContainer.getChildContainers();
if (childContainers && childContainers[childIndex]) {
- this.markNodeAsSelected(childContainers[childIndex].node, reason);
- if (childContainers[childIndex].hasChildren) {
- this.expandNode(childContainers[childIndex].node);
+ let container = childContainers[childIndex];
+
+ this.markNodeAsSelected(container.node, reason);
+ if (container.hasChildren) {
+ this.expandNode(container.node);
}
- this.emit("reselectedonremoved");
+ this.emit("reselectedonremoved", container);
}
}
};
// Start listening for mutations until we find a childList change that has
// removedNode removed.
this._inspector.on("markupmutation", onMutations);
},
@@ -2969,29 +2971,38 @@ ElementEditor.prototype = {
// Start listening for mutations until we find an attributes change
// that modifies this attribute.
this.markup._inspector.once("markupmutation", onMutations);
},
/**
* Called when the tag name editor has is done editing.
*/
- onTagEdit: function(newTagName, isCommit) {
+ onTagEdit: function(newTagName, isCommit, direction) {
if (!isCommit || newTagName.toLowerCase() === this.node.tagName.toLowerCase() ||
!("editTagName" in this.markup.walker)) {
return;
}
// Changing the tagName removes the node. Make sure the replacing node gets
// selected afterwards.
this.markup.reselectOnRemoved(this.node, "edittagname");
this.markup.walker.editTagName(this.node, newTagName).then(null, () => {
// Failed to edit the tag name, cancel the reselection.
this.markup.cancelReselectOnRemoved();
});
+
+ // Focus tag on Enter or next editable field on Tab / Shift + Tab.
+ this.markup.once("reselectedonremoved", (e, container) => {
+ container.editor.tag.focus();
+ if (direction) {
+ moveFocus(container.editor.doc, direction, true);
+ }
+ this.markup.emit("refocusedonremoved");
+ });
},
destroy: function() {
for (let key in this.animationTimers) {
clearTimeout(this.animationTimers[key]);
}
this.animationTimers = null;
}
diff --git a/devtools/client/markupview/test/browser.ini b/devtools/client/markupview/test/browser.ini
--- a/devtools/client/markupview/test/browser.ini
+++ b/devtools/client/markupview/test/browser.ini
@@ -110,14 +110,14 @@ skip-if = e10s # Bug 1036409 - The last
[browser_markupview_tag_edit_04.js]
[browser_markupview_tag_edit_05.js]
[browser_markupview_tag_edit_06.js]
[browser_markupview_tag_edit_07.js]
[browser_markupview_tag_edit_08.js]
[browser_markupview_tag_edit_09.js]
[browser_markupview_tag_edit_10.js]
[browser_markupview_tag_edit_11.js]
-[browser_markupview_tag_edit_12.js]
+[browser_markupview_tag_edit_12-focus-position.js]
[browser_markupview_textcontent_edit_01.js]
[browser_markupview_toggle_01.js]
[browser_markupview_toggle_02.js]
[browser_markupview_toggle_03.js]
[browser_markupview_update-on-navigtion.js]
diff --git a/devtools/client/markupview/test/browser_markupview_tag_edit_12.js b/devtools/client/markupview/test/browser_markupview_tag_edit_12-focus-position.js
rename from devtools/client/markupview/test/browser_markupview_tag_edit_12.js
rename to devtools/client/markupview/test/browser_markupview_tag_edit_12-focus-position.js
--- a/devtools/client/markupview/test/browser_markupview_tag_edit_12.js
+++ b/devtools/client/markupview/test/browser_markupview_tag_edit_12-focus-position.js
@@ -2,23 +2,29 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
// Tests that focus position is correct when tabbing through and editing
// attributes.
-const TEST_URL = "data:text/html;charset=utf8,<div id='attr' c='3' b='2' a='1'></div><div id='delattr' last='1' tobeinvalid='2'></div>";
+const TEST_URL = `
+ data:text/html;charset=utf8,
+ <div id='attr' c='3' b='2' a='1'></div>
+ <div id='delattr' last='1' tobeinvalid='2'></div>
+ <span></span>
+ <div id='with-attributes'></div>`;
add_task(function*() {
let {inspector} = yield addTab(TEST_URL).then(openInspector);
yield testAttributeEditing(inspector);
yield testAttributeDeletion(inspector);
+ yield testTagRenaming(inspector);
});
function* testAttributeEditing(inspector) {
info("Testing focus position after attribute editing");
// Modifying attributes reorders them in the internal representation to move
// the modified attribute to the end. breadcrumbs.js will update attributes
// to match original order if you selectNode before modifying attributes.
@@ -73,43 +79,89 @@ function* testAttributeDeletion(inspecto
let attrs = getNodeAttributesOtherThanId("#delattr");
info("Entering an invalid attribute to delete the attribute");
yield editAttributeAndTab('"', inspector);
checkFocusedAttribute(attrs[1].name, true);
info("Deleting the last attribute");
yield editAttributeAndTab(" ", inspector);
+ checkNewAttrIsActive();
+}
- // Check we're on the newattr element
- let focusedAttr = Services.focus.focusedElement;
- ok(focusedAttr.classList.contains("styleinspector-propertyeditor"), "in newattr");
- is(focusedAttr.tagName, "input", "newattr is active");
+function* testTagRenaming(inspector) {
+ const MOVEFOCUS_FORWARD = Ci.nsIFocusManager.MOVEFOCUS_FORWARD;
+ const MOVEFOCUS_BACKWARD = Ci.nsIFocusManager.MOVEFOCUS_BACKWARD;
+
+ info("Testing focus position after tag renaming");
+
+ info("Editing tag for node wihout attributes and tabbing forward");
+ yield selectNode("span", inspector);
+ yield editTag("span", "div", inspector, MOVEFOCUS_FORWARD);
+ checkNewAttrIsActive();
+
+ info("Editing tag for node with attributes and tabbing forward");
+ yield selectNode("#with-attributes", inspector);
+ yield editTag("#with-attributes", "span", inspector, MOVEFOCUS_FORWARD);
+ checkFocusedAttribute("id", true);
+
+ info("Editing tag and tabbing backward");
+ yield selectNode("#with-attributes", inspector);
+ yield editTag("#with-attributes", "div", inspector, MOVEFOCUS_BACKWARD);
+ checkNewAttrIsActive();
+
+ info("Editing tag and pressing Enter");
+ yield selectNode("#with-attributes", inspector);
+ yield editTag("#with-attributes", "span", inspector);
+ yield checkTagIsFocused("#with-attributes", inspector);
}
function* editAttributeAndTab(newValue, inspector, goPrevious) {
var onEditMutation = inspector.markup.once("refocusedonedit");
inspector.markup.doc.activeElement.value = newValue;
if (goPrevious) {
EventUtils.synthesizeKey("VK_TAB", { shiftKey: true },
inspector.panelWin);
} else {
EventUtils.sendKey("tab", inspector.panelWin);
}
yield onEditMutation;
}
+function* editTag(selector, value, inspector, direction) {
+ let container = yield getContainerForSelector(selector, inspector);
+ let onRefocused = container.markup.once("refocusedonremoved");
+ setEditableFieldValue(container.editor.tag, value, inspector, direction);
+ yield onRefocused;
+}
+
/**
- * Given a markup container, focus and turn in edit mode its first attribute
+ * Given an element's selector, focus and turn in edit mode its first attribute
* field.
*/
-function* activateFirstAttribute(container, inspector) {
- let {editor} = yield getContainerForSelector(container, inspector);
+function* activateFirstAttribute(selector, inspector) {
+ let {editor} = yield getContainerForSelector(selector, inspector);
editor.tag.focus();
// Go to "id" attribute and trigger edit mode.
EventUtils.sendKey("tab", inspector.panelWin);
EventUtils.sendKey("return", inspector.panelWin);
}
function getNodeAttributesOtherThanId(selector) {
return [...getNode(selector).attributes].filter(attr => attr.name !== "id");
}
+
+function checkNewAttrIsActive() {
+ let focusedElement = Services.focus.focusedElement;
+ let isNewAttr = focusedElement.classList.contains("styleinspector-propertyeditor");
+ let isActive = focusedElement.tagName === "input";
+
+ ok(isNewAttr && isActive, "newAttr is active");
+}
+
+function* checkTagIsFocused(selector, inspector) {
+ let {editor} = yield getContainerForSelector(selector, inspector);
+ let focusedElement = Services.focus.focusedElement;
+ let isFocused = focusedElement === editor.tag;
+
+ ok(isFocused, "tag is focused");
+}
diff --git a/devtools/client/markupview/test/head.js b/devtools/client/markupview/test/head.js
--- a/devtools/client/markupview/test/head.js
+++ b/devtools/client/markupview/test/head.js
@@ -353,30 +353,42 @@ var clickContainer = Task.async(function
*/
function isHighlighterVisible() {
let highlighter = gBrowser.selectedBrowser.parentNode
.querySelector(".highlighter-container .box-model-root");
return highlighter && !highlighter.hasAttribute("hidden");
}
/**
- * Focus a given editable element, enter edit mode, set value, and commit
- * @param {DOMNode} field The element that gets editable after receiving focus
- * and <ENTER> keypress
- * @param {String} value The string value to be set into the edited field
- * @param {InspectorPanel} inspector The instance of InspectorPanel currently
- * loaded in the toolbox
+ * Focus a given editable element, enter edit mode, set value, and commit.
+ *
+ * @param {DOMNode} field
+ * The element that gets editable after receiving focus
+ * and Enter keypress.
+ * @param {String} value
+ * The string value to be set into the edited field.
+ * @param {InspectorPanel} inspector
+ * The instance of InspectorPanel currently loaded in the toolbox.
+ * @param {direction}
+ * Search for Ci.nsIFocusManager constants.
*/
-function setEditableFieldValue(field, value, inspector) {
+function setEditableFieldValue(field, value, inspector, direction) {
field.focus();
EventUtils.sendKey("return", inspector.panelWin);
let input = inplaceEditor(field).input;
ok(input, "Found editable field for setting value: " + value);
input.value = value;
- EventUtils.sendKey("return", inspector.panelWin);
+
+ if (direction === Ci.nsIFocusManager.MOVEFOCUS_FORWARD) {
+ EventUtils.sendKey("tab", inspector.panelWin);
+ } else if (direction === Ci.nsIFocusManager.MOVEFOCUS_BACKWARD) {
+ EventUtils.synthesizeKey("VK_TAB", { shiftKey: true }, inspector.panelWin);
+ } else {
+ EventUtils.sendKey("return", inspector.panelWin);
+ }
}
/**
* Focus the new-attribute inplace-editor field of a node's markup container
* and enters the given text, then wait for it to be applied and the for the
* node to mutates (when new attribute(s) is(are) created)
* @param {String} selector The selector for the node to edit.
* @param {String} text The new attribute text to be entered (e.g. "id='test'")
diff --git a/devtools/client/shared/inplace-editor.js b/devtools/client/shared/inplace-editor.js
--- a/devtools/client/shared/inplace-editor.js
+++ b/devtools/client/shared/inplace-editor.js
@@ -987,28 +987,20 @@ InplaceEditor.prototype = {
this._apply(event, direction);
// Close the popup if open
if (this.popup && this.popup.isOpen) {
this.popup.hidePopup();
}
+ // If the focused element wasn't changed by the done callback,
+ // move the focus as requested.
if (direction !== null && focusManager.focusedElement === input) {
- // If the focused element wasn't changed by the done callback,
- // move the focus as requested.
- let next = moveFocus(this.doc.defaultView, direction);
-
- // If the next node to be focused has been tagged as an editable
- // node, trigger editing using the configured event
- if (next && next.ownerDocument === this.doc && next._editable) {
- let e = this.doc.createEvent("Event");
- e.initEvent(next._trigger, true, true);
- next.dispatchEvent(e);
- }
+ moveFocus(this.doc, direction, true);
}
this._clear();
} else if (event.keyCode === Ci.nsIDOMKeyEvent.DOM_VK_ESCAPE) {
// Cancel and blur ourselves.
// Now we don't want to suggest anything as we are moving out.
this._preventSuggestions = true;
// Close the popup if open
@@ -1227,22 +1219,41 @@ function copyTextStyles(from, to) {
let style = win.getComputedStyle(from);
to.style.fontFamily = style.getPropertyCSSValue("font-family").cssText;
to.style.fontSize = style.getPropertyCSSValue("font-size").cssText;
to.style.fontWeight = style.getPropertyCSSValue("font-weight").cssText;
to.style.fontStyle = style.getPropertyCSSValue("font-style").cssText;
}
/**
- * Trigger a focus change similar to pressing tab/shift-tab.
+ * Trigger a focus change similar to pressing Tab / Shift + Tab.
+ *
+ * @param {HTMLDocument}
+ * @param {Integer} direction
+ * Search for Ci.nsIFocusManager constants.
+ * @param {Boolean} editMode
+ * True if focused node should be in edit mode.
+ * @return {DOMNode}
+ * Focused node after focus change.
*/
-function moveFocus(win, direction) {
- return focusManager.moveFocus(win, null, direction, 0);
+function moveFocus(doc, direction, editMode) {
+ let next = focusManager.moveFocus(doc.defaultView, null, direction, 0);
+
+ // Set next node to edit mode by using configured event.
+ if (editMode && next && next.ownerDocument === doc && next._editable) {
+ let e = doc.createEvent("Event");
+ e.initEvent(next._trigger, true, true);
+ next.dispatchEvent(e);
+ }
+
+ return next;
}
+exports.moveFocus = moveFocus;
+
XPCOMUtils.defineLazyGetter(this, "focusManager", function() {
return Services.focus;
});
XPCOMUtils.defineLazyGetter(this, "CSSPropertyList", function() {
return domUtils.getCSSPropertyNames(domUtils.INCLUDE_ALIASES).sort();
});