-
Notifications
You must be signed in to change notification settings - Fork 20
/
create-attribute.js
69 lines (61 loc) · 1.97 KB
/
create-attribute.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var escape = require('escape-html');
var propConfig = require('./property-config');
var types = propConfig.attributeTypes;
var properties = propConfig.properties;
var attributeNames = propConfig.attributeNames;
var prefixAttribute = memoizeString(function (name) {
return escape(name) + '="';
});
module.exports = createAttribute;
/**
* Create attribute string.
*
* @param {String} name The name of the property or attribute
* @param {*} value The value
* @param {Boolean} [isAttribute] Denotes whether `name` is an attribute.
* @return {?String} Attribute string || null if not a valid property or custom attribute.
*/
function createAttribute(name, value, isAttribute) {
if (properties.hasOwnProperty(name)) {
if (shouldSkip(name, value)) return '';
name = (attributeNames[name] || name).toLowerCase();
var attrType = properties[name];
// for BOOLEAN `value` only has to be truthy
// for OVERLOADED_BOOLEAN `value` has to be === true
if ((attrType === types.BOOLEAN) ||
(attrType === types.OVERLOADED_BOOLEAN && value === true)) {
return escape(name);
}
return prefixAttribute(name) + escape(value) + '"';
} else if (isAttribute) {
if (value == null) return '';
return prefixAttribute(name) + escape(value) + '"';
}
// return null if `name` is neither a valid property nor an attribute
return null;
}
/**
* Should skip false boolean attributes.
*/
function shouldSkip(name, value) {
var attrType = properties[name];
return value == null ||
(attrType === types.BOOLEAN && !value) ||
(attrType === types.OVERLOADED_BOOLEAN && value === false);
}
/**
* Memoizes the return value of a function that accepts one string argument.
*
* @param {function} callback
* @return {function}
*/
function memoizeString(callback) {
var cache = {};
return function(string) {
if (cache.hasOwnProperty(string)) {
return cache[string];
} else {
return cache[string] = callback.call(this, string);
}
};
}