Bug 1463676 - Don't add an evaluated input to the history if it is the same as the previous evaluation; r=nchevobbe
MozReview-Commit-ID: 74TWpnO6wPx
--- a/devtools/client/webconsole/reducers/history.js
+++ b/devtools/client/webconsole/reducers/history.js
@@ -48,18 +48,26 @@ function history(state = getInitialState
return state;
}
function appendToHistory(state, prefsState, expression) {
// Clone state
state = {...state};
state.entries = [...state.entries];
- // Append new expression
- state.entries[state.index++] = expression;
+ // Append new expression only if it isn't the same as
+ // the one recently added.
+ // If it's the same don't forget to remove the current
+ // input value that has been appended in updatePlaceholder.
+ if (expression.trim() != state.entries[state.index - 1]) {
+ state.entries[state.index++] = expression;
+ } else if (state.index < state.entries.length) {
+ state.entries.pop();
+ }
+
state.placeHolder = state.entries.length;
// Remove entries if the limit is reached
if (state.entries.length > prefsState.historyCount) {
state.entries.splice(0, state.entries.length - prefsState.historyCount);
state.index = state.placeHolder = state.entries.length;
}
--- a/devtools/client/webconsole/test/mochitest/browser_jsterm_add_edited_input_to_history.js
+++ b/devtools/client/webconsole/test/mochitest/browser_jsterm_add_edited_input_to_history.js
@@ -47,9 +47,21 @@ async function testEditedInputHistory(hu
is(jsterm.getInputValue(), '"second item"', "test history up");
EventUtils.synthesizeKey("KEY_ArrowUp");
is(jsterm.getInputValue(), '"first item"', "test history up");
EventUtils.synthesizeKey("KEY_ArrowDown");
is(jsterm.getInputValue(), '"second item"', "test history down");
EventUtils.synthesizeKey("KEY_ArrowDown");
is(jsterm.getInputValue(), '"editing input 2"',
"test history down restores new in-progress input again");
+
+ // Appending the same value again should not impact the history.
+ // Let's also use some spaces around to check that the input value
+ // is properly trimmed.
+ await jsterm.execute('"second item"');
+ await jsterm.execute(' "second item" ');
+ EventUtils.synthesizeKey("KEY_ArrowUp");
+ is(jsterm.getInputValue(), '"second item"',
+ "test history up reaches duplicated entry just once");
+ EventUtils.synthesizeKey("KEY_ArrowUp");
+ is(jsterm.getInputValue(), '"first item"',
+ "test history up reaches the previous value");
}