about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/utils/twtstr.nim20
-rw-r--r--test/js/title.html10
2 files changed, 19 insertions, 11 deletions
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index a83dfeef..e5dab87a 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -130,19 +130,17 @@ func skipBlanks*(buf: openArray[char]; at: int): int =
   while result < buf.len and buf[result] in AsciiWhitespace:
     inc result
 
-func stripAndCollapse*(s: string): string =
+func stripAndCollapse*(s: openArray[char]): string =
+  var res = newStringOfCap(s.len)
   var space = false
-  result = ""
-  for i in s.skipBlanks(0) ..< s.len:
-    if s[i] notin AsciiWhitespace:
+  for c in s.toOpenArray(s.skipBlanks(0), s.high):
+    let cspace = c in AsciiWhitespace
+    if not cspace:
       if space:
-        result &= ' '
-        space = false
-      result &= s[i]
-    elif not space:
-      space = true
-    else:
-      result &= ' '
+        res &= ' '
+      res &= c
+    space = cspace
+  return res
 
 func until*(s: openArray[char]; c: set[char]; starti = 0): string =
   result = ""
diff --git a/test/js/title.html b/test/js/title.html
new file mode 100644
index 00000000..f364f3c6
--- /dev/null
+++ b/test/js/title.html
@@ -0,0 +1,10 @@
+<!doctype html>
+<title id=t>
+   Title    test
+   </title>
+<div id=x>Fail</div>
+<script src=asserts.js></script>
+<script>
+assert_equals(document.title, "Title test");
+document.getElementById("x").textContent = "Success";
+</script>