diff options
author | bptato <nincsnevem662@gmail.com> | 2023-12-22 16:06:01 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-12-22 16:06:01 +0100 |
commit | e57322493d12beb45d8c96a9e82cc7460fbb9144 (patch) | |
tree | 72f56da3d561975e8a493c3654ee5e00c02d345b /src/html | |
parent | 4cfc995f4b60e573020f2e93b8d4fb9269aece35 (diff) | |
download | chawan-e57322493d12beb45d8c96a9e82cc7460fbb9144.tar.gz |
dom: simplify fetch classic script
Diffstat (limited to 'src/html')
-rw-r--r-- | src/html/dom.nim | 43 |
1 files changed, 21 insertions, 22 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim index c6441924..e5230556 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -2988,30 +2988,29 @@ proc createClassicScript(source: string, baseURL: URL, options: ScriptOptions, m mutedErrors: mutedErrors ) -#TODO settings object +type OnCompleteProc = proc(element: HTMLScriptElement, res: ScriptResult) + proc fetchClassicScript(element: HTMLScriptElement, url: URL, - options: ScriptOptions, cors: CORSAttribute, - cs: Charset, onComplete: (proc(element: HTMLScriptElement, - res: ScriptResult))) = - if not element.scriptingEnabled: - element.onComplete(ScriptResult(t: RESULT_NULL)) + options: ScriptOptions, cors: CORSAttribute, cs: Charset, + onComplete: OnCompleteProc) = + let window = element.document.window + if not element.scriptingEnabled or window.loader.isNone: + element.onComplete(ScriptResult(t: RESULT_NULL)) + return + let loader = window.loader.get + let request = createPotentialCORSRequest(url, RequestDestination.SCRIPT, cors) + let response = loader.doRequest(request) + if response.res != 0: + element.onComplete(ScriptResult(t: RESULT_NULL)) + return + let cs = if cs == CHARSET_UNKNOWN: + CHARSET_UTF_8 else: - let loader = element.document.window.loader - if loader.isSome: - let loader = loader.get - let request = createPotentialCORSRequest(url, RequestDestination.SCRIPT, cors) - let response = loader.doRequest(request) - if response.res != 0: - element.onComplete(ScriptResult(t: RESULT_NULL)) - return - let cs = if cs == CHARSET_UNKNOWN: - CHARSET_UTF_8 - else: - cs - let decoder = newDecoderStream(response.body, cs = cs) - let source = newEncoderStream(decoder).readAll() - let script = createClassicScript(source, url, options, false) - element.markAsReady(ScriptResult(t: RESULT_SCRIPT, script: script)) + cs + let decoder = newDecoderStream(response.body, cs = cs) + let source = newEncoderStream(decoder).readAll() + let script = createClassicScript(source, url, options, false) + element.markAsReady(ScriptResult(t: RESULT_SCRIPT, script: script)) proc execute*(element: HTMLScriptElement) = let document = element.document |