This repository was archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathattributes.js
More file actions
81 lines (70 loc) · 2.08 KB
/
attributes.js
File metadata and controls
81 lines (70 loc) · 2.08 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Resolves node attribute names: moves `default` attribute value
* from stub to real attribute.
*
* This resolver should be applied *after* resource matcher
*/
if (typeof module === 'object' && typeof define !== 'function') {
var define = function (factory) {
module.exports = factory(require, exports, module);
};
}
define(function(require, exports, module) {
var utils = require('../../utils/common');
var findDefault = function(attr) {
return attr.isDefault;
};
var findImplied = function(attr) {
return attr.isImplied;
};
var findEmpty = function(attr) {
return !attr.value;
};
function resolveDefaultAttrs(node, parser) {
node.children.forEach(function(item) {
var attrList = item.attributeList();
var defaultAttrValue = item.attribute(parser.DEFAULT_ATTR_NAME);
if (typeof defaultAttrValue !== 'undefined') {
// remove stub attribute
item.attribute(parser.DEFAULT_ATTR_NAME, null);
if (attrList.length) {
// target for default value:
// 1. default attribute
// 2. implied attribute
// 3. first empty attribute
// find attribute marked as default
var defaultAttr = utils.find(attrList, findDefault)
|| utils.find(attrList, findImplied)
|| utils.find(attrList, findEmpty);
if (defaultAttr) {
var oldVal = item.attribute(defaultAttr.name);
var newVal = utils.replaceUnescapedSymbol(oldVal, '|', defaultAttrValue);
// no replacement, e.g. default value does not contains | symbol
if (oldVal == newVal) {
newVal = defaultAttrValue
}
item.attribute(defaultAttr.name, newVal);
}
}
} else {
// if no default attribute value, remove implied attributes
attrList.forEach(function(attr) {
if (attr.isImplied) {
item.attribute(attr.name, null);
}
});
}
resolveDefaultAttrs(item, parser);
});
}
return {
/**
* @param {AbbreviationNode} tree
* @param {Object} options
* @param {abbreviation} parser
*/
preprocessor: function(tree, options, parser) {
resolveDefaultAttrs(tree, parser);
}
};
});