discourse/app/assets/javascripts/discourse-common/addon/lib/attribute-hook.js
CommanderRoot 86a783b3ad
DEV: Replace deprecated String.prototype.substr() (#16233)
String.prototype.substr() is deprecated so we replace it with String.prototype.slice() which works similarily but isn't deprecated.

Signed-off-by: Tobias Speicher <rootcommander@gmail.com>
Co-authored-by: Jarek Radosz <jradosz@gmail.com>
2022-04-01 17:35:17 +02:00

43 lines
966 B
JavaScript

// FROM: https://github.com/Matt-Esch/virtual-dom
// License: MIT
function AttributeHook(namespace, value) {
if (!(this instanceof AttributeHook)) {
return new AttributeHook(namespace, value);
}
this.namespace = namespace;
this.value = value;
}
AttributeHook.prototype.hook = function (node, prop, prev) {
if (
prev &&
prev.type === "AttributeHook" &&
prev.value === this.value &&
prev.namespace === this.namespace
) {
return;
}
node.setAttributeNS(this.namespace, prop, this.value);
};
AttributeHook.prototype.unhook = function (node, prop, next) {
if (
next &&
next.type === "AttributeHook" &&
next.namespace === this.namespace
) {
return;
}
let colonPosition = prop.indexOf(":");
let localName = colonPosition > -1 ? prop.slice(colonPosition + 1) : prop;
node.removeAttributeNS(this.namespace, localName);
};
AttributeHook.prototype.type = "AttributeHook";
export default AttributeHook;