From fb177881037389aeb740bd6ccd164ee8ff6f5e45 Mon Sep 17 00:00:00 2001 From: Eric Kelly Date: Sun, 22 Jan 2017 22:53:09 -0800 Subject: [PATCH] Add support for component data-test-* attributes without values This adds support for `{{my-component data-test-foo}}`. Basically, this adds a handlebars transform that converts the previous example to `{{my-component data-test-foo=true}}`. --- index.js | 8 ++++ ...data-test-attributes-in-components-test.js | 5 ++- tests/dummy/app/templates/bind-test.hbs | 2 + ...form-test-selector-params-to-hash-pairs.js | 42 +++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 transform-test-selector-params-to-hash-pairs.js 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..43abddf1 100644 --- a/tests/acceptance/bind-data-test-attributes-in-components-test.js +++ b/tests/acceptance/bind-data-test-attributes-in-components-test.js @@ -47,5 +47,8 @@ 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 without values on components', function (assert) { + assert.equal(find('.test7').find('div[data-test-without-value]').length, 1, 'data-test-without-value exists'); + }); +} diff --git a/tests/dummy/app/templates/bind-test.hbs b/tests/dummy/app/templates/bind-test.hbs index 98364b18..90aa2493 100644 --- a/tests/dummy/app/templates/bind-test.hbs +++ b/tests/dummy/app/templates/bind-test.hbs @@ -9,3 +9,5 @@
{{data-test-component data-test="foo"}}
{{data-test-component data-non-test="foo"}}
+ +
{{data-test-component data-test-without-value}}
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..ed8568a4 --- /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 traverse = this.syntax.traverse; + + traverse(ast, { + MustacheStatement: function(node) { + 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;