Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #523 - "Slider cannot be initialized in iframe" #675

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function (Glide, Components, Events) {
r = document.querySelector(r)
}

if (r !== null) {
if (exist(r)) {
Html._r = r
} else {
warn('Root element must be a existing Html node')
Expand Down
3 changes: 2 additions & 1 deletion src/utils/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export function siblings (node) {
* @return {Boolean}
*/
export function exist (node) {
if (node && node instanceof window.HTMLElement) {
// We are usine duck-typing here because we can't use `instanceof` since if we're in an iframe the class instance will be different.
if (node && node.appendChild && node.isConnected) {
Comment on lines +31 to +32

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this seems like a better fix. However, I think it would be possible to avoid duck-typing by using ownerDocument.defaultView. Mostly I'm adding the comment because it seems like ownerDocument and defaultView are oft overlooked features of the DOM.

Suggested change
// We are usine duck-typing here because we can't use `instanceof` since if we're in an iframe the class instance will be different.
if (node && node.appendChild && node.isConnected) {
if (!node) return false
const nodeWindow = node.ownerDocument?.defaultView
if (nodeWindow && node instanceof nodeWindow.HTMLElement) {

I’d also probably just return the test nodeWindow && node instanceof nodeWindow.HTMLElement directly and eliminate the following returns but couldn’t do that in the scope of the suggestion.

return true
}

Expand Down