Skip to content

Commit

Permalink
cleanup unused code and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-mai authored and a-h committed Sep 27, 2023
1 parent fd73ec4 commit 9d7c432
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 71 deletions.
6 changes: 5 additions & 1 deletion parser/v2/elementparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ func TestElementParser(t *testing.T) {
},
},
},
IndentAttrs: true,
Children: []Node{
Text{
Value: "Test",
Expand Down Expand Up @@ -593,6 +594,7 @@ func TestElementParser(t *testing.T) {
},
},
},
IndentAttrs: true,
},
},
{
Expand Down Expand Up @@ -641,6 +643,7 @@ func TestElementParser(t *testing.T) {
},
},
},
IndentAttrs: true,
},
},
{
Expand Down Expand Up @@ -681,6 +684,7 @@ func TestElementParser(t *testing.T) {
},
},
},
IndentAttrs: true,
Children: []Node{
Text{Value: "Test"},
},
Expand Down Expand Up @@ -800,7 +804,7 @@ func TestElementParser(t *testing.T) {
},
},
{
name: "element: inputs can contain class attributes",
name: "element: inputs can contain class attributes",
input: `<input type="email" id="email" name="email" class={ "a", "b", "c", templ.KV("c", false)} placeholder="[email protected]" autocomplete="off"/>`,
expected: Element{
Name: "input",
Expand Down
2 changes: 2 additions & 0 deletions parser/v2/ifexpressionparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func TestIfExpression(t *testing.T) {
},
Whitespace{Value: "\n"},
},
IndentChildren: true,
},
Whitespace{Value: "\n"},
},
Expand Down Expand Up @@ -209,6 +210,7 @@ func TestIfExpression(t *testing.T) {
},
Whitespace{Value: "\n"},
},
IndentChildren: true,
},
Whitespace{Value: "\n"},
},
Expand Down
2 changes: 2 additions & 0 deletions parser/v2/switchexpressionparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ default:
},
Whitespace{Value: "\n\t"},
},
IndentChildren: true,
},
Whitespace{Value: "\n"},
},
Expand Down Expand Up @@ -173,6 +174,7 @@ default:
},
Whitespace{Value: "\n"},
},
IndentChildren: true,
},
Whitespace{Value: "\n"},
},
Expand Down
3 changes: 3 additions & 0 deletions parser/v2/templateparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,11 @@ func TestTemplateParser(t *testing.T) {
},
Whitespace{Value: "\n "},
},
IndentChildren: true,
},
Whitespace{Value: "\n"},
},
IndentChildren: true,
},
Whitespace{Value: "\n"},
},
Expand Down Expand Up @@ -300,6 +302,7 @@ func TestTemplateParser(t *testing.T) {
},
Whitespace{"\n\t\t"},
},
IndentChildren: true,
},
Whitespace{
Value: "\n\t",
Expand Down
43 changes: 1 addition & 42 deletions parser/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,6 @@ func (e Element) IsVoidElement() bool {
return ok
}

var blockElements = map[string]struct{}{
"address": {}, "article": {}, "aside": {}, "body": {}, "blockquote": {}, "canvas": {}, "dd": {}, "div": {}, "dl": {}, "dt": {}, "fieldset": {}, "figcaption": {}, "figure": {}, "footer": {}, "form": {}, "h1": {}, "h2": {}, "h3": {}, "h4": {}, "h5": {}, "h6": {}, "head": {}, "header": {}, "hr": {}, "html": {}, "li": {}, "main": {}, "meta": {}, "nav": {}, "noscript": {}, "ol": {}, "p": {}, "pre": {}, "script": {}, "section": {}, "table": {}, "tr": {}, "th": {}, "td": {}, "template": {}, "tfoot": {}, "turbo-stream": {}, "ul": {}, "video": {},
}

func (e Element) isBlockElement() bool {
_, ok := blockElements[e.Name]
return ok
}

func (e Element) hasNonWhitespaceChildren() bool {
for _, c := range e.Children {
if _, isWhitespace := c.(Whitespace); !isWhitespace {
Expand All @@ -352,32 +343,6 @@ func (e Element) hasNonWhitespaceChildren() bool {
return false
}

func (e Element) containsBlockElement() bool {
for _, c := range e.Children {
switch n := c.(type) {
case Whitespace:
continue
case Element:
if n.isBlockElement() {
return true
}
continue
case StringExpression:
continue
case Text:
continue
case TemplElementExpression:
if len(n.Children) > 0 {
return true
}
continue
}
// Any template elements should be considered block.
return true
}
return false
}

// Validate that no invalid expressions have been used.
func (e Element) Validate() (msgs []string, ok bool) {
// Validate that style attributes are constant.
Expand Down Expand Up @@ -433,7 +398,7 @@ func (e Element) Write(w io.Writer, indent int) error {
}
}
var closeAngleBracketIndent int
if e.IndentAttrs {
if e.IndentAttrs {
w.Write([]byte("\n"))
closeAngleBracketIndent = indent
}
Expand Down Expand Up @@ -541,7 +506,6 @@ func (e RawElement) Write(w io.Writer, indent int) error {
}

type Attribute interface {
IsMultilineAttr() bool
// Write out the string.
Write(w io.Writer, indent int) error
}
Expand All @@ -551,7 +515,6 @@ type BoolConstantAttribute struct {
Name string
}

func (bca BoolConstantAttribute) IsMultilineAttr() bool { return false }
func (bca BoolConstantAttribute) String() string {
return bca.Name
}
Expand All @@ -566,7 +529,6 @@ type ConstantAttribute struct {
Value string
}

func (ca ConstantAttribute) IsMultilineAttr() bool { return false }
func (ca ConstantAttribute) String() string {
return ca.Name + `="` + html.EscapeString(ca.Value) + `"`
}
Expand All @@ -581,7 +543,6 @@ type BoolExpressionAttribute struct {
Expression Expression
}

func (ea BoolExpressionAttribute) IsMultilineAttr() bool { return false }
func (ea BoolExpressionAttribute) String() string {
return ea.Name + `?={ ` + ea.Expression.Value + ` }`
}
Expand All @@ -596,7 +557,6 @@ type ExpressionAttribute struct {
Expression Expression
}

func (ea ExpressionAttribute) IsMultilineAttr() bool { return false }
func (ea ExpressionAttribute) String() string {
return ea.Name + `={ ` + ea.Expression.Value + ` }`
}
Expand All @@ -615,7 +575,6 @@ type ConditionalAttribute struct {
Else []Attribute
}

func (ca ConditionalAttribute) IsMultilineAttr() bool { return true }
func (ca ConditionalAttribute) String() string {
sb := new(strings.Builder)
_ = ca.Write(sb, 0)
Expand Down
Loading

0 comments on commit 9d7c432

Please sign in to comment.