mostly-dom
Version:
A virtual-dom for TypeScript
79 lines • 3.34 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
}
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { BaseModule } from './BaseModule';
import { emptyVNode } from './emptyVNode';
export function createAttributesModule() {
return new AttributesModule();
}
var NAMESPACE_URIS = {
xlink: 'http://www.w3.org/1999/xlink',
};
var booleanAttributes = [
'allowfullscreen', 'async', 'autofocus', 'autoplay', 'checked', 'compact',
'controls', 'declare', 'default', 'defaultchecked', 'defaultmuted',
'defaultselected', 'defer', 'disabled', 'draggable', 'enabled',
'formnovalidate', 'hidden', 'indeterminate', 'inert', 'ismap', 'itemscope',
'loop', 'multiple', 'muted', 'nohref', 'noresize', 'noshade', 'novalidate',
'nowrap', 'open', 'pauseonexit', 'readonly', 'required', 'reversed', 'scoped',
'seamless', 'selected', 'sortable', 'spellcheck', 'translate', 'truespeed',
'typemustmatch', 'visible',
];
var booleanAttributeDictionary = Object.create(null);
for (var i = 0, count = booleanAttributes.length; i < count; i++)
booleanAttributeDictionary[booleanAttributes[i]] = true;
// attributes module
var AttributesModule = /** @class */ (function (_super) {
__extends(AttributesModule, _super);
function AttributesModule() {
return _super !== null && _super.apply(this, arguments) || this;
}
AttributesModule.prototype.create = function (vNode) {
updateAttributes(emptyVNode, vNode);
};
AttributesModule.prototype.update = function (formerVNode, vNode) {
updateAttributes(formerVNode, vNode);
};
return AttributesModule;
}(BaseModule));
function updateAttributes(formerVNode, vNode) {
var attributeValue;
var formerAttributeValue;
var element = vNode.element;
var formerAttributes = formerVNode.props.attrs;
var attributes = vNode.props.attrs;
var attributeParts;
if (!formerAttributes && !attributes)
return;
formerAttributes = formerAttributes || {};
attributes = attributes || {};
for (var key in attributes) {
attributeValue = attributes[key];
formerAttributeValue = formerAttributes[key];
if (formerAttributeValue !== attributeValue) {
if (!attributeValue && booleanAttributeDictionary[key])
element.removeAttribute(key);
else {
attributeParts = key.split(':');
if (attributeParts.length > 1 && NAMESPACE_URIS.hasOwnProperty(attributeParts[0]))
element.setAttributeNS(NAMESPACE_URIS[attributeParts[0]], key, attributeValue);
else
element.setAttribute(key, attributeValue);
}
}
}
for (var key in formerAttributes)
if (!(key in attributes))
element.removeAttribute(key);
}
//# sourceMappingURL=attributes.js.map