Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
Decouple moving parts: use last id only
Browse files Browse the repository at this point in the history
  • Loading branch information
schneiderfelipe committed Jul 4, 2021
1 parent 1b229c9 commit ddeadd8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
17 changes: 2 additions & 15 deletions src/hyperscript.nim
Expand Up @@ -335,7 +335,7 @@ macro createElement(args: varargs[untyped]): untyped =


var
tag, id, style: string
tag, style: string
classes: seq[string]
attributes, children, events = newNimNode(nnkBracket)

Expand All @@ -354,14 +354,6 @@ macro createElement(args: varargs[untyped]): untyped =
addClass class.strVal


func addId(x: string) {.compileTime.} =
## Helper that adds a new class.
id.add x
func addId(x: NimNode) {.compileTime.} =
## Helper that adds a new class.
addId x.strVal


func addStyle(styles: auto) {.compileTime.} =
## Helper that adds styles.
style.add styles
Expand Down Expand Up @@ -399,8 +391,6 @@ macro createElement(args: varargs[untyped]): untyped =
case key:
of "class":
addClass val
of "id":
addId val
of "style":
addStyle val
else:
Expand Down Expand Up @@ -436,7 +426,7 @@ macro createElement(args: varargs[untyped]): untyped =
of '.':
addClass selector[1..^1]
of '#':
addId selector[1..^1]
addAttribute "id", selector[1..^1]
of '[':
let fs = selector[1..^1].split('=', 1)
addAttribute fs[0], fs[1].strip(chars = {'\'', '"'})
Expand Down Expand Up @@ -489,9 +479,6 @@ macro createElement(args: varargs[untyped]): untyped =
if len(classes) > 0:
addTupleExpr(attributes, "class", join(classes, " "))

if len(id) > 0:
addTupleExpr(attributes, "id", id)

if len(style) > 0:
addTupleExpr(attributes, "style", style.strip)

Expand Down
6 changes: 3 additions & 3 deletions tests/tests.nim
Expand Up @@ -144,10 +144,10 @@ suite "Using selector notation":
check h("div#header").tag == "div"
check h("div#header").attr("id") == "header"

test "two ids get concatenated":
test "the last given id takes precedence":
check h("div#header#main") is HTMLNode
check h("div#header#main").tag == "div"
check h("div#header#main").attr("id") == "headermain"
check h("div#header#main").attr("id") == "main"

test "can indicate a class":
check h("div.header") is HTMLNode
Expand Down Expand Up @@ -197,7 +197,7 @@ suite "Using selector notation":
test "can mix notations":
check h("div#header", id = "main") is HTMLNode
check h("div#header", id = "main").tag == "div"
check h("div#header", id = "main").attr("id") == "headermain"
check h("div#header", id = "main").attr("id") == "main"

check h("div.header", class = "note") is HTMLNode
check h("div.header", class = "note").tag == "div"
Expand Down

0 comments on commit ddeadd8

Please sign in to comment.