1 | Object.defineProperty(exports, "__esModule", { value: true });
|
2 | var is_1 = require("./is");
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | function htmlTreeAsString(elem, keyAttrs) {
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | try {
|
15 | var currentElem = elem;
|
16 | var MAX_TRAVERSE_HEIGHT = 5;
|
17 | var MAX_OUTPUT_LEN = 80;
|
18 | var out = [];
|
19 | var height = 0;
|
20 | var len = 0;
|
21 | var separator = ' > ';
|
22 | var sepLength = separator.length;
|
23 | var nextStr = void 0;
|
24 |
|
25 | while (currentElem && height++ < MAX_TRAVERSE_HEIGHT) {
|
26 | nextStr = _htmlElementAsString(currentElem, keyAttrs);
|
27 | // bail out if
|
28 | // - nextStr is the 'html' element
|
29 | // - the length of the string that would be created exceeds MAX_OUTPUT_LEN
|
30 | // (ignore this limit if we are on the first iteration)
|
31 | if (nextStr === 'html' || (height > 1 && len + out.length * sepLength + nextStr.length >= MAX_OUTPUT_LEN)) {
|
32 | break;
|
33 | }
|
34 | out.push(nextStr);
|
35 | len += nextStr.length;
|
36 | currentElem = currentElem.parentNode;
|
37 | }
|
38 | return out.reverse().join(separator);
|
39 | }
|
40 | catch (_oO) {
|
41 | return '<unknown>';
|
42 | }
|
43 | }
|
44 | exports.htmlTreeAsString = htmlTreeAsString;
|
45 | /**
|
46 | * Returns a simple, query-selector representation of a DOM element
|
47 | * e.g. [HTMLElement] => input#foo.btn[name=baz]
|
48 | * @returns generated DOM path
|
49 | */
|
50 | function _htmlElementAsString(el, keyAttrs) {
|
51 | var _a, _b;
|
52 | var elem = el;
|
53 | var out = [];
|
54 | var className;
|
55 | var classes;
|
56 | var key;
|
57 | var attr;
|
58 | var i;
|
59 | if (!elem || !elem.tagName) {
|
60 | return '';
|
61 | }
|
62 | out.push(elem.tagName.toLowerCase());
|
63 | // Pairs of attribute keys defined in `serializeAttribute` and their values on element.
|
64 | var keyAttrPairs = ((_a = keyAttrs) === null || _a === void 0 ? void 0 : _a.length) ? keyAttrs.filter(function (keyAttr) { return elem.getAttribute(keyAttr); }).map(function (keyAttr) { return [keyAttr, elem.getAttribute(keyAttr)]; })
|
65 | : null;
|
66 | if ((_b = keyAttrPairs) === null || _b === void 0 ? void 0 : _b.length) {
|
67 | keyAttrPairs.forEach(function (keyAttrPair) {
|
68 | out.push("[" + keyAttrPair[0] + "=\"" + keyAttrPair[1] + "\"]");
|
69 | });
|
70 | }
|
71 | else {
|
72 | if (elem.id) {
|
73 | out.push("#" + elem.id);
|
74 | }
|
75 | // eslint-disable-next-line prefer-const
|
76 | className = elem.className;
|
77 | if (className && is_1.isString(className)) {
|
78 | classes = className.split(/\s+/);
|
79 | for (i = 0; i < classes.length; i++) {
|
80 | out.push("." + classes[i]);
|
81 | }
|
82 | }
|
83 | }
|
84 | var allowedAttrs = ['type', 'name', 'title', 'alt'];
|
85 | for (i = 0; i < allowedAttrs.length; i++) {
|
86 | key = allowedAttrs[i];
|
87 | attr = elem.getAttribute(key);
|
88 | if (attr) {
|
89 | out.push("[" + key + "=\"" + attr + "\"]");
|
90 | }
|
91 | }
|
92 | return out.join('');
|
93 | }
|
94 | //# sourceMappingURL=browser.js.map |
\ | No newline at end of file |