From ca0cdda84831144ca167996e189af94ac7f1fd54 Mon Sep 17 00:00:00 2001 From: yosssi Date: Tue, 25 Feb 2014 21:32:15 +0900 Subject: [PATCH] Created a comment function. --- element.go | 8 +++++++- element_test.go | 14 ++++++++++++++ generator.go | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/element.go b/element.go index 63e44f4..5c6ad0c 100644 --- a/element.go +++ b/element.go @@ -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 { @@ -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 { @@ -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) diff --git a/element_test.go b/element_test.go index 1391a21..b22e168 100644 --- a/element_test.go +++ b/element_test.go @@ -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) diff --git a/generator.go b/generator.go index 124be47..14c0e42 100644 --- a/generator.go +++ b/generator.go @@ -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) == "" }