From ea0015db2575d0df18b428ac97403ac7c6e1a2e2 Mon Sep 17 00:00:00 2001 From: Riki Fridrich Date: Sat, 15 Feb 2020 10:47:50 +0100 Subject: [PATCH] fix: ignore class names not suitable for selectors refs #37 --- src/constants.js | 6 ++++++ src/selector-class.js | 3 ++- test/selector-class.spec.js | 7 +++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/constants.js b/src/constants.js index 7708c0c11..fb0db449e 100644 --- a/src/constants.js +++ b/src/constants.js @@ -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', diff --git a/src/selector-class.js b/src/selector-class.js index 55add245d..c32e4ffbb 100644 --- a/src/selector-class.js +++ b/src/selector-class.js @@ -1,4 +1,5 @@ import {sanitizeSelectorItem} from './utilities-selectors'; +import { INVALID_CLASS_RE } from './constants' /** * Get class selectors for an element. @@ -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)}`); } diff --git a/test/selector-class.spec.js b/test/selector-class.spec.js index 3571ea124..94a23ef49 100644 --- a/test/selector-class.spec.js +++ b/test/selector-class.spec.js @@ -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 = '
' + const result = getClassSelectors(root.firstChild) + assert.lengthOf(result, 1) + assert.include(result, '.a1'); + }) + });