Skip to content

Commit 5f4ed56

Browse files
authored
Fix for display:contents (#111)
1 parent 0bc45b5 commit 5f4ed56

6 files changed

Lines changed: 36 additions & 15 deletions

File tree

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"permissions": ["activeTab", "storage", "notifications"],
3232
"host_permissions": ["http://*/*", "https://*/*"],
3333
"update_url": "http://clients2.google.com/service/update2/crx",
34-
"version":"2.0.6",
34+
"version":"2.0.7",
3535
"options_page": "assets/options.html",
3636
"icons": {
3737
"16": "assets/icon-16.png",

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "search_and_replace",
3-
"version": "2.0.6",
3+
"version": "2.0.7",
44
"resolutions": {
55
"author": "Chris Taylor <cjtaylor38@gmail.com>"
66
},

src/elements.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ function ancestorIsVisible(element: Element, cloned = false) {
6565
return true
6666
}
6767

68+
/**
69+
* Check if an element is hidden by checking the following conditions
70+
* - If the element is the body, assume it's visible and return `false`
71+
* - If the element is an input, return true if the input is `hidden`
72+
* - If the element has style return true if the style is `none`
73+
* - If the element is not a clone, does not have display:`contents`,
74+
* and has a `checkVisibility` method, return the result of that method
75+
* - Get the computed style and return true if the computed style is `none`
76+
* - Otherwise return `false`
77+
* @param element
78+
* @param cloned
79+
*/
6880
export function isHidden(element: HTMLElement | Element, cloned = false) {
6981
if (element.tagName === 'BODY') {
7082
return false
@@ -78,19 +90,28 @@ export function isHidden(element: HTMLElement | Element, cloned = false) {
7890
}
7991
}
8092

81-
if (element && 'style' in element && element.style.display === 'none') {
82-
// if the element has style return true if the style is `none`
83-
return true
84-
}
93+
if (element && 'style' in element) {
94+
if (element.style.display === 'none') {
95+
// if the element has style return true if the style is `none`
96+
return true
97+
}
8598

86-
// clones are not visible so we can't use checkVisibility
87-
if (!cloned && 'checkVisibility' in element && typeof element.checkVisibility === 'function') {
88-
// use the relatively new checkVisibility method, which returns `true` if the element is visible
89-
return !element.checkVisibility()
99+
if (
100+
// clones are not visible, so we can't use checkVisibility on them
101+
!cloned &&
102+
// checkVisibility currently returns false for elements with `display: contents`
103+
// https://chromium-review.googlesource.com/c/chromium/src/+/4950634
104+
element.style.display !== 'contents' &&
105+
'checkVisibility' in element &&
106+
typeof element.checkVisibility === 'function'
107+
) {
108+
// use the relatively new checkVisibility method, which returns `true` if the element is visible
109+
return !element.checkVisibility()
110+
}
90111
}
91112

92113
// This method is not as accurate as checkVisibility
93-
// compute the style as its not immediately obvious if the element is hidden
114+
// compute the style as it's not immediately obvious if the element is hidden
94115
const computedStyle = getComputedStyle(element)
95116

96117
if (computedStyle.display === 'none') {

tests/test.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<body>
77
<div>
88
<h1>Tests</h1>
9-
<div>
9+
<div style="display: 'contents';">
1010
<h2>1 Input Field</h2>
1111
<form action="#" method="post">
1212
<label>

webpack/webpack.prod.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = merge(common, {
1010
new TerserPlugin({
1111
terserOptions: {
1212
compress: {
13-
drop_console: true,
13+
drop_console: ['log', 'info', 'debug'],
1414
},
1515
},
1616
}),

0 commit comments

Comments
 (0)