Skip to content

Commit

Permalink
Improve attribute parsing (Fixes netzpirat#20).
Browse files Browse the repository at this point in the history
  • Loading branch information
netzpirat committed Jan 24, 2012
1 parent e9608bb commit fe8b70f
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 31 deletions.
28 changes: 10 additions & 18 deletions lib/haml-coffee.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions lib/nodes/haml.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion spec/suites/haml_coffee_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@
"id" : "999",
"color" : "red"
}
]
],
"title": "Mr"
}
},

Expand Down
5 changes: 4 additions & 1 deletion spec/suites/templates/coffee/attributes.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
- for product in @products
%div#product.article.entry{:id => product.id, :class => product.color }

%div.x{'class': "#{if true then 'y' else ''}"}
%div.x{'class': "#{if true then 'y' else ''}"}

- for title in ['Mr', 'Mrs', 'Ms', 'Miss']
%option{value: title, selected: @title == title}= title
6 changes: 5 additions & 1 deletion spec/suites/templates/coffee/attributes.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
<div class='article blue entry' id='product_12345'></div>
<div class='article entry red' id='product_999'></div>
</div>
<div class='x y'></div>
<div class='x y'></div>
<option value='Mr' selected='selected'>Mr</option>
<option value='Mrs'>Mrs</option>
<option value='Ms'>Ms</option>
<option value='Miss'>Miss</option>
2 changes: 1 addition & 1 deletion src/haml-coffee.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ module.exports = class HamlCoffee
continue if /^(\s)*$/.test(line)

# Look ahead for more attributes and add them to the current line
while /^%.*[{(]/.test(expression) and not /^(\s*)[-=&!~.%#<]/.test(lines[0]) and /(?:(\w+[\w:-]*\w?|'\w+[\w:-]*\w?'|"\w+[\w:-]*\w?")\s*=\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[\w@.]+)|(:\w+[\w:-]*\w?|'\w+[\w:-]*\w?'|"\w+[\w:-]*\w?")\s*=>\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[-\w@.()\[\]'"]+)|(\w+[\w:-]*\w?|'\w+[\w:-]*\w?'|'\w+[\w:-]*\w?'):\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[-\w@.()\[\]'"]+))/.test(lines[0])
while /^%.*[{(]/.test(expression) and not /^(\s*)[-=&!~.%#<]/.test(lines[0]) and /(?:([-\w]+[\w:-]*\w?|'[-\w]+[\w:-]*\w?'|"[-\w]+[\w:-]*\w?")\s*=\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[\w@.]+)|(:\w+[\w:-]*\w?|'[-\w]+[\w:-]*\w?'|"[-\w]+[\w:-]*\w?")\s*=>\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[^},]+)|(\w+[\w:-]*\w?|'[-\w]+[\w:-]*\w?'|'[-\w]+[\w:-]*\w?'):\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[^},]+))/.test(lines[0])

attributes = lines.shift()
expression += ' ' + attributes.match(/^(\s*)(.*)/)[2]
Expand Down
6 changes: 3 additions & 3 deletions src/nodes/haml.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,11 @@ module.exports = class Haml extends Node

findAttributes = /// (?:
# HTML attributes
(\w+[\w:-]*\w?|'\w+[\w:-]*\w?'|"\w+[\w:-]*\w?")\s*=\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[\w@.]+)
([-\w]+[\w:-]*\w?|'\w+[\w:-]*\w?'|"\w+[\w:-]*\w?")\s*=\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[\w@.]+)
# Ruby 1.8 attributes
| (:\w+[\w:-]*\w?|'\w+[\w:-]*\w?'|"\w+[\w:-]*\w?")\s*=>\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[-\w@.()\[\]'"]+)
| (:\w+[\w:-]*\w?|'[-\w]+[\w:-]*\w?'|"[-\w]+[\w:-]*\w?")\s*=>\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[^},]+)
# Ruby 1.9 attributes
| (\w+[\w:-]*\w?|'\w+[\w:-]*\w?'|"\w+[\w:-]*\w?"):\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[-\w@.()\[\]'"]+)
| (\w+[\w:-]*\w?|'[-\w]+[\w:-]*\w?'|"[-\w]+[\w:-]*\w?"):\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[^},]+)
) ///g

# Prepare all attributes
Expand Down

0 comments on commit fe8b70f

Please sign in to comment.