fix: Unescape Unicode Characters accepts 4-6 hex digits for U+ prefix (#2287)

This commit is contained in:
Willi Ballenthin 2026-06-20 09:56:09 +02:00 committed by GitHub
parent 5a2eeed066
commit 9a8a279b82
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 90 additions and 1 deletions

View File

@ -56,7 +56,8 @@ class UnescapeUnicodeCharacters extends Operation {
*/
run(input, args) {
const prefix = prefixToRegex[args[0]],
regex = new RegExp(prefix+"([a-f\\d]{4})", "ig");
quantifier = args[0] === "U+" ? "{4,6}" : "{4}",
regex = new RegExp(prefix+"([a-f\\d]"+quantifier+")", "ig");
let output = "",
m,
i = 0;

View File

@ -0,0 +1,88 @@
/**
* Unescape Unicode Characters operation tests.
*
* @author williballenthin
* @copyright Crown Copyright 2024
* @license Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";
TestRegister.addTests([
{
name: "Unescape Unicode Characters: \\u 4-digit BMP",
input: "\\u03c3\\u03bf\\u03c5",
expectedOutput: "σου",
recipeConfig: [
{
op: "Unescape Unicode Characters",
args: ["\\u"],
},
],
},
{
name: "Unescape Unicode Characters: %u 4-digit BMP",
input: "%u03c3%u03bf%u03c5",
expectedOutput: "σου",
recipeConfig: [
{
op: "Unescape Unicode Characters",
args: ["%u"],
},
],
},
{
name: "Unescape Unicode Characters: U+ 4-digit BMP",
input: "U+0041",
expectedOutput: "A",
recipeConfig: [
{
op: "Unescape Unicode Characters",
args: ["U+"],
},
],
},
{
name: "Unescape Unicode Characters: U+ 5-digit astral plane emoji",
input: "U+1F600",
expectedOutput: "\u{1F600}",
recipeConfig: [
{
op: "Unescape Unicode Characters",
args: ["U+"],
},
],
},
{
name: "Unescape Unicode Characters: U+ 6-digit zero-padded",
input: "U+000041",
expectedOutput: "A",
recipeConfig: [
{
op: "Unescape Unicode Characters",
args: ["U+"],
},
],
},
{
name: "Unescape Unicode Characters: U+ mixed lengths",
input: "U+0041 U+1F600 U+000042",
expectedOutput: "A \u{1F600} B",
recipeConfig: [
{
op: "Unescape Unicode Characters",
args: ["U+"],
},
],
},
{
name: "Unescape Unicode Characters: passthrough with no matches",
input: "hello world",
expectedOutput: "hello world",
recipeConfig: [
{
op: "Unescape Unicode Characters",
args: ["\\u"],
},
],
},
]);