Skip to content

Commit

Permalink
JSX/XML short attributes
Browse files Browse the repository at this point in the history
Fixes #161
Fixes #834
  • Loading branch information
bitwiseman committed Jan 27, 2016
1 parent 442cc05 commit b5bdf3a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions js/lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -1868,7 +1868,7 @@
if (c === '`' || c === "'" || c === '"' || // string
(
(c === '/') || // regexp
(opts.e4x && c === "<" && input.slice(parser_pos - 1).match(/^<([-a-zA-Z:0-9_.]+|{[^{}]*}|!\[CDATA\[[\s\S]*?\]\])(\s+[-a-zA-Z:0-9_.]+\s*=\s*('[^']*'|"[^"]*"|{.*?}))*\s*(\/?)\s*>/)) // xml
(opts.e4x && c === "<" && input.slice(parser_pos - 1).match(/^<([-a-zA-Z:0-9_.]+|{.+?}|!\[CDATA\[[\s\S]*?\]\])(\s+{.+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*('[^']*'|"[^"]*"|{.+?}))*\s*(\/?)\s*>/)) // xml
) && ( // regex and xml can only appear in specific locations during parsing
(last_token.type === 'TK_RESERVED' && in_array(last_token.text , ['return', 'case', 'throw', 'else', 'do', 'typeof', 'yield'])) ||
(last_token.type === 'TK_END_EXPR' && last_token.text === ')' &&
Expand Down Expand Up @@ -1909,7 +1909,7 @@
//
// handle e4x xml literals
//
var xmlRegExp = /<(\/?)([-a-zA-Z:0-9_.]+|{[^{}]*}|!\[CDATA\[[\s\S]*?\]\])(\s+[-a-zA-Z:0-9_.]+\s*=\s*('[^']*'|"[^"]*"|{.*?}))*\s*(\/?)\s*>/g;
var xmlRegExp = /<(\/?)([-a-zA-Z:0-9_.]+|{.+?}|!\[CDATA\[[\s\S]*?\]\])(\s+{.+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*('[^']*'|"[^"]*"|{.+?}))*\s*(\/?)\s*>/g;
var xmlStr = input.slice(parser_pos - 1);
var match = xmlRegExp.exec(xmlStr);
if (match && match.index === 0) {
Expand Down
5 changes: 4 additions & 1 deletion js/test/generated/beautify-javascript-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
// xml literals with special characters in elem names - see http://www.w3.org/TR/REC-xml/#NT-NameChar
bt('xml = <_:.valid.xml- _:.valid.xml-="123"/>;');

// xml literals with attributes without equal sign
bt('xml = <elem someAttr/>;');

// Handles CDATA
bt('xml=<![CDATA[ b="c"><d/><e v={z}>\n foo</e>x/]]>;', 'xml = <![CDATA[ b="c"><d/><e v={z}>\n foo</e>x/]]>;');
bt('xml=<![CDATA[]]>;', 'xml = <![CDATA[]]>;');
Expand Down Expand Up @@ -459,7 +462,7 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
'\n' +
'var HelloMessage = React.createClass({\n' +
' render: function() {\n' +
' return <div>Hello {this.props.name}</div>;\n' +
' return <div {someAttr}>Hello {this.props.name}</div>;\n' +
' }\n' +
'});\n' +
'React.render(<HelloMessage name="John" />, mountNode);');
Expand Down
4 changes: 2 additions & 2 deletions python/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1676,7 +1676,7 @@ def __tokenize_next(self):
if c == '`' or c == "'" or c == '"' or \
( \
(c == '/') or \
(self.opts.e4x and c == "<" and re.match('^<([-a-zA-Z:0-9_.]+|{[^{}]*}|!\[CDATA\[[\s\S]*?\]\])(\s+[-a-zA-Z:0-9_.]+\s*=\s*(\'[^\']*\'|"[^"]*"|{.*?}))*\s*(/?)\s*>', self.input[self.parser_pos - 1:])) \
(self.opts.e4x and c == "<" and re.match('^<([-a-zA-Z:0-9_.]+|{.+?}|!\[CDATA\[[\s\S]*?\]\])(\s+{.+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*(\'[^\']*\'|"[^"]*"|{.+?}))*\s*(/?)\s*>', self.input[self.parser_pos - 1:])) \
) and ( \
(last_token.type == 'TK_RESERVED' and last_token.text in ['return', 'case', 'throw', 'else', 'do', 'typeof', 'yield']) or \
(last_token.type == 'TK_END_EXPR' and last_token.text == ')' and \
Expand Down Expand Up @@ -1709,7 +1709,7 @@ def __tokenize_next(self):

elif self.opts.e4x and sep == '<':
# handle e4x xml literals
xmlRegExp = re.compile('<(\/?)([-a-zA-Z:0-9_.]+|{[^{}]*}|!\[CDATA\[[\s\S]*?\]\])(\s+[-a-zA-Z:0-9_.]+\s*=\s*(\'[^\']*\'|"[^"]*"|{.*?}))*\s*(/?)\s*>')
xmlRegExp = re.compile('<(\/?)([-a-zA-Z:0-9_.]+|{.+?}|!\[CDATA\[[\s\S]*?\]\])(\s+{.+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*(\'[^\']*\'|"[^"]*"|{.+?}))*\s*(/?)\s*>')
xmlStr = self.input[self.parser_pos - 1:]
match = xmlRegExp.match(xmlStr)
if match:
Expand Down
5 changes: 4 additions & 1 deletion python/jsbeautifier/tests/generated/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ def unicode_char(value):
# xml literals with special characters in elem names - see http://www.w3.org/TR/REC-xml/#NT-NameChar
bt('xml = <_:.valid.xml- _:.valid.xml-="123"/>;')

# xml literals with attributes without equal sign
bt('xml = <elem someAttr/>;')

# Handles CDATA
bt('xml=<![CDATA[ b="c"><d/><e v={z}>\n foo</e>x/]]>;', 'xml = <![CDATA[ b="c"><d/><e v={z}>\n foo</e>x/]]>;')
bt('xml=<![CDATA[]]>;', 'xml = <![CDATA[]]>;')
Expand Down Expand Up @@ -245,7 +248,7 @@ def unicode_char(value):
'\n' +
'var HelloMessage = React.createClass({\n' +
' render: function() {\n' +
' return <div>Hello {this.props.name}</div>;\n' +
' return <div {someAttr}>Hello {this.props.name}</div>;\n' +
' }\n' +
'});\n' +
'React.render(<HelloMessage name="John" />, mountNode);')
Expand Down
6 changes: 5 additions & 1 deletion test/data/javascript/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ exports.test_data = {
comment: 'xml literals with special characters in elem names - see http://www.w3.org/TR/REC-xml/#NT-NameChar',
unchanged: 'xml = <_:.valid.xml- _:.valid.xml-="123"/>;'
},
{
comment: 'xml literals with attributes without equal sign',
unchanged: 'xml = <elem someAttr/>;'
},

{
comment: 'Handles CDATA',
Expand Down Expand Up @@ -322,7 +326,7 @@ exports.test_data = {
'',
'var HelloMessage = React.createClass({',
' render: function() {',
' return <div>Hello {this.props.name}</div>;',
' return <div {someAttr}>Hello {this.props.name}</div>;',
' }',
'});',
'React.render(<HelloMessage name="John" />, mountNode);',
Expand Down

0 comments on commit b5bdf3a

Please sign in to comment.