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 class only (for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
schneiderfelipe committed Jul 4, 2021
1 parent ddeadd8 commit 70bb94d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 20 deletions.
18 changes: 2 additions & 16 deletions src/hyperscript.nim
Expand Up @@ -336,7 +336,6 @@ macro createElement(args: varargs[untyped]): untyped =

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


Expand All @@ -346,14 +345,6 @@ macro createElement(args: varargs[untyped]): untyped =
(`first`, `second`)


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


func addStyle(styles: auto) {.compileTime.} =
## Helper that adds styles.
style.add styles
Expand Down Expand Up @@ -389,8 +380,6 @@ macro createElement(args: varargs[untyped]): untyped =
func addAttribute(key, val: auto) {.compileTime.} =
## Helper that adds a new attribute.
case key:
of "class":
addClass val
of "style":
addStyle val
else:
Expand Down Expand Up @@ -424,7 +413,7 @@ macro createElement(args: varargs[untyped]): untyped =
if len(selector) > 0:
case selector[0]:
of '.':
addClass selector[1..^1]
addAttribute "class", selector[1..^1]
of '#':
addAttribute "id", selector[1..^1]
of '[':
Expand All @@ -434,7 +423,7 @@ macro createElement(args: varargs[untyped]): untyped =
debugEcho "unknown selector: " & selector


# Process selector and update tag, id, classes and attributes
# Process selector and update tag and attributes
var j = selector.find({'.', '#', '['})
if j == -1:
# Only a tag
Expand Down Expand Up @@ -476,9 +465,6 @@ macro createElement(args: varargs[untyped]): untyped =
addChild arg


if len(classes) > 0:
addTupleExpr(attributes, "class", join(classes, " "))

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

Expand Down
8 changes: 4 additions & 4 deletions tests/tests.nim
Expand Up @@ -154,10 +154,10 @@ suite "Using selector notation":
check h("div.header").tag == "div"
check h("div.header").attr("class") == "header"

test "two classes get joined":
test "the last given class takes precedence":
check h("div.header.note") is HTMLNode
check h("div.header.note").tag == "div"
check h("div.header.note").attr("class") == "header note"
check h("div.header.note").attr("class") == "note"


test "can indicate an attribute":
Expand Down Expand Up @@ -201,11 +201,11 @@ suite "Using selector notation":

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

check h("a.link[href=/]", {class: "selected"}, "Home") is HTMLNode
check h("a.link[href=/]", {class: "selected"}, "Home").tag == "a"
check h("a.link[href=/]", {class: "selected"}, "Home").attr("class") == "link selected"
check h("a.link[href=/]", {class: "selected"}, "Home").attr("class") == "selected"
check h("a.link[href=/]", {class: "selected"}, "Home").attr("href") == "/"
check h("a.link[href=/]", {class: "selected"}, "Home").text == "Home"

Expand Down

0 comments on commit 70bb94d

Please sign in to comment.