From 18c7d72843ff6b4c3fdb4ac03532eb33bc635aa7 Mon Sep 17 00:00:00 2001 From: Samuel Walladge Date: Tue, 5 May 2020 09:25:23 +0930 Subject: [PATCH] restructure code to pass lints --- .../spec/formula_equation_preview_spec.js | 7 ++- .../js/capa/src/formula_equation_preview.js | 49 ++++++++++--------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/common/static/js/capa/spec/formula_equation_preview_spec.js b/common/static/js/capa/spec/formula_equation_preview_spec.js index 5e9cf7a487..17c147a8d0 100644 --- a/common/static/js/capa/spec/formula_equation_preview_spec.js +++ b/common/static/js/capa/spec/formula_equation_preview_spec.js @@ -3,7 +3,8 @@ describe('escapeSelector', function() { var escapeSelector = window.escapeSelector; it('correctly escapes css', function() { - // tests borrowed from https://github.com/jquery/jquery/blob/3edfa1bcdc50bca41ac58b2642b12f3feee03a3b/test/unit/selector.js#L2030 + // tests borrowed from + // https://github.com/jquery/jquery/blob/3edfa1bcdc50bca41ac58b2642b12f3feee03a3b/test/unit/selector.js#L2030 expect(escapeSelector('-')).toEqual('\\-'); expect(escapeSelector('-a')).toEqual('-a'); expect(escapeSelector('--')).toEqual('--'); @@ -20,7 +21,9 @@ describe('escapeSelector', function() { // This is the important one; xblocks and course ids often contain invalid characters, so if these aren't // escaped when embedding/searching xblock IDs using css selectors, bad things happen. expect(escapeSelector('course-v1:edX+DemoX+Demo_Course')).toEqual('course-v1\\:edX\\+DemoX\\+Demo_Course'); - expect(escapeSelector('block-v1:edX+DemoX+Demo_Course+type@sequential+block')).toEqual('block-v1\\:edX\\+DemoX\\+Demo_Course\\+type\\@sequential\\+block'); + expect(escapeSelector('block-v1:edX+DemoX+Demo_Course+type@sequential+block')).toEqual( + 'block-v1\\:edX\\+DemoX\\+Demo_Course\\+type\\@sequential\\+block' + ); }); }); diff --git a/common/static/js/capa/src/formula_equation_preview.js b/common/static/js/capa/src/formula_equation_preview.js index 1838bf498b..8456171972 100644 --- a/common/static/js/capa/src/formula_equation_preview.js +++ b/common/static/js/capa/src/formula_equation_preview.js @@ -1,34 +1,35 @@ function escapeSelector(id) { - // Wrapper around window.CSS.escape that uses a fallback method if CSS.escape is not available. - // This is designed to serialize a string to be used as a valid css selector. See https://drafts.csswg.org/cssom/#the-css.escape()-method - // For example, can be used with xblock and course ids, which often contain invalid characters that must be escaped - // to function properly in css selectors. - + 'use strict'; + // Wrapper around window.CSS.escape that uses a fallback method if CSS.escape is not available. This is designed to + // serialize a string to be used as a valid css selector. See + // https://drafts.csswg.org/cssom/#the-css.escape()-method For example, this can be used with xblock and course ids, + // which often contain invalid characters that must be escaped to function properly in css selectors. // TODO: if this escaping is also required elsewhere, it may be useful to add a global CSS.escape polyfill and // use that directly. - if (window.CSS && window.CSS.escape) { - return CSS.escape(id); - } else { - // CSS escape alternative borrowed from https://api.jquery.com/jQuery.escapeSelector/ source. When we upgrade to jQuery 3.0, we can use $.escapeSelector() instead of this shim escapeSelector function. - // source: https://github.com/jquery/jquery/blob/3edfa1bc/src/selector/escapeSelector.js - // CSS string/identifier serialization - // https://drafts.csswg.org/cssom/#common-serializing-idioms - var rcssescape = /([\0-\x1f\x7f]|^-?\d)|^-$|[^\x80-\uFFFF\w-]/g; - function fcssescape( ch, asCodePoint ) { - if ( asCodePoint ) { - // U+0000 NULL becomes U+FFFD REPLACEMENT CHARACTER - if ( ch === "\0" ) { - return "\uFFFD"; - } - // Control characters and (dependent upon position) numbers get escaped as code points - return ch.slice( 0, -1 ) + "\\" + ch.charCodeAt( ch.length - 1 ).toString( 16 ) + " "; + // CSS string/identifier serialization https://drafts.csswg.org/cssom/#common-serializing-idioms + // This code borrowed from https://api.jquery.com/jQuery.escapeSelector/ (source: + // https://github.com/jquery/jquery/blob/3edfa1bc/src/selector/escapeSelector.js). When we upgrade to jQuery 3.0, we + // can use $.escapeSelector() instead of this shim escapeSelector function. + var rcssescape = /([\0-\x1f\x7f]|^-?\d)|^-$|[^\x80-\uFFFF\w-]/g; // eslint-disable-line no-control-regex + function fcssescape(ch, asCodePoint) { + if (asCodePoint) { + // U+0000 NULL becomes U+FFFD REPLACEMENT CHARACTER + if (ch === '\0') { + return '\uFFFD'; } - // Other potentially-special ASCII characters get backslash-escaped - return "\\" + ch; + // Control characters and (dependent upon position) numbers get escaped as code points + return ch.slice(0, -1) + '\\' + ch.charCodeAt(ch.length - 1).toString(16) + ' '; } + // Other potentially-special ASCII characters get backslash-escaped + return '\\' + ch; + } + + if (window.CSS && window.CSS.escape) { + return window.CSS.escape(id); + } else { // ensure string and then run the replacements - return (id + "").replace(rcssescape, fcssescape); + return (id + '').replace(rcssescape, fcssescape); } }