Skip to content

Commit

Permalink
Merge pull request #28 from tatsushid/feature/double-quoted-value-in-…
Browse files Browse the repository at this point in the history
…attr

Add some simple compile tests and fix to use a double quoted template value in a tag attribute
  • Loading branch information
yosssi committed Oct 6, 2014
2 parents 2880e05 + f699368 commit 01d77e6
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 43 deletions.
83 changes: 83 additions & 0 deletions compile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package ace

import (
"testing"
)

func TestLineCompile(t *testing.T) {
for i, this := range []struct {
template string
expect string
}{
{
template: `a href=./foo foo`,
expect: `<a href="./foo">foo</a>`,
},
{
template: `span.color-red red`,
expect: `<span class="color-red">red</span>`,
},
{
template: `span#ref1 text`,
expect: `<span id="ref1">text</span>`,
},
{
template: `span#ref1.color-red.text-big text`,
expect: `<span id="ref1" class="color-red text-big">text</span>`,
},
{
template: `span.color-red#ref1.text-big text`,
expect: `<span id="ref1" class="color-red text-big">text</span>`,
},
{
template: `#ref1 text`,
expect: `<div id="ref1">text</div>`,
},
{
template: `#ref1.color-red.text-big text`,
expect: `<div id="ref1" class="color-red text-big">text</div>`,
},
{
template: `.color-red#ref1.text-big text`,
expect: `<div id="ref1" class="color-red text-big">text</div>`,
},
{
template: "div class=\"dialog {{ if eq .Attr `important` }}color-red{{end}}\" text",
expect: "<div class=\"dialog {{if eq .Attr `important`}}color-red{{end}}\">text</div>",
},
{
template: "div class=\"dialog {{ if eq .Attr `important` }}color-red text-big{{end}}\" text",
expect: "<div class=\"dialog {{if eq .Attr `important`}}color-red text-big{{end}}\">text</div>",
},
{
template: "div class=\"dialog {{ if eq .Attr \"important\" }}color-red{{end}}\" text",
expect: "<div class=\"dialog {{if eq .Attr \"important\"}}color-red{{end}}\">text</div>",
},
{
template: "div class=\"dialog {{ if eq .Attr \"important\" }}color-red text-big{{end}}\" text",
expect: "<div class=\"dialog {{if eq .Attr \"important\"}}color-red text-big{{end}}\">text</div>",
},
} {
name, filepath := "dummy", "dummy.ace"
base := NewFile(filepath, []byte(this.template))
inner := NewFile("", []byte{})

src := NewSource(base, inner, []*File{})
rslt, err := ParseSource(src, nil)
if err != nil {
t.Errorf("[%d] failed: %s", i, err)
continue
}

tpl, err := CompileResult(name, rslt, nil)
if err != nil {
t.Errorf("[%d] failed: %s", i, err)
continue
}

compiled := tpl.Lookup(name).Tree.Root.String()
if compiled != this.expect {
t.Errorf("[%d] Compiler didn't return an expected value, got %v but expected %v", i, compiled, this.expect)
}
}
}
85 changes: 42 additions & 43 deletions html_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,35 +131,7 @@ func (e *htmlTag) InsertBr() bool {

// setAttributes parses the tokens and set attributes to the element.
func (e *htmlTag) setAttributes() error {
var parsedTokens []string
var unclosedTokenValues []string
var unclosed bool
var closeMark string

for i, token := range e.ln.tokens {
if i == 0 {
continue
}
if unclosed {
unclosedTokenValues = append(unclosedTokenValues, token)

if closed(token, closeMark) {
parsedTokens = append(parsedTokens, strings.Join(unclosedTokenValues, space))
unclosedTokenValues = make([]string, 0)
unclosed = false
}
} else {
if unclosed, closeMark = unclosedToken(token, e.opts); unclosed {
unclosedTokenValues = append(unclosedTokenValues, token)
} else {
parsedTokens = append(parsedTokens, token)
}
}
}

if unclosed {
parsedTokens = append(parsedTokens, unclosedTokenValues...)
}
parsedTokens := e.parseTokens()

var i int
var token string
Expand Down Expand Up @@ -297,20 +269,47 @@ func extractClasses(s string) []string {
return classes
}

// unclosedToken returns true if the token is unclosed.
func unclosedToken(s string, opts *Options) (bool, string) {
if len(strings.Split(s, doubleQuote)) == 2 {
return true, doubleQuote
}
// parseTokens parses the tokens and return them
func (e *htmlTag) parseTokens() []string {
var inQuote bool
var inDelim bool
var tokens []string
var token string

if len(strings.Split(s, opts.DelimLeft)) == 2 {
return true, opts.DelimRight
str := strings.Join(e.ln.tokens[1:], space)
for _, chr := range str {
switch c := string(chr); c {
case space:
if inQuote || inDelim {
token += c
} else {
tokens = append(tokens, token)
token = ""
}
case doubleQuote:
if !inDelim {
if inQuote {
inQuote = false
} else {
inQuote = true
}
}
token += c
default:
token += c
if inDelim {
if strings.HasSuffix(token, e.opts.DelimRight) {
inDelim = false
}
} else {
if strings.HasSuffix(token, e.opts.DelimLeft) {
inDelim = true
}
}
}
}

return false, ""
}

// closedToken returns true if the token is closed.
func closed(s string, closeMark string) bool {
return strings.HasSuffix(s, closeMark)
if len(token) > 0 {
tokens = append(tokens, token)
}
return tokens
}

0 comments on commit 01d77e6

Please sign in to comment.