Skip to content

Commit

Permalink
feat: [#1334] Adds support for the setting handleDisabledFileLoadingA…
Browse files Browse the repository at this point in the history
…sSuccess, that can be used for triggering a load event instead of an error event when file loading is disabled
  • Loading branch information
capricorn86 committed Mar 20, 2024
1 parent 8fc9edc commit 8578ee7
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 14 deletions.
3 changes: 3 additions & 0 deletions packages/happy-dom/src/browser/types/IBrowserSettings.ts
Expand Up @@ -17,6 +17,9 @@ export default interface IBrowserSettings {
/** Disables computed style rendering. */
disableComputedStyleRendering: boolean;

/** Handle disabled resource loading as success */
handleDisabledFileLoadingAsSuccess: boolean;

/**
* Disables error capturing.
*
Expand Down
Expand Up @@ -14,6 +14,9 @@ export default interface IOptionalBrowserSettings {
/** Disables computed style rendering. */
disableComputedStyleRendering?: boolean;

/** Handle disabled file loading as success */
handleDisabledFileLoadingAsSuccess?: boolean;

/**
* Disables error capturing.
*
Expand Down
Expand Up @@ -63,13 +63,17 @@ export default class HTMLLinkElementStyleSheetLoader {
}

if (browserSettings.disableCSSFileLoading) {
WindowErrorUtility.dispatchError(
element,
new DOMException(
`Failed to load external stylesheet "${absoluteURL}". CSS file loading is disabled.`,
DOMExceptionNameEnum.notSupportedError
)
);
if (browserSettings.handleDisabledFileLoadingAsSuccess) {
element.dispatchEvent(new Event('load'));
} else {
WindowErrorUtility.dispatchError(
element,
new DOMException(
`Failed to load external stylesheet "${absoluteURL}". CSS file loading is disabled.`,
DOMExceptionNameEnum.notSupportedError
)
);
}
return;
}

Expand Down
Expand Up @@ -61,13 +61,17 @@ export default class HTMLScriptElementScriptLoader {
browserSettings.disableJavaScriptFileLoading ||
browserSettings.disableJavaScriptEvaluation
) {
WindowErrorUtility.dispatchError(
element,
new DOMException(
`Failed to load external script "${absoluteURL}". JavaScript file loading is disabled.`,
DOMExceptionNameEnum.notSupportedError
)
);
if (browserSettings.handleDisabledFileLoadingAsSuccess) {
element.dispatchEvent(new Event('load'));
} else {
WindowErrorUtility.dispatchError(
element,
new DOMException(
`Failed to load external script "${absoluteURL}". JavaScript file loading is disabled.`,
DOMExceptionNameEnum.notSupportedError
)
);
}
return;
}

Expand Down
Expand Up @@ -253,5 +253,24 @@ describe('HTMLLinkElement', () => {
'Failed to load external stylesheet "https://localhost:8080/test/path/file.css". CSS file loading is disabled.'
);
});

it('Triggers a load event when the Happy DOM setting "disableCSSFileLoading" and "handleDisabledFileLoadingAsSuccess" is set to "true".', async () => {
window = new Window({
settings: { disableCSSFileLoading: true, handleDisabledFileLoadingAsSuccess: true }
});
document = window.document;

const element = document.createElement('link');
let loadEvent: Event | null = null;

element.rel = 'stylesheet';
element.href = 'https://localhost:8080/test/path/file.css';
element.addEventListener('load', (event) => (loadEvent = <Event>event));

document.body.appendChild(element);

expect(element.sheet).toBe(null);
expect((<Event>(<unknown>loadEvent)).type).toBe('load');
});
});
});
Expand Up @@ -374,6 +374,26 @@ describe('HTMLScriptElement', () => {
);
});

it('Triggers a load event when attempting to perform an asynchrounous request and the Happy DOM setting "disableJavaScriptFileLoading" and "handleDisabledFileLoadingAsSuccess" is set to "true".', () => {
window = new Window({
settings: { disableJavaScriptFileLoading: true, handleDisabledFileLoadingAsSuccess: true }
});
document = window.document;

let loadEvent: Event | null = null;

const script = <HTMLScriptElement>window.document.createElement('script');
script.src = 'https://localhost:8080/path/to/script.js';
script.async = true;
script.addEventListener('load', (event) => {
loadEvent = <Event>event;
});

document.body.appendChild(script);

expect((<Event>(<unknown>loadEvent)).type).toBe('load');
});

it('Triggers an error event when attempting to perform a synchrounous request and the Happy DOM setting "disableJavaScriptFileLoading" is set to "true".', () => {
window = new Window({
settings: { disableJavaScriptFileLoading: true }
Expand Down

0 comments on commit 8578ee7

Please sign in to comment.