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

Generate HTML4 compliant ids #637

Merged
merged 3 commits into from
Sep 28, 2018
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
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