about summary refs log tree commit diff stats
path: root/src/html
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-02-13 16:48:58 +0100
committerbptato <nincsnevem662@gmail.com>2024-02-13 16:48:58 +0100
commitaadbaffac2430abf01a849b1f7bbfb154c27d229 (patch)
treeea88c5a91af1544dddd26d5d41e200dc4907936d /src/html
parent492a9e7cf80ae8e26b03602b5996f471c5edf4be (diff)
downloadchawan-aadbaffac2430abf01a849b1f7bbfb154c27d229.tar.gz
loader: fixes & improvements
* factor out pushBuffer to make loadFromCache async
* fix incorrect cache path
* replace rewind with loadFromCache (it does the same thing except
  actually works)
* remove rewindImpl callback, rewind in buffer instead
Diffstat (limited to 'src/html')
-rw-r--r--src/html/chadombuilder.nim44
1 files changed, 20 insertions, 24 deletions
diff --git a/src/html/chadombuilder.nim b/src/html/chadombuilder.nim
index d604f455..e202bc50 100644
--- a/src/html/chadombuilder.nim
+++ b/src/html/chadombuilder.nim
@@ -32,7 +32,6 @@ type
     stream: StringStream
     encoder: EncoderStream
     decoder: DecoderStream
-    rewindImpl: proc()
     # hack so we don't have to worry about leaks or the GC deallocating parser
     refs: seq[Document]
     stoppedFromScript: bool
@@ -301,8 +300,8 @@ proc switchCharset(wrapper: HTML5ParserWrapper) =
     errormode = ENCODER_ERROR_MODE_FATAL)
 
 proc newHTML5ParserWrapper*(stream: StringStream, window: Window, url: URL,
-    factory: CAtomFactory, rewindImpl: proc(), charsets: seq[Charset],
-    seekable: bool): HTML5ParserWrapper =
+    factory: CAtomFactory, charsets: seq[Charset], seekable: bool):
+    HTML5ParserWrapper =
   let opts = HTML5ParserOpts[Node, CAtom](
     isIframeSrcdoc: false, #TODO?
     scripting: window != nil and window.settings.scripting
@@ -313,7 +312,6 @@ proc newHTML5ParserWrapper*(stream: StringStream, window: Window, url: URL,
     builder: builder,
     opts: opts,
     stream: stream,
-    rewindImpl: rewindImpl,
     needsBOMSniff: seekable
   )
   builder.document.setActiveParser(wrapper)
@@ -390,11 +388,11 @@ proc CDB_parseDocumentWriteChunk(wrapper: pointer) {.exportc.} =
   if res == PRES_STOP:
     wrapper.stoppedFromScript = true
 
-proc parseAll*(wrapper: HTML5ParserWrapper) =
+proc parseAll*(wrapper: HTML5ParserWrapper): bool =
   let builder = wrapper.builder
   if wrapper.needsBOMSniff:
     if wrapper.stream.getPosition() + 3 >= wrapper.stream.data.len:
-      return
+      return true
     let scs = wrapper.bomSniff()
     if scs != CHARSET_UNKNOWN:
       builder.confidence = ccCertain
@@ -402,27 +400,25 @@ proc parseAll*(wrapper: HTML5ParserWrapper) =
       wrapper.seekable = false
       wrapper.switchCharset()
     wrapper.needsBOMSniff = false
-  while true:
-    let buffer = wrapper.encoder.readAll()
-    if wrapper.decoder.failed:
-      assert wrapper.seekable
-      # Retry with another charset.
-      builder.restart(wrapper)
-      wrapper.rewindImpl()
-      wrapper.switchCharset()
-      continue
-    if buffer.len == 0:
-      break
-    let res = wrapper.parseBuffer(buffer)
-    if res != PRES_STOP:
-      break
-    # res == PRES_STOP: A meta tag describing the charset has been found; force
-    # use of this charset.
+  let buffer = wrapper.encoder.readAll()
+  if wrapper.decoder.failed:
+    assert wrapper.seekable
+    # Retry with another charset.
+    builder.restart(wrapper)
+    wrapper.switchCharset()
+    return false
+  if buffer.len == 0:
+    return true
+  let res = wrapper.parseBuffer(buffer)
+  if res == PRES_STOP:
+    # A meta tag describing the charset has been found; force use of this
+    # charset.
     builder.restart(wrapper)
-    wrapper.rewindImpl()
     wrapper.charsetStack.add(builder.charset)
     wrapper.seekable = false
     wrapper.switchCharset()
+    return false
+  return true
 
 proc finish*(wrapper: HTML5ParserWrapper) =
   if wrapper.needsBOMSniff:
@@ -435,7 +431,7 @@ proc finish*(wrapper: HTML5ParserWrapper) =
     wrapper.needsBOMSniff = false
   wrapper.decoder.setInhibitCheckEnd(false)
   wrapper.wasICE = false
-  wrapper.parseAll()
+  doAssert wrapper.parseAll()
   wrapper.parser.finish()
   wrapper.builder.finish()
   for r in wrapper.refs:
ass='alt'>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101