Skip to content

Commit

Permalink
fix: correctly apply list of selector types when generating unique se…
Browse files Browse the repository at this point in the history
…lector

refs #8
  • Loading branch information
fczbkk committed Nov 18, 2015
1 parent f3fc971 commit 19ab55d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
26 changes: 4 additions & 22 deletions src/css-selector-generator.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,6 @@ class CssSelectorGenerator

return characters.join ''

console.log 'item', characters


###
// `\r` is already tokenized away at this point by the HTML parser.
// `:` can be escaped as `\:`, but that fails in IE < 8.
if (/[\t\n\v\f:]/.test(character)) {
value = '\\' + charCode.toString(16).toUpperCase() + ' ';
} else if (/[ !"#$%&'()*+,./;<=>?@\[\\\]^`{|}~]/.test(character)) {
value = '\\' + character;
} else {
value = character;
}
###

escape item
.replace /\%/g, '\\'
# special characters *+-./
.replace /\*\+\-\.\//g, '\\$&'

validateId: (id) ->
# ID must exist
Expand Down Expand Up @@ -157,16 +138,17 @@ class CssSelectorGenerator
return selectors.i if selectors.i?

# tag selector (should return unique for BODY)
return selectors.t if @testUniqueness element, selectors.t
if selectors.t?
return selectors.t if @testUniqueness element, selectors.t

# attribute selector
if selectors.a isnt null and selectors.a.length isnt 0
if selectors.a? and selectors.a.length isnt 0
for item in selectors.a
return item if @testUniqueness element, item

# TODO check each class separately
# class selector
if selectors.c.length isnt 0
if selectors.c? and selectors.c.length isnt 0
all_classes = selectors.c.join ''

# class selector without tag
Expand Down
11 changes: 11 additions & 0 deletions test/src/css-selector-generator.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ describe 'CSS Selector Generator', ->
x.setOptions selectors: ['attribute']
expect(x.options.selectors).toEqual ['attribute']

it 'should not use selectors that are not allowed', ->
root.innerHTML = '<a></a><a></a>'
x.setOptions selectors: ['tag']
expect(x.getSelector root.firstChild).toEqual null


describe 'selectors', ->

Expand Down Expand Up @@ -170,6 +175,12 @@ describe 'CSS Selector Generator', ->
expect(result.length).toBe 3
expect(x.getClassSelectors root).toEqual []

it 'should use attribute selector when enabled', ->
x.setOptions selectors: ['tag', 'id', 'class', 'nthchild', 'attribute']
root.innerHTML = '<a rel="aaa"></a><a rel="bbb"></a>'
result = x.getSelector root.firstChild
expect(result).toEqual '[rel=aaa]'

describe 'n-th child', ->

it 'should get n-th child selector for an element', ->
Expand Down

0 comments on commit 19ab55d

Please sign in to comment.