Skip to content

Commit

Permalink
Fixed JSX Multiline root tag
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseman committed Aug 29, 2016
1 parent 1f6943c commit 7230c8c
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 7 deletions.
11 changes: 7 additions & 4 deletions js/lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2050,7 +2050,7 @@ if (!Object.values) {

}

var startXmlRegExp = /<()([-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 startXmlRegExp = /<()([-a-zA-Z:0-9_.]+|{[\s\S]+?}|!\[CDATA\[[\s\S]*?\]\])(\s+{[\s\S]+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*('[^']*'|"[^"]*"|{[\s\S]+?}))*\s*(\/?)\s*>/g;

if (c === '`' || c === "'" || c === '"' || // string
(
Expand Down Expand Up @@ -2097,18 +2097,21 @@ if (!Object.values) {
// handle e4x xml literals
//

var xmlRegExp = /[\s\S]*?<(\/?)([-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 xmlRegExp = /[\s\S]*?<(\/?)([-a-zA-Z:0-9_.]+|{[\s\S]+?}|!\[CDATA\[[\s\S]*?\]\])(\s+{[\s\S]+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*('[^']*'|"[^"]*"|{[\s\S]+?}))*\s*(\/?)\s*>/g;
input.back();
var xmlStr = '';
var match = input.match(startXmlRegExp);
if (match) {
var rootTag = match[2];
// Trim root tag to attempt to
var rootTag = match[2].replace(/^{\s+/, '{').replace(/\s+}$/, '}');
var isCurlyRoot = rootTag.indexOf('{') === 0;
var depth = 0;
while (match) {
var isEndTag = !!match[1];
var tagName = match[2];
var isSingletonTag = (!!match[match.length - 1]) || (tagName.slice(0, 8) === "![CDATA[");
if (tagName === rootTag && !isSingletonTag) {
if (!isSingletonTag &&
(tagName === rootTag || (isCurlyRoot && tagName.replace(/^{\s+/, '{').replace(/\s+}$/, '}')))) {
if (isEndTag) {
--depth;
} else {
Expand Down
49 changes: 49 additions & 0 deletions js/test/generated/beautify-javascript-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,55 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
' );\n' +
' }\n' +
'});');

// Issue #914 - Multiline attribute in root tag
bt(
'return (\n' +
' <a href="#"\n' +
' onClick={e => {\n' +
' e.preventDefault()\n' +
' onClick()\n' +
' }}>\n' +
' {children}\n' +
' </a>\n' +
');');
bt(
'return (\n' +
' <{\n' +
' a + b\n' +
' } href="#"\n' +
' onClick={e => {\n' +
' e.preventDefault()\n' +
' onClick()\n' +
' }}>\n' +
' {children}\n' +
' </{\n' +
' a + b\n' +
' }>\n' +
');');
bt(
'return (\n' +
' <{\n' +
' a + b\n' +
' } href="#"\n' +
' onClick={e => {\n' +
' e.preventDefault()\n' +
' onClick()\n' +
' }}>\n' +
' {children}\n' +
' </{a + b}>\n' +
' );',
'return (\n' +
' <{\n' +
' a + b\n' +
' } href="#"\n' +
' onClick={e => {\n' +
' e.preventDefault()\n' +
' onClick()\n' +
' }}>\n' +
' {children}\n' +
' </{a + b}>\n' +
');');


reset_options();
Expand Down
9 changes: 6 additions & 3 deletions python/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1852,7 +1852,7 @@ def __tokenize_next(self):
comment = '//' + comment_match.group(0)
return comment, 'TK_COMMENT'

startXmlRegExp = 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*>')
startXmlRegExp = re.compile('<()([-a-zA-Z:0-9_.]+|{[\s\S]+?}|!\[CDATA\[[\s\S]*?\]\])(\s+{[\s\S]+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*(\'[^\']*\'|"[^"]*"|{[\s\S]+?}))*\s*(/?)\s*>')

self.has_char_escapes = False

Expand Down Expand Up @@ -1892,18 +1892,21 @@ def __tokenize_next(self):

elif self.opts.e4x and sep == '<':
# handle e4x xml literals
xmlRegExp = re.compile('[\s\S]*?<(\/?)([-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*>')
xmlRegExp = re.compile('[\s\S]*?<(\/?)([-a-zA-Z:0-9_.]+|{[\s\S]+?}|!\[CDATA\[[\s\S]*?\]\])(\s+{[\s\S]+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*(\'[^\']*\'|"[^"]*"|{[\s\S]+?}))*\s*(/?)\s*>')
self.input.back()
xmlStr = ""
match = self.input.match(xmlRegExp)
if match:
rootTag = match.group(2)
rootTag = re.sub(r'^{\s+', '{', re.sub(r'\s+}$', '}', rootTag))
isCurlyRoot = rootTag.startswith('{')
depth = 0
while (match):
isEndTag = match.group(1)
tagName = match.group(2)
isSingletonTag = (match.groups()[-1] != "") or (match.group(2)[0:8] == "![CDATA[")
if tagName == rootTag and not isSingletonTag:
if not isSingletonTag and (
tagName == rootTag or (isCurlyRoot and re.sub(r'^{\s+', '{', re.sub(r'\s+}$', '}', tagName)))):
if isEndTag:
depth -= 1
else:
Expand Down
49 changes: 49 additions & 0 deletions python/jsbeautifier/tests/generated/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,55 @@ def unicode_char(value):
' );\n' +
' }\n' +
'});')

# Issue #914 - Multiline attribute in root tag
bt(
'return (\n' +
' <a href="#"\n' +
' onClick={e => {\n' +
' e.preventDefault()\n' +
' onClick()\n' +
' }}>\n' +
' {children}\n' +
' </a>\n' +
');')
bt(
'return (\n' +
' <{\n' +
' a + b\n' +
' } href="#"\n' +
' onClick={e => {\n' +
' e.preventDefault()\n' +
' onClick()\n' +
' }}>\n' +
' {children}\n' +
' </{\n' +
' a + b\n' +
' }>\n' +
');')
bt(
'return (\n' +
' <{\n' +
' a + b\n' +
' } href="#"\n' +
' onClick={e => {\n' +
' e.preventDefault()\n' +
' onClick()\n' +
' }}>\n' +
' {children}\n' +
' </{a + b}>\n' +
' );',
'return (\n' +
' <{\n' +
' a + b\n' +
' } href="#"\n' +
' onClick={e => {\n' +
' e.preventDefault()\n' +
' onClick()\n' +
' }}>\n' +
' {children}\n' +
' </{a + b}>\n' +
');')


self.reset_options();
Expand Down
59 changes: 59 additions & 0 deletions test/data/javascript/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,65 @@ exports.test_data = {
' }',
'});'
]
},
{
comment: 'Issue #914 - Multiline attribute in root tag',
unchanged: [
'return (',
' <a href="#"',
' onClick={e => {',
' e.preventDefault()',
' onClick()',
' }}>',
' {children}',
' </a>',
');'
]
},
{
unchanged: [
'return (',
' <{',
' a + b',
' } href="#"',
' onClick={e => {',
' e.preventDefault()',
' onClick()',
' }}>',
' {children}',
' </{',
' a + b',
' }>',
');'
]
},
{
input: [
'return (',
' <{',
' a + b',
' } href="#"',
' onClick={e => {',
' e.preventDefault()',
' onClick()',
' }}>',
' {children}',
' </{a + b}>',
' );'
],
output: [
'return (',
' <{',
' a + b',
' } href="#"',
' onClick={e => {',
' e.preventDefault()',
' onClick()',
' }}>',
' {children}',
' </{a + b}>',
');'
]
}
]
}, {
Expand Down

0 comments on commit 7230c8c

Please sign in to comment.