Skip to content

Commit

Permalink
Add support for a defaultTagName
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 17, 2018
1 parent 89d1018 commit efbb741
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ var dot = '.'.charCodeAt(0)
var hash = '#'.charCodeAt(0)

/* Parse a simple CSS selector into a HAST node. */
function parse(selector) {
function parse(selector, defaultTagName) {
var value = selector || ''
var name = 'div'
var name = defaultTagName || 'div'
var props = {}
var index = -1
var length = value.length
Expand Down
7 changes: 6 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Yields:

## API

### `parseSelector([selector])`
### `parseSelector([selector][, defaultTagName])`

Parse a CSS `selector` to a [HAST][] node.

Expand All @@ -39,6 +39,11 @@ Parse a CSS `selector` to a [HAST][] node.
and an ID (`#baz`). Multiple classes are allowed. Uses the last ID if
multiple IDs are found.

###### `defaultTagName`

`string`, optional, defaults to `div` — Tag name to use if `selector` does not
specify one.

###### Returns

[`Node`][hast].
Expand Down
22 changes: 22 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ test('parseSelector()', function(t) {
'should return an element with a tag-name when given a tag-name'
)

t.deepEqual(
parseSelector(null, 'g'),
{
type: 'element',
tagName: 'g',
properties: {},
children: []
},
'should return an `defaultTagName` if no tag name is defined in `selector` (#1)'
)

t.deepEqual(
parseSelector('#id', 'g'),
{
type: 'element',
tagName: 'g',
properties: {id: 'id'},
children: []
},
'should return an `defaultTagName` if no tag name is defined in `selector` (#2)'
)

t.deepEqual(
parseSelector('.bar'),
{
Expand Down

0 comments on commit efbb741

Please sign in to comment.