Skip to content

Commit

Permalink
feat: set list of used selector types in options
Browse files Browse the repository at this point in the history
  • Loading branch information
fczbkk committed Jan 2, 2015
1 parent 383f0a7 commit 0eb56f4
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 129 deletions.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ It should work fine in any modern browser. It has no external dependencies.
## How to use

```javascript
// first, create instance of the object
// first, create instance of the object with default options
my_selector_generator = new CssSelectorGenerator

// create (or find reference to) any element
Expand All @@ -32,6 +32,35 @@ document.body.addEventListener('click', function (event) {
});
```

## Options

You can set the options either when creating an instance, or you can set them via `setOptions()` method:

```javascript
custom_options = {selectors: ['tag', 'id', 'class']}

// set options when creating an instance
my_selector_generator = new CssSelectorGenerator(custom_options);

// or set options later
my_selector_generator.setOptions(custom_options);
```

### selectors

default: `['tag', 'id', 'class', 'nthchild']`

So far the only option you can use is list of types of selectors, that will be used when constructing unique CSS selector. You may want to adjust this list for browser compatibility.

Available values:

- `'tag'` - Tag selector. E.g. `P`, `DIV`.
- `'id'` - ID selector. E.g. `#myElement`.
- `'class'` - Class selector. It will get all class names of the element. E.g. `.myClass`, `.firstClass.secondClass`.
- `'nthchild'` - N-th child selector. It is supported from IE9 and higher, but it is necessary to create unique CSS selector for every possible element. You can remove it from the list for backwards browser compatibility, but then make sure to use IDs or class names on each element you want to target. E.g. `:nth-child(0)`
- `'attribute'` - Attribute selector. Compatible wth IE7 and higher. It will not create matching pairs for element's ID and class name attributes. This type of selector is disabled by default. E.g. `[rel=someRel]`


## Bug reports, feature requests and contact

If you found any bugs, if you have feature requests or any questions, please, either [file an issue at GitHub][1] or send me an e-mail at [[email protected]][2]
Expand Down
59 changes: 52 additions & 7 deletions build/css-selector-generator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 32 additions & 8 deletions src/css-selector-generator.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
class CssSelectorGenerator
constructor: ->

default_options:
# choose from 'tag', 'id', 'class', 'nthchild', 'attribute'
selectors: ['tag', 'id', 'class', 'nthchild']

constructor: (options = {}) ->
@options = {}
@setOptions @default_options
@setOptions options

setOptions: (options = {}) ->
for key, val of options
@options[key] = val if @default_options.hasOwnProperty key

isElement: (element) ->
!!(element?.nodeType is 1)
Expand Down Expand Up @@ -76,13 +88,25 @@ class CssSelectorGenerator
is_unique

getAllSelectors: (element) ->
{
t: @getTagSelector element # tag
i: @getIdSelector element # ID
c: @getClassSelectors element # classes
a: @getAttributeSelectors element # attributes
n: @getNthChildSelector element # n-th child
}
result = t: null, i: null, c: null, a: null, n: null

if 'tag' in @options.selectors
result.t = @getTagSelector element

if 'id' in @options.selectors
result.i = @getIdSelector element

if 'class' in @options.selectors
result.c = @getClassSelectors element

if 'attribute' in @options.selectors
result.a = @getAttributeSelector element

if 'nthchild' in @options.selectors
result.n = @getNthChildSelector element

result


testUniqueness: (element, selector) ->
parent = element.parentNode
Expand Down
Loading

0 comments on commit 0eb56f4

Please sign in to comment.