Open juancarlospaco opened 3 years ago
Make a simple working one
import strutils, parseutils
proc js2css*(js: openArray[char], css: var openArray[char]) =
## Encode JavaScript on CSS, without increasing CSS size.
var i: int # I can confirm this proc works ok.
var j: int
var temp = newStringOfCap(8)
while true:
temp = $ord(js[i]).toBin(8) # Can it be 6?
for bc in temp:
while true:
let c = css[j]
if c in {'a'..'z', 'A'..'Z'}:
css[j] = if bc == '1': toUpperAscii(c) else: toLowerAscii(c)
inc j
break
inc j
if js.high == i: return
else: inc i
var ix = 0
proc css2js*(css: openArray[char]): string =
var temp = newStringOfCap(8)
for c in css:
if c in {'a'..'z', 'A'..'Z'}:
var x = ' '
if c in {'A'..'Z'}:
x = '1'
elif c in {'a'..'z'}:
x = '0'
temp.add(x)
if temp.len == 8:
var num: int
discard parseBin(temp, num)
result.add chr(num)
temp = "" # temp back to empty string?
inc ix
if ix == 20:
break
var stile = readFile("test.css") # Use a Bootstrap.css
js2css(readFile("test.js"), stile) # console.log("hello world")
echo css2js(stile)
TODO:
Client-Side:
let javascript: cstring = css2js( wholeCSS )
to get JS string.eval(javascript)
the JavaScript string.proc
only.Basically move css2js
to the client-side.
Interesting idea but not specific to nim, so I'd suspect this would be used (eg alongside minifiers) if this worked in practice.
Bootstrap CSS is 145 kilobytes, is like ~25 Kb of JS, for free, for the "core" of a simple Nim app should do, lazy load the rest.
but if some site causes bootstrap (with same version before encoding) to be cached, a site using this trick would cause that cache to be invalidated and the modified bootstrap would have to be re-downloaded.
Poor Of Concept
IQ 2000 secret hi tecc
Server-side:
uppercase == 1 / lowercase == 0
, on the chars of the CSSClient-Side:
uppercase == 1 / lowercase == 0
, as binary string.Example
I can embed the JS on the CSS, I can not get it back, needs
css2js
.More storage
space == 1 / tab == 0
this needs some kind of base4 encoding.Can you help me with
css2js
@xflywind ?.