Skip to content

Commit 7965dec

Browse files
committed
Code Editor: Fix keyboard accessibility of autocompletion for HTML attribute values and PHP keywords/variables.
Developed in WordPress#10807 Follow-up to [41376]. Props siliconforks, joedolson, westonruter, afercia, jorbin. Fixes #42822. git-svn-id: https://develop.svn.wordpress.org/trunk@61579 602fd350-edb4-49c9-b593-d223f7449a82
1 parent f8c0213 commit 7965dec

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/js/_enqueues/wp/code-editor.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* @output wp-admin/js/code-editor.js
33
*/
44

5+
/* eslint-env es2020 */
6+
57
if ( 'undefined' === typeof window.wp ) {
68
/**
79
* @namespace wp
@@ -315,21 +317,25 @@ if ( 'undefined' === typeof window.wp.codeEditor ) {
315317
innerMode = wp.CodeMirror.innerMode( codemirror.getMode(), token.state ).mode.name;
316318
lineBeforeCursor = codemirror.doc.getLine( codemirror.doc.getCursor().line ).substr( 0, codemirror.doc.getCursor().ch );
317319
if ( 'html' === innerMode || 'xml' === innerMode ) {
318-
shouldAutocomplete =
320+
shouldAutocomplete = (
319321
'<' === event.key ||
320-
'/' === event.key && 'tag' === token.type ||
321-
isAlphaKey && 'tag' === token.type ||
322-
isAlphaKey && 'attribute' === token.type ||
323-
'=' === token.string && token.state.htmlState && token.state.htmlState.tagName;
322+
( '/' === event.key && 'tag' === token.type ) ||
323+
( isAlphaKey && 'tag' === token.type ) ||
324+
( isAlphaKey && 'attribute' === token.type ) ||
325+
( '=' === event.key && (
326+
token.state.htmlState?.tagName ||
327+
token.state.curState?.htmlState?.tagName
328+
) )
329+
);
324330
} else if ( 'css' === innerMode ) {
325331
shouldAutocomplete =
326332
isAlphaKey ||
327333
':' === event.key ||
328-
' ' === event.key && /:\s+$/.test( lineBeforeCursor );
334+
( ' ' === event.key && /:\s+$/.test( lineBeforeCursor ) );
329335
} else if ( 'javascript' === innerMode ) {
330336
shouldAutocomplete = isAlphaKey || '.' === event.key;
331337
} else if ( 'clike' === innerMode && 'php' === codemirror.options.mode ) {
332-
shouldAutocomplete = 'keyword' === token.type || 'variable' === token.type;
338+
shouldAutocomplete = isAlphaKey && ( 'keyword' === token.type || 'variable' === token.type );
333339
}
334340
if ( shouldAutocomplete ) {
335341
codemirror.showHint( { completeSingle: false } );

0 commit comments

Comments
 (0)