-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parser): add optional argument iterator
The iterator is useful in building a customized output.
- Loading branch information
1 parent
b3312d1
commit a3deea8
Showing
3 changed files
with
48 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,36 @@ | ||
var parse = require('css/lib/parse'); | ||
|
||
/** | ||
* Parses inline style to object (server). | ||
* Parses inline style. | ||
* | ||
* Example: 'color:red' => { color: 'red' } | ||
* | ||
* @param {String} style | ||
* @return {Object|null} | ||
* @param {String} style - The inline style. | ||
* @param {Function} [iterator] - The iterator function. | ||
* @return {null|Object} | ||
*/ | ||
module.exports = function parseInlineStyleServer(style) { | ||
module.exports = function parseInlineStyle(style, iterator) { | ||
if (!style || typeof style !== 'string') return null; | ||
|
||
// make sure to wrap declarations in placeholder | ||
var declarations = parse('p{' + style + '}').stylesheet.rules[0].declarations; | ||
var output = {}; | ||
var declaration, property, value; | ||
|
||
declarations.forEach(function(declaration) { | ||
var value = declaration.value; | ||
if (value) { | ||
output[declaration.property] = value; | ||
var output = null; | ||
var hasIterator = typeof iterator === 'function'; | ||
|
||
for (var i = 0, len = declarations.length; i < len; i++) { | ||
declaration = declarations[i]; | ||
property = declaration.property; | ||
value = declaration.value; | ||
|
||
if (hasIterator) { | ||
iterator(property, value, declaration); | ||
} else if (value) { | ||
output || (output = {}); | ||
output[property] = value; | ||
} | ||
}); | ||
} | ||
|
||
return output; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,45 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const css = require('css'); | ||
const cases = require('./cases'); | ||
const parser = require('../') | ||
|
||
describe('parser', () => { | ||
cases.default.forEach(({ style, expected }) => { | ||
|
||
// error case | ||
describe(`when style=\`${style}\``, () => { | ||
if (expected === Error) { | ||
it('throws error', () => { | ||
assert.throws(() => parser(style), Error); | ||
}); | ||
|
||
} else { | ||
it(`returns ${JSON.stringify(expected)}`, () => { | ||
assert.deepEqual(parser(style), expected); | ||
}); | ||
return; | ||
} | ||
|
||
// normal case | ||
it(`returns ${JSON.stringify(expected)}`, () => { | ||
assert.deepEqual(parser(style), expected); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when iterator is passed', () => { | ||
it('returns null', () => { | ||
const style = cases.styles[0]; | ||
const iterator = () => {}; | ||
assert.equal(parser(style, iterator), null); | ||
}); | ||
|
||
it('passes the arguments: name, value, declaration', () => { | ||
const style = 'color: #f00;'; | ||
parser(style, (name, value, declaration) => { | ||
assert.equal(name, 'color'); | ||
assert.equal(value, '#f00'); | ||
assert.deepEqual( | ||
declaration, | ||
css.parse(`p{${style}}`).stylesheet.rules[0].declarations[0] | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |