Skip to content

Commit

Permalink
Merge pull request #25 from Bala-raj/fix/iframe/bug
Browse files Browse the repository at this point in the history
fix(iframe): introduced isContainerInIframe to add events attached to…
  • Loading branch information
Dunstonhary authored Nov 7, 2023
2 parents e120db7 + a95d3ba commit 0a6d085
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:

strategy:
matrix:
node-version: [8.x]
node-version: [12.x]

steps:
- uses: actions/checkout@v1
Expand Down
14 changes: 11 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ export default class AudioPlayer extends Component {
playbackRate: '1x',
showPlaybackRateList: false,
volumeRange: 50,
isContainerInIframe : (window.top !== window) || false,
};

this.state = Object.assign({}, this.defaultState);

// html audio element used for playback
Expand Down Expand Up @@ -193,9 +194,13 @@ export default class AudioPlayer extends Component {

componentDidMount() {
// add event listeners bound outside the scope of our component

window.addEventListener('mouseup', this.seekReleaseListener);
document.addEventListener('touchend', this.seekReleaseListener);
window.addEventListener('resize', this.resizeListener);
if(this.state.isContainerInIframe) {
window.self.document.addEventListener('touchend', this.seekReleaseListener);
}
this.resizeListener();

const audio = this.audio;
Expand Down Expand Up @@ -364,7 +369,8 @@ export default class AudioPlayer extends Component {
// make sure we don't select stuff in the background while seeking
if (event.type === 'mousedown' || event.type === 'touchstart') {
this.seekInProgress = true;
document.body.classList.add('noselect');
const body = this.state.isContainerInIframe ? window.self.document.body : document.body;
body.classList.add('noselect');
} else if (!this.seekInProgress) {
return;
}
Expand Down Expand Up @@ -407,7 +413,9 @@ export default class AudioPlayer extends Component {
* go of the mouse, so if .noselect was applied
* to the document body, get rid of it.
*/
document.body.classList.remove('noselect');
const body = this.state.isContainerInIframe ? window.self.document.body : document.body;
body.classList.remove('noselect');

if (!this.seekInProgress) {
return;
}
Expand Down
31 changes: 30 additions & 1 deletion src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,34 @@ describe('Filename manipulation', () => {

});

})
});
describe('Iframe', () => {

const topRef = Object.assign({}, window.top); //this is immediate parent
const selfRef = Object.assign({}, window.self);//this is self window
let iframe,iframeDocument, divContainer;


beforeEach(() => {
delete window.top;
delete window.self;


window.top = topRef;
window.self = selfRef;

iframe = document.createElement('iframe');
iframeDocument = document.implementation.createHTMLDocument('IFrame Document');
divContainer = iframeDocument.createElement('div');
divContainer.textContent = 'This is a div container inside the iframe document';
});

it('should return isContainerInIframe as TRUE', () => {

const DOM = ReactDOM.render(<MyComponent />, divContainer); // Render your component
iframeDocument.body.appendChild(divContainer);
document.body.appendChild(iframe);
expect(DOM.state.isContainerInIframe).toBeTruthy();
});
});

0 comments on commit 0a6d085

Please sign in to comment.