Skip to content

Commit

Permalink
chore: missing test cases (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
joerdav authored Nov 9, 2022
1 parent 9c5e3bc commit 5787320
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
30 changes: 30 additions & 0 deletions parser/v2/elementparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,36 @@ func TestElementParserErrors(t *testing.T) {
Col: 7,
}),
},
{
name: "element: style must only contain text",
input: `<style><button /></style>`,
expected: newParseError("<style>: invalid node contents: script and style attributes must only contain text",
Position{
Index: 0,
Line: 0,
Col: 0,
},
Position{
Index: 25,
Line: 0,
Col: 25,
}),
},
{
name: "element: script must only contain text",
input: `<script><button /></script>`,
expected: newParseError("<script>: invalid node contents: script and style attributes must only contain text",
Position{
Index: 0,
Line: 0,
Col: 0,
},
Position{
Index: 27,
Line: 0,
Col: 27,
}),
},
{
name: "element: attempted use of expression for style attribute (open/close)",
input: `<a style={ value }></a>`,
Expand Down
63 changes: 63 additions & 0 deletions parser/v2/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,69 @@ templ input(items []string) {
</div>
}
`,
},
{
name: "if statements are placed on a new line",
input: ` // first line removed to make indentation clear in Go code
package test
templ input(items []string) {
<div>{ "the" }<div>{ "other" }</div>if items != nil {
<div>{ items[0] }</div>
} else {
<div>{ items[1] }</div>
}
</div>
}
`,
expected: `// first line removed to make indentation clear in Go code
package test
templ input(items []string) {
<div>
{ "the" }
<div>{ "other" }</div>
if items != nil {
<div>{ items[0] }</div>
} else {
<div>{ items[1] }</div>
}
</div>
}
`,
},
{
name: "switch statements are placed on a new line",
input: ` // first line removed to make indentation clear in Go code
package test
templ input(items []string) {
<div>{ "the" }<div>{ "other" }</div>switch items[0] {
case "a":
<div>{ items[0] }</div>
case "b":
<div>{ items[1] }</div>
}</div>
}
`,
expected: `// first line removed to make indentation clear in Go code
package test
templ input(items []string) {
<div>
{ "the" }
<div>{ "other" }</div>
switch items[0] {
case "a":
<div>{ items[0] }</div>
case "b":
<div>{ items[1] }</div>
}
</div>
}
`,
},
{
Expand Down
49 changes: 49 additions & 0 deletions safehtml/style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func TestSanitizeCSS(t *testing.T) {
inputValue: `1 1 1 1`,
expectedValue: `1 1 1 1`,
},
{
name: "properties are case corrected",
inputProperty: "Border",
expectedProperty: "border",
inputValue: `1 1 1 1`,
expectedValue: `1 1 1 1`,
},
{
name: "expressions are not allowed",
inputProperty: "width",
Expand Down Expand Up @@ -80,6 +87,13 @@ func TestSanitizeCSS(t *testing.T) {
inputValue: `"quotes`,
expectedValue: InnocuousPropertyValue,
},
{
name: "font-family non standard names are not allowed",
inputProperty: "font-family",
expectedProperty: "font-family",
inputValue: `foo@bar`,
expectedValue: InnocuousPropertyValue,
},
{
name: "obfuscated values are not allowed",
inputProperty: "width",
Expand Down Expand Up @@ -129,6 +143,34 @@ func TestSanitizeCSS(t *testing.T) {
inputValue: `url(vbscript:alert(1337))`,
expectedValue: InnocuousPropertyValue,
},
{
name: "background-image absolute FTP URL",
inputProperty: "background-image",
expectedProperty: "background-image",
inputValue: `url("ftp://safe.example.com/img.png")`,
expectedValue: InnocuousPropertyValue,
},
{
name: "background-image invalid URL",
inputProperty: "background-image",
expectedProperty: "background-image",
inputValue: `url("` + string([]byte{0x7f}) + `")`,
expectedValue: InnocuousPropertyValue,
},
{
name: "background-image invalid prefix",
inputProperty: "background-image",
expectedProperty: "background-image",
inputValue: `/img.png")`,
expectedValue: InnocuousPropertyValue,
},
{
name: "background-image invalid sufix",
inputProperty: "background-image",
expectedProperty: "background-image",
inputValue: `url("/img.png`,
expectedValue: InnocuousPropertyValue,
},
{
name: "background-image safe URL",
inputProperty: "background-image",
Expand All @@ -143,6 +185,13 @@ func TestSanitizeCSS(t *testing.T) {
inputValue: `url("http://safe.example.com/img.png")`,
expectedValue: `url("http://safe.example.com/img.png")`,
},
{
name: "background-image safe mailto URL",
inputProperty: "background-image",
expectedProperty: "background-image",
inputValue: `url("mailto:[email protected]")`,
expectedValue: `url("mailto:[email protected]")`,
},
{
name: "background-image multiple URLs",
inputProperty: "background-image",
Expand Down

0 comments on commit 5787320

Please sign in to comment.