-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from swup/feat/attributes
Update additional body attributes
- Loading branch information
Showing
5 changed files
with
136 additions
and
7 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
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,29 @@ | ||
export function updateAttributes( | ||
target: Element, | ||
source: Element, | ||
filters: (string | RegExp)[] = [] | ||
): void { | ||
const keep = new Set<string>(); | ||
|
||
for (const { name, value } of getAttributes(source, filters)) { | ||
target.setAttribute(name, value); | ||
keep.add(name); | ||
} | ||
|
||
for (const { name } of getAttributes(target, filters)) { | ||
if (!keep.has(name)) { | ||
target.removeAttribute(name); | ||
} | ||
} | ||
} | ||
|
||
function getAttributes(el: Element, filters: (string | RegExp)[] = []): Attr[] { | ||
const all = Array.from(el.attributes); | ||
if (!filters.length) return all; | ||
|
||
return all.filter(({ name }) => | ||
filters.some((pattern) => | ||
pattern instanceof RegExp ? pattern.test(name) : name === pattern | ||
) | ||
); | ||
} |
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
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,72 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { updateAttributes } from '../../src/attributes.js'; | ||
|
||
function createElement(html: string): Element { | ||
const el = document.createElement('div'); | ||
el.innerHTML = html; | ||
return el.firstElementChild!; | ||
}; | ||
|
||
const mergeAttributes = (currentEl: string, incomingEl: string, ...args): string => { | ||
const current = createElement(currentEl); | ||
const incoming = createElement(incomingEl); | ||
updateAttributes(current, incoming, ...args); | ||
return current.outerHTML; | ||
}; | ||
|
||
describe('updateAttributes', () => { | ||
describe('attributes', () => { | ||
it('adds attributes', () => { | ||
expect(mergeAttributes('<div></div>', '<div a="b"></div>')).toBe('<div a="b"></div>'); | ||
expect(mergeAttributes('<div></div>', '<div a="b" b="c"></div>')).toBe('<div a="b" b="c"></div>'); | ||
}); | ||
|
||
it('updates attributes', () => { | ||
expect(mergeAttributes('<div a="b" b="c"></div>', '<div a="c" b="c"></div>')).toBe('<div a="c" b="c"></div>'); | ||
}); | ||
|
||
it('removes attributes', () => { | ||
expect(mergeAttributes('<div a="b"></div>', '<div></div>')).toBe('<div></div>'); | ||
expect(mergeAttributes('<div a="b" b="c"></div>', '<div a="b"></div>')).toBe('<div a="b"></div>'); | ||
}); | ||
|
||
it('allows filtering attributes', () => { | ||
expect(mergeAttributes('<div></div>', '<div a="b"></div>', [''])).toBe('<div></div>'); | ||
expect(mergeAttributes('<div></div>', '<div a="b"></div>', ['b'])).toBe('<div></div>'); | ||
expect(mergeAttributes('<div></div>', '<div b="c"></div>', ['a'])).toBe('<div></div>'); | ||
expect(mergeAttributes('<div></div>', '<div a="b" b="c"></div>', ['a'])).toBe('<div a="b"></div>'); | ||
expect(mergeAttributes('<div></div>', '<div a="b" b="c"></div>', ['b'])).toBe('<div b="c"></div>'); | ||
expect(mergeAttributes('<div></div>', '<div a="b" b="c"></div>', ['a', 'b'])).toBe('<div a="b" b="c"></div>'); | ||
expect(mergeAttributes('<div></div>', '<div a="b" abc="d" bcd="e"></div>', [/^ab/])).toBe('<div abc="d"></div>'); | ||
}); | ||
|
||
it('handles boolean attributes', () => { | ||
expect(mergeAttributes('<div disabled></div>', '<div hidden></div>')).toBe('<div hidden=""></div>'); | ||
expect(mergeAttributes('<div></div>', '<div disabled></div>')).toBe('<div disabled=""></div>'); | ||
}); | ||
}); | ||
|
||
describe('types', () => { | ||
const el = document.createElement('div'); | ||
|
||
it('returns nothing', () => { | ||
expect(updateAttributes(el, el)).toBeUndefined(); | ||
}); | ||
|
||
it('only accepts elements', () => { | ||
try { | ||
updateAttributes(el, el); | ||
// @ts-expect-error | ||
updateAttributes('', el); | ||
// @ts-expect-error | ||
updateAttributes(el, ''); | ||
// @ts-expect-error | ||
updateAttributes(el, 1); | ||
// @ts-expect-error | ||
updateAttributes(el, []); | ||
// @ts-expect-error | ||
updateAttributes(el); | ||
} catch (error) {} | ||
}); | ||
}); | ||
}); |
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