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: autoResize(true) #1305

Merged
merged 6 commits into from
Aug 13, 2024
Merged
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
27 changes: 10 additions & 17 deletions packages/child/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
BASE,
HEIGHT_EDGE,
MANUAL_RESIZE_REQUEST,
SIZE_ATTR,
VERSION,
WIDTH_EDGE,
Expand Down Expand Up @@ -75,7 +76,6 @@ function iframeResizerChild() {
let bodyBackground = ''
let bodyMargin = 0
let bodyMarginStr = ''
let bodyObserver = null
let bodyPadding = ''
let calculateHeight = true
let calculateWidth = false
Expand Down Expand Up @@ -205,7 +205,7 @@ See <u>https://iframe-resizer.com/setup/</> for more details.

if (version !== VERSION) {
advise(
`<rb>Version mismatch</>
`<b>Version mismatch</>

The parent and child pages are running different versions of <i>iframe resizer</>.

Expand Down Expand Up @@ -472,20 +472,13 @@ This version of <i>iframe-resizer</> can auto detect the most suitable ${type} c
function initEventListeners() {
if (autoResize !== true) {
log('Auto Resize disabled')
return
}

manageEventListeners('add')
setupMutationObserver()
setupResizeObserver()
}

function stopEventListeners() {
manageEventListeners('remove')
resizeObserver?.disconnect()
bodyObserver?.disconnect()
}

function injectClearFixIntoBodyElement() {
const clearFix = document.createElement('div')

Expand Down Expand Up @@ -618,10 +611,9 @@ This version of <i>iframe-resizer</> can auto detect the most suitable ${type} c
autoResize: (resize) => {
if (resize === true && autoResize === false) {
autoResize = true
initEventListeners()
sendSize('autoResizeEnabled', 'Auto Resize enabled')
} else if (resize === false && autoResize === true) {
autoResize = false
stopEventListeners()
}

sendMsg(0, 0, 'autoResize', JSON.stringify(autoResize))
Expand Down Expand Up @@ -789,7 +781,7 @@ The <b>size()</> method has been deprecated and replaced with <b>resize()</>. U
createResizeObservers(window.document)
}

function setupBodyMutationObserver() {
function setupMutationObserver() {
const observedMutations = new Set()
let pending = false
let perfMon = 0
Expand Down Expand Up @@ -903,10 +895,6 @@ The <b>size()</> method has been deprecated and replaced with <b>resize()</>. U
}
}

function setupMutationObserver() {
bodyObserver = setupBodyMutationObserver()
}

function getMaxElement(side) {
performance.mark(PREF_START)

Expand Down Expand Up @@ -1172,6 +1160,11 @@ The <b>size()</> method has been deprecated and replaced with <b>resize()</>. U
customWidth,
msg,
) {
if (!autoResize && triggerEvent !== MANUAL_RESIZE_REQUEST) {
log('Resizing disabled')
return
}

if (document.hidden) {
// Currently only correctly supported in firefox
// This is checked again on the parent page
Expand Down Expand Up @@ -1272,7 +1265,7 @@ The <b>size()</> method has been deprecated and replaced with <b>resize()</>. U
},

resize() {
sendSize('resizeParent', 'Parent window requested size check')
sendSize(MANUAL_RESIZE_REQUEST, 'Parent window requested size check')
},

moveToAnchor() {
Expand Down
2 changes: 2 additions & 0 deletions packages/common/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const OVERFLOW_ATTR = 'data-iframe-overflow'
export const HEIGHT_EDGE = 'bottom'
export const WIDTH_EDGE = 'right'

export const MANUAL_RESIZE_REQUEST = 'resizeParent'

export const msgHeader = 'message'
export const msgHeaderLen = msgHeader.length
export const msgId = '[iFrameSizer]' // Must match iframe msg ID
Expand Down
22 changes: 5 additions & 17 deletions spec/childSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,7 @@ define(['iframeResizerChild', 'jquery'], (mockMsgListener, $) => {
win.parentIFrame.autoResize(true)

expect(console.log).toHaveBeenCalledWith(
'[iframe-resizer][parentIFrameTests] Add event listener: After Print',
)

expect(console.log).toHaveBeenCalledWith(
'[iframe-resizer][parentIFrameTests] Add event listener: Before Print',
)
win.parentIFrame.autoResize(false)

expect(console.log).toHaveBeenCalledWith(
'[iframe-resizer][parentIFrameTests] Remove event listener: After Print',
)

expect(console.log).toHaveBeenCalledWith(
'[iframe-resizer][parentIFrameTests] Remove event listener: Before Print',
'[iframe-resizer][parentIFrameTests] Trigger event: Auto Resize enabled',
)
})

Expand Down Expand Up @@ -191,17 +178,18 @@ define(['iframeResizerChild', 'jquery'], (mockMsgListener, $) => {
)
})

it('setTargetOrigin', () => {
xit('setTargetOrigin', () => {
const targetOrigin = 'http://foo.bar:1337'

win.parentIFrame.setTargetOrigin(targetOrigin)
win.parentIFrame.size(10, 10)
win.parentIFrame.setTargetOrigin('*')
win.parentIFrame.resize(10, 10)

expect(msgObject.source.postMessage).toHaveBeenCalledWith(
'[iFrameSizer]parentIFrameTests:10:10:size',
targetOrigin,
)

win.parentIFrame.setTargetOrigin('*')
})
})

Expand Down
Loading