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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const {
AUTOCOMPLETE_CLEAR,
AUTOCOMPLETE_DATA_RECEIVE,
AUTOCOMPLETE_PENDING_REQUEST,
AUTOCOMPLETE_RETRIEVE_FROM_CACHE,
EVALUATE_EXPRESSION,
UPDATE_HISTORY_POSITION,
REVERSE_SEARCH_INPUT_CHANGE,
REVERSE_SEARCH_BACK,
REVERSE_SEARCH_NEXT,
WILL_NAVIGATE,
} = require("resource://devtools/client/webconsole/constants.js");
function getDefaultState(overrides = {}) {
return Object.freeze({
cache: null,
matches: [],
matchProp: null,
isElementAccess: false,
pendingRequestId: null,
isUnsafeGetter: false,
getterPath: null,
authorizedEvaluations: [],
...overrides,
});
}
function autocomplete(state = getDefaultState(), action) {
switch (action.type) {
case WILL_NAVIGATE:
return getDefaultState();
case AUTOCOMPLETE_RETRIEVE_FROM_CACHE:
return autoCompleteRetrieveFromCache(state, action);
case AUTOCOMPLETE_PENDING_REQUEST:
return {
...state,
cache: null,
pendingRequestId: action.id,
};
case AUTOCOMPLETE_DATA_RECEIVE:
if (action.id !== state.pendingRequestId) {
return state;
}
if (action.data.matches === null) {
return getDefaultState();
}
if (action.data.isUnsafeGetter) {
// We only want to display the getter confirm popup if the last char is a dot or
// an opening bracket, or if the user forced the autocompletion with Ctrl+Space.
if (
action.input.endsWith(".") ||
action.input.endsWith("[") ||
action.force
) {
return {
...getDefaultState(),
isUnsafeGetter: true,
getterPath: action.data.getterPath,
authorizedEvaluations: action.authorizedEvaluations,
};
}
return {
...state,
pendingRequestId: null,
};
}
return {
...state,
authorizedEvaluations: action.authorizedEvaluations,
getterPath: null,
isUnsafeGetter: false,
pendingRequestId: null,
cache: {
input: action.input,
frameActorId: action.frameActorId,
...action.data,
},
...action.data,
};
// Reset the autocomplete data when:
// - clear is explicitely called
// - the user navigates the history
// - or an expression was evaluated.
case AUTOCOMPLETE_CLEAR:
return getDefaultState({
authorizedEvaluations: state.authorizedEvaluations,
});
case EVALUATE_EXPRESSION:
case UPDATE_HISTORY_POSITION:
case REVERSE_SEARCH_INPUT_CHANGE:
case REVERSE_SEARCH_BACK:
case REVERSE_SEARCH_NEXT:
return getDefaultState();
}
return state;
}
/**
* Retrieve from cache action reducer.
*
* @param {Object} state
* @param {Object} action
* @returns {Object} new state.
*/
function autoCompleteRetrieveFromCache(state, action) {
const { input } = action;
const { cache } = state;
let filterBy = input;
if (cache.isElementAccess) {
// if we're performing an element access, we can simply retrieve whatever comes
// after the last opening bracket.
filterBy = input.substring(input.lastIndexOf("[") + 1);
} else {
// Find the last non-alphanumeric other than "_", ":", or "$" if it exists.
const lastNonAlpha = input.match(/[^a-zA-Z0-9_$:][a-zA-Z0-9_$:]*$/);
// If input contains non-alphanumerics, use the part after the last one
// to filter the cache.
if (lastNonAlpha) {
filterBy = input.substring(input.lastIndexOf(lastNonAlpha) + 1);
}
}
const stripWrappingQuotes = s =>
s.replace(/^['"`](.+(?=['"`]$))['"`]$/g, "$1");
const filterByLc = filterBy.toLocaleLowerCase();
const looseMatching =
!filterBy || filterBy[0].toLocaleLowerCase() === filterBy[0];
const needStripQuote = cache.isElementAccess && !/^[`"']/.test(filterBy);
const newList = cache.matches.filter(l => {
if (needStripQuote) {
l = stripWrappingQuotes(l);
}
if (looseMatching) {
return l.toLocaleLowerCase().startsWith(filterByLc);
}
return l.startsWith(filterBy);
});
newList.sort((a, b) => {
const startingQuoteRegex = /^('|"|`)/;
const aFirstMeaningfulChar = startingQuoteRegex.test(a) ? a[1] : a[0];
const bFirstMeaningfulChar = startingQuoteRegex.test(b) ? b[1] : b[0];
const lA =
aFirstMeaningfulChar.toLocaleLowerCase() === aFirstMeaningfulChar;
const lB =
bFirstMeaningfulChar.toLocaleLowerCase() === bFirstMeaningfulChar;
if (lA === lB) {
if (a === filterBy) {
return -1;
}
if (b === filterBy) {
return 1;
}
return a.localeCompare(b);
}
return lA ? -1 : 1;
});
return {
...state,
isUnsafeGetter: false,
getterPath: null,
matches: newList,
matchProp: filterBy,
isElementAccess: cache.isElementAccess,
};
}
exports.autocomplete = autocomplete;
|