Merge "Include workarounds for character literal syntax bugs"
This commit is contained in:
@@ -79,7 +79,9 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
var CPP_DIRECTIVE_WITH_LT_PATTERN = /^\s*#(if|define).*</;
|
var CPP_DIRECTIVE_WITH_LT_PATTERN = /^\s*#(if|define).*</;
|
||||||
|
var CPP_WCHAR_PATTERN = /L\'.\'/g;
|
||||||
var JAVA_PARAM_ANNOT_PATTERN = /(@[^\s]+)\(([^)]+)\)/g;
|
var JAVA_PARAM_ANNOT_PATTERN = /(@[^\s]+)\(([^)]+)\)/g;
|
||||||
|
var GO_BACKSLASH_LITERAL = '\'\\\\\'';
|
||||||
var GLOBAL_LT_PATTERN = /</g;
|
var GLOBAL_LT_PATTERN = /</g;
|
||||||
|
|
||||||
Polymer({
|
Polymer({
|
||||||
@@ -341,14 +343,28 @@
|
|||||||
* @return {string} A potentially-rewritten line of code.
|
* @return {string} A potentially-rewritten line of code.
|
||||||
*/
|
*/
|
||||||
_workaround: function(language, line) {
|
_workaround: function(language, line) {
|
||||||
|
if (language === 'cpp') {
|
||||||
/**
|
/**
|
||||||
* Prevent confusing < and << operators for the start of a meta string by
|
* Prevent confusing < and << operators for the start of a meta string
|
||||||
* converting them to a different operator.
|
* by converting them to a different operator.
|
||||||
* {@see Issue 4864}
|
* {@see Issue 4864}
|
||||||
* {@see https://github.com/isagalaev/highlight.js/issues/1341}
|
* {@see https://github.com/isagalaev/highlight.js/issues/1341}
|
||||||
*/
|
*/
|
||||||
if (language === 'cpp' && CPP_DIRECTIVE_WITH_LT_PATTERN.test(line)) {
|
if (CPP_DIRECTIVE_WITH_LT_PATTERN.test(line)) {
|
||||||
return line.replace(GLOBAL_LT_PATTERN, '|');
|
line = line.replace(GLOBAL_LT_PATTERN, '|');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rewrite CPP wchar_t characters literals to wchar_t string literals
|
||||||
|
* because HLJS only understands the string form.
|
||||||
|
* {@see Issue 5242}
|
||||||
|
* {#see https://github.com/isagalaev/highlight.js/issues/1412}
|
||||||
|
*/
|
||||||
|
if (CPP_WCHAR_PATTERN.test(line)) {
|
||||||
|
line = line.replace(CPP_WCHAR_PATTERN, 'L"."');
|
||||||
|
}
|
||||||
|
|
||||||
|
return line;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -362,6 +378,15 @@
|
|||||||
return line.replace(JAVA_PARAM_ANNOT_PATTERN, '$1 $2 ');
|
return line.replace(JAVA_PARAM_ANNOT_PATTERN, '$1 $2 ');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HLJS misunderstands backslash character literals in Go.
|
||||||
|
* {@see Issue 5007}
|
||||||
|
* {#see https://github.com/isagalaev/highlight.js/issues/1411}
|
||||||
|
*/
|
||||||
|
if (language === 'go' && line.indexOf(GO_BACKSLASH_LITERAL) !== -1) {
|
||||||
|
return line.replace(GO_BACKSLASH_LITERAL, '"\\\\"');
|
||||||
|
}
|
||||||
|
|
||||||
return line;
|
return line;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -431,7 +431,7 @@ limitations under the License.
|
|||||||
assert.equal(element._workaround('java', line), line);
|
assert.equal(element._workaround('java', line), line);
|
||||||
|
|
||||||
// Does nothing to regular annotation.
|
// Does nothing to regular annotation.
|
||||||
var line = 'public static void foo(@Nullable int bar) { }';
|
line = 'public static void foo(@Nullable int bar) { }';
|
||||||
assert.equal(element._workaround('java', line), line);
|
assert.equal(element._workaround('java', line), line);
|
||||||
|
|
||||||
// Converts parameterized annotation.
|
// Converts parameterized annotation.
|
||||||
@@ -440,5 +440,35 @@ limitations under the License.
|
|||||||
' int bar) { }';
|
' int bar) { }';
|
||||||
assert.equal(element._workaround('java', line), expected);
|
assert.equal(element._workaround('java', line), expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('workaround CPP whcar_t character literals', function() {
|
||||||
|
// Does nothing to regular line.
|
||||||
|
var line = 'int main(int argc, char** argv) { return 0; }';
|
||||||
|
assert.equal(element._workaround('cpp', line), line);
|
||||||
|
|
||||||
|
// Does nothing to wchar_t string.
|
||||||
|
line = 'wchar_t* sz = L"abc 123";';
|
||||||
|
assert.equal(element._workaround('cpp', line), line);
|
||||||
|
|
||||||
|
// Converts wchar_t character literal to string.
|
||||||
|
line = 'wchar_t myChar = L\'#\'';
|
||||||
|
var expected = 'wchar_t myChar = L"."';
|
||||||
|
assert.equal(element._workaround('cpp', line), expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('workaround go backslash character literals', function() {
|
||||||
|
// Does nothing to regular line.
|
||||||
|
var line = 'func foo(in []byte) (lit []byte, n int, err error) {';
|
||||||
|
assert.equal(element._workaround('go', line), line);
|
||||||
|
|
||||||
|
// Does nothing to string with backslash literal
|
||||||
|
line = 'c := "\\\\"';
|
||||||
|
assert.equal(element._workaround('go', line), line);
|
||||||
|
|
||||||
|
// Converts backslash literal character to a string.
|
||||||
|
line = 'c := \'\\\\\'';
|
||||||
|
var expected = 'c := "\\\\"';
|
||||||
|
assert.equal(element._workaround('go', line), expected);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user