Commit 895077ca authored by Eduardo Sanz García's avatar Eduardo Sanz García Committed by Eduardo

Rename `isDocumentReady` to `onDocumentReady`

The name of `isDocumentReady` gives the impressiong that the function
would return a boolean.  `onDocumentReady`, on the other hand, expresses
more clearly that the execution of the callback will be triggered after
the iframe's documen is ready (loaded and parsed).

On a follow up commit I change the `onDocumentReady` to return a Promise
instead of executing a callback.
parent 91c4b724
...@@ -40,7 +40,7 @@ export default class FrameObserver { ...@@ -40,7 +40,7 @@ export default class FrameObserver {
*/ */
_addFrame(frame) { _addFrame(frame) {
if (isAccessible(frame)) { if (isAccessible(frame)) {
isDocumentReady(frame, () => { onDocumentReady(frame, () => {
const frameWindow = /** @type {Window} */ (frame.contentWindow); const frameWindow = /** @type {Window} */ (frame.contentWindow);
frameWindow.addEventListener('unload', () => { frameWindow.addEventListener('unload', () => {
this._removeFrame(frame); this._removeFrame(frame);
...@@ -92,10 +92,12 @@ function isAccessible(iframe) { ...@@ -92,10 +92,12 @@ function isAccessible(iframe) {
} }
/** /**
* Triggers a callback when the iframe's DOM is ready (loaded and parsed)
*
* @param {HTMLIFrameElement} iframe * @param {HTMLIFrameElement} iframe
* @param {() => void} callback * @param {() => void} callback
*/ */
export function isDocumentReady(iframe, callback) { export function onDocumentReady(iframe, callback) {
const iframeDocument = /** @type {Document} */ (iframe.contentDocument); const iframeDocument = /** @type {Document} */ (iframe.contentDocument);
if (iframeDocument.readyState === 'loading') { if (iframeDocument.readyState === 'loading') {
iframeDocument.addEventListener('DOMContentLoaded', () => { iframeDocument.addEventListener('DOMContentLoaded', () => {
......
import { DEBOUNCE_WAIT } from '../../frame-observer'; import { DEBOUNCE_WAIT } from '../../frame-observer';
import { HypothesisInjector } from '../../hypothesis-injector'; import { HypothesisInjector } from '../../hypothesis-injector';
import { isDocumentReady } from '../../frame-observer'; import { onDocumentReady } from '../../frame-observer';
describe('HypothesisInjector integration test', () => { describe('HypothesisInjector integration test', () => {
let container; let container;
...@@ -65,7 +65,7 @@ describe('HypothesisInjector integration test', () => { ...@@ -65,7 +65,7 @@ describe('HypothesisInjector integration test', () => {
createHypothesisInjector(); createHypothesisInjector();
const validFramePromise = new Promise(resolve => { const validFramePromise = new Promise(resolve => {
isDocumentReady(validFrame, () => { onDocumentReady(validFrame, () => {
assert( assert(
validFrame.contentDocument.body.hasChildNodes(), validFrame.contentDocument.body.hasChildNodes(),
'expected valid frame to be modified' 'expected valid frame to be modified'
...@@ -74,7 +74,7 @@ describe('HypothesisInjector integration test', () => { ...@@ -74,7 +74,7 @@ describe('HypothesisInjector integration test', () => {
}); });
}); });
const invalidFramePromise = new Promise(resolve => { const invalidFramePromise = new Promise(resolve => {
isDocumentReady(invalidFrame, () => { onDocumentReady(invalidFrame, () => {
assert( assert(
!invalidFrame.contentDocument.body.hasChildNodes(), !invalidFrame.contentDocument.body.hasChildNodes(),
'expected invalid frame to not be modified' 'expected invalid frame to not be modified'
...@@ -105,7 +105,7 @@ describe('HypothesisInjector integration test', () => { ...@@ -105,7 +105,7 @@ describe('HypothesisInjector integration test', () => {
createHypothesisInjector(); createHypothesisInjector();
return new Promise(resolve => { return new Promise(resolve => {
isDocumentReady(frame, () => { onDocumentReady(frame, () => {
const scriptElement = const scriptElement =
frame.contentDocument.querySelector('script[src]'); frame.contentDocument.querySelector('script[src]');
assert(scriptElement, 'expected embed script to be injected'); assert(scriptElement, 'expected embed script to be injected');
...@@ -128,7 +128,7 @@ describe('HypothesisInjector integration test', () => { ...@@ -128,7 +128,7 @@ describe('HypothesisInjector integration test', () => {
createHypothesisInjector(); createHypothesisInjector();
return new Promise(resolve => { return new Promise(resolve => {
isDocumentReady(frame, () => { onDocumentReady(frame, () => {
const scriptElement = const scriptElement =
frame.contentDocument.querySelector('script[src]'); frame.contentDocument.querySelector('script[src]');
assert.isNull( assert.isNull(
...@@ -150,7 +150,7 @@ describe('HypothesisInjector integration test', () => { ...@@ -150,7 +150,7 @@ describe('HypothesisInjector integration test', () => {
return new Promise(resolve => { return new Promise(resolve => {
// Yield to let the DOM and CrossFrame catch up // Yield to let the DOM and CrossFrame catch up
waitForFrameObserver(() => { waitForFrameObserver(() => {
isDocumentReady(frame, () => { onDocumentReady(frame, () => {
assert( assert(
frame.contentDocument.body.hasChildNodes(), frame.contentDocument.body.hasChildNodes(),
'expected dynamically added frame to be modified' 'expected dynamically added frame to be modified'
...@@ -192,7 +192,7 @@ describe('HypothesisInjector integration test', () => { ...@@ -192,7 +192,7 @@ describe('HypothesisInjector integration test', () => {
createHypothesisInjector(); createHypothesisInjector();
return new Promise(resolve => { return new Promise(resolve => {
isDocumentReady(frame, () => { onDocumentReady(frame, () => {
assert( assert(
frame.contentDocument.body.hasChildNodes(), frame.contentDocument.body.hasChildNodes(),
'expected initial frame to be modified' 'expected initial frame to be modified'
...@@ -207,7 +207,7 @@ describe('HypothesisInjector integration test', () => { ...@@ -207,7 +207,7 @@ describe('HypothesisInjector integration test', () => {
// Yield again // Yield again
waitForFrameObserver(() => { waitForFrameObserver(() => {
isDocumentReady(frame, () => { onDocumentReady(frame, () => {
assert( assert(
frame.contentDocument.body.hasChildNodes(), frame.contentDocument.body.hasChildNodes(),
'expected dynamically added frame to be modified' 'expected dynamically added frame to be modified'
...@@ -230,7 +230,7 @@ describe('HypothesisInjector integration test', () => { ...@@ -230,7 +230,7 @@ describe('HypothesisInjector integration test', () => {
return new Promise(resolve => { return new Promise(resolve => {
// Yield to let the DOM and CrossFrame catch up // Yield to let the DOM and CrossFrame catch up
waitForFrameObserver(() => { waitForFrameObserver(() => {
isDocumentReady(frame, () => { onDocumentReady(frame, () => {
assert( assert(
frame.contentDocument.body.hasChildNodes(), frame.contentDocument.body.hasChildNodes(),
'expected dynamically added frame to be modified' 'expected dynamically added frame to be modified'
...@@ -245,7 +245,7 @@ describe('HypothesisInjector integration test', () => { ...@@ -245,7 +245,7 @@ describe('HypothesisInjector integration test', () => {
// Yield // Yield
waitForFrameObserver(() => { waitForFrameObserver(() => {
isDocumentReady(frame, () => { onDocumentReady(frame, () => {
assert( assert(
frame.contentDocument.body.hasChildNodes(), frame.contentDocument.body.hasChildNodes(),
'expected dynamically added frame to be modified' 'expected dynamically added frame to be modified'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment