Skip to content

Commit

Permalink
fix: fixed insertion of additional whitespace on format (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonnyLoughlin authored Dec 20, 2023
1 parent d614702 commit 83497bc
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.495
0.2.497
10 changes: 5 additions & 5 deletions generator/test-complex-attributes/template.templ
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package testcomplexattributes

templ ComplexAttributes() {
<div
x-data="{darkMode: localStorage.getItem('darkMode') || localStorage.setItem('darkMode', 'system')}"
x-init="$watch('darkMode', val => localStorage.setItem('darkMode', val))"
:class="{'dark': darkMode === 'dark' || (darkMode === 'system' && window.matchMedia('(prefers-color-scheme: dark)').matches)}"
x-data="{darkMode: localStorage.getItem('darkMode') || localStorage.setItem('darkMode', 'system')}"
x-init="$watch('darkMode', val => localStorage.setItem('darkMode', val))"
:class="{'dark': darkMode === 'dark' || (darkMode === 'system' && window.matchMedia('(prefers-color-scheme: dark)').matches)}"
></div>
<div x-data="{ count: 0 }">
<button
x-on:click="count++"
x-on:click="count++"
>Increment</button>
<span x-text="count"></span>
</div>
<div x-data="{ count: 0 }">
<button
@click="count++"
@click="count++"
>Increment</button>
<span x-text="count"></span>
</div>
Expand Down
22 changes: 11 additions & 11 deletions generator/test-element-attributes/template.templ
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,36 @@ css unimportant() {

templ render(p person) {
<div
style="width: 100;"
if p.important {
style="width: 100;"
if p.important {
class={ important() }
}
>Important</div>
<div
style="width: 100;"
if !p.important {
style="width: 100;"
if !p.important {
class={ unimportant }
}
>Unimportant</div>
<div
style="width: 100;"
if p.important {
style="width: 100;"
if p.important {
class={ important }
} else {
class={ unimportant }
}
>Else</div>
<div
data-script="on click
data-script="on click
do something
end"
></div>
<h2>HTMX Wildcard attribute</h2>
<form
hx-post="/api/secret/unlock"
hx-target="#secret"
hx-target-*="#errors"
hx-indicator="#loading-indicator"
hx-post="/api/secret/unlock"
hx-target="#secret"
hx-target-*="#errors"
hx-indicator="#loading-indicator"
>
<input type="button" value="Unlock"/>
</form>
Expand Down
6 changes: 3 additions & 3 deletions generator/test-script-usage/template.templ
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ script conditionalScript() {

templ Conditional(show bool) {
<input
type="button"
value="Click me"
if show {
type="button"
value="Click me"
if show {
onclick={ conditionalScript() }
}
/>
Expand Down
24 changes: 15 additions & 9 deletions parser/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ func (c ConstantCSSProperty) Write(w io.Writer, indent int) error {
}
return nil
}

func (c ConstantCSSProperty) String(minified bool) string {
sb := new(strings.Builder)
sb.WriteString(c.Name)
Expand Down Expand Up @@ -340,9 +341,11 @@ func (t HTMLTemplate) Write(w io.Writer, indent int) error {
// text node, or string expression.
type TrailingSpace string

const SpaceNone TrailingSpace = ""
const SpaceHorizontal TrailingSpace = " "
const SpaceVertical TrailingSpace = "\n"
const (
SpaceNone TrailingSpace = ""
SpaceHorizontal TrailingSpace = " "
SpaceVertical TrailingSpace = "\n"
)

var ErrNonSpaceCharacter = errors.New("non space character found")

Expand Down Expand Up @@ -380,9 +383,11 @@ type WhitespaceTrailer interface {
Trailing() TrailingSpace
}

var _ WhitespaceTrailer = Element{}
var _ WhitespaceTrailer = Text{}
var _ WhitespaceTrailer = StringExpression{}
var (
_ WhitespaceTrailer = Element{}
_ WhitespaceTrailer = Text{}
_ WhitespaceTrailer = StringExpression{}
)

// Text node within the document.
type Text struct {
Expand Down Expand Up @@ -494,9 +499,10 @@ func (e Element) Write(w io.Writer, indent int) error {
return err
}
attrIndent = indent + 1
}
if _, err := w.Write([]byte(" ")); err != nil {
return err
} else {
if _, err := w.Write([]byte(" ")); err != nil {
return err
}
}
if err := a.Write(w, attrIndent); err != nil {
return err
Expand Down
28 changes: 14 additions & 14 deletions parser/v2/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,11 @@ package test
templ conditionalAttributes(addClass bool) {
<div
id="conditional"
if addClass {
id="conditional"
if addClass {
class="itWasTrue"
}
width="300"
width="300"
>Content</div>
}
`,
Expand All @@ -441,11 +441,11 @@ package test
templ conditionalAttributes(addClass bool) {
<div
id="conditional"
if addClass {
id="conditional"
if addClass {
class="itWasTrue"
}
width="300"
width="300"
>Content</div>
}
`,
Expand All @@ -469,8 +469,8 @@ package test
templ conditionalAttributes(addClass bool) {
<div
id="conditional"
if addClass {
id="conditional"
if addClass {
class="itWasTrue"
}
>
Expand Down Expand Up @@ -499,13 +499,13 @@ package test
templ conditionalAttributes(addClass bool) {
<div
id="conditional"
if addClass {
id="conditional"
if addClass {
class="itWasTrue"
} else {
class="itWasNotTrue"
}
width="300"
width="300"
>Content</div>
}
`,
Expand Down Expand Up @@ -567,9 +567,9 @@ package main
templ x(id string, class string) {
<button
id={ id }
name={ "name" }
class={
id={ id }
name={ "name" }
class={
"blue",
class,
map[string]bool{
Expand Down

0 comments on commit 83497bc

Please sign in to comment.