Skip to content

Commit 15804f9

Browse files
committed
resolved comments
1 parent 57364b5 commit 15804f9

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

lib/core/utils/dq-element.js

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,39 @@ function escapeCSSSelector(str) {
3333
.replace(/[!"#$%&'()*+,./:;<=>?@[\\\]^`{|}~]/g, '\\$&')
3434
.replace(/^\d/, '\\3$& ');
3535
}
36+
function generateSelectorWithShadow(elm) {
37+
const selectors = getShadowSelector(elm);
38+
if (typeof selectors === 'string') {
39+
return selectors;
40+
} else {
41+
// merge selectors of an array with ,
42+
return selectors.join(',').replace(/,$/, '');
43+
}
44+
}
45+
46+
function getShadowSelector(elm) {
47+
if (!elm) {
48+
return '';
49+
}
50+
let doc = (elm.getRootNode && elm.getRootNode()) || document;
51+
// Not a DOCUMENT_FRAGMENT - shadow DOM
52+
if (doc.nodeType !== 11) {
53+
return getFullPathSelector(elm);
54+
}
55+
56+
const stack = [];
57+
while (doc.nodeType === 11) {
58+
if (!doc.host) {
59+
return '';
60+
}
61+
stack.unshift({ elm, doc });
62+
elm = doc.host;
63+
doc = elm.getRootNode();
64+
}
65+
66+
stack.unshift({ elm, doc });
67+
return stack.map(item => getFullPathSelector(item.elm));
68+
}
3669

3770
function getFullPathSelector(elm) {
3871
if (elm.nodeName === 'HTML' || elm.nodeName === 'BODY') {
@@ -116,8 +149,12 @@ function getSourceOpt(element) {
116149
.filter(attr => !attr.name.startsWith('data-percy-'))
117150
.map(attr => `${attr.name}="${attr.value}"`)
118151
.join(' ');
119-
120-
result = attributes ? `<${tagName} ${attributes}>` : `<${tagName}>`;
152+
const closingTag = element.children.length ? false : true;
153+
if (closingTag) {
154+
result = `<${tagName} ${attributes}>${element.textContent}</${tagName}>`;
155+
} else {
156+
result = attributes ? `<${tagName} ${attributes}>` : `<${tagName}>`;
157+
}
121158
result = truncate(result, 300); // Truncate to 300 characters
122159
// Store in cache
123160
sourceCache.set(element, result);
@@ -210,9 +247,7 @@ DqElement.prototype = {
210247
*/
211248
get selector() {
212249
if (axe._cache.get('runTypeAOpt')) {
213-
return (
214-
this.spec.selector || [getFullPathSelector(this.element, this._options)]
215-
);
250+
return this.spec.selector || [generateSelectorWithShadow(this.element)];
216251
}
217252
return this.spec.selector || [getSelector(this.element, this._options)];
218253
},

0 commit comments

Comments
 (0)