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

remove all non ASCI letters, numbers or hyphen, underscore, dot from id #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 10 additions & 10 deletions addon/components/fm-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ export default Ember.Component.extend({
if(!this.get('optionLabelPath')) {
this.set('optionLabelPath', 'content.label');
}
var dataAttributes = Object.keys(this.get('attrs')).filter(function(attr) {
return /data-/.test(attr);
});
this.set('dataAttributes', dataAttributes);
if(!Ember.isEmpty(this.get('attrs'))) {
var dataAttributes = Object.keys(this.get('attrs')).filter(function(attr) {
return /data-/.test(attr);
});
this.set('dataAttributes', dataAttributes);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Necessary cause unit tests fails since this.get('attrs') was null and Object.keys(null) throws.


this._super(arguments);
},
Expand Down Expand Up @@ -52,12 +54,10 @@ export default Ember.Component.extend({
}
}),
generateSafeId: function(id) {
var tmp = document.createElement("DIV");
tmp.innerHTML = id;
id = tmp.textContent || tmp.innerText || "";
id = id.replace(/[\.,\/#!$%\^&\*;:{}=\`'"~()]/g,"");
id = id.replace(/\s/g, "-");
return id;
// only allow ASCII letters and digits, '_', '-', '.'
// as recommended in MDN
// https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id
return id.replace(/\s/g, '-').replace(/[^\w-.]/g, '');
},

actions: {
Expand Down
51 changes: 51 additions & 0 deletions tests/integration/components/fm-field-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,54 @@ test('errors are shown after user interaction but not before (select)', function
'errorClass is removed when errors empty got empty'
);
});

test('inputId property is assigned as id to input and for on label and drops non valid characters', function(assert) {
const id = 'az_AZ-01.aZ';
this.set('inputId', id);
this.set('label', 'some content');
this.render(hbs `{{fm-field inputId=inputId label=label}}`);
assert.equal(
this.$('input').attr('id'),
id,
'inputId set as id of input element'
);
assert.equal(
this.$('label').attr('for'),
id,
'inputId set as for attr of label element'
);
});

test('label is used as id if inputId is not set', function(assert) {
const id = 'az_AZ-01.aZ';
this.set('label', id);
this.render(hbs `{{fm-field label=label}}`);
assert.equal(
this.$('input').attr('id'),
id,
'normalized label text used as id of input element'
);
assert.equal(
this.$('label').attr('for'),
id,
'normalized label text used as for attr of label element'
);
});


test('label text used as id is normalized', function(assert) {
const label = 'A custom label?';
const id = 'A-custom-label';
this.set('label', label);
this.render(hbs `{{fm-field label=label}}`);
assert.equal(
this.$('input').attr('id'),
id,
'normalized label text used as id of input element'
);
assert.equal(
this.$('label').attr('for'),
id,
'normalized label text used as for attr of label element'
);
});
34 changes: 34 additions & 0 deletions tests/unit/components/fm-field-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { moduleForComponent, test } from 'ember-qunit';

moduleForComponent('fm-field', 'Unit | Component | fm field', {
unit: true
});

test('generate safe id', function(assert) {
const component = this.subject();
assert.equal(
component.generateSafeId('abcABC'),
'abcABC',
'ASCII letters aren\'t removed'
);
assert.equal(
component.generateSafeId('123'),
'123',
'numbers aren\'t removed'
);
assert.equal(
component.generateSafeId('_-.'),
'_-.',
'underscore, hyphen and dot aren\'t removed'
);
assert.equal(
component.generateSafeId('a b c'),
'a-b-c',
'whitespaces and tabs are replaced by hyphen'
);
assert.equal(
component.generateSafeId('a?b!c"d#eäfßg'),
'abcdefg',
'non ASCII characters are removed'
);
});