This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
forked from speced/respec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
394 lines (370 loc) · 17.5 KB
/
utils.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*jshint laxcomma: true*/
// Module core/utils
// As the name implies, this contains a ragtag gang of methods that just don't fit
// anywhere else.
"use strict";
define(
["core/pubsubhub"],
function (pubsubhub) {
var utils = {
// --- SET UP
run: function (conf, doc, cb, msg) {
msg.pub("start", "core/utils");
msg.pub("end", "core/utils");
cb();
}
/**
* Allows a node to be swapped into a different document at
* some insertion point(Element). This function is useful for
* opportunistic insertion of DOM nodes into a document, without
* first knowing if that is the final document where the node will
* reside.
*
* @param {Node} node The node to be swapped.
* @return {Function} A function that takes a document and a new
* insertion point (element). When called,
* node gets inserted into doc at before a given
* insertion point (node) - or just appended, if
* the element has no children.
*/
, makeOwnerSwapper: function(node) {
if(!(node instanceof Node)){
throw TypeError();
}
return function(doc, insertionPoint) {
node.remove();
doc.adoptNode(node);
var firstElementChild = this.findFirstElementChild(insertionPoint);
if (firstElementChild) {
insertionPoint.insertBefore(node, firstElementChild);
return;
}
insertionPoint.appendChild(node);
}.bind(this);
}
/**
* Finds the first Element child, given a node. Provides support for
* Microsoft Edge's missing support of .firstElementChild.
*
* @param {Node} node The node to be traversed.
* @return {Element} The first Element in the child list.
*/
, findFirstElementChild: function(node){
if(!node.hasChildNodes()){
return null;
}
// We have native support
if(node.firstElementChild){
return node.firstElementChild;
}
return Array
.from(node.childNodes)
.find(function(node){
return node.nodeType === Node.ELEMENT_NODE;
});
}
, calculateLeftPad: function(text) {
if (typeof text !== "string") {
throw new TypeError("Invalid input");
}
var spaceOrTab = /^[\ |\t]*/;
// Find smallest padding value
var leftPad = text
.split("\n")
.filter(function(item) {
return item;
})
.reduce(function(smallest, item) {
// can't go smaller than 0
if (smallest === 0) {
return smallest;
}
var match = item.match(spaceOrTab)[0] || "";
return Math.min(match.length, smallest);
}, +Infinity);
return (leftPad === +Infinity) ? 0 : leftPad;
}
/**
* Makes a ES conforming iterator allowing objects to be used with
* methods that can interface with Iterators (Array.from(), etc.).
*
* @param {Function} nextLikeFunction A function that returns a next value;
* @return {Object} An object that implements the Iterator prop.
*/
, toESIterable: function(nextLikeFunction) {
if (!(nextLikeFunction instanceof Function)) {
throw TypeError("Expected a function");
}
var next = function() {
return {
value: nextLikeFunction(),
get done() {
return this.value === null;
}
};
};
// We structure the iterator like this, or else
// RequireJS gets upset.
var iterator = {};
iterator[Symbol.iterator] = function() {
return {
next: next
};
};
return iterator;
}
, normalizePadding: function (text) {
if (!text) {
return "";
}
if (typeof text !== "string") {
throw TypeError("Invalid input");
}
if(text === "\n"){
return "\n";
}
function isEmpty(node) {
return node.textContent === "";
}
function isWhiteSpace(node) {
return !/\S/gm.test(node.textContent);
}
function filterLastChildIsPadding(node) {
if (node.parentElement.lastChild === node && (isWhiteSpace(node) || isEmpty(node))) {
return NodeFilter.FILTER_ACCEPT;
}
return NodeFilter.FILTER_REJECT;
}
function isTextNode(node) {
return node.nodeType === Node.TEXT_NODE;
}
function parentIs(type) {
return function checkParent(node) {
if (!node) {
return false;
}
var match = node.parentNode && node.parentNode.localName === type;
return (!match) ? checkParent(node.parentNode) : true;
};
}
var filterParentIsPre = parentIs("pre");
// Force into body
var parserInput = "<body>" + text;
var doc = new DOMParser().parseFromString(parserInput, "text/html");
var firstPaddedLine = Array
.from(doc.body.childNodes)
.filter(isTextNode)
.map(function(textNode) {
return textNode.textContent;
})
.find(function(textContent) {
var result = /^[\#|\s|\w]+/gm.test(textContent);
return result;
});
// There is no padding, so just return what we started with.
if (!firstPaddedLine) {
return text;
}
var baseColumn = this.calculateLeftPad(firstPaddedLine);
// Only if we have a baseColumn to work with ...
// With only the text nodes that are not children of pre elements,
// we left align all those text nodes.
if(baseColumn){
Array
.from(doc.body.childNodes)
.filter(isTextNode)
.filter(function(textNode) {
// 🎵 Hey, processor! Leave those pre's alone! 🎵
return !filterParentIsPre(textNode);
})
.filter(function(textNode) {
// we don't care about last nodes that are just white space
var isLastChild = textNode.parentElement.lastChild === textNode;
var isJustWS = isWhiteSpace(textNode);
return !(isLastChild && isJustWS);
})
.map(function toTrimmedTextNode(textNode) {
var rawText = textNode.textContent;
// We remove tailing space on the right, which is just there
// to pad out tags like:
// <div>
// <div>
// Next line has 2 spaces hidden!
// __</div>
// </div>
//
var trimmedRight = rawText.trimRight();
var trimBy = this.calculateLeftPad(trimmedRight) || baseColumn;
if (!trimBy) {
return null; //nothing to do
}
var exp = "^ {" + trimBy + "}";
var startTrim = new RegExp(exp, "gm");
var trimmedText = (trimBy) ? rawText.replace(startTrim, "") : rawText;
var newNode = textNode.ownerDocument.createTextNode(trimmedText);
// We can then swap the old with the new
return {
oldNode: textNode,
newNode: newNode,
};
}.bind(this))
.filter(function(nodes) {
return nodes;
})
.forEach(function(nodes) {
var oldNode = nodes.oldNode;
var newNode = nodes.newNode;
oldNode.parentElement.replaceChild(newNode, oldNode);
});
}
var nodeIterator = doc.createNodeIterator(doc.body, NodeFilter.SHOW_TEXT, filterLastChildIsPadding);
var iterable = this.toESIterable(nodeIterator.nextNode.bind(nodeIterator));
// Remove trailing whitespace nodes
Array
.from(iterable)
.forEach(function(node) {
node.remove();
});
var result = doc.body.innerHTML;
return result;
}
// --- RESPEC STUFF -------------------------------------------------------------------------------
, removeReSpec: function (doc) {
$(".remove, script[data-requiremodule]", doc).remove();
}
// --- STRING HELPERS -----------------------------------------------------------------------------
// Takes an array and returns a string that separates each of its items with the proper commas and
// "and". The second argument is a mapping function that can convert the items before they are
// joined
, joinAnd: function (arr, mapper) {
if (!arr || !arr.length) return "";
mapper = mapper || function (ret) { return ret; };
var ret = "";
if (arr.length === 1) return mapper(arr[0], 0);
for (var i = 0, n = arr.length; i < n; i++) {
if (i > 0) {
if (n === 2) ret += ' ';
else ret += ', ';
if (i == n - 1) ret += 'and ';
}
ret += mapper(arr[i], i);
}
return ret;
}
// Takes a string, applies some XML escapes, and returns the escaped string.
// Note that overall using either Handlebars' escaped output or jQuery is much
// preferred to operating on strings directly.
, xmlEscape: function (s) {
return s.replace(/&/g, "&")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/</g, "<");
}
// Trims string at both ends and replaces all other white space with a single space
, norm: function (str) {
return str.replace(/^\s+/, "").replace(/\s+$/, "").split(/\s+/).join(" ");
}
// --- DATE HELPERS -------------------------------------------------------------------------------
// Takes a Date object and an optional separator and returns the year,month,day representation with
// the custom separator (defaulting to none) and proper 0-padding
, concatDate: function (date, sep) {
if (!sep) sep = "";
return "" + date.getFullYear() + sep + this.lead0(date.getMonth() + 1) + sep + this.lead0(date.getDate());
}
// takes a string, prepends a "0" if it is of length 1, does nothing otherwise
, lead0: function (str) {
str = "" + str;
return (str.length == 1) ? "0" + str : str;
}
// takes a YYYY-MM-DD date and returns a Date object for it
, parseSimpleDate: function (str) {
return new Date(str.substr(0, 4), (str.substr(5, 2) - 1), str.substr(8, 2));
}
// takes what document.lastModified returns and produces a Date object for it
, parseLastModified: function (str) {
if (!str) return new Date();
return new Date(Date.parse(str));
// return new Date(str.substr(6, 4), (str.substr(0, 2) - 1), str.substr(3, 2));
}
// list of human names for months (in English)
, humanMonths: ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"]
// given either a Date object or a date in YYYY-MM-DD format, return a human-formatted
// date suitable for use in a W3C specification
, humanDate: function (date) {
if (!(date instanceof Date)) date = this.parseSimpleDate(date);
return this.lead0(date.getDate()) + " " + this.humanMonths[date.getMonth()] + " " + date.getFullYear();
}
// given either a Date object or a date in YYYY-MM-DD format, return an ISO formatted
// date suitable for use in a xsd:datetime item
, isoDate: function (date) {
if (!(date instanceof Date)) date = this.parseSimpleDate(date);
// return "" + date.getUTCFullYear() +'-'+ this.lead0(date.getUTCMonth() + 1)+'-' + this.lead0(date.getUTCDate()) +'T'+this.lead0(date.getUTCHours())+':'+this.lead0(date.getUTCMinutes()) +":"+this.lead0(date.getUTCSeconds())+'+0000';
return date.toISOString() ;
}
// Given an object, it converts it to a key value pair separated by
// ("=", configurable) and a delimiter (" ," configurable).
// for example, {"foo": "bar", "baz": 1} becomes "foo=bar, baz=1"
, toKeyValuePairs: function(obj, delimiter, separator) {
if(!separator){
separator = "=";
}
if(!delimiter){
delimiter = ", ";
}
return Object.getOwnPropertyNames(obj)
.map(function(key){
return key + separator + JSON.stringify(obj[key]);
})
.join(delimiter);
}
// --- STYLE HELPERS ------------------------------------------------------------------------------
// take a document and either a link or an array of links to CSS and appends a <link/> element
// to the head pointing to each
, linkCSS: function (doc, styles) {
if (!Array.isArray(styles)) {
styles = [styles];
}
styles
.map(function (url) {
var link = doc.createElement("link");
link.rel = "stylesheet";
link.href = url;
return link;
})
.reduce(function(elem, nextLink) {
elem.appendChild(nextLink);
return elem;
}, doc.head);
}
// --- TRANSFORMATIONS ------------------------------------------------------------------------------
// Run list of transforms over content and return result.
// Please note that this is a legacy method that is only kept in order to maintain compatibility
// with RSv1. It is therefore not tested and not actively supported.
, runTransforms: function (content, flist) {
var args = [this, content]
, funcArgs = Array.prototype.slice.call(arguments)
;
funcArgs.shift(); funcArgs.shift();
args = args.concat(funcArgs);
if (flist) {
var methods = flist.split(/\s+/);
for (var j = 0; j < methods.length; j++) {
var meth = methods[j];
if (window[meth]) {
// the initial call passed |this| directly, so we keep it that way
try {
content = window[meth].apply(this, args);
}
catch (e) {
pubsubhub.pub("warn", "call to " + meth + "() failed with " + e) ;
}
}
}
}
return content;
}
};
return utils;
}
);