Improve compliance of DOMTokenList

1 - Make element.classList settable
2 - On replace, validate in expected order
3 - On replace, fire mutation observer even if new == old
4 - On replace, handle duplicate values
This commit is contained in:
Karl Seguin
2026-02-12 14:07:31 +08:00
parent 5d8739bfb2
commit b139c05960
3 changed files with 110 additions and 13 deletions

View File

@@ -93,6 +93,29 @@
}
</script>
<script id=replace_errors>
{
const div = document.createElement('div');
div.className = 'foo bar';
testing.withError((err) => {
testing.expectEqual('SyntaxError', err.name);
}, () => div.classList.replace('', 'baz'));
testing.withError((err) => {
testing.expectEqual('SyntaxError', err.name);
}, () => div.classList.replace('foo', ''));
testing.withError((err) => {
testing.expectEqual('InvalidCharacterError', err.name);
}, () => div.classList.replace('foo bar', 'baz'));
testing.withError((err) => {
testing.expectEqual('InvalidCharacterError', err.name);
}, () => div.classList.replace('foo', 'bar baz'));
}
</script>
<script id=item>
{
const div = document.createElement('div');
@@ -166,6 +189,29 @@
}
</script>
<script id=classList_assignment>
{
const div = document.createElement('div');
// Direct assignment should work (equivalent to classList.value = ...)
div.classList = 'foo bar baz';
testing.expectEqual('foo bar baz', div.className);
testing.expectEqual(3, div.classList.length);
testing.expectEqual(true, div.classList.contains('foo'));
// Assigning again should replace
div.classList = 'qux';
testing.expectEqual('qux', div.className);
testing.expectEqual(1, div.classList.length);
testing.expectEqual(false, div.classList.contains('foo'));
// Empty assignment
div.classList = '';
testing.expectEqual('', div.className);
testing.expectEqual(0, div.classList.length);
}
</script>
<script id=errors>
{
const div = document.createElement('div');