Skip to content

Commit

Permalink
fix: use CSS.escape to escape selector content
Browse files Browse the repository at this point in the history
refs #60
  • Loading branch information
fczbkk committed Sep 8, 2021
1 parent a2f10d8 commit efbbefb
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 14 deletions.
9 changes: 9 additions & 0 deletions src/utilities-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ export const SPECIAL_CHARACTERS_RE = /[ !"#$%&'()\[\]{|}<>*+,./;=?@^`~\\]/
* Escapes special characters used by CSS selector items.
*/
export function sanitizeSelectorItem (input = ''): string {
return CSS?.escape?.(input) ?? legacySanitizeSelectorItem(input)

This comment has been minimized.

Copy link
@fregante

fregante Jun 9, 2022

Contributor

?. doesn't work this way. Try missing?.prop in your console, it still causes an error. You'd have to use globalThis.missing?.prop, but then globalThis (or window) might also be undefined.

If no one complained so far, I'd suggest just dropping IE11 support because it's been broken since 3.4.3 due to this line.

}

/**
* Legacy version of escaping utility, originally used for IE11-. Should
* probably be replaced by a polyfill:
* https://github.com/mathiasbynens/CSS.escape
*/
export function legacySanitizeSelectorItem (input = ''): string {
return input.split('')
.map((character) => {
if (character === ':') {
Expand Down
4 changes: 2 additions & 2 deletions test/selector-attribute.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('selector - attribute', function () {
it('should quote attribute values', function () {
root.innerHTML = '<div aaa="bbb:ccc"></div>'
const result = getAttributeSelectors([root.firstElementChild])
assert.include(result, '[aaa=\'bbb\\3A ccc\']')
assert.include(result, '[aaa=\'bbb\\:ccc\']')
})

it('should ignore Angular attributes', function () {
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('selector - attribute', function () {
it('should escape attribute values', () => {
root.innerHTML = '<div width="30%"></div><div width="100%"></div>'
const result = getCssSelector(root.firstElementChild)
assert.deepEqual(result, "[width='30\\%']")
assert.deepEqual(result, "[width='\\33 0\\%']")
})

})
2 changes: 1 addition & 1 deletion test/selector-class.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('selector - class', function () {
root.innerHTML = '<div class="aaa:bbb"></div>'
const result = getClassSelectors([root.firstElementChild])
assert.lengthOf(result, 1)
assert.include(result, '.aaa\\3A bbb')
assert.include(result, '.aaa\\:bbb')
})

it('should ignore class names that start with a number', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/selector-id.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('selector - ID', function () {

it('should escape colon character', function () {
root.innerHTML = '<div id="aaa:bbb"></div>'
assert.deepEqual(getIdSelector([root.firstElementChild]), ['#aaa\\3A bbb'])
assert.deepEqual(getIdSelector([root.firstElementChild]), ['#aaa\\:bbb'])
})

it('should ignore ID beginning with a number', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/selector-tag.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('selector - tag', function () {
root.innerHTML = '<aaa:bbb></aaa:bbb>'
const element = root.firstElementChild
const selector = getTagSelector([element])
assert.equal(selector, 'aaa\\3A bbb')
assert.equal(selector, 'aaa\\:bbb')
assert.equal(root.querySelector(selector), element)
})

Expand Down
10 changes: 1 addition & 9 deletions test/utilities-sanitize-selector-item.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,11 @@ describe('utilities - sanitizeSelectorItem', function () {
})

it('should escape colon', function () {
assert.equal(sanitizeSelectorItem('aaa:bbb'), 'aaa\\3A bbb')
assert.equal(sanitizeSelectorItem('aaa:bbb'), 'aaa\\:bbb')
})

it('should escape special characters', function () {
assert.equal(sanitizeSelectorItem('aaa[bbb=ccc]'), 'aaa\\[bbb\\=ccc\\]')
})

it('should escape letters with accents', function () {
assert.equal(sanitizeSelectorItem('aáä'), 'a\\E1\\E4')
})

it('should escape double unicode characters', function () {
assert.equal(sanitizeSelectorItem('😀'), '\\uD83D\\uDE00')
})

})

0 comments on commit efbbefb

Please sign in to comment.