karaxnim/karax

Cannot add multiple styles

xbello opened this issue · 3 comments

I have this minimal working example, compiled with "nim c" to create HTML:

import karax / [karaxdsl, vdom, vstyles]

let node = buildHtml(tdiv):
let myStyle = style((StyleAttr.width, "25%"))

tdiv(style = myStyle):
   text ""

echo $node

Outputs the expected:

<div><div style="width: 25%; "></div></div>

Now I need to add a second class, as shown in the sample code in tests/blur.nim:

let node = buildHtml(tdiv):
let myStyle = style((StyleAttr.width, "25%"),
                    (StyleAttr.border, "1px"))
tdiv(style = myStyle):
   text ""

echo $node

Expected:

<div><div style="border: 1px; width: 25%;  "></div></div>

The code compiles, but when running it tries to insert at -1:

karax_report.nim(4) karax_report
~/.nimble/pkgs/karax-1.1.2/karax/vstyles.nim(281) style
~/.nimble/pkgs/karax-1.1.2/karax/vstyles.nim(260) setAttr    
~/.nimble/pkgs/karax-1.1.2/karax/vstyles.nim(248) setAttr
~/.choosenim/toolchains/nim-1.2.6/lib/system/fatal.nim(49) sysFatal
Error: unhandled exception: index -1 not in 0 .. 3 [IndexError]
o5k4r commented

Your strings have to be prefixed with "cstring". Try ...

let node = buildHtml(tdiv):
let myStyle = style((StyleAttr.width, cstring"25%"),
                    (StyleAttr.border, cstring"1px"))
tdiv(style = myStyle):
   text ""

echo $node

I'm trying to compile to C, and not JS. I patched my code in vstyles.nim from:

 247 for j in countdown(s.len-1, i, 2):

to

247 for j in countdown(s.len-1, max(3, i), 2):

And it seems to work FOR MY PROBLEM, not sure about more general cases.

  • fixed in linked PR, but see also toCss in #158 which allows using let myStyle = "width: 25%; border: 1px".toCss

Your strings have to be prefixed with "cstring". Try ...

actually, kstring, so it works with nim c and nim js