From 8578ee7ef2564608b946b282607723246210381c Mon Sep 17 00:00:00 2001 From: David Ortner Date: Wed, 20 Mar 2024 13:50:32 +0100 Subject: [PATCH] feat: [#1334] Adds support for the setting handleDisabledFileLoadingAsSuccess, that can be used for triggering a load event instead of an error event when file loading is disabled --- .../src/browser/types/IBrowserSettings.ts | 3 +++ .../browser/types/IOptionalBrowserSettings.ts | 3 +++ .../HTMLLinkElementStyleSheetLoader.ts | 18 ++++++++++------- .../HTMLScriptElementScriptLoader.ts | 18 ++++++++++------- .../html-link-element/HTMLLinkElement.test.ts | 19 ++++++++++++++++++ .../HTMLScriptElement.test.ts | 20 +++++++++++++++++++ 6 files changed, 67 insertions(+), 14 deletions(-) diff --git a/packages/happy-dom/src/browser/types/IBrowserSettings.ts b/packages/happy-dom/src/browser/types/IBrowserSettings.ts index c53cddb3a..2c0105bb5 100644 --- a/packages/happy-dom/src/browser/types/IBrowserSettings.ts +++ b/packages/happy-dom/src/browser/types/IBrowserSettings.ts @@ -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. * diff --git a/packages/happy-dom/src/browser/types/IOptionalBrowserSettings.ts b/packages/happy-dom/src/browser/types/IOptionalBrowserSettings.ts index fed9ef5b4..b7ae0856e 100644 --- a/packages/happy-dom/src/browser/types/IOptionalBrowserSettings.ts +++ b/packages/happy-dom/src/browser/types/IOptionalBrowserSettings.ts @@ -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. * diff --git a/packages/happy-dom/src/nodes/html-link-element/HTMLLinkElementStyleSheetLoader.ts b/packages/happy-dom/src/nodes/html-link-element/HTMLLinkElementStyleSheetLoader.ts index bae857b91..69f9de8ac 100644 --- a/packages/happy-dom/src/nodes/html-link-element/HTMLLinkElementStyleSheetLoader.ts +++ b/packages/happy-dom/src/nodes/html-link-element/HTMLLinkElementStyleSheetLoader.ts @@ -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; } diff --git a/packages/happy-dom/src/nodes/html-script-element/HTMLScriptElementScriptLoader.ts b/packages/happy-dom/src/nodes/html-script-element/HTMLScriptElementScriptLoader.ts index 7f57a6c40..1cc436901 100644 --- a/packages/happy-dom/src/nodes/html-script-element/HTMLScriptElementScriptLoader.ts +++ b/packages/happy-dom/src/nodes/html-script-element/HTMLScriptElementScriptLoader.ts @@ -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; } diff --git a/packages/happy-dom/test/nodes/html-link-element/HTMLLinkElement.test.ts b/packages/happy-dom/test/nodes/html-link-element/HTMLLinkElement.test.ts index db85f0d66..7f4aa3e8d 100644 --- a/packages/happy-dom/test/nodes/html-link-element/HTMLLinkElement.test.ts +++ b/packages/happy-dom/test/nodes/html-link-element/HTMLLinkElement.test.ts @@ -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)); + + document.body.appendChild(element); + + expect(element.sheet).toBe(null); + expect(((loadEvent)).type).toBe('load'); + }); }); }); diff --git a/packages/happy-dom/test/nodes/html-script-element/HTMLScriptElement.test.ts b/packages/happy-dom/test/nodes/html-script-element/HTMLScriptElement.test.ts index 1e7363443..2c9f1ab3f 100644 --- a/packages/happy-dom/test/nodes/html-script-element/HTMLScriptElement.test.ts +++ b/packages/happy-dom/test/nodes/html-script-element/HTMLScriptElement.test.ts @@ -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 = window.document.createElement('script'); + script.src = 'https://localhost:8080/path/to/script.js'; + script.async = true; + script.addEventListener('load', (event) => { + loadEvent = event; + }); + + document.body.appendChild(script); + + expect(((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 }