Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: strip data-test-* attributes without explicit value from production build #88

Merged
merged 4 commits into from
Apr 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions strip-test-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

let TEST_SELECTOR_PREFIX = /data-test-.*/;

function isTestSelector(attribute) {
return TEST_SELECTOR_PREFIX.test(attribute);
}

function StripTestSelectorsTransform() {
this.syntax = null;
}
Expand All @@ -14,11 +18,15 @@ StripTestSelectorsTransform.prototype.transform = function(ast) {
walker.visit(ast, function(node) {
if (node.type === 'ElementNode') {
node.attributes = node.attributes.filter(function(attribute) {
return !TEST_SELECTOR_PREFIX.test(attribute.name);
return !isTestSelector(attribute.name);
});
} else if (node.type === 'MustacheStatement' || node.type === 'BlockStatement') {
node.params = node.params.filter(function(param) {
return !isTestSelector(param.original);
});

node.hash.pairs = node.hash.pairs.filter(function(pair) {
return !TEST_SELECTOR_PREFIX.test(pair.key);
return !isTestSelector(pair.key);
});
}
});
Expand Down
11 changes: 11 additions & 0 deletions tests/dummy/app/components/print-test-attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Ember from 'ember';

const { Component } = Ember;

const component = Component.extend();

component.reopenClass({
positionalParams: 'params'
});

export default component;
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
<div class="data-test-second">{{data-test-second}}</div>
<div class="data-non-test">{{data-non-test}}</div>
<div class="data-test">{{data-test}}</div>
<div class="data-test-positional-params">{{params.length}}</div>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,30 @@ moduleForComponent('print-test-attributes', 'StripTestSelectorsTransform plugin'

if (config.stripTestSelectors) {

test('it strips data-test-* attributes from components with single positional params', function(assert) {
this.render(hbs`{{print-test-attributes data-test-should-not-be}}`);

assert.equal(this.$('.data-test-positional-params').text(), 0, 'there should be no params');
});

test('it strips data-test-* attributes from components with positional params data-test-* as first param', function(assert) {
this.render(hbs`{{print-test-attributes data-test-should-not-be "param1"}}`);

assert.equal(this.$('.data-test-positional-params').text(), 1, 'there should be only one param');
});

test('it strips data-test-* attributes from components with multiple positional params', function(assert) {
this.render(hbs`{{print-test-attributes "param1" data-test-should-not-be}}`);

assert.equal(this.$('.data-test-positional-params').text(), 1, 'there should be only one param');
});

test('it strips data-test-* attributes from components with block and multiple positional params', function(assert) {
this.render(hbs`{{#print-test-attributes "param1" data-test-should-not-be}}{{/print-test-attributes}}`);

assert.equal(this.$('.data-test-positional-params').text(), 1, 'there should be only one param');
});

test('it strips data-test-* attributes from components', function(assert) {
this.render(hbs`{{print-test-attributes data-test-first="foobar"}}`);

Expand Down