[FIX] blockdom: properly handle falsy attributes

This commit fixes some issues with falsy attributes not being properly
set/removed in various situations. Also, the behaviour was not
consistent between normal attribute (key/value) and generic attributes
(pair or object)
This commit is contained in:
Géry Debongnie
2021-12-16 14:30:09 +01:00
committed by Aaron Bohy
parent 983b9f996d
commit e2819323ee
2 changed files with 67 additions and 4 deletions
+16 -4
View File
@@ -1,6 +1,6 @@
import type { Setter } from "./block_compiler";
const { setAttribute, removeAttribute } = Element.prototype;
const { setAttribute: elemSetAttribute, removeAttribute } = Element.prototype;
const tokenList = DOMTokenList.prototype;
const tokenListAdd = tokenList.add;
const tokenListRemove = tokenList.remove;
@@ -14,11 +14,23 @@ const wordRegexp = /\s+/;
* file.
*/
function setAttribute(this: HTMLElement, key: string, value: any) {
switch (value) {
case false:
case undefined:
removeAttribute.call(this, key);
break;
case true:
elemSetAttribute.call(this, key, "");
break;
default:
elemSetAttribute.call(this, key, value);
}
}
export function createAttrUpdater(attr: string): Setter<HTMLElement> {
return function (this: HTMLElement, value: any) {
if (value !== false && value !== undefined) {
setAttribute.call(this, attr, value === true ? "" : value);
}
setAttribute.call(this, attr, value);
};
}