forked from aptyInc/optimal-select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimal-select.js
4183 lines (3493 loc) · 415 KB
/
optimal-select.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["OptimalSelect"] = factory();
else
root["OptimalSelect"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 8);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
/**
* # Utilities
*
* Convenience helpers.
*/
/**
* Create an array with the DOM nodes of the list
*
* @param {NodeList} nodes - [description]
* @return {Array.<HTMLElement>} - [description]
*/
var convertNodeList = exports.convertNodeList = function convertNodeList(nodes) {
var length = nodes.length;
var arr = new Array(length);
for (var i = 0; i < length; i++) {
arr[i] = nodes[i];
}
return arr;
};
/**
* Escape special characters and line breaks as a simplified version of 'CSS.escape()'
*
* Description of valid characters: https://mathiasbynens.be/notes/css-escapes
*
* @param {String?} value - [description]
* @return {String} - [description]
*/
var escapeValue = exports.escapeValue = function escapeValue(value) {
return value && value.replace(/['"`\\/:?&!#$%^()[\]{|}*+;,.<=>@~]/g, '\\$&').replace(/\n/g, '\xA0');
};
/**
* Partition array into two groups determined by predicate
*/
var partition = exports.partition = function partition(array, predicate) {
return array.reduce(function (_ref, item) {
var _ref2 = _slicedToArray(_ref, 2),
inner = _ref2[0],
outer = _ref2[1];
return predicate(item) ? [inner.concat(item), outer] : [inner, outer.concat(item)];
}, [[], []]);
};
/**
* Determine if string is valid CSS identifier
*
* In CSS, identifiers (including element names, classes, and IDs in selectors) can contain
* only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-)
* and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by
* a digit.
*
* Identifiers can also contain escaped characters and any ISO 10646 character as a numeric
* code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".
* @param {String} value
* @return {Boolean}
*/
var isValidCSSIdentifier = exports.isValidCSSIdentifier = function isValidCSSIdentifier(value) {
return !!value && !/(^\d)|(^--)|(^-\d)/.test(value) && !/([^\\]|^)['"`/:?&!#$%^()[\]{|}*+;,.<=>@~]/.test(value);
};
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.checkIgnore = exports.combinations = exports.initOptions = exports.defaultIgnore = undefined;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; /**
* # Match
*
* Retrieve selector for a node.
*/
exports.default = match;
var _pattern = __webpack_require__(2);
var _selector = __webpack_require__(3);
var _utilities = __webpack_require__(0);
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
/**
* @typedef {import('./select').Options} Options
* @typedef {import('./pattern').Pattern} Pattern
* @typedef {import('./pattern').ToStringApi} Pattern
*/
var defaultIgnore = exports.defaultIgnore = {
attribute: function attribute(attributeName) {
return ['style', 'data-reactid', 'data-react-checksum'].indexOf(attributeName) > -1;
},
contains: function contains() {
return true;
}
};
var initOptions = exports.initOptions = function initOptions() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return _extends({}, options, {
root: options.root || document,
skip: options.skip || null,
priority: options.priority || ['id', 'class', 'href', 'src'],
ignore: options.ignore || {}
});
};
/**
* Get the path of the element
*
* @param {HTMLElement} node - [description]
* @param {Options} [options] - [description]
* @return {Array.<Pattern>} - [description]
*/
function match(node) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var nested = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
options = initOptions(options);
var _options = options,
root = _options.root,
skip = _options.skip,
ignore = _options.ignore,
format = _options.format;
var path = [];
var element = node;
var length = path.length;
var select = (0, _selector.getSelect)(options);
var toString = (0, _pattern.getToString)(options);
var skipCompare = skip && (Array.isArray(skip) ? skip : [skip]).map(function (entry) {
if (typeof entry !== 'function') {
return function (element) {
return element === entry;
};
}
return entry;
});
var skipChecks = function skipChecks(element) {
return skip && skipCompare.some(function (compare) {
return compare(element);
});
};
Object.keys(ignore).forEach(function (type) {
var predicate = ignore[type];
if (typeof predicate === 'function') return;
if (typeof predicate === 'number') {
predicate = predicate.toString();
}
if (typeof predicate === 'string') {
predicate = new RegExp((0, _utilities.escapeValue)(predicate).replace(/\\/g, '\\\\'));
}
if (typeof predicate === 'boolean') {
predicate = predicate ? /(?:)/ : /.^/;
}
// check class-/attributename for regex
ignore[type] = function (name, value) {
return predicate.test(value);
};
});
while (element !== root && element.nodeType !== 11) {
if (skipChecks(element) !== true) {
// ~ global
if (checkAttributes(element, path, options, select, toString, root)) break;
if (checkTag(element, path, options, select, toString, root)) break;
// ~ local
checkAttributes(element, path, options, select, toString);
if (path.length === length) {
checkTag(element, path, options, select, toString);
}
if (path.length === length && [1, 'xpath'].includes(format) && !nested && element === node) {
checkRecursiveDescendants(element, path, options, select, toString);
}
if (path.length === length && [1, 'xpath', 'jquery'].includes(format)) {
checkText(element, path, options, select, toString, format === 'jquery');
}
if (path.length === length) {
checkNthChild(element, path, options);
}
}
element = element.parentNode;
length = path.length;
}
if (element === root) {
var pattern = findPattern(element, options, select, toString);
path.unshift(pattern);
}
return path;
}
/**
* Extend path with attribute identifier
*
* @param {HTMLElement} element - [description]
* @param {Array.<Pattern>} path - [description]
* @param {Options} options - [description]
* @param {function} select - [description]
* @param {ToStringApi} toString - [description]
* @param {HTMLElement} parent - [description]
* @return {boolean} - [description]
*/
var checkAttributes = function checkAttributes(element, path, _ref, select, toString) {
var priority = _ref.priority,
ignore = _ref.ignore;
var parent = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : element.parentNode;
var pattern = findAttributesPattern(priority, element, ignore, select, toString, parent);
if (pattern) {
path.unshift(pattern);
return true;
}
return false;
};
/**
* Calculates array of combinations of items in input array.
* @param {Array.<any>} values - array of values
* @param {Object} options - options: min - minimum subset size; max - maximum subset size
* @return {Array.<Array.<any>>?} array of subsets
*/
var combinations = exports.combinations = function combinations(values, options) {
var _ref2 = options || {},
min = _ref2.min,
max = _ref2.max;
var result = [[]];
values.forEach(function (v) {
result.forEach(function (r) {
if (!max || r.length < max) {
result.push(r.concat(v));
}
});
});
result.shift();
return min ? result.filter(function (r) {
return r.length >= min;
}) : result;
};
// limit subset size to increase performance
var maxSubsetSize = [{ items: 13, max: 1 }, { items: 10, max: 2 }, { items: 8, max: 3 }, { items: 5, max: 4 }];
/**
* Get class selector
*
* @param {Array.<string>} classes - [description]
* @param {function} select - [description]
* @param {ToStringApi} toString - [description]
* @param {HTMLElement} parent - [description]
* @param {Pattern} base - [description]
* @return {Array.<string>?} - [description]
*/
var getClassSelector = function getClassSelector() {
var classes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var select = arguments[1];
var toString = arguments[2];
var parent = arguments[3];
var base = arguments[4];
var _ref3 = maxSubsetSize.find(function (_ref4) {
var items = _ref4.items;
return classes.length > items;
}) || { max: classes.length },
max = _ref3.max;
var result = combinations(classes, { max: max });
for (var i = 0; i < result.length; i++) {
var pattern = toString.pattern(_extends({}, base, { classes: result[i] }));
var matches = select(pattern, parent);
if (matches.length === 1) {
return result[i];
}
}
return null;
};
/**
* Lookup attribute identifier
*
* @param {Array.<string>} priority - [description]
* @param {HTMLElement} element - [description]
* @param {Object} ignore - [description]
* @param {function} select - [description]
* @param {ToStringApi} toString - [description]
* @param {ParentNode} parent - [description]
* @return {Pattern?} - [description]
*/
var findAttributesPattern = function findAttributesPattern(priority, element, ignore, select, toString) {
var parent = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : element.parentNode;
var attributes = element.attributes;
var attributeNames = Object.keys(attributes).map(function (val) {
return attributes[val].name;
}).filter(function (a) {
return priority.indexOf(a) < 0;
});
var sortedKeys = [].concat(_toConsumableArray(priority), _toConsumableArray(attributeNames));
var pattern = (0, _pattern.createPattern)();
pattern.tag = element.tagName.toLowerCase();
var isOptimal = function isOptimal(pattern) {
return select(toString.pattern(pattern), parent).length === 1;
};
for (var i = 0, l = sortedKeys.length; i < l; i++) {
var key = sortedKeys[i];
var attribute = attributes[key];
var attributeName = (0, _utilities.escapeValue)(attribute && attribute.name);
var attributeValue = (0, _utilities.escapeValue)(attribute && attribute.value);
var useNamedIgnore = attributeName !== 'class';
var currentIgnore = useNamedIgnore && ignore[attributeName] || ignore.attribute;
var currentDefaultIgnore = useNamedIgnore && defaultIgnore[attributeName] || defaultIgnore.attribute;
if (checkIgnore(currentIgnore, attributeName, attributeValue, currentDefaultIgnore)) {
continue;
}
switch (attributeName) {
case 'class':
{
var _ret = function () {
var classNames = attributeValue.trim().split(/\s+/g);
if (!classNames[0]) {
// empty string
return 'break';
}
var classIgnore = ignore.class || defaultIgnore.class;
if (classIgnore) {
classNames = classNames.filter(function (className) {
return !classIgnore(className);
});
}
if (classNames.length > 0) {
var classes = getClassSelector(classNames, select, toString, parent, pattern);
if (classes) {
pattern.classes = classes;
if (isOptimal(pattern)) {
return {
v: pattern
};
}
}
}
}();
switch (_ret) {
case 'break':
break;
default:
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}
}
break;
default:
pattern.attributes.push({ name: attributeName, value: attributeValue });
if (isOptimal(pattern)) {
return pattern;
}
}
}
return null;
};
/**
* Extend path with tag identifier
*
* @param {HTMLElement} element - [description]
* @param {Options} options - [description]
* @param {Array.<Pattern>} path - [description]
* @param {function} select - [description]
* @param {ToStringApi} toString - [description]
* @param {HTMLElement} parent - [description]
* @return {boolean} - [description]
*/
var checkTag = function checkTag(element, path, _ref5, select, toString) {
var ignore = _ref5.ignore;
var parent = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : element.parentNode;
var pattern = findTagPattern(element, ignore);
if (pattern) {
var matches = [];
matches = select(toString.pattern(pattern), parent);
if (matches.length === 1) {
path.unshift(pattern);
if (pattern.tag === 'iframe') {
return false;
}
return true;
}
}
return false;
};
/**
* Lookup tag identifier
*
* @param {HTMLElement} element - [description]
* @param {Object} ignore - [description]
* @return {Pattern?} - [description]
*/
var findTagPattern = function findTagPattern(element, ignore) {
var tagName = element.tagName.toLowerCase();
if (checkIgnore(ignore.tag, null, tagName)) {
return null;
}
var pattern = (0, _pattern.createPattern)();
pattern.tag = tagName;
return pattern;
};
/**
* Extend path with specific child identifier
*
* @param {HTMLElement} element - [description]
* @param {Options} options - [description]
* @param {Array.<Pattern>} path - [description]
* @return {boolean} - [description]
*/
var checkNthChild = function checkNthChild(element, path, _ref6) {
var ignore = _ref6.ignore;
var parent = element.parentNode;
var children = parent.children;
for (var i = 0, l = children.length; i < l; i++) {
var child = children[i];
if (child === element) {
var childPattern = findTagPattern(child, ignore);
if (!childPattern) {
return console.warn('\n Element couldn\'t be matched through strict ignore pattern!\n ', child, ignore, childPattern);
}
childPattern.relates = 'child';
childPattern.pseudo = ['nth-child(' + (i + 1) + ')'];
path.unshift(childPattern);
return true;
}
}
return false;
};
/**
* Extend path with contains
*
* @param {HTMLElement} element - [description]
* @param {Array.<Pattern>} path - [description]
* @param {Options} options - [description]
* @param {function} select - [description]
* @param {ToStringApi} toString - [description]
* @param {boolean} nested - [description]
* @return {boolean} - [description]
*/
var checkText = function checkText(element, path, _ref7, select, toString, nested) {
var ignore = _ref7.ignore;
var pattern = findTagPattern(element, ignore);
if (!pattern) {
return false;
}
var textContent = nested ? element.textContent : element.firstChild && element.firstChild.nodeValue || '';
if (!textContent) {
return false;
}
pattern.relates = 'child';
var parent = element.parentNode;
var texts = textContent.replace(/\n+/g, '\n').split('\n').map(function (text) {
return text.trim();
}).filter(function (text) {
return text.length > 0;
});
var contains = [];
while (texts.length > 0) {
var text = texts.shift();
if (checkIgnore(ignore.contains, null, text, defaultIgnore.contains)) {
break;
}
contains.push('contains("' + text + '")');
var matches = select(toString.pattern(_extends({}, pattern, { pseudo: contains })), parent);
if (matches.length === 1) {
pattern.pseudo = contains;
path.unshift(pattern);
return true;
}
if (matches.length === 0) {
return false;
}
}
return false;
};
/**
* Extend path with descendant tag
*
* @param {HTMLElement} element - [description]
* @param {Array.<Pattern>} path - [description]
* @param {Options} options - [description]
* @param {function} select - [description]
* @param {ToStringApi} toString - [description]
* @return {boolean} - [description]
*/
var checkRecursiveDescendants = function checkRecursiveDescendants(element, path, options, select, toString) {
var pattern = findTagPattern(element, options.ignore);
if (!pattern) {
return false;
}
var descendants = Array.from(element.querySelectorAll('*'));
while (descendants.length > 0) {
var descendantPath = match(descendants.shift(), _extends({}, options, { root: element }), true);
// avoid descendant selectors with nth-child
if (!descendantPath.some(function (pattern) {
return pattern.pseudo.some(function (p) {
return p.startsWith('nth-child');
});
})) {
var parent = element.parentElement;
var matches = select(toString.pattern(_extends({}, pattern, { descendants: [descendantPath] })), parent);
if (matches.length === 1) {
pattern.descendants = [descendantPath];
path.unshift(pattern);
return true;
}
}
}
return false;
};
/**
* Lookup identifier
*
* @param {HTMLElement} element - [description]
* @param {Options} options - [description]
* @param {function} select - [description]
* @param {ToStringApi} toString - [description]
* @return {Pattern} - [description]
*/
var findPattern = function findPattern(element, _ref8, select, toString) {
var priority = _ref8.priority,
ignore = _ref8.ignore;
var pattern = findAttributesPattern(priority, element, ignore, select, toString);
if (!pattern) {
pattern = findTagPattern(element, ignore);
}
return pattern;
};
/**
* Validate with custom and default functions
*
* @param {Function} predicate - [description]
* @param {string?} name - [description]
* @param {string} value - [description]
* @param {Function} defaultPredicate - [description]
* @return {boolean} - [description]
*/
var checkIgnore = exports.checkIgnore = function checkIgnore(predicate, name, value, defaultPredicate) {
if (!value) {
return true;
}
var check = predicate || defaultPredicate;
if (!check) {
return false;
}
return check(name, value, defaultPredicate);
};
/***/ },
/* 2 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getToString = exports.descendantsToXPath = exports.pathToXPath = exports.patternToXPath = exports.pseudoToXPath = exports.classesToXPath = exports.attributesToXPath = exports.pathToSelector = exports.patternToSelector = exports.pseudoToSelector = exports.classesToSelector = exports.attributesToSelector = exports.createPattern = undefined;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _utilities = __webpack_require__(0);
/**
* @typedef {Object} Pattern
* @property {('descendant' | 'child')} [relates]
* @property {string} [tag]
* @property {Array.<{ name: string, value: string? }>} attributes
* @property {Array.<string>} classes
* @property {Array.<string>} pseudo
* @property {Array.<Array.<Pattern>>} descendants
*/
/**
* Creates a new pattern structure
*
* @param {Partial<Pattern>} pattern
* @returns {Pattern}
*/
var createPattern = exports.createPattern = function createPattern() {
var base = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return _extends({ attributes: [], classes: [], pseudo: [], descendants: [] }, base);
};
/**
* Convert attributes to CSS selector
*
* @param {Array.<{ name: string, value: string? }>} attributes
* @returns {string}
*/
var attributesToSelector = exports.attributesToSelector = function attributesToSelector(attributes) {
return attributes.map(function (_ref) {
var name = _ref.name,
value = _ref.value;
if (value === null) {
return '[' + name + ']';
}
if (name === 'id' && (0, _utilities.isValidCSSIdentifier)(value)) {
return '#' + value;
}
return '[' + name + '="' + value + '"]';
}).join('');
};
/**
* Convert classes to CSS selector
*
* @param {Array.<string>} classes
* @returns {string}
*/
var classesToSelector = exports.classesToSelector = function classesToSelector(classes) {
return classes.map(function (c) {
return (0, _utilities.isValidCSSIdentifier)(c) ? '.' + c : '[class~="' + c + '"]';
}).join('');
};
/**
* Convert pseudo selectors to CSS selector
*
* @param {Array.<string>} pseudo
* @returns {string}
*/
var pseudoToSelector = exports.pseudoToSelector = function pseudoToSelector(pseudo) {
return pseudo.length ? ':' + pseudo.join(':') : '';
};
/**
* Convert pattern to CSS selector
*
* @param {Pattern} pattern
* @returns {string}
*/
var patternToSelector = exports.patternToSelector = function patternToSelector(pattern) {
var relates = pattern.relates,
tag = pattern.tag,
attributes = pattern.attributes,
classes = pattern.classes,
pseudo = pattern.pseudo;
var value = '' + (relates === 'child' ? '> ' : '') + (tag || '') + attributesToSelector(attributes) + classesToSelector(classes) + pseudoToSelector(pseudo);
return value;
};
/**
* Converts path to string
*
* @param {Array.<Pattern>} path
* @returns {string}
*/
var pathToSelector = exports.pathToSelector = function pathToSelector(path) {
return path.map(patternToSelector).join(' ');
};
var convertEscaping = function convertEscaping(value) {
return value && value.replace(/\\([`\\/:?&!#$%^()[\]{|}*+;,.<=>@~])/g, '$1').replace(/\\(['"])/g, '$1$1').replace(/\\A /g, '\n');
};
/**
* Convert attributes to XPath string
*
* @param {Array.<{ name: string, value: string? }>} attributes
* @returns {string}
*/
var attributesToXPath = exports.attributesToXPath = function attributesToXPath(attributes) {
return attributes.map(function (_ref2) {
var name = _ref2.name,
value = _ref2.value;
if (value === null) {
return '[@' + name + ']';
}
return '[@' + name + '="' + convertEscaping(value) + '"]';
}).join('');
};
/**
* Convert classes to XPath string
*
* @param {Array.<string>} classes
* @returns {string}
*/
var classesToXPath = exports.classesToXPath = function classesToXPath(classes) {
return classes.map(function (c) {
return '[contains(concat(" ",normalize-space(@class)," ")," ' + c + ' ")]';
}).join('');
};
/**
* Convert pseudo selectors to XPath string
*
* @param {Array.<string>} pseudo
* @returns {string}
*/
var pseudoToXPath = exports.pseudoToXPath = function pseudoToXPath(pseudo) {
return pseudo.map(function (p) {
var match = p.match(/^(nth-child|nth-of-type|contains)\((.+)\)$/);
if (!match) {
return '';
}
switch (match[1]) {
case 'nth-child':
return '[(count(preceding-sibling::*)+1) = ' + match[2] + ']';
case 'nth-of-type':
return '[' + match[2] + ']';
case 'contains':
return '[contains(text(),' + match[2] + ')]';
default:
return '';
}
}).join('');
};
/**
* Convert pattern to XPath string
*
* @param {Pattern} pattern
* @returns {string}
*/
var patternToXPath = exports.patternToXPath = function patternToXPath(pattern) {
var relates = pattern.relates,
tag = pattern.tag,
attributes = pattern.attributes,
classes = pattern.classes,
pseudo = pattern.pseudo,
descendants = pattern.descendants;
var value = '' + (relates === 'child' ? '/' : '//') + (tag || '*') + attributesToXPath(attributes) + classesToXPath(classes) + pseudoToXPath(pseudo) + descendantsToXPath(descendants);
return value;
};
/**
* Converts path to XPath string
*
* @param {Array.<Pattern>} path
* @returns {string}
*/
var pathToXPath = exports.pathToXPath = function pathToXPath(path) {
return '.' + path.map(patternToXPath).join('');
};
/**
* Convert child selectors to XPath string
*
* @param {Array.<Array.<Pattern>>} children
* @returns {string}
*/
var descendantsToXPath = exports.descendantsToXPath = function descendantsToXPath(children) {
return children.length ? '[' + children.map(pathToXPath).join('][') + ']' : '';
};
var toString = {
'css': {
attributes: attributesToSelector,
classes: classesToSelector,
pseudo: pseudoToSelector,
pattern: patternToSelector,
path: pathToSelector
},
'xpath': {
attributes: attributesToXPath,
classes: classesToXPath,
pseudo: pseudoToXPath,
pattern: patternToXPath,
path: pathToXPath
},
'jquery': {}
};
toString.jquery = toString.css;
toString[0] = toString.css;
toString[1] = toString.xpath;
/**
* @typedef {Object} ToStringApi
* @property {(attributes: Array.<{ name: string, value: string? }>) => string} attributes
* @property {(classes: Array.<string>) => string} classes
* @property {(pseudo: Array.<string>) => string} pseudo
* @property {(pattern: Pattern) => string} pattern
* @property {(path: Array.<Pattern>) => string} path
*/
/**
*
* @param {Options} options
* @returns {ToStringApi}
*/
var getToString = exports.getToString = function getToString() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return toString[options.format || 'css'];
};
/***/ },
/* 3 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
// import Sizzle from 'sizzle'
var Sizzle = void 0;
/**
* Select element using jQuery
* @param {string} selector
* @param {HTMLElement} parent
* @return Array.<HTMLElement>
*/
var selectJQuery = function selectJQuery(selector) {
var parent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
if (!Sizzle) {
Sizzle = __webpack_require__(7);
}
return Sizzle(selector, parent || document);
};
/**
* Select element using XPath
* @param {string} selector
* @param {HTMLElement} parent
* @return Array.<HTMLElement>
*/
var selectXPath = function selectXPath(selector) {
var parent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
parent = parent || document;
var doc = parent;
while (doc.parentNode) {
doc = doc.parentNode;
}
if (doc !== parent && !selector.startsWith('.')) {
selector = '.' + selector;
}
var iterator = doc.evaluate(selector, parent, null, 0);
var elements = [];
var element;
while (element = iterator.iterateNext()) {
elements.push(element);
}
return elements;
};
/**
* Select element using CSS
* @param {string} selector