-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use addValueToAttribute and removeValueFromAttribute utils
- Loading branch information
Showing
4 changed files
with
104 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Adds a value to an attribute containing space-delimited values. | ||
*/ | ||
export declare function addValueToAttribute(element: HTMLElement, attr: string, value: string): void; | ||
|
||
/** | ||
* Removes a value from an attribute containing space-delimited values. | ||
*/ | ||
export declare function removeValueFromAttribute(element: HTMLElement, attr: string, value: string): void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* @param {string} value | ||
* @return {Set<string>} | ||
*/ | ||
function deserializeAttributeValue(value) { | ||
if (!value) { | ||
return new Set(); | ||
} | ||
|
||
return new Set(value.split(' ')); | ||
} | ||
|
||
/** | ||
* @param {Set<string>} values | ||
* @return {string} | ||
*/ | ||
function serializeAttributeValue(values) { | ||
return [...values].join(' '); | ||
} | ||
|
||
/** | ||
* Adds a value to an attribute containing space-delimited values. | ||
* | ||
* @param {HTMLElement} element | ||
* @param {string} attr | ||
* @param {string} value | ||
*/ | ||
export function addValueToAttribute(element, attr, value) { | ||
const values = deserializeAttributeValue(element.getAttribute(attr)); | ||
values.add(value); | ||
element.setAttribute(attr, serializeAttributeValue(values)); | ||
} | ||
|
||
/** | ||
* Removes a value from an attribute containing space-delimited values. | ||
* | ||
* @param {HTMLElement} element | ||
* @param {string} attr | ||
* @param {string} value | ||
*/ | ||
export function removeValueFromAttribute(element, attr, value) { | ||
const values = deserializeAttributeValue(element.getAttribute(attr)); | ||
values.delete(value); | ||
if (values.size === 0) { | ||
element.removeAttribute(attr); | ||
return; | ||
} | ||
element.setAttribute(attr, serializeAttributeValue(values)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { expect } from '@esm-bundle/chai'; | ||
import { addValueToAttribute, removeValueFromAttribute } from '../src/utils.js'; | ||
|
||
describe('utils', () => { | ||
describe('addValueToAttribute', () => { | ||
let element; | ||
|
||
beforeEach(() => { | ||
element = document.createElement('div'); | ||
}); | ||
|
||
it('should add a value to an attribute', () => { | ||
addValueToAttribute(element, 'aria-labelledby', 'label-id'); | ||
expect(element.getAttribute('aria-labelledby')).to.equal('label-id'); | ||
|
||
addValueToAttribute(element, 'aria-labelledby', 'error-id'); | ||
expect(element.getAttribute('aria-labelledby')).to.equal('label-id error-id'); | ||
}); | ||
|
||
it('should not duplicate values in the attribute', () => { | ||
addValueToAttribute(element, 'aria-labelledby', 'label-id'); | ||
addValueToAttribute(element, 'aria-labelledby', 'label-id'); | ||
expect(element.getAttribute('aria-labelledby')).to.equal('label-id'); | ||
}); | ||
}); | ||
|
||
describe('removeValueFromAttribute', () => { | ||
let element; | ||
|
||
beforeEach(() => { | ||
element = document.createElement('div'); | ||
element.setAttribute('aria-labelledby', 'label-id error-id'); | ||
}); | ||
|
||
it('should remove a value from an attribute', () => { | ||
removeValueFromAttribute(element, 'aria-labelledby', 'error-id'); | ||
expect(element.getAttribute('aria-labelledby')).to.equal('label-id'); | ||
|
||
removeValueFromAttribute(element, 'aria-labelledby', 'label-id'); | ||
expect(element.hasAttribute('aria-labelledby')).to.be.false; | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters