Skip to content

Commit

Permalink
fix: ignore class names not suitable for selectors
Browse files Browse the repository at this point in the history
refs #37
  • Loading branch information
fczbkk committed Feb 15, 2020
1 parent 06550e4 commit ea0015d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ export const INVALID_ID_RE = new RegExp([
'^\\d', // begins with a number
].join('|'));

// RegExp that will match invalid patterns that can be used in class attribute.
export const INVALID_CLASS_RE = new RegExp([
'^$', // empty or not set
'^\\d', // begins with a number
].join('|'));

// Order in which a combined selector is constructed.
export const SELECTOR_PATTERN = [
'nthoftype',
Expand Down
3 changes: 2 additions & 1 deletion src/selector-class.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {sanitizeSelectorItem} from './utilities-selectors';
import { INVALID_CLASS_RE } from './constants'

/**
* Get class selectors for an element.
Expand All @@ -9,6 +10,6 @@ export function getClassSelectors (element) {
return (element.getAttribute('class') || '')
.trim()
.split(/\s+/)
.filter((item) => item !== '')
.filter((item) => !INVALID_CLASS_RE.test(item))
.map((item) => `.${sanitizeSelectorItem(item)}`);
}
7 changes: 7 additions & 0 deletions test/selector-class.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,11 @@ describe('selector - class', function () {
assert.include(result, '.aaa\\3A bbb');
});

it('should ignore class names that start with a number', function () {
root.innerHTML = '<div class="1 1a a1"></div>'
const result = getClassSelectors(root.firstChild)
assert.lengthOf(result, 1)
assert.include(result, '.a1');
})

});

0 comments on commit ea0015d

Please sign in to comment.