diff --git a/index.js b/index.js index 4fe22377..df329ac4 100644 --- a/index.js +++ b/index.js @@ -35,6 +35,14 @@ module.exports = { plugin: StripTestSelectorsTransform, baseDir: function() { return __dirname; } }); + } else { + var TransformTestSelectorParamsToHashPairs = require('./transform-test-selector-params-to-hash-pairs'); + + registry.add('htmlbars-ast-plugin', { + name: 'transform-test-selector-params-to-hash-pairs', + plugin: TransformTestSelectorParamsToHashPairs, + baseDir: function() { return __dirname; } + }); } } }, diff --git a/tests/acceptance/bind-data-test-attributes-in-components-test.js b/tests/acceptance/bind-data-test-attributes-in-components-test.js index 3562f661..9043c002 100644 --- a/tests/acceptance/bind-data-test-attributes-in-components-test.js +++ b/tests/acceptance/bind-data-test-attributes-in-components-test.js @@ -39,7 +39,7 @@ if (!config.stripTestSelectors) { assert.equal(find('.test4').find('div[data-non-test]').length, 0, 'data-non-test does not exists'); }); - test('it leaves data-test attributes untouched on components', function (assert) { + test('it leaves data-test attribute untouched on components', function (assert) { assert.equal(find('.test5').find('div[data-test]').length, 0, 'data-test does not exists'); }); @@ -47,5 +47,20 @@ if (!config.stripTestSelectors) { assert.equal(find('.test6').find('div[data-non-test]').length, 0, 'data-non-test does not exists'); }); -} + test('it binds data-test-* attributes with boolean values on components', function (assert) { + assert.equal(find('.test7').find('div[data-test-with-boolean-value]').length, 1, 'data-test-with-boolean-value exists'); + }); + test('it binds data-test-* attributes without values on components', function (assert) { + assert.equal(find('.test8').find('div[data-test-without-value]').length, 1, 'data-test-without-value exists'); + }); + + test('it binds data-test-* attributes without values on block components', function (assert) { + assert.equal(find('.test9').find('div[data-test-without-value]').length, 1, 'data-test-without-value exists'); + }); + + test('it leaves data-test attribute without value untouched on components', function (assert) { + assert.equal(find('.test10').find('div[data-test]').length, 0, 'data-test does not exists'); + }); + +} diff --git a/tests/dummy/app/templates/bind-test.hbs b/tests/dummy/app/templates/bind-test.hbs index 98364b18..533f59da 100644 --- a/tests/dummy/app/templates/bind-test.hbs +++ b/tests/dummy/app/templates/bind-test.hbs @@ -9,3 +9,11 @@
{{data-test-component data-test="foo"}}
{{data-test-component data-non-test="foo"}}
+ +
{{data-test-component data-test-with-boolean-value=true}}
+ +
{{data-test-component data-test-without-value}}
+ +
{{#data-test-component data-test-without-value}}foo{{/data-test-component}}
+ +
{{data-test-component data-test}}
diff --git a/transform-test-selector-params-to-hash-pairs.js b/transform-test-selector-params-to-hash-pairs.js new file mode 100644 index 00000000..4d317a47 --- /dev/null +++ b/transform-test-selector-params-to-hash-pairs.js @@ -0,0 +1,42 @@ +/* eslint-env node */ +var TEST_SELECTOR_PREFIX = /data-test-.*/; + +function TransformTestSelectorParamsToHashPairs() { + this.syntax = null; +} + +function isTestSelectorParam(param) { + return param.type === 'PathExpression' + && TEST_SELECTOR_PREFIX.test(param.original); +} + +TransformTestSelectorParamsToHashPairs.prototype.transform = function(ast) { + var b = this.syntax.builders; + var walker = new this.syntax.Walker(); + + walker.visit(ast, function(node) { + if (node.type === 'MustacheStatement' || node.type === 'BlockStatement') { + var testSelectorParams = []; + var otherParams = []; + + node.params.forEach(function(param) { + if (isTestSelectorParam(param)) { + testSelectorParams.push(param); + } else { + otherParams.push(param); + } + }); + + node.params = otherParams; + + testSelectorParams.forEach(function(param) { + var pair = b.pair(param.original, b.boolean(true)); + node.hash.pairs.push(pair); + }); + } + }); + + return ast; +}; + +module.exports = TransformTestSelectorParamsToHashPairs;