From 5174e7481ffe2282cca0697b83fd19d49800e802 Mon Sep 17 00:00:00 2001 From: tophf Date: Wed, 4 Apr 2018 11:37:39 +0300 Subject: [PATCH] stricter "var(" check and code cosmetics --- edit/codemirror-default.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/edit/codemirror-default.js b/edit/codemirror-default.js index cb37dea3..998b605a 100644 --- a/edit/codemirror-default.js +++ b/edit/codemirror-default.js @@ -215,6 +215,8 @@ CodeMirror.hint && (() => { const USO_VALID_VAR = 'variable-3 ' + USO_VAR; const USO_INVALID_VAR = 'error ' + USO_VAR; const RX_IMPORTANT = /(i(m(p(o(r(t(a(nt?)?)?)?)?)?)?)?)?(?=\b|\W|$)/iy; + const RX_VAR_KEYWORD = /(^|[^-\w\u0080-\uFFFF])var\(/iy; + const RX_END_OF_VAR = /[\s,)]|$/g; const originalHelper = CodeMirror.hint.css || (() => {}); const helper = cm => { @@ -254,7 +256,7 @@ CodeMirror.hint && (() => { // --css-variables const startsWithDoubleDash = text[prev] === '-' && text[prev + 1] === '-'; if (startsWithDoubleDash || - leftPart === '(' && /\bvar/i.test(text.slice(prev - 4, prev))) { + leftPart === '(' && testAt(RX_VAR_KEYWORD, Math.max(0, prev - 4), text)) { // simplified regex without CSS escapes const RX_CSS_VAR = new RegExp( '(?:^|[\\s/;{])(' + @@ -270,9 +272,8 @@ CodeMirror.hint && (() => { if (!startsWithDoubleDash) { prev++; } - const rxEnd = /[\s,)]|$/g; - rxEnd.lastIndex = prev; - end = rxEnd.exec(text).index; + RX_END_OF_VAR.lastIndex = prev; + end = RX_END_OF_VAR.exec(text).index; return { list: [...list.keys()].sort(), from: {line, ch: prev}, @@ -322,4 +323,10 @@ CodeMirror.hint && (() => { } return token; } + + function testAt(rx, index, text) { + if (!rx) return false; + rx.lastIndex = index; + return rx.test(text); + } })();