Merge remote-tracking branch 'rasmuserik/e4x'

Conflicts:
	js/test/beautify-tests.js
This commit is contained in:
Liam Newman 2013-04-21 18:51:17 -07:00
commit 54701f8996
3 changed files with 61 additions and 9 deletions

View File

@ -690,13 +690,18 @@
}
if (c === "'" || c === '"' || // string
(c === '/' &&
((last_type === 'TK_WORD' && is_special_word (flags.last_text)) ||
(last_type === 'TK_END_EXPR' && in_array(previous_flags.mode, [MODE.Conditional, MODE.ForInitializer])) ||
(in_array(last_type, ['TK_COMMENT', 'TK_START_EXPR', 'TK_START_BLOCK',
'TK_END_BLOCK', 'TK_OPERATOR', 'TK_EQUALS', 'TK_EOF', 'TK_SEMICOLON', 'TK_COMMA'
]))))) { // regexp
(
(c === '/') || // regexp
(options.e4x && c ==="<" && input.slice(parser_pos - 1).match(/^<[a-zA-Z:0-9]+\s*([a-zA-Z:0-9]+="[^"]*"\s*)*\/?\s*>/)) // xml
) && ( // regex and xml can only appear in specific locations during parsing
(last_type === 'TK_WORD' && is_special_word (flags.last_text)) ||
(last_type === 'TK_END_EXPR' && in_array(previous_flags.mode, [MODE.Conditional, MODE.ForInitializer])) ||
(in_array(last_type, ['TK_COMMENT', 'TK_START_EXPR', 'TK_START_BLOCK',
'TK_END_BLOCK', 'TK_OPERATOR', 'TK_EQUALS', 'TK_EOF', 'TK_SEMICOLON', 'TK_COMMA']))
)) {
var sep = c,
esc = false,
has_char_escapes = false;
@ -706,7 +711,7 @@
if (parser_pos < input_length) {
if (sep === '/') {
//
// handle regexp separately...
// handle regexp
//
var in_char_class = false;
while (esc || in_char_class || input.charAt(parser_pos) !== sep) {
@ -728,10 +733,39 @@
return [resulting_string, 'TK_STRING'];
}
}
} else if (options.e4x && sep === '<') {
//
// handle e4x xml literals
//
var xmlRegExp = /<(\/?)([a-zA-Z:0-9]+)\s*([a-zA-Z:0-9]+="[^"]*"\s*)*(\/?)\s*>/g;
var xmlStr = input.slice(parser_pos - 1);
var match = xmlRegExp.exec(xmlStr);
if (match && match.index === 0) {
var rootTag = match[2];
var depth = 0;
while (match) {
var isEndTag = !! match[1];
var tagName = match[2];
var isSingletonTag = !! match[match.length - 1];
if (tagName === rootTag && !isSingletonTag) {
if (isEndTag) {
--depth;
} else {
++depth;
}
}
if (depth <= 0) {
break;
}
match = xmlRegExp.exec(xmlStr);
}
var xmlLength = match ? match.index + match[0].length : xmlStr.length;
parser_pos += xmlLength - 1;
return [xmlStr.slice(0, xmlLength), "TK_STRING"];
}
} else {
//
// and handle string also separately
// handle string
//
while (esc || input.charAt(parser_pos) !== sep) {
resulting_string += input.charAt(parser_pos);

View File

@ -57,6 +57,7 @@ var fs = require('fs'),
"keep_array_indentation": Boolean,
"unescape_strings": Boolean,
"wrap_line_length": Number,
"e4x": Boolean,
// HTML-only
"max_char": Number,
"unformatted": [String, Array],
@ -88,6 +89,7 @@ var fs = require('fs'),
"k": ["--keep_array_indentation"],
"x": ["--unescape_strings"],
"w": ["--wrap_line_length"],
"X": ["--e4x"],
// HTML-only
"W": ["--max_char"],
"U": ["--unformatted"],
@ -190,6 +192,7 @@ function usage(err) {
msg.push(' -k, --keep-array-indentation Preserve array indentation');
msg.push(' -x, --unescape-strings Decode printable characters encoded in xNN notation');
msg.push(' -w, --wrap-line-length Wrap lines at next opportunity after N characters [0]');
msg.push(' -X, --e4x Pass E4X xml literals through untouched');
msg.push(' --good-stuff Warm the cockles of Crockford\'s heart');
break;
case "html":

View File

@ -922,6 +922,21 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify)
'function f( a, b ) {\n if ( a ) b( )\n}\nfunction g( a, b ) {\n if ( !a ) b( )\n}');
opts.space_in_paren = false;
// Test that e4x literals passed through when e4x-option is enabled
bt('xml=<a b="c"><d/><e>\n foo</e>x</a>;', 'xml = < a b = "c" > < d / > < e >\n foo < /e>x</a > ;');
opts.e4x = true;
bt('xml=<a b="c"><d/><e>\n foo</e>x</a>;', 'xml = <a b="c"><d/><e>\n foo</e>x</a>;');
// Handles messed up tags, as long as it isn't the same name
// as the root tag. Also handles tags of same name as root tag
// as long as nesting matches.
bt('xml=<a x="jn"><c></b></f><a><d jnj="jnn"><f></a ></nj></a>;',
'xml = <a x="jn"><c></b></f><a><d jnj="jnn"><f></a ></nj></a>;');
// If xml is not terminated, the remainder of the file is treated
// as part of the xml-literal (passed through unaltered)
test_fragment('xml=<a></b>\nc<b;', 'xml = <a></b>\nc<b;');
opts.e4x = false;
Urlencoded.run_tests(sanitytest);
return sanitytest;