From 03f76d5d165bce490763af16dbef9b58aa826eed Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sat, 30 May 2020 03:30:30 +0200 Subject: [PATCH 1/2] util: support Combining Diacritical Marks for Symbols This adds support for the "Combining Diacritical Marks for Symbols" unicode group to calculate a zero length width even if Node.js is built without ICU. Signed-off-by: Ruben Bridgewater --- lib/internal/util/inspect.js | 2 ++ test/parallel/test-readline-position.js | 21 ++++++--------------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index fbc0a4ee2d6b6e..1f2d800d8c1588 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -2057,6 +2057,8 @@ if (internalBinding('config').hasIntl) { (code > 0x7F && code <= 0x9F) || // C1 control codes (code >= 0x300 && code <= 0x36F) || // Combining Diacritical Marks (code >= 0x200B && code <= 0x200F) || // Modifying Invisible Characters + // Combining Diacritical Marks for Symbols + (code >= 0x20D0 && code <= 0x20FF) || (code >= 0xFE00 && code <= 0xFE0F) || // Variation Selectors (code >= 0xFE20 && code <= 0xFE2F) || // Combining Half Marks (code >= 0xE0100 && code <= 0xE01EF); // Variation Selectors diff --git a/test/parallel/test-readline-position.js b/test/parallel/test-readline-position.js index f01786eaf19d3c..3603a42ecedc68 100644 --- a/test/parallel/test-readline-position.js +++ b/test/parallel/test-readline-position.js @@ -1,7 +1,6 @@ // Flags: --expose-internals 'use strict'; const common = require('../common'); -const { internalBinding } = require('internal/test/binding'); const { PassThrough } = require('stream'); const readline = require('readline'); const assert = require('assert'); @@ -21,22 +20,14 @@ common.skipIfDumbTerminal(); const tests = [ [1, 'a'], [2, 'ab'], - [2, '丁'] + [2, '丁'], + [0, '\u0301'], // COMBINING ACUTE ACCENT + [1, 'a\u0301'], // á + [0, '\u20DD'], // COMBINING ENCLOSING CIRCLE + [2, 'a\u20DDb'], // a⃝b + [0, '\u200E'], // LEFT-TO-RIGHT MARK ]; - // The non-ICU JS implementation of character width calculation is only aware - // of the wide/narrow distinction. Only test these more advanced cases when - // ICU is available. - if (internalBinding('config').hasIntl) { - tests.push( - [0, '\u0301'], // COMBINING ACUTE ACCENT - [1, 'a\u0301'], // á - [0, '\u20DD'], // COMBINING ENCLOSING CIRCLE - [2, 'a\u20DDb'], // a⃝b - [0, '\u200E'] // LEFT-TO-RIGHT MARK - ); - } - for (const [cursor, string] of tests) { rl.write(string); assert.strictEqual(rl.getCursorPos().cols, cursor); From afc46f7dea9b26b37da778c70a1214d8c077e3f9 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sat, 30 May 2020 05:56:48 +0200 Subject: [PATCH 2/2] util: fix width detection for DEL without ICU This makes sure the DEL character (ASCII 127) is detected as a zero width character even if Node.js is not built with ICU. Signed-off-by: Ruben Bridgewater --- lib/internal/util/inspect.js | 2 +- test/parallel/test-icu-stringwidth.js | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 1f2d800d8c1588..72be2889a09b5a 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -2054,7 +2054,7 @@ if (internalBinding('config').hasIntl) { const isZeroWidthCodePoint = (code) => { return code <= 0x1F || // C0 control codes - (code > 0x7F && code <= 0x9F) || // C1 control codes + (code >= 0x7F && code <= 0x9F) || // C1 control codes (code >= 0x300 && code <= 0x36F) || // Combining Diacritical Marks (code >= 0x200B && code <= 0x200F) || // Modifying Invisible Characters // Combining Diacritical Marks for Symbols diff --git a/test/parallel/test-icu-stringwidth.js b/test/parallel/test-icu-stringwidth.js index 4e8389961d0d6d..66142a8d6811c0 100644 --- a/test/parallel/test-icu-stringwidth.js +++ b/test/parallel/test-icu-stringwidth.js @@ -2,9 +2,6 @@ 'use strict'; const common = require('../common'); -if (!common.hasIntl) - common.skip('missing Intl'); - const assert = require('assert'); const { getStringWidth } = require('internal/util/inspect'); @@ -88,7 +85,7 @@ for (let i = 0; i < 256; i++) { } } -{ +if (common.hasIntl) { const a = '한글'.normalize('NFD'); // 한글 const b = '한글'.normalize('NFC'); // 한글 assert.strictEqual(a.length, 6);