ec20607f by Michael Richards

Merge pull request #218 from mikeric/fix-template-parser-and-builder

Fix template parser and builder
2 parents d29df654 1ae4a3fe
...@@ -33,6 +33,7 @@ class Rivets.TextTemplateParser ...@@ -33,6 +33,7 @@ class Rivets.TextTemplateParser
33 @parse: (template, delimiters) -> 33 @parse: (template, delimiters) ->
34 tokens = [] 34 tokens = []
35 length = template.length 35 length = template.length
36 delimiterOffset = delimiters[1].length
36 index = 0 37 index = 0
37 lastIndex = 0 38 lastIndex = 0
38 39
...@@ -62,6 +63,6 @@ class Rivets.TextTemplateParser ...@@ -62,6 +63,6 @@ class Rivets.TextTemplateParser
62 63
63 value = template.slice(lastIndex, index).trim() 64 value = template.slice(lastIndex, index).trim()
64 tokens.push type: @types.binding, value: value 65 tokens.push type: @types.binding, value: value
65 lastIndex = index + 2 66 lastIndex = index + delimiterOffset
66 67
67 tokens 68 tokens
......
...@@ -54,20 +54,13 @@ class Rivets.View ...@@ -54,20 +54,13 @@ class Rivets.View
54 if delimiters = @config.templateDelimiters 54 if delimiters = @config.templateDelimiters
55 if (tokens = parser.parse(node.data, delimiters)).length 55 if (tokens = parser.parse(node.data, delimiters)).length
56 unless tokens.length is 1 and tokens[0].type is parser.types.text 56 unless tokens.length is 1 and tokens[0].type is parser.types.text
57 [startToken, restTokens...] = tokens 57 for token in tokens
58 node.data = startToken.value
59
60 if startToken.type is 0
61 node.data = startToken.value
62 else
63 buildBinding 'TextBinding', node, null, startToken.value
64
65 for token in restTokens
66 text = document.createTextNode token.value 58 text = document.createTextNode token.value
67 node.parentNode.appendChild text 59 node.parentNode.insertBefore text, node
68 60
69 if token.type is 1 61 if token.type is 1
70 buildBinding 'TextBinding', text, null, token.value 62 buildBinding 'TextBinding', text, null, token.value
63 node.parentNode.removeChild node
71 else if componentRegExp.test node.tagName 64 else if componentRegExp.test node.tagName
72 type = node.tagName.replace(componentRegExp, '').toLowerCase() 65 type = node.tagName.replace(componentRegExp, '').toLowerCase()
73 @bindings.push new Rivets.ComponentBinding @, node, type 66 @bindings.push new Rivets.ComponentBinding @, node, type
...@@ -94,7 +87,7 @@ class Rivets.View ...@@ -94,7 +87,7 @@ class Rivets.View
94 type = attribute.name.replace bindingRegExp, '' 87 type = attribute.name.replace bindingRegExp, ''
95 buildBinding 'Binding', node, type, attribute.value 88 buildBinding 'Binding', node, type, attribute.value
96 89
97 parse childNode for childNode in node.childNodes 90 parse childNode for childNode in (n for n in node.childNodes)
98 91
99 parse el for el in @els 92 parse el for el in @els
100 93
......