-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathjsonml-utils.js
More file actions
249 lines (202 loc) · 5.26 KB
/
Copy pathjsonml-utils.js
File metadata and controls
249 lines (202 loc) · 5.26 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
jsonml-utils.js
JsonML manipulation methods
Created: 2006-11-09-0116
Modified: 2012-11-03-2051
Copyright (c)2006-2012 Stephen M. McKamey
Distributed under The MIT License: http://jsonml.org/license
This file ensures a global JsonML object adding these utility methods for manipulating JsonML elements:
// tests if a given object is a valid JsonML element
bool JsonML.isElement(jml);
// gets the name of a JsonML element
string JsonML.getTagName(jml);
// tests if a given object is a JsonML attributes collection
bool JsonML.isAttributes(jml);
// tests if a JsonML element has a JsonML attributes collection
bool JsonML.hasAttributes(jml);
// gets the attributes collection for a JsonML element
object JsonML.getAttributes(jml);
// sets multiple attributes for a JsonML element
void JsonML.addAttributes(jml, attr);
// gets a single attribute for a JsonML element
object JsonML.getAttribute(jml, key);
// sets a single attribute for a JsonML element
void JsonML.setAttribute(jml, key, value);
// appends a JsonML child node to a parent JsonML element
void JsonML.appendChild(parent, child);
// gets an array of the child nodes of a JsonML element
array JsonML.getChildren(jml);
*/
var JsonML = JsonML || {};
if (typeof module === 'object') {
module.exports = JsonML;
}
(function(JsonML) {
'use strict';
/* Utility Methods -------------------------*/
/**
* Determines if the value is an Array
*
* @private
* @param {*} val the object being tested
* @return {boolean}
*/
var isArray = Array.isArray || function(val) {
return (val instanceof Array);
};
/**
* @param {*} jml
* @return {boolean}
*/
JsonML.isFragment = function(jml) {
return isArray(jml) && (jml[0] === '');
};
/**
* @param {*} jml
* @return {string}
*/
JsonML.getTagName = function(jml) {
return jml[0] || '';
};
/**
* @param {*} jml
* @return {boolean}
*/
var isElement = JsonML.isElement = function(jml) {
return isArray(jml) && ('string' === typeof jml[0]);
};
/**
* @param {*} jml
* @return {boolean}
*/
var isAttributes = JsonML.isAttributes = function(jml) {
return !!jml && ('object' === typeof jml) && !isArray(jml);
};
/**
* @param {*} jml
* @return {boolean}
*/
var hasAttributes = JsonML.hasAttributes = function(jml) {
if (!isElement(jml)) {
throw new SyntaxError('invalid JsonML');
}
return isAttributes(jml[1]);
};
/**
* @param {*} jml
* @param {boolean} addIfMissing
* @return {object}
*/
var getAttributes = JsonML.getAttributes = function(jml, addIfMissing) {
if (hasAttributes(jml)) {
return jml[1];
}
if (!addIfMissing) {
return undefined;
}
// need to add an attribute object
var name = jml.shift();
var attr = {};
jml.unshift(attr);
jml.unshift(name||'');
return attr;
};
/**
* @param {*} jml
* @param {object} attr
*/
var addAttributes = JsonML.addAttributes = function(jml, attr) {
if (!isElement(jml) || !isAttributes(attr)) {
throw new SyntaxError('invalid JsonML');
}
if (!isAttributes(jml[1])) {
// just insert attributes
var name = jml.shift();
jml.unshift(attr);
jml.unshift(name||'');
return;
}
// merge attribute objects
var old = jml[1];
for (var key in attr) {
if (attr.hasOwnProperty(key)) {
old[key] = attr[key];
}
}
};
/**
* @param {*} jml
* @param {string} key
* @return {string|number|boolean}
*/
JsonML.getAttribute = function(jml, key) {
if (!hasAttributes(jml)) {
return undefined;
}
return jml[1][key];
};
/**
* @param {*} jml
* @param {string} key
* @param {string|number|boolean} value
*/
JsonML.setAttribute = function(jml, key, value) {
getAttributes(jml, true)[key] = value;
};
/**
* @param {*} jml
* @param {array|object|string} child
*/
var appendChild = JsonML.appendChild = function(parent, child) {
if (!isArray(parent)) {
throw new SyntaxError('invalid JsonML');
}
if (isArray(child) && child[0] === '') {
// result was multiple JsonML sub-trees (i.e. documentFragment)
child.shift();// remove fragment ident
// directly append children
while (child.length) {
appendChild(parent, child.shift(), arguments[2]);
}
} else if (child && 'object' === typeof child) {
if (isArray(child)) {
if (!isElement(child)) {
throw new SyntaxError('invalid JsonML');
}
if (typeof arguments[2] === 'function') {
// onAppend callback for JBST use
(arguments[2])(parent, child);
}
// result was a JsonML node
parent.push(child);
} else if (JsonML.isRaw(child)) {
// result was a JsonML node
parent.push(child);
} else {
// result was JsonML attributes
addAttributes(parent, child);
}
} else if ('undefined' !== typeof child && child !== null) {
// must convert to string or JsonML will discard
child = String(child);
// skip processing empty string literals
if (child && parent.length > 1 && 'string' === typeof parent[parent.length-1]) {
// combine strings
parent[parent.length-1] += child;
} else if (child || !parent.length) {
// append
parent.push(child);
}
}
};
/**
* @param {*} jml
* @return {array}
*/
JsonML.getChildren = function(jml) {
if (hasAttributes(jml)) {
return jml.slice(2);
}
return jml.slice(1);
};
})(JsonML);