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