Skip to content

Commit

Permalink
Support ... in element attributes (#16)
Browse files Browse the repository at this point in the history
For passing arbitrary attributes to elements.
  • Loading branch information
MichaelHatherly authored Nov 26, 2023
1 parent 2f8c17b commit b658c1f
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/nodes/element.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ end

function expression(c::BuilderContext, a::Attribute)
name = Symbol(a.name)
if a.dynamic
if name == :...
return Expr(:(...), Meta.parse(a.value))
elseif a.dynamic
return Expr(:(kw), name, Meta.parse(a.value))
elseif a.interpolate
return Expr(:(kw), name, Meta.parse("\"\"\"$(a.value)\"\"\""))
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ struct Prop

function Prop(name, value)
vararg = false
if endswith(name, "...")
if endswith(name, __SPECIAL_SPLAT_SYMBOL__)
name == value || error("invalid syntax for '...' prop: $(name)...=$(value).")
name, _ = rsplit(name, "..."; limit = 2)
name, _ = rsplit(name, __SPECIAL_SPLAT_SYMBOL__; limit = 2)
value = name
vararg = true
end
Expand Down
15 changes: 13 additions & 2 deletions src/symbol-swapping.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
const __SPECIAL_AT_SYMBOL__ = "__special_at_symbol__"
const __SPECIAL_DOLLAR_SYMBOL__ = "__special_dollar_symbol__"
const __SPECIAL_SPLAT_SYMBOL__ = "__special_splat_symbol__"

function _swap_special_symbols(s::String)::String
return _replace(s, "@" => __SPECIAL_AT_SYMBOL__, "\$" => __SPECIAL_DOLLAR_SYMBOL__)
return _replace(
s,
"@" => __SPECIAL_AT_SYMBOL__,
"\$" => __SPECIAL_DOLLAR_SYMBOL__,
"..." => __SPECIAL_SPLAT_SYMBOL__,
)
end

function _restore_special_symbols(other)
return other
end

function _restore_special_symbols(s::AbstractString)
return _replace(s, __SPECIAL_AT_SYMBOL__ => "@", __SPECIAL_DOLLAR_SYMBOL__ => "\$")
return _replace(
s,
__SPECIAL_AT_SYMBOL__ => "@",
__SPECIAL_DOLLAR_SYMBOL__ => "\$",
__SPECIAL_SPLAT_SYMBOL__ => "...",
)
end

@static if VERSION >= v"1.7"
Expand Down
15 changes: 15 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@ end
b = 10,
)

@test_reference joinpath(basic, "splatted-props.1.txt") render(
Templates.var"splatted-props";
props = (;),
)

@test_reference joinpath(basic, "splatted-props.2.txt") render(
Templates.var"splatted-props";
props = (; value = 2),
)

@test_reference joinpath(basic, "splatted-props.3.txt") render(
Templates.var"splatted-props";
props = (; option = "option"),
)

@test_throws_st UndefVarError render(Templates.var"file-and-line-info-1") [
"file-and-line-info.html:2",
"file-and-line-info.html:1",
Expand Down
1 change: 1 addition & 0 deletions test/templates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ template"templates/basic/layout-usage.html"
template"templates/basic/special-symbols.html"
template"templates/basic/custom-elements.html"
template"templates/basic/splat.html"
template"templates/basic/splatted-props.html"
template"templates/basic/file-and-line-info.html"

module Complex
Expand Down
1 change: 1 addition & 0 deletions test/templates/basic/splatted-props.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div><b>1</b><div other="1"></div></div>
1 change: 1 addition & 0 deletions test/templates/basic/splatted-props.2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div><b>2</b><div value="2" other="1"></div></div>
1 change: 1 addition & 0 deletions test/templates/basic/splatted-props.3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div><b>1</b><div option other="1"></div></div>
12 changes: 12 additions & 0 deletions test/templates/basic/splatted-props.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<function splatted-props props>
<div>
<splatted-props-inner ...=props />
<div ...="merge(props, (; other = 1))"></div>
</div>
</function>

<function splatted-props-inner value="1" _...>
<b>
<julia value />
</b>
</function>

0 comments on commit b658c1f

Please sign in to comment.