@ohos-ports/advanced-mark.js 3.0.0-beta.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.
package/dist/mark.js ADDED
@@ -0,0 +1,1682 @@
1
+ /*!***************************************************
2
+ * advanced-mark.js v3.0.0
3
+ * https://github.com/angezid/advanced-mark.js
4
+ * MIT licensed
5
+ * Copyright (c) 2022–2026, angezid
6
+ * Based on 'mark.js', license https://git.io/vwTVl
7
+ *****************************************************/
8
+
9
+ (function (global, factory) {
10
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
11
+ typeof define === 'function' && define.amd ? define(factory) :
12
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Mark = factory());
13
+ })(this, (function () { 'use strict';
14
+
15
+ function _typeof(o) {
16
+ "@babel/helpers - typeof";
17
+
18
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
19
+ return typeof o;
20
+ } : function (o) {
21
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
22
+ }, _typeof(o);
23
+ }
24
+ function _classCallCheck(instance, Constructor) {
25
+ if (!(instance instanceof Constructor)) {
26
+ throw new TypeError("Cannot call a class as a function");
27
+ }
28
+ }
29
+ function _defineProperties(target, props) {
30
+ for (var i = 0; i < props.length; i++) {
31
+ var descriptor = props[i];
32
+ descriptor.enumerable = descriptor.enumerable || false;
33
+ descriptor.configurable = true;
34
+ if ("value" in descriptor) descriptor.writable = true;
35
+ Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
36
+ }
37
+ }
38
+ function _createClass(Constructor, protoProps, staticProps) {
39
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
40
+ if (staticProps) _defineProperties(Constructor, staticProps);
41
+ Object.defineProperty(Constructor, "prototype", {
42
+ writable: false
43
+ });
44
+ return Constructor;
45
+ }
46
+ function _extends() {
47
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
48
+ for (var i = 1; i < arguments.length; i++) {
49
+ var source = arguments[i];
50
+ for (var key in source) {
51
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
52
+ target[key] = source[key];
53
+ }
54
+ }
55
+ }
56
+ return target;
57
+ };
58
+ return _extends.apply(this, arguments);
59
+ }
60
+ function _toPrimitive(input, hint) {
61
+ if (typeof input !== "object" || input === null) return input;
62
+ var prim = input[Symbol.toPrimitive];
63
+ if (prim !== undefined) {
64
+ var res = prim.call(input, hint || "default");
65
+ if (typeof res !== "object") return res;
66
+ throw new TypeError("@@toPrimitive must return a primitive value.");
67
+ }
68
+ return (hint === "string" ? String : Number)(input);
69
+ }
70
+ function _toPropertyKey(arg) {
71
+ var key = _toPrimitive(arg, "string");
72
+ return typeof key === "symbol" ? key : String(key);
73
+ }
74
+
75
+ var DOMIterator = /*#__PURE__*/function () {
76
+ function DOMIterator(ctx, opt) {
77
+ _classCallCheck(this, DOMIterator);
78
+ this.ctx = ctx;
79
+ this.opt = opt;
80
+ this.map = new Map();
81
+ }
82
+ _createClass(DOMIterator, [{
83
+ key: "getContexts",
84
+ value: function getContexts() {
85
+ var ctx = this.ctx,
86
+ win = this.opt.window,
87
+ sort = false;
88
+ if (!ctx) return [];
89
+ if (Array.isArray(ctx)) {
90
+ sort = true;
91
+ } else if (typeof ctx === 'string') {
92
+ ctx = win.document.querySelectorAll(ctx);
93
+ } else if (typeof ctx.length === 'undefined') {
94
+ ctx = [ctx];
95
+ }
96
+ var array = [];
97
+ var _loop = function _loop(i) {
98
+ if (!array.includes(ctx[i]) && !array.some(function (node) {
99
+ return node.contains(ctx[i]);
100
+ })) {
101
+ array.push(ctx[i]);
102
+ }
103
+ };
104
+ for (var i = 0; i < ctx.length; i++) {
105
+ _loop(i);
106
+ }
107
+ if (sort) {
108
+ array.sort(function (a, b) {
109
+ return (a.compareDocumentPosition(b) & win.Node.DOCUMENT_POSITION_FOLLOWING) > 0 ? -1 : 1;
110
+ });
111
+ }
112
+ return array;
113
+ }
114
+ }, {
115
+ key: "getIframeContents",
116
+ value: function getIframeContents(iframe, successFn, errorFn) {
117
+ try {
118
+ var doc = iframe.contentWindow.document;
119
+ if (doc) {
120
+ this.map.set(iframe, 'ready');
121
+ successFn({
122
+ iframe: iframe,
123
+ context: doc
124
+ });
125
+ }
126
+ } catch (e) {
127
+ errorFn({
128
+ iframe: iframe,
129
+ error: e
130
+ });
131
+ }
132
+ }
133
+ }, {
134
+ key: "observeIframeLoad",
135
+ value: function observeIframeLoad(ifr, successFn, errorFn) {
136
+ var _this = this;
137
+ if (this.map.has(ifr)) return;
138
+ var id = null;
139
+ var listener = function listener() {
140
+ clearTimeout(id);
141
+ ifr.removeEventListener('load', listener);
142
+ _this.getIframeContents(ifr, successFn, errorFn);
143
+ };
144
+ ifr.addEventListener('load', listener);
145
+ this.map.set(ifr, true);
146
+ id = setTimeout(listener, this.opt.iframesTimeout);
147
+ }
148
+ }, {
149
+ key: "onIframeReady",
150
+ value: function onIframeReady(ifr, successFn, errorFn) {
151
+ var bl = 'about:blank',
152
+ src = ifr.getAttribute('src'),
153
+ win = ifr.contentWindow;
154
+ try {
155
+ if (win.document.readyState !== 'complete' || src && src.trim() !== bl && win.location.href === bl) {
156
+ this.observeIframeLoad(ifr, successFn, errorFn);
157
+ } else {
158
+ this.getIframeContents(ifr, successFn, errorFn);
159
+ }
160
+ } catch (e) {
161
+ errorFn(e);
162
+ }
163
+ }
164
+ }, {
165
+ key: "waitForIframes",
166
+ value: function waitForIframes(ctx, doneCb) {
167
+ var _this2 = this;
168
+ var shadow = this.opt.shadowDOM;
169
+ var count = 0,
170
+ iframes = 0,
171
+ array,
172
+ node;
173
+ var collect = function collect(context) {
174
+ var iterator = _this2.createIterator(context, _this2.opt.window.NodeFilter.SHOW_ELEMENT);
175
+ while (node = iterator.nextNode()) {
176
+ if (_this2.isIframe(node) && !_this2.map.has(node)) {
177
+ array.push(node);
178
+ iframes++;
179
+ }
180
+ if (shadow && node.shadowRoot && node.shadowRoot.mode === 'open') {
181
+ collect(node.shadowRoot);
182
+ }
183
+ }
184
+ };
185
+ var loop = function loop(obj) {
186
+ array = [];
187
+ if (!obj.iframe || obj.context.location.href !== 'about:blank') {
188
+ collect(obj.context);
189
+ if (!obj.iframe && !array.length) {
190
+ doneCb();
191
+ return;
192
+ }
193
+ }
194
+ if (array.length) {
195
+ array.forEach(function (iframe) {
196
+ _this2.onIframeReady(iframe, function (obj) {
197
+ count++;
198
+ loop(obj);
199
+ }, function (obj) {
200
+ if (_this2.opt.debug) {
201
+ console.log(obj.error || obj);
202
+ }
203
+ if (++count === iframes) doneCb();
204
+ });
205
+ });
206
+ } else if (count === iframes) {
207
+ doneCb();
208
+ }
209
+ };
210
+ loop({
211
+ context: ctx
212
+ });
213
+ }
214
+ }, {
215
+ key: "createIterator",
216
+ value: function createIterator(ctx, whatToShow) {
217
+ var win = this.opt.window;
218
+ return win.document.createNodeIterator(ctx, whatToShow, function () {
219
+ return win.NodeFilter.FILTER_ACCEPT;
220
+ }, false);
221
+ }
222
+ }, {
223
+ key: "addRemoveStyle",
224
+ value: function addRemoveStyle(root, style, add) {
225
+ if (add && !style) return;
226
+ var elem = root.querySelector('style[data-markjs]');
227
+ if (add) {
228
+ if (!elem) {
229
+ elem = this.opt.window.document.createElement('style');
230
+ elem.setAttribute('data-markjs', 'true');
231
+ root.appendChild(elem);
232
+ }
233
+ elem.textContent = style;
234
+ } else if (elem) {
235
+ root.removeChild(elem);
236
+ }
237
+ }
238
+ }, {
239
+ key: "isIframe",
240
+ value: function isIframe(node) {
241
+ return node.tagName === 'IFRAME' && !DOMIterator.matches(node, this.opt.exclude);
242
+ }
243
+ }, {
244
+ key: "iterateThroughNodes",
245
+ value: function iterateThroughNodes(ctx, whatToShow, filterCb, eachCb, doneCb) {
246
+ var _this3 = this;
247
+ var filter = this.opt.window.NodeFilter,
248
+ shadow = this.opt.shadowDOM,
249
+ iframe = this.opt.iframes;
250
+ if (iframe || shadow) {
251
+ var showElement = (whatToShow & filter.SHOW_ELEMENT) > 0,
252
+ showText = (whatToShow & filter.SHOW_TEXT) > 0;
253
+ var traverse = function traverse(node) {
254
+ var iterator = _this3.createIterator(node, whatToShow | filter.SHOW_ELEMENT),
255
+ root;
256
+ while (node = iterator.nextNode()) {
257
+ if (node.nodeType === 1) {
258
+ if (showElement && filterCb(node)) {
259
+ eachCb(node);
260
+ }
261
+ if (iframe && _this3.isIframe(node) && _this3.map.get(node) === 'ready') {
262
+ var doc = node.contentWindow.document;
263
+ if (doc) {
264
+ if (_this3.opt.highlight && showText) {
265
+ node.contentWindow.CSS.highlights.set(_this3.opt.highlightName, _this3.opt.highlight);
266
+ }
267
+ _this3.addRemoveStyle(doc.head, iframe.style, showText);
268
+ traverse(doc);
269
+ }
270
+ }
271
+ if (shadow && (root = node.shadowRoot) && root.mode === 'open') {
272
+ _this3.addRemoveStyle(root, shadow.style, showText);
273
+ traverse(root);
274
+ }
275
+ } else if (showText && node.nodeType === 3 && filterCb(node)) {
276
+ eachCb(node);
277
+ }
278
+ }
279
+ };
280
+ traverse(ctx);
281
+ } else {
282
+ var iterator = this.createIterator(ctx, whatToShow);
283
+ var node;
284
+ while (node = iterator.nextNode()) {
285
+ if (filterCb(node)) {
286
+ eachCb(node);
287
+ }
288
+ }
289
+ }
290
+ doneCb();
291
+ }
292
+ }, {
293
+ key: "forEachNode",
294
+ value: function forEachNode(whatToShow, each, filter) {
295
+ var _this4 = this;
296
+ var done = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : function () {};
297
+ var contexts = this.getContexts();
298
+ var open = contexts.length;
299
+ if (!open) done();
300
+ var ready = function ready() {
301
+ contexts.forEach(function (ctx) {
302
+ _this4.iterateThroughNodes(ctx, whatToShow, filter, each, function () {
303
+ if (--open <= 0) done();
304
+ });
305
+ });
306
+ };
307
+ if (this.opt.iframes) {
308
+ var count = open;
309
+ contexts.forEach(function (ctx) {
310
+ _this4.waitForIframes(ctx, function () {
311
+ if (--count <= 0) ready();
312
+ });
313
+ });
314
+ } else {
315
+ ready();
316
+ }
317
+ }
318
+ }], [{
319
+ key: "matches",
320
+ value: function matches(element, selector) {
321
+ if (!selector || !selector.length) {
322
+ return false;
323
+ }
324
+ var selectors = typeof selector === 'string' ? [selector] : selector,
325
+ fn = element.matches;
326
+ return fn && selectors.some(function (sel) {
327
+ return fn.call(element, sel);
328
+ });
329
+ }
330
+ }]);
331
+ return DOMIterator;
332
+ }();
333
+
334
+ var RegExpCreator = /*#__PURE__*/function () {
335
+ function RegExpCreator(options) {
336
+ _classCallCheck(this, RegExpCreator);
337
+ this.opt = _extends({}, {
338
+ 'diacritics': true,
339
+ 'synonyms': {},
340
+ 'accuracy': 'partially',
341
+ 'caseSensitive': false,
342
+ 'ignoreJoiners': false,
343
+ 'ignorePunctuation': [],
344
+ 'wildcards': 'disabled'
345
+ }, options);
346
+ }
347
+ _createClass(RegExpCreator, [{
348
+ key: "chars",
349
+ get: function get() {
350
+ var _this = this;
351
+ if (!this._chars) {
352
+ this._chars = [];
353
+ ['aàáảãạăằắẳẵặâầấẩẫậäåāą', 'cçćč', 'dđď', 'eèéẻẽẹêềếểễệëěēę', 'iìíỉĩịîïī', 'lł', 'nñňń', 'oòóỏõọôồốổỗộơởỡớờợöøōő', 'rř', 'sšśșş', 'tťțţ', 'uùúủũụưừứửữựûüůūű', 'yýỳỷỹỵÿ', 'zžżź'].forEach(function (str) {
354
+ _this._chars.push(str, str.toUpperCase());
355
+ });
356
+ }
357
+ return this._chars;
358
+ }
359
+ }, {
360
+ key: "create",
361
+ value: function create(terms) {
362
+ var _this2 = this;
363
+ var flags = "g".concat(this.opt.caseSensitive ? '' : 'i');
364
+ terms = terms.map(function (str) {
365
+ return '(' + _this2.createPattern(str, flags) + ')';
366
+ });
367
+ var obj = this.createAccuracy(terms.join('|'));
368
+ return new RegExp("".concat(obj.lookbehind, "(").concat(obj.pattern, ")").concat(obj.lookahead), flags);
369
+ }
370
+ }, {
371
+ key: "createPattern",
372
+ value: function createPattern(str, flags) {
373
+ str = this.checkWildcardsEscape(str);
374
+ str = this.createSynonyms(str, flags);
375
+ var joiners = this.getJoinersPunctuation();
376
+ if (joiners) {
377
+ str = this.setupIgnoreJoiners(str);
378
+ }
379
+ if (this.opt.diacritics) {
380
+ str = this.createDiacritics(str);
381
+ }
382
+ str = str.replace(/\s+/g, '[\\s]+');
383
+ if (joiners) {
384
+ str = this.createJoiners(str, joiners);
385
+ }
386
+ if (this.opt.wildcards !== 'disabled') {
387
+ str = this.createWildcards(str);
388
+ }
389
+ return str;
390
+ }
391
+ }, {
392
+ key: "escape",
393
+ value: function escape(str) {
394
+ return str.replace(/[[\]/{}()*+?.\\^$|]/g, '\\$&');
395
+ }
396
+ }, {
397
+ key: "preprocess",
398
+ value: function preprocess(val) {
399
+ if (val && val.length) {
400
+ return this.distinct(typeof val === 'string' ? val.split('') : val).join('').replace(/[-^\]\\]/g, '\\$&');
401
+ }
402
+ return '';
403
+ }
404
+ }, {
405
+ key: "distinct",
406
+ value: function distinct(array) {
407
+ var result = [];
408
+ array.forEach(function (item) {
409
+ if (item.trim() && !result.includes(item)) {
410
+ result.push(item);
411
+ }
412
+ });
413
+ return result;
414
+ }
415
+ }, {
416
+ key: "createSynonyms",
417
+ value: function createSynonyms(str, flags) {
418
+ var _this3 = this;
419
+ var syn = this.opt.synonyms;
420
+ for (var key in syn) {
421
+ if (syn.hasOwnProperty(key)) {
422
+ var array = Array.isArray(syn[key]) ? syn[key] : [syn[key]];
423
+ array.unshift(key);
424
+ array = this.distinct(array);
425
+ if (array.length > 1) {
426
+ array.sort(function (a, b) {
427
+ return b.length - a.length;
428
+ });
429
+ array = array.map(function (term) {
430
+ return _this3.checkWildcardsEscape(term);
431
+ });
432
+ var pattern = array.map(function (term) {
433
+ return _this3.escape(term);
434
+ }).join('|');
435
+ str = str.replace(new RegExp(pattern, flags), "(?:".concat(array.join('|'), ")"));
436
+ }
437
+ }
438
+ }
439
+ return str;
440
+ }
441
+ }, {
442
+ key: "checkWildcardsEscape",
443
+ value: function checkWildcardsEscape(str) {
444
+ if (this.opt.wildcards !== 'disabled') {
445
+ str = str.replace(/(\\.)+|[?*]/g, function (m, gr) {
446
+ return gr ? m : m === '?' ? '\x01' : '\x02';
447
+ }).replace(/\\(?=[?*\x01\x02])/g, '');
448
+ }
449
+ return this.escape(str);
450
+ }
451
+ }, {
452
+ key: "createWildcards",
453
+ value: function createWildcards(str) {
454
+ var spaces = this.opt.wildcards === 'withSpaces',
455
+ boundary = this.opt.blockElementsBoundary,
456
+ anyChar = "[^".concat(spaces && boundary ? '\x01' : '', "]*?");
457
+ return str.replace(/\x01/g, spaces ? '[^]?' : '\\S?').replace(/\x02/g, spaces ? anyChar : '\\S*');
458
+ }
459
+ }, {
460
+ key: "setupIgnoreJoiners",
461
+ value: function setupIgnoreJoiners(str) {
462
+ var reg = /((?:\\\\)+|\x02|\(\?:|\|)|\\?(?:[\uD800-\uDBFF][\uDC00-\uDFFF]|.)(?=([|)\x02]|$)|.)/g;
463
+ return str.replace(reg, function (m, gr1, gr2) {
464
+ return gr1 || typeof gr2 !== 'undefined' ? m : m + '\x00';
465
+ });
466
+ }
467
+ }, {
468
+ key: "createJoiners",
469
+ value: function createJoiners(str, joiners) {
470
+ return str.split(/\x00+/).join("[".concat(joiners, "]*"));
471
+ }
472
+ }, {
473
+ key: "getJoinersPunctuation",
474
+ value: function getJoinersPunctuation() {
475
+ var punct = this.preprocess(this.opt.ignorePunctuation),
476
+ str = punct ? punct : '';
477
+ if (this.opt.ignoreJoiners) {
478
+ str += "\\u00ad\\u200b\\u200c\\u200d";
479
+ }
480
+ return str;
481
+ }
482
+ }, {
483
+ key: "createDiacritics",
484
+ value: function createDiacritics(str) {
485
+ var _this4 = this;
486
+ var array = this.chars;
487
+ return str.split('').map(function (ch) {
488
+ for (var i = 0; i < array.length; i += 2) {
489
+ var lowerCase = array[i].includes(ch);
490
+ if (_this4.opt.caseSensitive) {
491
+ if (lowerCase) {
492
+ return '[' + array[i] + ']';
493
+ }
494
+ if (array[i + 1].includes(ch)) {
495
+ return '[' + array[i + 1] + ']';
496
+ }
497
+ } else if (lowerCase || array[i + 1].includes(ch)) {
498
+ return '[' + array[i] + array[i + 1] + ']';
499
+ }
500
+ }
501
+ return ch;
502
+ }).join('');
503
+ }
504
+ }, {
505
+ key: "createAccuracy",
506
+ value: function createAccuracy(str) {
507
+ var chars = '!-/:-@[-`{-~¡¿';
508
+ var accuracy = this.opt.accuracy,
509
+ lookbehind = '()',
510
+ pattern = str,
511
+ lookahead = '',
512
+ limiters;
513
+ if (accuracy !== 'partially') {
514
+ if (typeof accuracy !== 'string') {
515
+ limiters = this.preprocess(accuracy.limiters);
516
+ accuracy = accuracy.value;
517
+ }
518
+ if (accuracy === 'exactly') {
519
+ var charSet = limiters ? '[\\s' + limiters + ']' : '\\s';
520
+ lookbehind = "(^|".concat(charSet, ")");
521
+ lookahead = "(?=$|".concat(charSet, ")");
522
+ } else {
523
+ var chs = limiters || chars,
524
+ _charSet = "[^\\s".concat(chs, "]*");
525
+ pattern = "(?:".concat(str, ")");
526
+ if (accuracy === 'complementary') {
527
+ pattern = _charSet + pattern + _charSet;
528
+ } else if (accuracy === 'startsWith') {
529
+ lookbehind = "(^|[\\s".concat(chs, "])");
530
+ pattern = pattern.split(/\[\\s\]\+/).join(_charSet + '[\\s]+') + _charSet;
531
+ }
532
+ }
533
+ }
534
+ return {
535
+ lookbehind: lookbehind,
536
+ pattern: pattern,
537
+ lookahead: lookahead
538
+ };
539
+ }
540
+ }]);
541
+ return RegExpCreator;
542
+ }();
543
+
544
+ var Mark$1 = /*#__PURE__*/function () {
545
+ function Mark(ctx) {
546
+ _classCallCheck(this, Mark);
547
+ this.ctx = ctx;
548
+ this.nodeNames = ['script', 'style', 'title', 'head', 'html'];
549
+ }
550
+ _createClass(Mark, [{
551
+ key: "opt",
552
+ get: function get() {
553
+ return this._opt;
554
+ },
555
+ set: function set(val) {
556
+ if (!(val && val.window && val.window.document) && typeof window === 'undefined') {
557
+ throw new Error('Mark.js: please provide a window object as an option.');
558
+ }
559
+ var win = val && val.window || window,
560
+ highlight = val && val.highlight && val.highlight instanceof Highlight;
561
+ this._opt = _extends({}, {
562
+ 'window': win,
563
+ 'element': '',
564
+ 'className': '',
565
+ 'exclude': [],
566
+ 'iframes': false,
567
+ 'iframesTimeout': 5000,
568
+ 'separateWordSearch': true,
569
+ 'staticRanges': true,
570
+ 'rangeAcrossElements': true,
571
+ 'acrossElements': false,
572
+ 'ignoreGroups': 0,
573
+ 'each': function each() {},
574
+ 'noMatch': function noMatch() {},
575
+ 'filter': function filter() {
576
+ return true;
577
+ },
578
+ 'done': function done() {},
579
+ 'debug': false,
580
+ 'log': win.console
581
+ }, val);
582
+ if (!this._opt.element) {
583
+ this._opt.element = 'mark';
584
+ }
585
+ this.filter = win.NodeFilter;
586
+ this.empty = win.document.createTextNode('');
587
+ if (!this._opt.highlightName) {
588
+ this._opt.highlightName = 'advanced-markjs';
589
+ }
590
+ if (highlight) {
591
+ this.rangeArray = [];
592
+ } else {
593
+ this._opt.highlight = null;
594
+ }
595
+ }
596
+ }, {
597
+ key: "iterator",
598
+ get: function get() {
599
+ return new DOMIterator(this.ctx, this.opt);
600
+ }
601
+ }, {
602
+ key: "log",
603
+ value: function log(msg) {
604
+ var level = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'debug';
605
+ if (this.opt.debug) {
606
+ var log = this.opt.log;
607
+ if (_typeof(log) === 'object' && typeof log[level] === 'function') {
608
+ log[level]("mark.js: ".concat(msg));
609
+ }
610
+ }
611
+ }
612
+ }, {
613
+ key: "report",
614
+ value: function report(array) {
615
+ var _this = this;
616
+ array.forEach(function (item) {
617
+ _this.log("".concat(item.text, " ").concat(JSON.stringify(item.obj)), item.level || 'debug');
618
+ if (!item.skip) {
619
+ _this.opt.noMatch(item.obj);
620
+ }
621
+ });
622
+ }
623
+ }, {
624
+ key: "getSeachTerms",
625
+ value: function getSeachTerms(sv) {
626
+ var search = typeof sv === 'string' ? [sv] : sv,
627
+ separate = this.opt.separateWordSearch,
628
+ array = [],
629
+ termStats = {},
630
+ split = function split(str) {
631
+ str.split(/ +/).forEach(function (word) {
632
+ return add(word);
633
+ });
634
+ },
635
+ add = function add(str) {
636
+ if (str.trim() && !array.includes(str)) {
637
+ array.push(str);
638
+ termStats[str] = 0;
639
+ }
640
+ };
641
+ search.forEach(function (str) {
642
+ if (separate) {
643
+ if (separate === 'preserveTerms') {
644
+ str.split(/"("*[^"]+"*)"/).forEach(function (term, i) {
645
+ if (i % 2 > 0) add(term);else split(term);
646
+ });
647
+ } else {
648
+ split(str);
649
+ }
650
+ } else {
651
+ add(str);
652
+ }
653
+ });
654
+ array.sort(function (a, b) {
655
+ return b.length - a.length;
656
+ });
657
+ return {
658
+ terms: array,
659
+ termStats: termStats
660
+ };
661
+ }
662
+ }, {
663
+ key: "isNumeric",
664
+ value: function isNumeric(value) {
665
+ return Number(parseFloat(value)) == value;
666
+ }
667
+ }, {
668
+ key: "checkRanges",
669
+ value: function checkRanges(array, logs, min, max) {
670
+ var _this2 = this;
671
+ var level = 'error';
672
+ var ranges = array.filter(function (range) {
673
+ if (_this2.isNumeric(range.start) && _this2.isNumeric(range.length)) {
674
+ range.start = parseInt(range.start);
675
+ range.length = parseInt(range.length);
676
+ if (range.start >= min && range.start < max && range.length > 0) {
677
+ return true;
678
+ }
679
+ }
680
+ logs.push({
681
+ text: 'Invalid range: ',
682
+ obj: range,
683
+ level: level
684
+ });
685
+ return false;
686
+ }).sort(function (a, b) {
687
+ return a.start - b.start;
688
+ });
689
+ if (this.opt.wrapAllRanges) {
690
+ return ranges;
691
+ }
692
+ var lastIndex = 0,
693
+ index;
694
+ return ranges.filter(function (range) {
695
+ index = range.start + range.length;
696
+ if (range.start >= lastIndex) {
697
+ lastIndex = index;
698
+ return true;
699
+ }
700
+ logs.push({
701
+ text: (index < lastIndex ? 'Nest' : 'Overlapp') + 'ing range: ',
702
+ obj: range,
703
+ level: level
704
+ });
705
+ return false;
706
+ });
707
+ }
708
+ }, {
709
+ key: "setType",
710
+ value: function setType(tags, boundary) {
711
+ var custom = Array.isArray(boundary.tagNames) && boundary.tagNames.length;
712
+ if (custom) {
713
+ boundary.tagNames.forEach(function (name) {
714
+ return tags[name.toLowerCase()] = 2;
715
+ });
716
+ }
717
+ if (!custom || boundary.extend) {
718
+ for (var key in tags) {
719
+ tags[key] = 2;
720
+ }
721
+ }
722
+ tags['br'] = 3;
723
+ }
724
+ }, {
725
+ key: "getTextNodesAcross",
726
+ value: function getTextNodesAcross(cb) {
727
+ var _this3 = this;
728
+ var tags = {
729
+ div: 1,
730
+ p: 1,
731
+ li: 1,
732
+ td: 1,
733
+ tr: 1,
734
+ th: 1,
735
+ ul: 1,
736
+ ol: 1,
737
+ dd: 1,
738
+ dl: 1,
739
+ dt: 1,
740
+ h1: 1,
741
+ h2: 1,
742
+ h3: 1,
743
+ h4: 1,
744
+ h5: 1,
745
+ h6: 1,
746
+ hr: 1,
747
+ blockquote: 1,
748
+ figcaption: 1,
749
+ figure: 1,
750
+ pre: 1,
751
+ table: 1,
752
+ thead: 1,
753
+ tbody: 1,
754
+ tfoot: 1,
755
+ input: 1,
756
+ img: 1,
757
+ nav: 1,
758
+ details: 1,
759
+ label: 1,
760
+ form: 1,
761
+ select: 1,
762
+ menu: 1,
763
+ br: 3,
764
+ menuitem: 1,
765
+ main: 1,
766
+ section: 1,
767
+ article: 1,
768
+ aside: 1,
769
+ picture: 1,
770
+ output: 1,
771
+ button: 1,
772
+ header: 1,
773
+ footer: 1,
774
+ address: 1,
775
+ area: 1,
776
+ canvas: 1,
777
+ map: 1,
778
+ fieldset: 1,
779
+ textarea: 1,
780
+ track: 1,
781
+ video: 1,
782
+ audio: 1,
783
+ body: 1,
784
+ iframe: 1,
785
+ meter: 1,
786
+ object: 1,
787
+ svg: 1
788
+ };
789
+ var nodes = [],
790
+ boundary = this.opt.blockElementsBoundary,
791
+ priorityType = boundary ? 2 : 1;
792
+ var ch = '\x01',
793
+ tempType,
794
+ type,
795
+ prevNode;
796
+ if (boundary) {
797
+ this.setType(tags, boundary);
798
+ if (boundary["char"]) {
799
+ ch = boundary["char"].charAt(0);
800
+ }
801
+ }
802
+ var obj = {
803
+ text: '',
804
+ regex: /\s/,
805
+ tags: tags,
806
+ boundary: boundary,
807
+ str: '',
808
+ ch: ch
809
+ };
810
+ this.iterator.forEachNode(this.filter.SHOW_ELEMENT | this.filter.SHOW_TEXT, function (node) {
811
+ if (prevNode) {
812
+ nodes.push(_this3.getNodeInfo(prevNode, node, type, obj));
813
+ }
814
+ type = null;
815
+ prevNode = node;
816
+ }, function (node) {
817
+ if (node.nodeType === 1) {
818
+ tempType = tags[node.nodeName.toLowerCase()];
819
+ if (tempType === 3) {
820
+ obj.str += '\n';
821
+ }
822
+ if (!type || tempType === priorityType) {
823
+ type = tempType;
824
+ }
825
+ return false;
826
+ }
827
+ return !_this3.excluded(node.parentNode);
828
+ }, function () {
829
+ if (prevNode) {
830
+ nodes.push(_this3.getNodeInfo(prevNode, null, type, obj));
831
+ }
832
+ cb({
833
+ text: obj.text,
834
+ nodes: nodes,
835
+ lastIndex: 0
836
+ });
837
+ });
838
+ }
839
+ }, {
840
+ key: "getNodeInfo",
841
+ value: function getNodeInfo(prevNode, node, type, obj) {
842
+ var start = obj.text.length,
843
+ ch = obj.ch;
844
+ var offset = 0,
845
+ str = obj.str,
846
+ text = prevNode.textContent;
847
+ if (node) {
848
+ var startBySpace = obj.regex.test(node.textContent[0]),
849
+ both = startBySpace && obj.regex.test(text[text.length - 1]);
850
+ if (obj.boundary || !both) {
851
+ var separate = type;
852
+ if (!type) {
853
+ var parent = prevNode.parentNode;
854
+ while (parent) {
855
+ type = obj.tags[parent.nodeName.toLowerCase()];
856
+ if (type) {
857
+ separate = !(parent === node.parentNode || parent.contains(node));
858
+ break;
859
+ }
860
+ parent = parent.parentNode;
861
+ }
862
+ }
863
+ if (separate) {
864
+ if (!both) {
865
+ str += type === 1 ? ' ' : type === 2 ? ' ' + ch + ' ' : '';
866
+ } else if (type === 2) {
867
+ str += both ? ch : startBySpace ? ' ' + ch : ch + ' ';
868
+ }
869
+ }
870
+ }
871
+ }
872
+ if (str) {
873
+ text += str;
874
+ offset = str.length;
875
+ obj.str = '';
876
+ }
877
+ obj.text += text;
878
+ return this.createInfo(prevNode, start, obj.text.length - offset, offset);
879
+ }
880
+ }, {
881
+ key: "getRangesTextNodes",
882
+ value: function getRangesTextNodes(cb, lines) {
883
+ var _this4 = this;
884
+ var nodes = [],
885
+ regex = /\n/g,
886
+ newLines = [0],
887
+ show = this.filter.SHOW_TEXT | (lines ? this.filter.SHOW_ELEMENT : 0);
888
+ var text = '',
889
+ len = 0,
890
+ rm;
891
+ this.iterator.forEachNode(show, function (node) {
892
+ if (lines) {
893
+ while ((rm = regex.exec(node.textContent)) !== null) {
894
+ newLines.push(len + rm.index);
895
+ }
896
+ }
897
+ text += node.textContent;
898
+ nodes.push({
899
+ start: len,
900
+ end: len = text.length,
901
+ offset: 0,
902
+ node: node
903
+ });
904
+ }, function (node) {
905
+ if (lines && node.nodeType === 1) {
906
+ if (node.tagName.toLowerCase() === 'br') {
907
+ newLines.push(len);
908
+ }
909
+ return false;
910
+ }
911
+ return !_this4.excluded(node.parentNode);
912
+ }, function () {
913
+ var dict = {
914
+ text: text,
915
+ nodes: nodes,
916
+ lastIndex: 0
917
+ };
918
+ if (lines) {
919
+ newLines.push(len);
920
+ dict.newLines = newLines;
921
+ }
922
+ cb(dict);
923
+ });
924
+ }
925
+ }, {
926
+ key: "getTextNodes",
927
+ value: function getTextNodes(cb) {
928
+ var _this5 = this;
929
+ var nodes = [];
930
+ var start = 0;
931
+ this.iterator.forEachNode(this.filter.SHOW_TEXT, function (node) {
932
+ nodes.push({
933
+ node: node,
934
+ start: start
935
+ });
936
+ start += node.textContent.length;
937
+ }, function (node) {
938
+ return !_this5.excluded(node.parentNode);
939
+ }, function () {
940
+ cb({
941
+ nodes: nodes,
942
+ lastIndex: 0
943
+ });
944
+ });
945
+ }
946
+ }, {
947
+ key: "excluded",
948
+ value: function excluded(elem) {
949
+ return this.nodeNames.includes(elem.nodeName.toLowerCase()) || DOMIterator.matches(elem, this.opt.exclude);
950
+ }
951
+ }, {
952
+ key: "wrapRangeInsert",
953
+ value: function wrapRangeInsert(dict, n, s, e, start, index) {
954
+ var ended = e === n.node.textContent.length,
955
+ end = n.end;
956
+ var type = 1,
957
+ splitIndex = e,
958
+ node = n.node;
959
+ if (s !== 0) {
960
+ node = node.splitText(s);
961
+ splitIndex = e - s;
962
+ type = ended ? 2 : 3;
963
+ } else if (ended) {
964
+ type = 0;
965
+ }
966
+ var retNode = ended ? this.empty : node.splitText(splitIndex),
967
+ mark = this.createElement(node),
968
+ markChild = mark.childNodes[0],
969
+ nodeInfo = this.createInfo(retNode, type === 0 || type === 2 ? end : n.start + e, end, n.offset);
970
+ if (type === 0) {
971
+ n.node = markChild;
972
+ return {
973
+ mark: mark,
974
+ nodeInfo: nodeInfo,
975
+ increment: 0
976
+ };
977
+ }
978
+ var info = this.createInfo(markChild, type === 1 ? n.start : start, n.start + e, 0);
979
+ if (type === 1) {
980
+ dict.nodes.splice(index, 1, info, nodeInfo);
981
+ } else {
982
+ if (type === 2) {
983
+ dict.nodes.splice(index + 1, 0, info);
984
+ } else {
985
+ dict.nodes.splice(index + 1, 0, info, nodeInfo);
986
+ }
987
+ n.end = start;
988
+ n.offset = 0;
989
+ }
990
+ return {
991
+ mark: mark,
992
+ nodeInfo: nodeInfo,
993
+ increment: type < 3 ? 1 : 2
994
+ };
995
+ }
996
+ }, {
997
+ key: "createInfo",
998
+ value: function createInfo(node, start, end, offset) {
999
+ return {
1000
+ node: node,
1001
+ start: start,
1002
+ end: end,
1003
+ offset: offset
1004
+ };
1005
+ }
1006
+ }, {
1007
+ key: "wrapRange",
1008
+ value: function wrapRange(n, start, end, eachCb) {
1009
+ var node = n.node,
1010
+ retNode;
1011
+ if (this.rangeArray) {
1012
+ this.createRange(node, start, node, end, n.start + start, eachCb);
1013
+ retNode = node;
1014
+ } else {
1015
+ var ended = end === node.textContent.length,
1016
+ index = end;
1017
+ if (start !== 0) {
1018
+ node = node.splitText(start);
1019
+ index = end - start;
1020
+ }
1021
+ retNode = ended ? this.empty : node.splitText(index);
1022
+ eachCb(this.createElement(node));
1023
+ }
1024
+ return retNode;
1025
+ }
1026
+ }, {
1027
+ key: "createRange",
1028
+ value: function createRange(startNode, startOffset, endNode, endOffset, absoluteOffset, eachCb) {
1029
+ var range;
1030
+ if (this.opt.staticRanges) {
1031
+ range = new StaticRange({
1032
+ startContainer: startNode,
1033
+ startOffset: startOffset,
1034
+ endContainer: endNode,
1035
+ endOffset: endOffset
1036
+ });
1037
+ } else {
1038
+ range = new Range();
1039
+ range.setStart(startNode, startOffset);
1040
+ range.setEnd(endNode, endOffset);
1041
+ }
1042
+ range.absoluteOffset = absoluteOffset;
1043
+ eachCb(range, true);
1044
+ if (range) this.rangeArray.push(range);
1045
+ }
1046
+ }, {
1047
+ key: "createElement",
1048
+ value: function createElement(node) {
1049
+ var markNode = this.opt.window.document.createElement(this.opt.element);
1050
+ markNode.setAttribute('data-markjs', 'true');
1051
+ if (this.opt.className) {
1052
+ markNode.setAttribute('class', this.opt.className);
1053
+ }
1054
+ markNode.textContent = node.textContent;
1055
+ node.parentNode.replaceChild(markNode, node);
1056
+ return markNode;
1057
+ }
1058
+ }, {
1059
+ key: "wrapRangeAcross",
1060
+ value: function wrapRangeAcross(dict, start, end, filterCb, eachCb) {
1061
+ var i = dict.lastIndex,
1062
+ rangeStart = true,
1063
+ startInfo,
1064
+ filterNodes = [],
1065
+ e;
1066
+ var wrapAllRanges = this.opt.wrapAllRanges,
1067
+ highlightAPI = !!this.opt.highlight,
1068
+ singleRange = highlightAPI && this.opt.rangeAcrossElements;
1069
+ if (wrapAllRanges) {
1070
+ while (i > 0 && dict.nodes[i].start > start) {
1071
+ i--;
1072
+ }
1073
+ }
1074
+ for (i; i < dict.nodes.length; i++) {
1075
+ if (i + 1 === dict.nodes.length || dict.nodes[i + 1].start > start) {
1076
+ var n = dict.nodes[i];
1077
+ if (singleRange) {
1078
+ filterNodes.push(n.node);
1079
+ } else if (!filterCb(n.node)) {
1080
+ break;
1081
+ }
1082
+ var s = start - n.start;
1083
+ e = (end > n.end ? n.end : end) - n.start;
1084
+ if (s >= 0 && e > s) {
1085
+ if (singleRange) {
1086
+ if (rangeStart) {
1087
+ startInfo = [n.node, s, n.start + s];
1088
+ }
1089
+ } else if (!highlightAPI && wrapAllRanges) {
1090
+ var obj = this.wrapRangeInsert(dict, n, s, e, start, i);
1091
+ n = obj.nodeInfo;
1092
+ eachCb(obj.mark, rangeStart);
1093
+ } else {
1094
+ n.node = this.wrapRange(n, s, e, function (elemOrRange) {
1095
+ eachCb(elemOrRange, rangeStart);
1096
+ });
1097
+ if (!highlightAPI) n.start += e;
1098
+ }
1099
+ rangeStart = false;
1100
+ }
1101
+ if (end > n.end) {
1102
+ start = n.end + n.offset;
1103
+ } else {
1104
+ if (startInfo && filterCb(filterNodes)) {
1105
+ this.createRange(startInfo[0], startInfo[1], n.node, e, startInfo[2], eachCb);
1106
+ }
1107
+ break;
1108
+ }
1109
+ }
1110
+ }
1111
+ dict.lastIndex = i;
1112
+ }
1113
+ }, {
1114
+ key: "wrapGroups",
1115
+ value: function wrapGroups(n, match, regex, filterCb, eachCb) {
1116
+ var lastIndex = 0,
1117
+ offset = 0,
1118
+ i = 0,
1119
+ highlightAPI = this.opt.highlight,
1120
+ isWrapped = false,
1121
+ group,
1122
+ start,
1123
+ end = 0;
1124
+ while (++i < match.length) {
1125
+ group = match[i];
1126
+ if (group) {
1127
+ start = match.indices[i][0];
1128
+ if (start >= lastIndex) {
1129
+ end = match.indices[i][1];
1130
+ if (filterCb(n.node, group, i)) {
1131
+ n.node = this.wrapRange(n, start - offset, end - offset, function (elemOrRange) {
1132
+ eachCb(elemOrRange);
1133
+ });
1134
+ if (end > lastIndex) {
1135
+ lastIndex = end;
1136
+ }
1137
+ if (!highlightAPI) offset = end;
1138
+ isWrapped = true;
1139
+ }
1140
+ }
1141
+ }
1142
+ }
1143
+ if (isWrapped) {
1144
+ if (!highlightAPI) regex.lastIndex = 0;
1145
+ } else if (match[0].length === 0) {
1146
+ this.setLastIndex(regex, end);
1147
+ }
1148
+ }
1149
+ }, {
1150
+ key: "wrapGroupsAcross",
1151
+ value: function wrapGroupsAcross(dict, match, regex, filterCb, eachCb) {
1152
+ var lastIndex = 0,
1153
+ i = 0,
1154
+ end = 0,
1155
+ start,
1156
+ group,
1157
+ isWrapped;
1158
+ while (++i < match.length) {
1159
+ group = match[i];
1160
+ if (group) {
1161
+ start = match.indices[i][0];
1162
+ if (this.opt.wrapAllRanges || start >= lastIndex) {
1163
+ end = match.indices[i][1];
1164
+ isWrapped = false;
1165
+ this.wrapRangeAcross(dict, start, end, function (nodeOrArray) {
1166
+ return filterCb(nodeOrArray, group, i);
1167
+ }, function (elemOrRange, groupStart) {
1168
+ isWrapped = true;
1169
+ eachCb(elemOrRange, groupStart);
1170
+ });
1171
+ if (isWrapped && end > lastIndex) {
1172
+ lastIndex = end;
1173
+ }
1174
+ }
1175
+ }
1176
+ }
1177
+ if (match[0].length === 0) {
1178
+ this.setLastIndex(regex, end);
1179
+ }
1180
+ }
1181
+ }, {
1182
+ key: "setLastIndex",
1183
+ value: function setLastIndex(regex, end) {
1184
+ var index = regex.lastIndex;
1185
+ regex.lastIndex = end > index ? end : end > 0 ? index + 1 : Infinity;
1186
+ }
1187
+ }, {
1188
+ key: "processGroups",
1189
+ value: function processGroups(regex, unused, info, filterCb, eachCb, endCb) {
1190
+ var _this6 = this;
1191
+ var count = info.count,
1192
+ match,
1193
+ filterStart,
1194
+ eachStart;
1195
+ this.getTextNodes(function (dict) {
1196
+ dict.nodes.every(function (n) {
1197
+ while ((match = regex.exec(n.node.textContent)) !== null) {
1198
+ info.match = match;
1199
+ filterStart = eachStart = true;
1200
+ _this6.wrapGroups(n, match, regex, function (node, group, grIndex) {
1201
+ info.matchStart = filterStart;
1202
+ info.groupIndex = grIndex;
1203
+ filterStart = false;
1204
+ return filterCb(node, group, info);
1205
+ }, function (elemOrRange) {
1206
+ if (eachStart) count++;
1207
+ info.count = count;
1208
+ info.matchStart = eachStart;
1209
+ eachStart = false;
1210
+ eachCb(elemOrRange, info);
1211
+ });
1212
+ if (info.abort) break;
1213
+ }
1214
+ return !info.abort;
1215
+ });
1216
+ endCb(count);
1217
+ });
1218
+ }
1219
+ }, {
1220
+ key: "processGroupsAcross",
1221
+ value: function processGroupsAcross(regex, unused, info, filterCb, eachCb, endCb) {
1222
+ var _this7 = this;
1223
+ var count = info.count,
1224
+ match,
1225
+ filterStart,
1226
+ eachStart;
1227
+ this.getTextNodesAcross(function (dict) {
1228
+ while ((match = regex.exec(dict.text)) !== null) {
1229
+ info.match = match;
1230
+ filterStart = eachStart = true;
1231
+ _this7.wrapGroupsAcross(dict, match, regex, function (nodeOrArray, group, grIndex) {
1232
+ info.groupStart = undefined;
1233
+ info.matchStart = filterStart;
1234
+ info.groupIndex = grIndex;
1235
+ filterStart = false;
1236
+ return filterCb(nodeOrArray, group, info);
1237
+ }, function (elemOrRange, groupStart) {
1238
+ if (eachStart) count++;
1239
+ info.count = count;
1240
+ info.matchStart = eachStart;
1241
+ info.groupStart = groupStart;
1242
+ eachCb(elemOrRange, info);
1243
+ eachStart = false;
1244
+ });
1245
+ if (info.abort) break;
1246
+ }
1247
+ endCb(count);
1248
+ });
1249
+ }
1250
+ }, {
1251
+ key: "processMatches",
1252
+ value: function processMatches(regex, ignoreGroups, info, filterCb, eachCb, endCb) {
1253
+ var _this8 = this;
1254
+ var index = ignoreGroups === 0 ? 0 : ignoreGroups + 1;
1255
+ var count = info.count,
1256
+ match,
1257
+ str;
1258
+ this.getTextNodes(function (dict) {
1259
+ dict.nodes.every(function (n) {
1260
+ while ((match = regex.exec(n.node.textContent)) !== null) {
1261
+ if ((str = match[index]) === '') {
1262
+ regex.lastIndex++;
1263
+ continue;
1264
+ }
1265
+ info.match = match;
1266
+ if (!filterCb(n.node, str, info)) {
1267
+ continue;
1268
+ }
1269
+ var i = 0,
1270
+ start = match.index;
1271
+ while (++i < index) {
1272
+ if (match[i]) {
1273
+ start += match[i].length;
1274
+ }
1275
+ }
1276
+ n.node = _this8.wrapRange(n, start, start + str.length, function (elemOrRange) {
1277
+ info.count = ++count;
1278
+ eachCb(elemOrRange, info);
1279
+ });
1280
+ if (!_this8.opt.highlight) regex.lastIndex = 0;
1281
+ if (info.abort) break;
1282
+ }
1283
+ return !info.abort;
1284
+ });
1285
+ endCb(count);
1286
+ });
1287
+ }
1288
+ }, {
1289
+ key: "processMatchesAcross",
1290
+ value: function processMatchesAcross(regex, ignoreGroups, info, filterCb, eachCb, endCb) {
1291
+ var _this9 = this;
1292
+ var index = ignoreGroups === 0 ? 0 : ignoreGroups + 1;
1293
+ var count = info.count,
1294
+ match,
1295
+ str,
1296
+ matchStart;
1297
+ this.getTextNodesAcross(function (dict) {
1298
+ while ((match = regex.exec(dict.text)) !== null) {
1299
+ if ((str = match[index]) === '') {
1300
+ regex.lastIndex++;
1301
+ continue;
1302
+ }
1303
+ info.match = match;
1304
+ matchStart = true;
1305
+ var i = 0,
1306
+ start = match.index;
1307
+ while (++i < index) {
1308
+ if (match[i]) {
1309
+ start += match[i].length;
1310
+ }
1311
+ }
1312
+ _this9.wrapRangeAcross(dict, start, start + str.length, function (nodeOrArray) {
1313
+ info.matchStart = matchStart;
1314
+ matchStart = false;
1315
+ return filterCb(nodeOrArray, str, info);
1316
+ }, function (elemOrRange, mStart) {
1317
+ if (mStart) count++;
1318
+ info.count = count;
1319
+ info.matchStart = mStart;
1320
+ eachCb(elemOrRange, info);
1321
+ });
1322
+ if (info.abort) break;
1323
+ }
1324
+ endCb(count);
1325
+ });
1326
+ }
1327
+ }, {
1328
+ key: "processRanges",
1329
+ value: function processRanges(ranges, filterCb, eachCb, endCb) {
1330
+ var _this10 = this;
1331
+ var lines = this.opt.markLines,
1332
+ logs = [],
1333
+ skipped = [],
1334
+ level = 'warn';
1335
+ var count = 0;
1336
+ this.getRangesTextNodes(function (dict) {
1337
+ var max = lines ? dict.newLines.length : dict.text.length,
1338
+ array = _this10.checkRanges(ranges, logs, lines ? 1 : 0, max);
1339
+ array.forEach(function (range, index) {
1340
+ var start = range.start,
1341
+ end = start + range.length;
1342
+ if (end > max) {
1343
+ logs.push({
1344
+ text: "Range was limited to: ".concat(max),
1345
+ obj: range,
1346
+ skip: true,
1347
+ level: level
1348
+ });
1349
+ end = max;
1350
+ }
1351
+ if (lines) {
1352
+ start = dict.newLines[start - 1];
1353
+ if (dict.text[start] === '\n') {
1354
+ start++;
1355
+ }
1356
+ end = dict.newLines[end - 1];
1357
+ }
1358
+ var substr = dict.text.slice(start, end);
1359
+ if (substr.trim()) {
1360
+ _this10.wrapRangeAcross(dict, start, end, function (nodeOrArray) {
1361
+ return filterCb(nodeOrArray, range, substr, index);
1362
+ }, function (elemOrRange, rangeStart) {
1363
+ if (rangeStart) {
1364
+ count++;
1365
+ }
1366
+ eachCb(elemOrRange, range, {
1367
+ matchStart: rangeStart,
1368
+ count: count
1369
+ });
1370
+ });
1371
+ } else {
1372
+ logs.push({
1373
+ text: 'Skipping whitespace only range: ',
1374
+ obj: range,
1375
+ level: level
1376
+ });
1377
+ skipped.push(range);
1378
+ }
1379
+ });
1380
+ _this10.log("Valid ranges: ".concat(JSON.stringify(array.filter(function (range) {
1381
+ return !skipped.includes(range);
1382
+ }))));
1383
+ endCb(count, logs);
1384
+ }, lines);
1385
+ }
1386
+ }, {
1387
+ key: "unwrapMatches",
1388
+ value: function unwrapMatches(node) {
1389
+ var parent = node.parentNode,
1390
+ first = node.firstChild;
1391
+ if (node.childNodes.length === 1) {
1392
+ if (first.nodeType === 3) {
1393
+ var previous = node.previousSibling,
1394
+ next = node.nextSibling;
1395
+ if (previous && previous.nodeType === 3) {
1396
+ if (next && next.nodeType === 3) {
1397
+ previous.nodeValue += first.nodeValue + next.nodeValue;
1398
+ parent.removeChild(next);
1399
+ } else {
1400
+ previous.nodeValue += first.nodeValue;
1401
+ }
1402
+ } else if (next && next.nodeType === 3) {
1403
+ next.nodeValue = first.nodeValue + next.nodeValue;
1404
+ } else {
1405
+ parent.replaceChild(node.firstChild, node);
1406
+ return;
1407
+ }
1408
+ parent.removeChild(node);
1409
+ } else {
1410
+ parent.replaceChild(node.firstChild, node);
1411
+ }
1412
+ } else {
1413
+ if (!first) {
1414
+ parent.removeChild(node);
1415
+ } else {
1416
+ var docFrag = this.opt.window.document.createDocumentFragment();
1417
+ while (node.firstChild) {
1418
+ docFrag.appendChild(node.removeChild(node.firstChild));
1419
+ }
1420
+ parent.replaceChild(docFrag, node);
1421
+ }
1422
+ parent.normalize();
1423
+ }
1424
+ }
1425
+ }, {
1426
+ key: "markRegExp",
1427
+ value: function markRegExp(regexp, opt) {
1428
+ var _this11 = this;
1429
+ this.opt = opt;
1430
+ var totalMarks = 0,
1431
+ matchesSoFar = 0,
1432
+ across = this.opt.acrossElements,
1433
+ fn = 'processMatches';
1434
+ if (this.opt.separateGroups) {
1435
+ if (!regexp.hasIndices) {
1436
+ throw new Error('Mark.js: RegExp must have a `d` flag');
1437
+ }
1438
+ fn = across ? 'processGroupsAcross' : 'processGroups';
1439
+ } else if (across) {
1440
+ fn = 'processMatchesAcross';
1441
+ }
1442
+ var info = {
1443
+ count: 0,
1444
+ abort: false
1445
+ };
1446
+ if (!regexp.global && !regexp.sticky) {
1447
+ var splits = regexp.toString().split('/');
1448
+ regexp = new RegExp(regexp.source, 'g' + splits[splits.length - 1]);
1449
+ this.log('RegExp is recompiled - it must have a `g` flag', 'warn');
1450
+ }
1451
+ this.log("RegExp \"".concat(regexp, "\""));
1452
+ this[fn](regexp, this.opt.ignoreGroups, info, function (nodeOrArray, match, filterInfo) {
1453
+ return _this11.opt.filter(nodeOrArray, match, matchesSoFar, filterInfo);
1454
+ }, function (elemOrRange, eachInfo) {
1455
+ matchesSoFar = eachInfo.count;
1456
+ totalMarks++;
1457
+ _this11.opt.each(elemOrRange, eachInfo);
1458
+ }, function (totalMatches) {
1459
+ if (totalMatches === 0) {
1460
+ _this11.opt.noMatch(regexp);
1461
+ }
1462
+ _this11.registerHighlight();
1463
+ _this11.opt.done(totalMarks, totalMatches);
1464
+ });
1465
+ }
1466
+ }, {
1467
+ key: "mark",
1468
+ value: function mark(sv, opt) {
1469
+ var _this12 = this;
1470
+ this.opt = opt;
1471
+ var _this$getSeachTerms = this.getSeachTerms(sv),
1472
+ terms = _this$getSeachTerms.terms,
1473
+ termStats = _this$getSeachTerms.termStats;
1474
+ if (!terms.length) {
1475
+ this.opt.done(0, 0, termStats);
1476
+ return;
1477
+ }
1478
+ var index = 0,
1479
+ totalMarks = 0,
1480
+ matchesSoFar = 0,
1481
+ term;
1482
+ var across = this.opt.acrossElements,
1483
+ fn = across ? 'processMatchesAcross' : 'processMatches',
1484
+ array = this.getRegExps(terms),
1485
+ info = {
1486
+ count: 0,
1487
+ abort: false
1488
+ };
1489
+ var loop = function loop(_ref) {
1490
+ var regex = _ref.regex,
1491
+ regTerms = _ref.regTerms;
1492
+ _this12.log("RegExp ".concat(regex));
1493
+ _this12[fn](regex, 1, info, function (nodeOrArray, _, filterInfo) {
1494
+ if (!across || filterInfo.matchStart) {
1495
+ term = _this12.getCurrentTerm(filterInfo.match, regTerms);
1496
+ }
1497
+ return _this12.opt.filter(nodeOrArray, term, matchesSoFar, termStats[term], filterInfo);
1498
+ }, function (elemOrRange, eachInfo) {
1499
+ totalMarks++;
1500
+ matchesSoFar = eachInfo.count;
1501
+ if (!across || eachInfo.matchStart) {
1502
+ termStats[term] += 1;
1503
+ }
1504
+ _this12.opt.each(elemOrRange, eachInfo);
1505
+ }, function (totalMatches) {
1506
+ var noMatches = regTerms.filter(function (term) {
1507
+ return termStats[term] === 0;
1508
+ });
1509
+ if (noMatches.length) {
1510
+ _this12.opt.noMatch(noMatches);
1511
+ }
1512
+ if (!info.abort && ++index < array.length) {
1513
+ loop(array[index]);
1514
+ } else {
1515
+ _this12.registerHighlight();
1516
+ _this12.opt.done(totalMarks, totalMatches, termStats);
1517
+ }
1518
+ });
1519
+ };
1520
+ loop(array[0]);
1521
+ }
1522
+ }, {
1523
+ key: "getCurrentTerm",
1524
+ value: function getCurrentTerm(match, terms) {
1525
+ var i = match.length;
1526
+ while (--i > 2) {
1527
+ if (match[i]) {
1528
+ return terms[i - 3];
1529
+ }
1530
+ }
1531
+ return ' ';
1532
+ }
1533
+ }, {
1534
+ key: "getRegExps",
1535
+ value: function getRegExps(terms) {
1536
+ var creator = new RegExpCreator(this.opt),
1537
+ option = this.opt.combineBy || this.opt.combinePatterns,
1538
+ length = terms.length,
1539
+ array = [];
1540
+ var num = 100,
1541
+ value;
1542
+ if (option === Infinity) {
1543
+ num = length;
1544
+ } else if (!isNaN(+option) && (value = parseInt(option)) > 0) {
1545
+ num = value;
1546
+ }
1547
+ for (var i = 0; i < length; i += num) {
1548
+ var chunk = terms.slice(i, Math.min(i + num, length));
1549
+ array.push({
1550
+ regex: creator.create(chunk),
1551
+ regTerms: chunk
1552
+ });
1553
+ }
1554
+ return array;
1555
+ }
1556
+ }, {
1557
+ key: "markRanges",
1558
+ value: function markRanges(ranges, opt) {
1559
+ var _this13 = this;
1560
+ this.opt = opt;
1561
+ if (Array.isArray(ranges)) {
1562
+ var totalMarks = 0;
1563
+ this.processRanges(ranges, function (nodeOrArray, range, match, index) {
1564
+ return _this13.opt.filter(nodeOrArray, range, match, index);
1565
+ }, function (elemOrRange, range, rangeInfo) {
1566
+ totalMarks++;
1567
+ _this13.opt.each(elemOrRange, range, rangeInfo);
1568
+ }, function (totalRanges, logs) {
1569
+ _this13.report(logs);
1570
+ _this13.registerHighlight();
1571
+ _this13.opt.done(totalMarks, totalRanges);
1572
+ });
1573
+ } else {
1574
+ this.report([{
1575
+ text: 'markRanges() accept an array of objects: ',
1576
+ obj: ranges,
1577
+ level: 'error'
1578
+ }]);
1579
+ this.opt.done(0, 0);
1580
+ }
1581
+ }
1582
+ }, {
1583
+ key: "unmark",
1584
+ value: function unmark(opt) {
1585
+ var _this14 = this;
1586
+ this.opt = opt;
1587
+ var registry = CSS.highlights,
1588
+ exclude = this.opt.exclude && this.opt.exclude.length;
1589
+ if (registry) {
1590
+ var names = this.opt.highlightName,
1591
+ highlight;
1592
+ if (typeof names === 'string') names = [names];
1593
+ names.forEach(function (name) {
1594
+ if ((highlight = registry.get(name)) && highlight.size) {
1595
+ registry["delete"](name);
1596
+ if (exclude) {
1597
+ highlight.forEach(function (range) {
1598
+ var node = range.startContainer;
1599
+ if (node.nodeType === 3) node = node.parentNode;
1600
+ if (!_this14.excluded(node)) highlight["delete"](range);
1601
+ });
1602
+ } else {
1603
+ highlight.clear();
1604
+ }
1605
+ if (highlight.size) registry.set(name, highlight);
1606
+ }
1607
+ });
1608
+ }
1609
+ if (this.opt.highlight) {
1610
+ this.opt.done();
1611
+ return;
1612
+ }
1613
+ var selector = this.opt.element + '[data-markjs]';
1614
+ if (this.opt.className) {
1615
+ selector += ".".concat(this.opt.className);
1616
+ }
1617
+ this.log("Removal selector \"".concat(selector, "\""));
1618
+ this.iterator.forEachNode(this.filter.SHOW_ELEMENT, function (node) {
1619
+ _this14.unwrapMatches(node);
1620
+ }, function (node) {
1621
+ return DOMIterator.matches(node, selector) && !(exclude && _this14.excluded(node));
1622
+ }, this.opt.done);
1623
+ }
1624
+ }, {
1625
+ key: "registerHighlight",
1626
+ value: function registerHighlight() {
1627
+ var _this15 = this;
1628
+ var highlight = this.opt.highlight;
1629
+ if (highlight) {
1630
+ var name = this.opt.highlightName,
1631
+ registry = CSS.highlights;
1632
+ if (this.rangeArray.length) {
1633
+ registry["delete"](name);
1634
+ if (highlight.size) {
1635
+ highlight.forEach(function (range) {
1636
+ _this15.rangeArray.push(range);
1637
+ });
1638
+ highlight.clear();
1639
+ }
1640
+ this.rangeArray.sort(function (a, b) {
1641
+ return a.absoluteOffset - b.absoluteOffset;
1642
+ });
1643
+ this.rangeArray.forEach(function (range) {
1644
+ highlight.add(range);
1645
+ });
1646
+ this.rangeArray = [];
1647
+ }
1648
+ if (highlight.size) registry.set(name, highlight);
1649
+ }
1650
+ }
1651
+ }]);
1652
+ return Mark;
1653
+ }();
1654
+
1655
+ function Mark(ctx) {
1656
+ var _this = this;
1657
+ var instance = new Mark$1(ctx);
1658
+ this.mark = function (sv, opt) {
1659
+ instance.mark(sv, opt);
1660
+ return _this;
1661
+ };
1662
+ this.markRegExp = function (sv, opt) {
1663
+ instance.markRegExp(sv, opt);
1664
+ return _this;
1665
+ };
1666
+ this.markRanges = function (sv, opt) {
1667
+ instance.markRanges(sv, opt);
1668
+ return _this;
1669
+ };
1670
+ this.unmark = function (opt) {
1671
+ instance.unmark(opt);
1672
+ return _this;
1673
+ };
1674
+ this.getVersion = function () {
1675
+ return '3.0.0';
1676
+ };
1677
+ return this;
1678
+ }
1679
+
1680
+ return Mark;
1681
+
1682
+ }));