forked from autarc/optimal-select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimal-select.js
1480 lines (1270 loc) · 133 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 = 6);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.convertNodeList = convertNodeList;
exports.escapeValue = escapeValue;
/**
* # Utilities
*
* Convenience helpers.
*/
/**
* Create an array with the DOM nodes of the list
*
* @param {NodeList} nodes - [description]
* @return {Array.<HTMLElement>} - [description]
*/
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]
*/
function escapeValue(value) {
return value && value.replace(/['"`\\/:\?&!#$%^()[\]{|}*+;,.<=>@~]/g, '\\$&').replace(/\n/g, '\A');
}
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getCommonAncestor = getCommonAncestor;
exports.getCommonProperties = getCommonProperties;
/**
* # Common
*
* Process collections for similarities.
*/
/**
* Find the last common ancestor of elements
*
* @param {Array.<HTMLElements>} elements - [description]
* @return {HTMLElement} - [description]
*/
function getCommonAncestor(elements) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _options$root = options.root,
root = _options$root === undefined ? document : _options$root;
var ancestors = [];
elements.forEach(function (element, index) {
var parents = [];
while (element !== root) {
element = element.parentNode;
parents.unshift(element);
}
ancestors[index] = parents;
});
ancestors.sort(function (curr, next) {
return curr.length - next.length;
});
var shallowAncestor = ancestors.shift();
var ancestor = null;
var _loop = function _loop() {
var parent = shallowAncestor[i];
var missing = ancestors.some(function (otherParents) {
return !otherParents.some(function (otherParent) {
return otherParent === parent;
});
});
if (missing) {
// TODO: find similar sub-parents, not the top root, e.g. sharing a class selector
return 'break';
}
ancestor = parent;
};
for (var i = 0, l = shallowAncestor.length; i < l; i++) {
var _ret = _loop();
if (_ret === 'break') break;
}
return ancestor;
}
/**
* Get a set of common properties of elements
*
* @param {Array.<HTMLElement>} elements - [description]
* @return {Object} - [description]
*/
function getCommonProperties(elements) {
var commonProperties = {
classes: [],
attributes: {},
tag: null
};
elements.forEach(function (element) {
var commonClasses = commonProperties.classes,
commonAttributes = commonProperties.attributes,
commonTag = commonProperties.tag;
// ~ classes
if (commonClasses !== undefined) {
var classes = element.getAttribute('class');
if (classes) {
classes = classes.trim().split(' ');
if (!commonClasses.length) {
commonProperties.classes = classes;
} else {
commonClasses = commonClasses.filter(function (entry) {
return classes.some(function (name) {
return name === entry;
});
});
if (commonClasses.length) {
commonProperties.classes = commonClasses;
} else {
delete commonProperties.classes;
}
}
} else {
// TODO: restructure removal as 2x set / 2x delete, instead of modify always replacing with new collection
delete commonProperties.classes;
}
}
// ~ attributes
if (commonAttributes !== undefined) {
(function () {
var elementAttributes = element.attributes;
var attributes = Object.keys(elementAttributes).reduce(function (attributes, key) {
var attribute = elementAttributes[key];
var attributeName = attribute.name;
// NOTE: workaround detection for non-standard phantomjs NamedNodeMap behaviour
// (issue: https://github.com/ariya/phantomjs/issues/14634)
if (attribute && attributeName !== 'class') {
attributes[attributeName] = attribute.value;
}
return attributes;
}, {});
var attributesNames = Object.keys(attributes);
var commonAttributesNames = Object.keys(commonAttributes);
if (attributesNames.length) {
if (!commonAttributesNames.length) {
commonProperties.attributes = attributes;
} else {
commonAttributes = commonAttributesNames.reduce(function (nextCommonAttributes, name) {
var value = commonAttributes[name];
if (value === attributes[name]) {
nextCommonAttributes[name] = value;
}
return nextCommonAttributes;
}, {});
if (Object.keys(commonAttributes).length) {
commonProperties.attributes = commonAttributes;
} else {
delete commonProperties.attributes;
}
}
} else {
delete commonProperties.attributes;
}
})();
}
// ~ tag
if (commonTag !== undefined) {
var tag = element.tagName.toLowerCase();
if (!commonTag) {
commonProperties.tag = tag;
} else if (tag !== commonTag) {
delete commonProperties.tag;
}
}
});
return commonProperties;
}
/***/ },
/* 2 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = optimize;
var _adapt = __webpack_require__(3);
var _adapt2 = _interopRequireDefault(_adapt);
var _utilities = __webpack_require__(0);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Apply different optimization techniques
*
* @param {string} selector - [description]
* @param {HTMLElement|Array.<HTMLElement>} element - [description]
* @param {Object} options - [description]
* @return {string} - [description]
*/
/**
* # Optimize
*
* 1.) Improve efficiency through shorter selectors by removing redundancy
* 2.) Improve robustness through selector transformation
*/
function optimize(selector, elements) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
// convert single entry and NodeList
if (!Array.isArray(elements)) {
elements = !elements.length ? [elements] : (0, _utilities.convertNodeList)(elements);
}
if (!elements.length || elements.some(function (element) {
return element.nodeType !== 1;
})) {
throw new Error('Invalid input - to compare HTMLElements its necessary to provide a reference of the selected node(s)! (missing "elements")');
}
var globalModified = (0, _adapt2.default)(elements[0], options);
// chunk parts outside of quotes (http://stackoverflow.com/a/25663729)
var path = selector.replace(/> /g, '>').split(/\s+(?=(?:(?:[^"]*"){2})*[^"]*$)/);
if (path.length < 2) {
return optimizePart('', selector, '', elements);
}
var shortened = [path.pop()];
while (path.length > 1) {
var current = path.pop();
var prePart = path.join(' ');
var postPart = shortened.join(' ');
var pattern = prePart + ' ' + postPart;
var matches = document.querySelectorAll(pattern);
if (matches.length !== elements.length) {
shortened.unshift(optimizePart(prePart, current, postPart, elements));
}
}
shortened.unshift(path[0]);
path = shortened;
// optimize start + end
path[0] = optimizePart('', path[0], path.slice(1).join(' '), elements);
path[path.length - 1] = optimizePart(path.slice(0, -1).join(' '), path[path.length - 1], '', elements);
if (globalModified) {
delete true;
}
return path.join(' ').replace(/>/g, '> ').trim();
}
/**
* Improve a chunk of the selector
*
* @param {string} prePart - [description]
* @param {string} current - [description]
* @param {string} postPart - [description]
* @param {Array.<HTMLElement>} elements - [description]
* @return {string} - [description]
*/
function optimizePart(prePart, current, postPart, elements) {
if (prePart.length) prePart = prePart + ' ';
if (postPart.length) postPart = ' ' + postPart;
// robustness: attribute without value (generalization)
if (/\[*\]/.test(current)) {
var key = current.replace(/=.*$/, ']');
var pattern = '' + prePart + key + postPart;
var matches = document.querySelectorAll(pattern);
if (compareResults(matches, elements)) {
current = key;
} else {
// robustness: replace specific key-value with base tag (heuristic)
var references = document.querySelectorAll('' + prePart + key);
var _loop = function _loop() {
var reference = references[i];
if (elements.some(function (element) {
return reference.contains(element);
})) {
var description = reference.tagName.toLowerCase();
pattern = '' + prePart + description + postPart;
matches = document.querySelectorAll(pattern);
if (compareResults(matches, elements)) {
current = description;
}
return 'break';
}
};
for (var i = 0, l = references.length; i < l; i++) {
var pattern;
var matches;
var _ret = _loop();
if (_ret === 'break') break;
}
}
}
// robustness: descendant instead child (heuristic)
if (/>/.test(current)) {
var descendant = current.replace(/>/, '');
var pattern = '' + prePart + descendant + postPart;
var matches = document.querySelectorAll(pattern);
if (compareResults(matches, elements)) {
current = descendant;
}
}
// robustness: 'nth-of-type' instead 'nth-child' (heuristic)
if (/:nth-child/.test(current)) {
// TODO: consider complete coverage of 'nth-of-type' replacement
var type = current.replace(/nth-child/g, 'nth-of-type');
var pattern = '' + prePart + type + postPart;
var matches = document.querySelectorAll(pattern);
if (compareResults(matches, elements)) {
current = type;
}
}
// efficiency: combinations of classname (partial permutations)
if (/\.\S+\.\S+/.test(current)) {
var names = current.trim().split('.').slice(1).map(function (name) {
return '.' + name;
}).sort(function (curr, next) {
return curr.length - next.length;
});
while (names.length) {
var partial = current.replace(names.shift(), '').trim();
var pattern = ('' + prePart + partial + postPart).trim();
if (!pattern.length || pattern.charAt(0) === '>' || pattern.charAt(pattern.length - 1) === '>') {
break;
}
var matches = document.querySelectorAll(pattern);
if (compareResults(matches, elements)) {
current = partial;
}
}
// robustness: degrade complex classname (heuristic)
names = current && current.match(/\./g);
if (names && names.length > 2) {
var _references = document.querySelectorAll('' + prePart + current);
var _loop2 = function _loop2() {
var reference = _references[i];
if (elements.some(function (element) {
return reference.contains(element);
})) {
// TODO:
// - check using attributes + regard excludes
var description = reference.tagName.toLowerCase();
pattern = '' + prePart + description + postPart;
matches = document.querySelectorAll(pattern);
if (compareResults(matches, elements)) {
current = description;
}
return 'break';
}
};
for (var i = 0, l = _references.length; i < l; i++) {
var pattern;
var matches;
var _ret2 = _loop2();
if (_ret2 === 'break') break;
}
}
}
return current;
}
/**
* Evaluate matches with expected elements
*
* @param {Array.<HTMLElement>} matches - [description]
* @param {Array.<HTMLElement>} elements - [description]
* @return {Boolean} - [description]
*/
function compareResults(matches, elements) {
var length = matches.length;
return length === elements.length && elements.every(function (element) {
for (var i = 0; i < length; i++) {
if (matches[i] === element) {
return true;
}
}
return false;
});
}
module.exports = exports['default'];
/***/ },
/* 3 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
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 _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"); } }; }();
exports.default = adapt;
/**
* # Adapt
*
* Check and extend the environment for universal usage.
*/
/**
* Modify the context based on the environment
*
* @param {HTMLELement} element - [description]
* @param {Object} options - [description]
* @return {boolean} - [description]
*/
function adapt(element, options) {
// detect environment setup
if (true) {
return false;
} else {
global.document = options.context || function () {
var root = element;
while (root.parent) {
root = root.parent;
}
return root;
}();
}
// https://github.com/fb55/domhandler/blob/master/index.js#L75
var ElementPrototype = Object.getPrototypeOf(true);
// alternative descriptor to access elements with filtering invalid elements (e.g. textnodes)
if (!Object.getOwnPropertyDescriptor(ElementPrototype, 'childTags')) {
Object.defineProperty(ElementPrototype, 'childTags', {
enumerable: true,
get: function get() {
return this.children.filter(function (node) {
// https://github.com/fb55/domelementtype/blob/master/index.js#L12
return node.type === 'tag' || node.type === 'script' || node.type === 'style';
});
}
});
}
if (!Object.getOwnPropertyDescriptor(ElementPrototype, 'attributes')) {
// https://developer.mozilla.org/en-US/docs/Web/API/Element/attributes
// https://developer.mozilla.org/en-US/docs/Web/API/NamedNodeMap
Object.defineProperty(ElementPrototype, 'attributes', {
enumerable: true,
get: function get() {
var attribs = this.attribs;
var attributesNames = Object.keys(attribs);
var NamedNodeMap = attributesNames.reduce(function (attributes, attributeName, index) {
attributes[index] = {
name: attributeName,
value: attribs[attributeName]
};
return attributes;
}, {});
Object.defineProperty(NamedNodeMap, 'length', {
enumerable: false,
configurable: false,
value: attributesNames.length
});
return NamedNodeMap;
}
});
}
if (!ElementPrototype.getAttribute) {
// https://docs.webplatform.org/wiki/dom/Element/getAttribute
// https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute
ElementPrototype.getAttribute = function (name) {
return this.attribs[name] || null;
};
}
if (!ElementPrototype.getElementsByTagName) {
// https://docs.webplatform.org/wiki/dom/Document/getElementsByTagName
// https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByTagName
ElementPrototype.getElementsByTagName = function (tagName) {
var HTMLCollection = [];
traverseDescendants(this.childTags, function (descendant) {
if (descendant.name === tagName || tagName === '*') {
HTMLCollection.push(descendant);
}
});
return HTMLCollection;
};
}
if (!ElementPrototype.getElementsByClassName) {
// https://docs.webplatform.org/wiki/dom/Document/getElementsByClassName
// https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByClassName
ElementPrototype.getElementsByClassName = function (className) {
var names = className.trim().replace(/\s+/g, ' ').split(' ');
var HTMLCollection = [];
traverseDescendants([this], function (descendant) {
var descendantClassName = descendant.attribs.class;
if (descendantClassName && names.every(function (name) {
return descendantClassName.indexOf(name) > -1;
})) {
HTMLCollection.push(descendant);
}
});
return HTMLCollection;
};
}
if (!ElementPrototype.querySelectorAll) {
// https://docs.webplatform.org/wiki/css/selectors_api/querySelectorAll
// https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelectorAll
ElementPrototype.querySelectorAll = function (selectors) {
var _this = this;
selectors = selectors.replace(/(>)(\S)/g, '$1 $2').trim(); // add space for '>' selector
// using right to left execution => https://github.com/fb55/css-select#how-does-it-work
var instructions = getInstructions(selectors);
var discover = instructions.shift();
var total = instructions.length;
return discover(this).filter(function (node) {
var step = 0;
while (step < total) {
node = instructions[step](node, _this);
if (!node) {
// hierarchy doesn't match
return false;
}
step += 1;
}
return true;
});
};
}
if (!ElementPrototype.contains) {
// https://developer.mozilla.org/en-US/docs/Web/API/Node/contains
ElementPrototype.contains = function (element) {
var inclusive = false;
traverseDescendants([this], function (descendant, done) {
if (descendant === element) {
inclusive = true;
done();
}
});
return inclusive;
};
}
return true;
}
/**
* Retrieve transformation steps
*
* @param {Array.<string>} selectors - [description]
* @return {Array.<Function>} - [description]
*/
function getInstructions(selectors) {
return selectors.split(' ').reverse().map(function (selector, step) {
var discover = step === 0;
var _selector$split = selector.split(':'),
_selector$split2 = _slicedToArray(_selector$split, 2),
type = _selector$split2[0],
pseudo = _selector$split2[1];
var validate = null;
var instruction = null;
(function () {
switch (true) {
// child: '>'
case />/.test(type):
instruction = function checkParent(node) {
return function (validate) {
return validate(node.parent) && node.parent;
};
};
break;
// class: '.'
case /^\./.test(type):
var names = type.substr(1).split('.');
validate = function validate(node) {
var nodeClassName = node.attribs.class;
return nodeClassName && names.every(function (name) {
return nodeClassName.indexOf(name) > -1;
});
};
instruction = function checkClass(node, root) {
if (discover) {
return node.getElementsByClassName(names.join(' '));
}
return typeof node === 'function' ? node(validate) : getAncestor(node, root, validate);
};
break;
// attribute: '[key="value"]'
case /^\[/.test(type):
var _type$replace$split = type.replace(/\[|\]|"/g, '').split('='),
_type$replace$split2 = _slicedToArray(_type$replace$split, 2),
attributeKey = _type$replace$split2[0],
attributeValue = _type$replace$split2[1];
validate = function validate(node) {
var hasAttribute = Object.keys(node.attribs).indexOf(attributeKey) > -1;
if (hasAttribute) {
// regard optional attributeValue
if (!attributeValue || node.attribs[attributeKey] === attributeValue) {
return true;
}
}
return false;
};
instruction = function checkAttribute(node, root) {
if (discover) {
var _ret2 = function () {
var NodeList = [];
traverseDescendants([node], function (descendant) {
if (validate(descendant)) {
NodeList.push(descendant);
}
});
return {
v: NodeList
};
}();
if ((typeof _ret2 === 'undefined' ? 'undefined' : _typeof(_ret2)) === "object") return _ret2.v;
}
return typeof node === 'function' ? node(validate) : getAncestor(node, root, validate);
};
break;
// id: '#'
case /^#/.test(type):
var id = type.substr(1);
validate = function validate(node) {
return node.attribs.id === id;
};
instruction = function checkId(node, root) {
if (discover) {
var _ret3 = function () {
var NodeList = [];
traverseDescendants([node], function (descendant, done) {
if (validate(descendant)) {
NodeList.push(descendant);
done();
}
});
return {
v: NodeList
};
}();
if ((typeof _ret3 === 'undefined' ? 'undefined' : _typeof(_ret3)) === "object") return _ret3.v;
}
return typeof node === 'function' ? node(validate) : getAncestor(node, root, validate);
};
break;
// universal: '*'
case /\*/.test(type):
validate = function validate(node) {
return true;
};
instruction = function checkUniversal(node, root) {
if (discover) {
var _ret4 = function () {
var NodeList = [];
traverseDescendants([node], function (descendant) {
return NodeList.push(descendant);
});
return {
v: NodeList
};
}();
if ((typeof _ret4 === 'undefined' ? 'undefined' : _typeof(_ret4)) === "object") return _ret4.v;
}
return typeof node === 'function' ? node(validate) : getAncestor(node, root, validate);
};
break;
// tag: '...'
default:
validate = function validate(node) {
return node.name === type;
};
instruction = function checkTag(node, root) {
if (discover) {
var _ret5 = function () {
var NodeList = [];
traverseDescendants([node], function (descendant) {
if (validate(descendant)) {
NodeList.push(descendant);
}
});
return {
v: NodeList
};
}();
if ((typeof _ret5 === 'undefined' ? 'undefined' : _typeof(_ret5)) === "object") return _ret5.v;
}
return typeof node === 'function' ? node(validate) : getAncestor(node, root, validate);
};
}
})();
if (!pseudo) {
return instruction;
}
var rule = pseudo.match(/-(child|type)\((\d+)\)$/);
var kind = rule[1];
var index = parseInt(rule[2], 10) - 1;
var validatePseudo = function validatePseudo(node) {
if (node) {
var compareSet = node.parent.childTags;
if (kind === 'type') {
compareSet = compareSet.filter(validate);
}
var nodeIndex = compareSet.findIndex(function (child) {
return child === node;
});
if (nodeIndex === index) {
return true;
}
}
return false;
};
return function enhanceInstruction(node) {
var match = instruction(node);
if (discover) {
return match.reduce(function (NodeList, matchedNode) {
if (validatePseudo(matchedNode)) {
NodeList.push(matchedNode);
}
return NodeList;
}, []);
}
return validatePseudo(match) && match;
};
});
}
/**
* Walking recursive to invoke callbacks
*
* @param {Array.<HTMLElement>} nodes - [description]
* @param {Function} handler - [description]
*/
function traverseDescendants(nodes, handler) {
nodes.forEach(function (node) {
var progress = true;
handler(node, function () {
return progress = false;
});
if (node.childTags && progress) {
traverseDescendants(node.childTags, handler);
}
});
}
/**
* Bubble up from bottom to top
*
* @param {HTMLELement} node - [description]
* @param {HTMLELement} root - [description]
* @param {Function} validate - [description]
* @return {HTMLELement} - [description]
*/
function getAncestor(node, root, validate) {
while (node.parent) {
node = node.parent;
if (validate(node)) {
return node;
}
if (node === root) {
break;
}
}
return null;
}
module.exports = exports['default'];
/***/ },
/* 4 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
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; }; /**
* # Select
*
* Construct a unique CSS query selector to access the selected DOM element(s).
* For longevity it applies different matching and optimization strategies.
*/
exports.getSingleSelector = getSingleSelector;
exports.getMultiSelector = getMultiSelector;
exports.default = getQuerySelector;
var _adapt = __webpack_require__(3);
var _adapt2 = _interopRequireDefault(_adapt);
var _match = __webpack_require__(5);
var _match2 = _interopRequireDefault(_match);
var _optimize = __webpack_require__(2);
var _optimize2 = _interopRequireDefault(_optimize);
var _utilities = __webpack_require__(0);
var _common = __webpack_require__(1);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Get a selector for the provided element
*
* @param {HTMLElement} element - [description]
* @param {Object} options - [description]
* @return {string} - [description]
*/
function getSingleSelector(element) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (element.nodeType === 3) {
element = element.parentNode;
}
if (element.nodeType !== 1) {
throw new Error('Invalid input - only HTMLElements or representations of them are supported! (not "' + (typeof element === 'undefined' ? 'undefined' : _typeof(element)) + '")');
}