Skip to content

Commit

Permalink
Created a comment function.
Browse files Browse the repository at this point in the history
  • Loading branch information
yosssi committed Feb 25, 2014
1 parent d347831 commit ca0cdda
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
8 changes: 7 additions & 1 deletion element.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (e *Element) parse() error {
return errors.New(fmt.Sprintf("The element has no tokens. (line no: %d)", e.LineNo))
}
switch {
case e.Type == TypeContent || e.Type == TypeBlock || e.Type == TypeExpression || e.Type == TypeLiteral:
case e.Type == TypeContent || e.Type == TypeBlock || e.Type == TypeExpression || e.Type == TypeLiteral || e.comment():
default:
for i, token := range e.Tokens {
switch {
Expand Down Expand Up @@ -190,6 +190,7 @@ func (e *Element) AppendChild(child *Element) {
// Html writes the element's html to the buffer.
func (e *Element) Html(bf *bytes.Buffer) error {
switch {
case e.comment():
case e.Type == TypeContent || e.Type == TypeExpression:
e.writeText(bf)
for _, child := range e.Children {
Expand Down Expand Up @@ -386,6 +387,11 @@ func (e *Element) writeLiteralValue(bf *bytes.Buffer) {
bf.WriteString(e.literalValue())
}

// comment returns if the string is a comment or not.
func (e *Element) comment() bool {
return strings.HasPrefix(e.Text, "//")
}

// NewElement generates a new element and returns it.
func NewElement(text string, lineNo int, indent int, parent *Element, tpl *Template, block *Block) (*Element, error) {
text = strings.TrimSpace(text)
Expand Down
14 changes: 14 additions & 0 deletions element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,20 @@ func TestElementWriteLiteralValue(t *testing.T) {
}
}

func TestElementComment(t *testing.T) {
// When the element is a comment.
e := &Element{Text: "//aaa"}
if e.comment() != true {
t.Errorf("Return value should be true.")
}

// When the element is not a comment.
e = &Element{Text: "aaa"}
if e.comment() != false {
t.Errorf("Return value should be true.")
}
}

func TestNewElement(t *testing.T) {
// When an error occurs while parsing.
_, err := NewElement("div#id1#id2", 1, 0, nil, nil, nil)
Expand Down
2 changes: 1 addition & 1 deletion generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ indentLoop:
return i
}

// empty returns if the string is empty.
// empty returns if the string is empty or not.
func empty(s string) bool {
return strings.TrimSpace(s) == ""
}
Expand Down

0 comments on commit ca0cdda

Please sign in to comment.