diff --git a/js-modules/prettify.js b/js-modules/prettify.js index 8e8c4e31..2c46ef28 100644 --- a/js-modules/prettify.js +++ b/js-modules/prettify.js @@ -497,7 +497,18 @@ var prettyPrint; fallthroughStylePatterns.push( [PR_COMMENT, /^\/\*[\s\S]*?(?:\*\/|$)/, null]); } - if (options['regexLiterals']) { + var regexLiterals = options['regexLiterals']; + if (regexLiterals) { + /** + * @const + */ + var regexExcls = regexLiterals > 1 + ? '' // Multiline regex literals + : '\n\r'; + /** + * @const + */ + var regexAny = regexExcls ? '.' : '[\\S\\s]'; /** * @const */ @@ -505,18 +516,19 @@ var prettyPrint; // A regular expression literal starts with a slash that is // not followed by * or / so that it is not confused with // comments. - '/(?=[^/*])' + '/(?=[^/*' + regexExcls + '])' // and then contains any number of raw characters, - + '(?:[^/\\x5B\\x5C]' + + '(?:[^/\\x5B\\x5C' + regexExcls + ']' // escape sequences (\x5C), - + '|\\x5C[\\s\\S]' + + '|\\x5C' + regexAny // or non-nesting character sets (\x5B\x5D); - + '|\\x5B(?:[^\\x5C\\x5D]|\\x5C[\\s\\S])*(?:\\x5D|$))+' + + '|\\x5B(?:[^\\x5C\\x5D' + regexExcls + ']' + + '|\\x5C' + regexAny + ')*(?:\\x5D|$))+' // finally closed by a /. + '/'); fallthroughStylePatterns.push( ['lang-regex', - new RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')') + RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')') ]); } @@ -728,7 +740,7 @@ var prettyPrint; 'keywords': PERL_KEYWORDS, 'hashComments': true, 'multiLineStrings': true, - 'regexLiterals': true + 'regexLiterals': 2 // multiline regex literals }), ['perl', 'pl', 'pm']); registerLangHandler(sourceDecorator({ 'keywords': RUBY_KEYWORDS, diff --git a/src/prettify.js b/src/prettify.js index 5236b625..10c9fb73 100644 --- a/src/prettify.js +++ b/src/prettify.js @@ -852,7 +852,18 @@ var prettyPrint; fallthroughStylePatterns.push( [PR_COMMENT, /^\/\*[\s\S]*?(?:\*\/|$)/, null]); } - if (options['regexLiterals']) { + var regexLiterals = options['regexLiterals']; + if (regexLiterals) { + /** + * @const + */ + var regexExcls = regexLiterals > 1 + ? '' // Multiline regex literals + : '\n\r'; + /** + * @const + */ + var regexAny = regexExcls ? '.' : '[\\S\\s]'; /** * @const */ @@ -860,18 +871,19 @@ var prettyPrint; // A regular expression literal starts with a slash that is // not followed by * or / so that it is not confused with // comments. - '/(?=[^/*])' + '/(?=[^/*' + regexExcls + '])' // and then contains any number of raw characters, - + '(?:[^/\\x5B\\x5C]' + + '(?:[^/\\x5B\\x5C' + regexExcls + ']' // escape sequences (\x5C), - + '|\\x5C[\\s\\S]' + + '|\\x5C' + regexAny // or non-nesting character sets (\x5B\x5D); - + '|\\x5B(?:[^\\x5C\\x5D]|\\x5C[\\s\\S])*(?:\\x5D|$))+' + + '|\\x5B(?:[^\\x5C\\x5D' + regexExcls + ']' + + '|\\x5C' + regexAny + ')*(?:\\x5D|$))+' // finally closed by a /. + '/'); fallthroughStylePatterns.push( ['lang-regex', - new RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')') + RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')') ]); } @@ -1336,7 +1348,7 @@ var prettyPrint; 'keywords': PERL_KEYWORDS, 'hashComments': true, 'multiLineStrings': true, - 'regexLiterals': true + 'regexLiterals': 2 // multiline regex literals }), ['perl', 'pl', 'pm']); registerLangHandler(sourceDecorator({ 'keywords': RUBY_KEYWORDS, diff --git a/src/run_prettify.js b/src/run_prettify.js index c2fab699..dd136b92 100644 --- a/src/run_prettify.js +++ b/src/run_prettify.js @@ -1074,7 +1074,18 @@ var IN_GLOBAL_SCOPE = false; fallthroughStylePatterns.push( [PR_COMMENT, /^\/\*[\s\S]*?(?:\*\/|$)/, null]); } - if (options['regexLiterals']) { + var regexLiterals = options['regexLiterals']; + if (regexLiterals) { + /** + * @const + */ + var regexExcls = regexLiterals > 1 + ? '' // Multiline regex literals + : '\n\r'; + /** + * @const + */ + var regexAny = regexExcls ? '.' : '[\\S\\s]'; /** * @const */ @@ -1082,18 +1093,19 @@ var IN_GLOBAL_SCOPE = false; // A regular expression literal starts with a slash that is // not followed by * or / so that it is not confused with // comments. - '/(?=[^/*])' + '/(?=[^/*' + regexExcls + '])' // and then contains any number of raw characters, - + '(?:[^/\\x5B\\x5C]' + + '(?:[^/\\x5B\\x5C' + regexExcls + ']' // escape sequences (\x5C), - + '|\\x5C[\\s\\S]' + + '|\\x5C' + regexAny // or non-nesting character sets (\x5B\x5D); - + '|\\x5B(?:[^\\x5C\\x5D]|\\x5C[\\s\\S])*(?:\\x5D|$))+' + + '|\\x5B(?:[^\\x5C\\x5D' + regexExcls + ']' + + '|\\x5C' + regexAny + ')*(?:\\x5D|$))+' // finally closed by a /. + '/'); fallthroughStylePatterns.push( ['lang-regex', - new RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')') + RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')') ]); } @@ -1558,7 +1570,7 @@ var IN_GLOBAL_SCOPE = false; 'keywords': PERL_KEYWORDS, 'hashComments': true, 'multiLineStrings': true, - 'regexLiterals': true + 'regexLiterals': 2 // multiline regex literals }), ['perl', 'pl', 'pm']); registerLangHandler(sourceDecorator({ 'keywords': RUBY_KEYWORDS,