Skip to content

Commit

Permalink
Generate HTML4 compliant ids (#637)
Browse files Browse the repository at this point in the history
* Generate HTML4 compliant ids

* Add CHANGELOG entry
  • Loading branch information
timroes authored Sep 28, 2018
1 parent 82823b7 commit bf85ffe
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ No public interface changes since `4.3.0`.

- Added a new `colorPalette` service for retrieving and generating color arrays for use in charts ([#1209](https://github.com/elastic/eui/pull/1209))
- Added `1` as a valid value for the `columns` prop in `EuiFlexGrid` ([#1210](https://github.com/elastic/eui/pull/1210))
- Make `htmlIdGenerator` only return valid HTML4 ids ([#637](https://github.com/elastic/eui/pull/637))
- Use `cursor: pointer` to indicate clickable `EuiTable` rows ([#1213](https://github.com/elastic/eui/pull/1213))
- Add `lockOpen` icon ([#1215](https://github.com/elastic/eui/pull/1215))

Expand Down
5 changes: 3 additions & 2 deletions src/services/accessibility/html_id_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import uuid from 'uuid';
* This function returns a function to generate ids.
* This can be used to generate unique, but predictable ids to pair labels
* with their inputs. It takes an optional prefix as a parameter. If you don't
* specify it, it generates a random id prefix.
* specify it, it generates a random id prefix. If you specify a custom prefix
* it should begin with an letter to be HTML4 compliant.
*/
export function htmlIdGenerator(idPrefix) {
const prefix = idPrefix || uuid.v1();
const prefix = idPrefix || `i${uuid.v1()}`;
return (suffix) => `${prefix}_${suffix || uuid.v1()}`;
}
4 changes: 4 additions & 0 deletions src/services/accessibility/html_id_generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ describe('htmlIdGenerator', () => {
expect(idGenerator1('foo')).not.toBe(idGenerator2('foo'));
});

it('should generate ids beginning with "i" when not passing a prefix', () => {
expect(htmlIdGenerator()()).toMatch(/^i/);
});

it('should generate different ids if no suffix is passed', () => {
const generator = htmlIdGenerator();
expect(generator()).not.toBe(generator());
Expand Down

0 comments on commit bf85ffe

Please sign in to comment.