bajo-spatial 2.2.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,366 +1,369 @@
1
1
  // jshint multistr:true
2
2
 
3
3
  (function (w, d) {
4
- 'use strict'
5
-
6
- const TABLE_NAME = 'hljs-ln'
7
- const LINE_NAME = 'hljs-ln-line'
8
- const CODE_BLOCK_NAME = 'hljs-ln-code'
9
- const NUMBERS_BLOCK_NAME = 'hljs-ln-numbers'
10
- const NUMBER_LINE_NAME = 'hljs-ln-n'
11
- const DATA_ATTR_NAME = 'data-line-number'
12
- const BREAK_LINE_REGEXP = /\r\n|\r|\n/g
13
-
14
- if (w.hljs) {
15
- w.hljs.initLineNumbersOnLoad = initLineNumbersOnLoad
16
- w.hljs.lineNumbersBlock = lineNumbersBlock
17
- w.hljs.lineNumbersValue = lineNumbersValue
18
-
19
- addStyles()
20
- } else {
21
- w.console.error('highlight.js not detected!')
22
- }
23
-
24
- function isHljsLnCodeDescendant (domElt) {
25
- let curElt = domElt
26
- while (curElt) {
27
- if (curElt.className && curElt.className.indexOf('hljs-ln-code') !== -1) {
28
- return true
29
- }
30
- curElt = curElt.parentNode
4
+ 'use strict';
5
+
6
+ var TABLE_NAME = 'hljs-ln',
7
+ LINE_NAME = 'hljs-ln-line',
8
+ CODE_BLOCK_NAME = 'hljs-ln-code',
9
+ NUMBERS_BLOCK_NAME = 'hljs-ln-numbers',
10
+ NUMBER_LINE_NAME = 'hljs-ln-n',
11
+ DATA_ATTR_NAME = 'data-line-number',
12
+ BREAK_LINE_REGEXP = /\r\n|\r|\n/g;
13
+
14
+ if (w.hljs) {
15
+ w.hljs.initLineNumbersOnLoad = initLineNumbersOnLoad;
16
+ w.hljs.lineNumbersBlock = lineNumbersBlock;
17
+ w.hljs.lineNumbersValue = lineNumbersValue;
18
+
19
+ addStyles();
20
+ } else {
21
+ w.console.error('highlight.js not detected!');
31
22
  }
32
- return false
33
- }
34
23
 
35
- function getHljsLnTable (hljsLnDomElt) {
36
- let curElt = hljsLnDomElt
37
- while (curElt.nodeName !== 'TABLE') {
38
- curElt = curElt.parentNode
39
- }
40
- return curElt
41
- }
42
-
43
- // Function to workaround a copy issue with Microsoft Edge.
44
- // Due to hljs-ln wrapping the lines of code inside a <table> element,
45
- // itself wrapped inside a <pre> element, window.getSelection().toString()
46
- // does not contain any line breaks. So we need to get them back using the
47
- // rendered code in the DOM as reference.
48
- function edgeGetSelectedCodeLines (selection) {
49
- // current selected text without line breaks
50
- const selectionText = selection.toString()
51
-
52
- // get the <td> element wrapping the first line of selected code
53
- let tdAnchor = selection.anchorNode
54
- while (tdAnchor.nodeName !== 'TD') {
55
- tdAnchor = tdAnchor.parentNode
24
+ function isHljsLnCodeDescendant(domElt) {
25
+ var curElt = domElt;
26
+ while (curElt) {
27
+ if (curElt.className && curElt.className.indexOf('hljs-ln-code') !== -1) {
28
+ return true;
29
+ }
30
+ curElt = curElt.parentNode;
31
+ }
32
+ return false;
56
33
  }
57
34
 
58
- // get the <td> element wrapping the last line of selected code
59
- let tdFocus = selection.focusNode
60
- while (tdFocus.nodeName !== 'TD') {
61
- tdFocus = tdFocus.parentNode
35
+ function getHljsLnTable(hljsLnDomElt) {
36
+ var curElt = hljsLnDomElt;
37
+ while (curElt.nodeName !== 'TABLE') {
38
+ curElt = curElt.parentNode;
39
+ }
40
+ return curElt;
62
41
  }
63
42
 
64
- // extract line numbers
65
- let firstLineNumber = parseInt(tdAnchor.dataset.lineNumber)
66
- let lastLineNumber = parseInt(tdFocus.dataset.lineNumber)
67
-
68
- // multi-lines copied case
69
- if (firstLineNumber != lastLineNumber) {
70
- let firstLineText = tdAnchor.textContent
71
- let lastLineText = tdFocus.textContent
72
-
73
- // if the selection was made backward, swap values
74
- if (firstLineNumber > lastLineNumber) {
75
- let tmp = firstLineNumber
76
- firstLineNumber = lastLineNumber
77
- lastLineNumber = tmp
78
- tmp = firstLineText
79
- firstLineText = lastLineText
80
- lastLineText = tmp
81
- }
82
-
83
- // discard not copied characters in first line
84
- while (selectionText.indexOf(firstLineText) !== 0) {
85
- firstLineText = firstLineText.slice(1)
86
- }
87
-
88
- // discard not copied characters in last line
89
- while (selectionText.lastIndexOf(lastLineText) === -1) {
90
- lastLineText = lastLineText.slice(0, -1)
91
- }
92
-
93
- // reconstruct and return the real copied text
94
- let selectedText = firstLineText
95
- const hljsLnTable = getHljsLnTable(tdAnchor)
96
- for (let i = firstLineNumber + 1; i < lastLineNumber; ++i) {
97
- const codeLineSel = format('.{0}[{1}="{2}"]', [CODE_BLOCK_NAME, DATA_ATTR_NAME, i])
98
- const codeLineElt = hljsLnTable.querySelector(codeLineSel)
99
- selectedText += '\n' + codeLineElt.textContent
100
- }
101
- selectedText += '\n' + lastLineText
102
- return selectedText
103
- // single copied line case
104
- } else {
105
- return selectionText
106
- }
107
- }
108
-
109
- // ensure consistent code copy/paste behavior across all browsers
110
- // (see https://github.com/wcoder/highlightjs-line-numbers.js/issues/51)
111
- document.addEventListener('copy', function (e) {
112
- // get current selection
113
- const selection = window.getSelection()
114
- // override behavior when one wants to copy line of codes
115
- if (isHljsLnCodeDescendant(selection.anchorNode)) {
116
- let selectionText
117
- // workaround an issue with Microsoft Edge as copied line breaks
118
- // are removed otherwise from the selection string
119
- if (window.navigator.userAgent.indexOf('Edge') !== -1) {
120
- selectionText = edgeGetSelectedCodeLines(selection)
121
- } else {
122
- // other browsers can directly use the selection string
123
- selectionText = selection.toString()
124
- }
125
- e.clipboardData.setData(
126
- 'text/plain',
127
- selectionText
128
- .replace(/(^\t)/gm, '')
129
- )
130
- e.preventDefault()
43
+ // Function to workaround a copy issue with Microsoft Edge.
44
+ // Due to hljs-ln wrapping the lines of code inside a <table> element,
45
+ // itself wrapped inside a <pre> element, window.getSelection().toString()
46
+ // does not contain any line breaks. So we need to get them back using the
47
+ // rendered code in the DOM as reference.
48
+ function edgeGetSelectedCodeLines(selection) {
49
+ // current selected text without line breaks
50
+ var selectionText = selection.toString();
51
+
52
+ // get the <td> element wrapping the first line of selected code
53
+ var tdAnchor = selection.anchorNode;
54
+ while (tdAnchor.nodeName !== 'TD') {
55
+ tdAnchor = tdAnchor.parentNode;
56
+ }
57
+
58
+ // get the <td> element wrapping the last line of selected code
59
+ var tdFocus = selection.focusNode;
60
+ while (tdFocus.nodeName !== 'TD') {
61
+ tdFocus = tdFocus.parentNode;
62
+ }
63
+
64
+ // extract line numbers
65
+ var firstLineNumber = parseInt(tdAnchor.dataset.lineNumber);
66
+ var lastLineNumber = parseInt(tdFocus.dataset.lineNumber);
67
+
68
+ // multi-lines copied case
69
+ if (firstLineNumber != lastLineNumber) {
70
+
71
+ var firstLineText = tdAnchor.textContent;
72
+ var lastLineText = tdFocus.textContent;
73
+
74
+ // if the selection was made backward, swap values
75
+ if (firstLineNumber > lastLineNumber) {
76
+ var tmp = firstLineNumber;
77
+ firstLineNumber = lastLineNumber;
78
+ lastLineNumber = tmp;
79
+ tmp = firstLineText;
80
+ firstLineText = lastLineText;
81
+ lastLineText = tmp;
82
+ }
83
+
84
+ // discard not copied characters in first line
85
+ while (selectionText.indexOf(firstLineText) !== 0) {
86
+ firstLineText = firstLineText.slice(1);
87
+ }
88
+
89
+ // discard not copied characters in last line
90
+ while (selectionText.lastIndexOf(lastLineText) === -1) {
91
+ lastLineText = lastLineText.slice(0, -1);
92
+ }
93
+
94
+ // reconstruct and return the real copied text
95
+ var selectedText = firstLineText;
96
+ var hljsLnTable = getHljsLnTable(tdAnchor);
97
+ for (var i = firstLineNumber + 1 ; i < lastLineNumber ; ++i) {
98
+ var codeLineSel = format('.{0}[{1}="{2}"]', [CODE_BLOCK_NAME, DATA_ATTR_NAME, i]);
99
+ var codeLineElt = hljsLnTable.querySelector(codeLineSel);
100
+ selectedText += '\n' + codeLineElt.textContent;
101
+ }
102
+ selectedText += '\n' + lastLineText;
103
+ return selectedText;
104
+ // single copied line case
105
+ } else {
106
+ return selectionText;
107
+ }
131
108
  }
132
- })
133
109
 
134
- function addStyles () {
135
- const css = d.createElement('style')
136
- css.type = 'text/css'
137
- css.innerHTML = format(
138
- '.{0}{border-collapse:collapse}' +
110
+ // ensure consistent code copy/paste behavior across all browsers
111
+ // (see https://github.com/wcoder/highlightjs-line-numbers.js/issues/51)
112
+ document.addEventListener('copy', function(e) {
113
+ // get current selection
114
+ var selection = window.getSelection();
115
+ // override behavior when one wants to copy line of codes
116
+ if (isHljsLnCodeDescendant(selection.anchorNode)) {
117
+ var selectionText;
118
+ // workaround an issue with Microsoft Edge as copied line breaks
119
+ // are removed otherwise from the selection string
120
+ if (window.navigator.userAgent.indexOf('Edge') !== -1) {
121
+ selectionText = edgeGetSelectedCodeLines(selection);
122
+ } else {
123
+ // other browsers can directly use the selection string
124
+ selectionText = selection.toString();
125
+ }
126
+ e.clipboardData.setData(
127
+ 'text/plain',
128
+ selectionText
129
+ .replace(/(^\t)/gm, '')
130
+ );
131
+ e.preventDefault();
132
+ }
133
+ });
134
+
135
+ function addStyles () {
136
+ var css = d.createElement('style');
137
+ css.type = 'text/css';
138
+ css.innerHTML = format(
139
+ '.{0}{border-collapse:collapse}' +
139
140
  '.{0} td{padding:0}' +
140
141
  '.{1}:before{content:attr({2})}',
141
- [
142
- TABLE_NAME,
143
- NUMBER_LINE_NAME,
144
- DATA_ATTR_NAME
145
- ])
146
- d.getElementsByTagName('head')[0].appendChild(css)
147
- }
148
-
149
- function initLineNumbersOnLoad (options) {
150
- if (d.readyState === 'interactive' || d.readyState === 'complete') {
151
- documentReady(options)
152
- } else {
153
- w.addEventListener('DOMContentLoaded', function () {
154
- documentReady(options)
155
- })
142
+ [
143
+ TABLE_NAME,
144
+ NUMBER_LINE_NAME,
145
+ DATA_ATTR_NAME
146
+ ]);
147
+ d.getElementsByTagName('head')[0].appendChild(css);
156
148
  }
157
- }
158
-
159
- function documentReady (options) {
160
- try {
161
- const blocks = d.querySelectorAll('code.hljs,code.nohighlight')
162
149
 
163
- for (const i in blocks) {
164
- if (blocks.hasOwnProperty(i)) {
165
- if (!isPluginDisabledForBlock(blocks[i])) {
166
- lineNumbersBlock(blocks[i], options)
167
- }
150
+ function initLineNumbersOnLoad (options) {
151
+ if (d.readyState === 'interactive' || d.readyState === 'complete') {
152
+ documentReady(options);
153
+ } else {
154
+ w.addEventListener('DOMContentLoaded', function () {
155
+ documentReady(options);
156
+ });
168
157
  }
169
- }
170
- } catch (e) {
171
- w.console.error('LineNumbers error: ', e)
172
158
  }
173
- }
174
159
 
175
- function isPluginDisabledForBlock (element) {
176
- return element.classList.contains('nohljsln')
177
- }
160
+ function documentReady (options) {
161
+ try {
162
+ var blocks = d.querySelectorAll('code.hljs,code.nohighlight');
163
+
164
+ for (var i in blocks) {
165
+ if (blocks.hasOwnProperty(i)) {
166
+ if (!isPluginDisabledForBlock(blocks[i])) {
167
+ lineNumbersBlock(blocks[i], options);
168
+ }
169
+ }
170
+ }
171
+ } catch (e) {
172
+ w.console.error('LineNumbers error: ', e);
173
+ }
174
+ }
178
175
 
179
- function lineNumbersBlock (element, options) {
180
- if (typeof element !== 'object') return
176
+ function isPluginDisabledForBlock(element) {
177
+ return element.classList.contains('nohljsln');
178
+ }
181
179
 
182
- async(function () {
183
- element.innerHTML = lineNumbersInternal(element, options)
184
- })
185
- }
180
+ function lineNumbersBlock (element, options) {
181
+ if (typeof element !== 'object') return;
186
182
 
187
- function lineNumbersValue (value, options) {
188
- if (typeof value !== 'string') return
183
+ async(function () {
184
+ element.innerHTML = lineNumbersInternal(element, options);
185
+ });
186
+ }
189
187
 
190
- const element = document.createElement('code')
191
- element.innerHTML = value
188
+ function lineNumbersValue (value, options) {
189
+ if (typeof value !== 'string') return;
192
190
 
193
- return lineNumbersInternal(element, options)
194
- }
191
+ var element = document.createElement('code')
192
+ element.innerHTML = value
195
193
 
196
- function lineNumbersInternal (element, options) {
197
- const internalOptions = mapOptions(element, options)
194
+ return lineNumbersInternal(element, options);
195
+ }
198
196
 
199
- duplicateMultilineNodes(element)
197
+ function lineNumbersInternal (element, options) {
200
198
 
201
- return addLineNumbersBlockFor(element.innerHTML, internalOptions)
202
- }
199
+ var internalOptions = mapOptions(element, options);
203
200
 
204
- function addLineNumbersBlockFor (inputHtml, options) {
205
- const lines = getLines(inputHtml)
201
+ duplicateMultilineNodes(element);
206
202
 
207
- // if last line contains only carriage return remove it
208
- if (lines[lines.length - 1].trim() === '') {
209
- lines.pop()
203
+ return addLineNumbersBlockFor(element.innerHTML, internalOptions);
210
204
  }
211
205
 
212
- if (lines.length > 1 || options.singleLine) {
213
- let html = ''
206
+ function addLineNumbersBlockFor (inputHtml, options) {
207
+ var lines = getLines(inputHtml);
208
+
209
+ // if last line contains only carriage return remove it
210
+ if (lines[lines.length-1].trim() === '') {
211
+ lines.pop();
212
+ }
213
+
214
+ if (lines.length > 1 || options.singleLine) {
215
+ var html = '';
214
216
 
215
- for (let i = 0, l = lines.length; i < l; i++) {
216
- html += format(
217
- '<tr>' +
217
+ for (var i = 0, l = lines.length; i < l; i++) {
218
+ html += format(
219
+ '<tr>' +
218
220
  '<td class="{0} {1}" {3}="{5}">' +
219
221
  '</td>' +
220
222
  '<td class="{0} {4}" {3}="{5}">' +
221
223
  '{6}' +
222
224
  '</td>' +
223
225
  '</tr>',
224
- [
225
- LINE_NAME,
226
- NUMBERS_BLOCK_NAME,
227
- NUMBER_LINE_NAME,
228
- DATA_ATTR_NAME,
229
- CODE_BLOCK_NAME,
230
- i + options.startFrom,
231
- lines[i].length > 0 ? lines[i] : ' '
232
- ])
233
- }
234
-
235
- return format('<table class="{0}">{1}</table>', [TABLE_NAME, html])
236
- }
226
+ [
227
+ LINE_NAME,
228
+ NUMBERS_BLOCK_NAME,
229
+ NUMBER_LINE_NAME,
230
+ DATA_ATTR_NAME,
231
+ CODE_BLOCK_NAME,
232
+ i + options.startFrom,
233
+ lines[i].length > 0 ? lines[i] : ' '
234
+ ]);
235
+ }
236
+
237
+ return format('<table class="{0}">{1}</table>', [ TABLE_NAME, html ]);
238
+ }
237
239
 
238
- return inputHtml
239
- }
240
+ return inputHtml;
241
+ }
240
242
 
241
- /**
243
+ /**
242
244
  * @param {HTMLElement} element Code block.
243
245
  * @param {Object} options External API options.
244
246
  * @returns {Object} Internal API options.
245
247
  */
246
- function mapOptions (element, options) {
247
- options = options || {}
248
- return {
249
- singleLine: getSingleLineOption(options),
250
- startFrom: getStartFromOption(element, options)
248
+ function mapOptions (element, options) {
249
+ options = options || {};
250
+ return {
251
+ singleLine: getSingleLineOption(options),
252
+ startFrom: getStartFromOption(element, options)
253
+ };
251
254
  }
252
- }
253
255
 
254
- function getSingleLineOption (options) {
255
- const defaultValue = false
256
- if (options.singleLine) {
257
- return options.singleLine
256
+ function getSingleLineOption (options) {
257
+ var defaultValue = false;
258
+ if (!!options.singleLine) {
259
+ return options.singleLine;
260
+ }
261
+ return defaultValue;
258
262
  }
259
- return defaultValue
260
- }
261
263
 
262
- function getStartFromOption (element, options) {
263
- const defaultValue = 1
264
- let startFrom = defaultValue
264
+ function getStartFromOption (element, options) {
265
+ var defaultValue = 1;
266
+ var startFrom = defaultValue;
265
267
 
266
- if (isFinite(options.startFrom)) {
267
- startFrom = options.startFrom
268
- }
268
+ if (isFinite(options.startFrom)) {
269
+ startFrom = options.startFrom;
270
+ }
269
271
 
270
- // can be overridden because local option is priority
271
- const value = getAttribute(element, 'data-ln-start-from')
272
- if (value !== null) {
273
- startFrom = toNumber(value, defaultValue)
274
- }
272
+ // can be overridden because local option is priority
273
+ var value = getAttribute(element, 'data-ln-start-from');
274
+ if (value !== null) {
275
+ startFrom = toNumber(value, defaultValue);
276
+ }
275
277
 
276
- return startFrom
277
- }
278
+ return startFrom;
279
+ }
278
280
 
279
- /**
281
+ /**
280
282
  * Recursive method for fix multi-line elements implementation in highlight.js
281
283
  * Doing deep passage on child nodes.
282
284
  * @param {HTMLElement} element
283
285
  */
284
- function duplicateMultilineNodes (element) {
285
- const nodes = element.childNodes
286
- for (const node in nodes) {
287
- if (nodes.hasOwnProperty(node)) {
288
- const child = nodes[node]
289
- if (getLinesCount(child.textContent) > 0) {
290
- if (child.childNodes.length > 0) {
291
- duplicateMultilineNodes(child)
292
- } else {
293
- duplicateMultilineNode(child.parentNode)
294
- }
286
+ function duplicateMultilineNodes (element) {
287
+ var nodes = element.childNodes;
288
+ for (var node in nodes) {
289
+ if (nodes.hasOwnProperty(node)) {
290
+ var child = nodes[node];
291
+ if (getLinesCount(child.textContent) > 0) {
292
+ if (child.childNodes.length > 0) {
293
+ duplicateMultilineNodes(child);
294
+ } else {
295
+ duplicateMultilineNode(child.parentNode);
296
+ }
297
+ }
298
+ }
295
299
  }
296
- }
297
300
  }
298
- }
299
301
 
300
- /**
302
+ /**
301
303
  * Method for fix multi-line elements implementation in highlight.js
302
304
  * @param {HTMLElement} element
303
305
  */
304
- function duplicateMultilineNode (element) {
305
- const className = element.className
306
+ function duplicateMultilineNode (element) {
307
+ var className = element.className;
306
308
 
307
- if (!/hljs-/.test(className)) return
309
+ if ( ! /hljs-/.test(className)) return;
308
310
 
309
- const lines = getLines(element.innerHTML)
311
+ var lines = getLines(element.innerHTML);
310
312
 
311
- for (var i = 0, result = ''; i < lines.length; i++) {
312
- const lineText = lines[i].length > 0 ? lines[i] : ' '
313
- result += format('<span class="{0}">{1}</span>\n', [className, lineText])
314
- }
313
+ for (var i = 0, result = ''; i < lines.length; i++) {
314
+ var lineText = lines[i].length > 0 ? lines[i] : ' ';
315
+ result += format('<span class="{0}">{1}</span>\n', [ className, lineText ]);
316
+ }
315
317
 
316
- element.innerHTML = result.trim()
317
- }
318
+ element.innerHTML = result.trim();
319
+ }
318
320
 
319
- function getLines (text) {
320
- if (text.length === 0) return []
321
- return text.split(BREAK_LINE_REGEXP)
322
- }
321
+ function getLines (text) {
322
+ if (text.length === 0) return [];
323
+ return text.split(BREAK_LINE_REGEXP);
324
+ }
323
325
 
324
- function getLinesCount (text) {
325
- return (text.trim().match(BREAK_LINE_REGEXP) || []).length
326
- }
326
+ function getLinesCount (text) {
327
+ return (text.trim().match(BREAK_LINE_REGEXP) || []).length;
328
+ }
327
329
 
328
- ///
329
- /// HELPERS
330
- ///
330
+ ///
331
+ /// HELPERS
332
+ ///
331
333
 
332
- function async (func) {
333
- w.setTimeout(func, 0)
334
- }
334
+ function async (func) {
335
+ w.setTimeout(func, 0);
336
+ }
335
337
 
336
- /**
338
+ /**
337
339
  * {@link https://wcoder.github.io/notes/string-format-for-string-formating-in-javascript}
338
340
  * @param {string} format
339
341
  * @param {array} args
340
342
  */
341
- function format (format, args) {
342
- return format.replace(/\{(\d+)\}/g, function (m, n) {
343
- return args[n] !== undefined ? args[n] : m
344
- })
345
- }
343
+ function format (format, args) {
344
+ return format.replace(/\{(\d+)\}/g, function(m, n){
345
+ return args[n] !== undefined ? args[n] : m;
346
+ });
347
+ }
346
348
 
347
- /**
349
+ /**
348
350
  * @param {HTMLElement} element Code block.
349
351
  * @param {String} attrName Attribute name.
350
352
  * @returns {String} Attribute value or empty.
351
353
  */
352
- function getAttribute (element, attrName) {
353
- return element.hasAttribute(attrName) ? element.getAttribute(attrName) : null
354
- }
354
+ function getAttribute (element, attrName) {
355
+ return element.hasAttribute(attrName) ? element.getAttribute(attrName) : null;
356
+ }
355
357
 
356
- /**
358
+ /**
357
359
  * @param {String} str Source string.
358
360
  * @param {Number} fallback Fallback value.
359
361
  * @returns Parsed number or fallback value.
360
362
  */
361
- function toNumber (str, fallback) {
362
- if (!str) return fallback
363
- const number = Number(str)
364
- return isFinite(number) ? number : fallback
365
- }
366
- }(window, document))
363
+ function toNumber (str, fallback) {
364
+ if (!str) return fallback;
365
+ var number = Number(str);
366
+ return isFinite(number) ? number : fallback;
367
+ }
368
+
369
+ }(window, document));