Skip to content
Snippets Groups Projects
Commit 18c7d728 authored by Samuel Walladge's avatar Samuel Walladge
Browse files

restructure code to pass lints

parent fe06bc87
No related merge requests found
......@@ -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'
);
});
});
......
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);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment