1 |
|
2 |
|
3 |
|
4 |
|
5 | var reUnescapedHtml = /[&<>"]/g
|
6 | , reHasUnescapedHtml = RegExp(reUnescapedHtml.source)
|
7 | , htmlEscapes = {
|
8 | '&': '&',
|
9 | '<': '<',
|
10 | '>': '>',
|
11 | '"': '"'
|
12 | }
|
13 | ;
|
14 |
|
15 | exports.escape = function escape(string) {
|
16 | var result = String(string);
|
17 | return (result && reHasUnescapedHtml.test(result))
|
18 | ? result.replace(reUnescapedHtml, function replacement(chr) { return htmlEscapes[chr]; })
|
19 | : result;
|
20 | };
|
21 |
|
22 |
|
23 | exports.condenseArray = function condenseArray(items) {
|
24 | var currentTag
|
25 | , newTag
|
26 | , currentTagCompressed = null
|
27 | , output = []
|
28 | ;
|
29 |
|
30 | items.forEach(function eachItem(item) {
|
31 | if (typeof item === 'string') {
|
32 | if (currentTag === '$text') {
|
33 | currentTagCompressed += item;
|
34 | } else {
|
35 | if (currentTagCompressed !== null) output.push(currentTagCompressed);
|
36 | currentTagCompressed = item;
|
37 | currentTag = '$text';
|
38 | }
|
39 | } else {
|
40 | newTag = item.$name;
|
41 | if (newTag === currentTag) {
|
42 | currentTagCompressed.push(item);
|
43 | } else {
|
44 | if (currentTagCompressed !== null) output.push(currentTagCompressed);
|
45 | currentTagCompressed = [ item ];
|
46 | currentTag = newTag;
|
47 | }
|
48 | }
|
49 | });
|
50 | if (currentTagCompressed !== null) output.push(currentTagCompressed);
|
51 | return output;
|
52 | };
|
53 |
|
54 | function shallowClone(obj) {
|
55 | var output = {}
|
56 | , key
|
57 | ;
|
58 |
|
59 | if (obj === null || typeof obj !== 'object') return obj;
|
60 | for (key in obj) {
|
61 | if (obj.hasOwnProperty(key)) {
|
62 | output[key] = obj[key];
|
63 | }
|
64 | }
|
65 | return output;
|
66 | }
|
67 |
|
68 | exports.simplifyNode = function simplifyNode(node, dropName, keepArrays) {
|
69 | var output = {}
|
70 | , keys
|
71 | ;
|
72 |
|
73 | if (!exports.isSomething(node)) return null;
|
74 | if (node === null || typeof node !== 'object') return shallowClone(node);
|
75 | if (node.constructor === Array) return (!keepArrays && node.length === 1) ? node[0] : node;
|
76 |
|
77 | keys = Object.keys(node);
|
78 | keys.forEach(function eachKey(key) {
|
79 | var value = node[key]
|
80 | , markup
|
81 | ;
|
82 |
|
83 | if (key === '$markup') {
|
84 | markup = simplifyAll(value);
|
85 | if (markup.length > 0) output.$markup = markup;
|
86 | } else if (!(dropName && key === '$name') && exports.isSomething(value)) {
|
87 | if (!keepArrays && value.constructor === Array && value.length === 1) {
|
88 | output[key] = value[0];
|
89 | } else {
|
90 | output[key] = value;
|
91 | }
|
92 | }
|
93 | });
|
94 |
|
95 | keys = Object.keys(output);
|
96 | if (keys.length === 1 && keys[0] !== '$name') {
|
97 | output = shallowClone(output[keys[0]]);
|
98 | } else if (keys.length === 2 && !dropName && output.hasOwnProperty('$attrs')) {
|
99 | output = shallowClone(output.$attrs);
|
100 | output.$name = node.$name;
|
101 | }
|
102 | return output;
|
103 | };
|
104 |
|
105 | exports.shouldObjectifyMarkup = function shouldObjectifyMarkup(items) {
|
106 | var currentTag
|
107 | , foundTags = {}
|
108 | , shouldObjectify = true
|
109 | ;
|
110 |
|
111 | items.every(function eachIem(item) {
|
112 | if (typeof item === 'string') {
|
113 | currentTag = '$text';
|
114 | } else {
|
115 | currentTag = item[0].$name;
|
116 | }
|
117 |
|
118 | if (foundTags[currentTag]) {
|
119 | shouldObjectify = false;
|
120 | return false;
|
121 | }
|
122 | foundTags[currentTag] = true;
|
123 | return true;
|
124 | });
|
125 | return shouldObjectify;
|
126 | };
|
127 |
|
128 | exports.moosh = function moosh(item1, item2, useArrays) {
|
129 | if (item1 === undefined) {
|
130 | if (item2 !== null
|
131 | && typeof item2 === 'object'
|
132 | && item2.constructor === Array
|
133 | && item2.length === 1
|
134 | ) {
|
135 | return useArrays ? item2 : item2[0];
|
136 | }
|
137 | return useArrays ? [ item2 ] : item2;
|
138 | }
|
139 |
|
140 | if (item2 === undefined) {
|
141 | if (item1 !== null
|
142 | && typeof item1 === 'object'
|
143 | && item1.constructor === Array
|
144 | && item1.length === 1
|
145 | ) {
|
146 | return useArrays ? item1 : item1[0];
|
147 | }
|
148 | return useArrays ? [ item1 ] : item1;
|
149 | }
|
150 |
|
151 | if (item1 !== null && typeof item1 === 'object' && item1.constructor === Array) {
|
152 | if (item2 !== null && typeof item2 === 'object' && item2.constructor === Array) {
|
153 | return item1.concat(item2);
|
154 | }
|
155 | item1.push(item2);
|
156 | return item1;
|
157 | }
|
158 |
|
159 | if (item2 !== null && typeof item2 === 'object' && item2.constructor === Array) {
|
160 | return [ item1 ].concat(item2);
|
161 | }
|
162 |
|
163 | return [ item1, item2 ];
|
164 | };
|
165 |
|
166 | function simplifyAll(input, dropName) {
|
167 | var output = [];
|
168 | if (input !== null && typeof input === 'object' && input.constructor === Array) {
|
169 | input.forEach(function eachItem(item) {
|
170 | output.push(exports.simplifyNode(item, dropName));
|
171 | });
|
172 | return output;
|
173 | }
|
174 | return exports.simplifyNode(input, dropName);
|
175 | }
|
176 |
|
177 | exports.objectifyMarkup = function objectifyMarkup(node, keepArrays) {
|
178 | var key
|
179 | , output = {}
|
180 | ;
|
181 |
|
182 | Object.keys(node).forEach(function eachKey(nodeKey) {
|
183 | if (nodeKey !== '$markup') {
|
184 | output[nodeKey] = node[nodeKey];
|
185 | }
|
186 | });
|
187 |
|
188 | if (node.$markup) {
|
189 | node.$markup.forEach(function eachMarkupItem(item) {
|
190 | if (typeof item === 'string') {
|
191 | output.$text = exports.moosh(output.$text, item, keepArrays);
|
192 | } else if (typeof item === 'object') {
|
193 | if (item.constructor === Array) {
|
194 | key = item[0].$name;
|
195 | } else {
|
196 | key = item.$name;
|
197 | }
|
198 | output[key] = exports.moosh(output[key], simplifyAll(item, true), keepArrays);
|
199 | }
|
200 | });
|
201 | }
|
202 | return output;
|
203 | };
|
204 |
|
205 | exports.isSomething = function isSomething(value) {
|
206 | if (value === undefined || value === null) {
|
207 | return false;
|
208 | }
|
209 |
|
210 | if (value.constructor === Array && value.length === 0) {
|
211 | return false;
|
212 | }
|
213 |
|
214 | if (typeof value === 'object' && Object.keys(value).length === 0) {
|
215 | return false;
|
216 | }
|
217 |
|
218 | if (typeof value === 'string' && value.length === 0) {
|
219 | return false;
|
220 | }
|
221 | return true;
|
222 | };
|