From 91aaae0e32893c8958941ef9ae3003cda73a7b52 Mon Sep 17 00:00:00 2001 From: Sabine Date: Tue, 4 Aug 2026 18:01:02 +0200 Subject: [PATCH] feat(developer): remove convet_utils and convert_utils.tests --- common/web/types/src/main.ts | 2 - common/web/types/src/util/convert-utils.ts | 173 ------------------ .../types/tests/util/convert-utils.tests.ts | 71 ------- .../src/keylayout-to-kmn/kmn-file-writer.ts | 3 +- 4 files changed, 1 insertion(+), 248 deletions(-) delete mode 100644 common/web/types/src/util/convert-utils.ts delete mode 100644 common/web/types/tests/util/convert-utils.tests.ts diff --git a/common/web/types/src/main.ts b/common/web/types/src/main.ts index e2c3316b2a..779af42ba5 100644 --- a/common/web/types/src/main.ts +++ b/common/web/types/src/main.ts @@ -19,8 +19,6 @@ export * as KmpJsonFile from './package/kmp-json-file.js'; export { Uni_IsSurrogate1, Uni_IsSurrogate2 } from './util/util.js'; export * as util from './util/util.js'; -export * as convertUtil from './util/convert-utils.js'; - export { ObjectWithCompileContext } from './util/types.js'; export * as KeymanFileTypes from './util/file-types.js'; diff --git a/common/web/types/src/util/convert-utils.ts b/common/web/types/src/util/convert-utils.ts deleted file mode 100644 index 6e64bd55b7..0000000000 --- a/common/web/types/src/util/convert-utils.ts +++ /dev/null @@ -1,173 +0,0 @@ - -/* - * Keyman is copyright (C) SIL Global. MIT License. - * - * Created by S. Schmitt on 2026-01-19 - * - * util for conversion functions for Keyman - * - */ - -import { util } from '@keymanapp/common-types'; - -export class UnicodeCharacterConversion { - // &#x followed by 1.-6. hex digits will later be used for conversion - private static re_hex = /^&#x([0-9a-f]{1,6});$/i; - - // &# followed by 1.-6. decimal digits will later be used for conversion - private static re_dec = /^&#([0-9]{1,7});$/; - - // & followed by gt, lt, quot, amp, apos will later be used for conversion - private static re_nam = /^&(gt|lt|quot|amp|apos);$/i; - - // &# followed by anything will later be refused for conversion - private static re_html_inv = /^(&#)+(.?)+$/i; - - // one or more characters except starting & will later be used for conversion - private static re_chr = /^(?!&).+$/i; - - // '&', '&#','&#x' with or without ; will later be refused for conversion - private static re_chr_inv = /^((&;?)+|(&#;?)+|(&#x;?)+;?)$|^$/i; - - - public convert(inputString: string): string | undefined { - const m_hex = UnicodeCharacterConversion.re_hex.exec(inputString); - const m_dec = UnicodeCharacterConversion.re_dec.exec(inputString); - const m_nam = UnicodeCharacterConversion.re_nam.exec(inputString); - const m_html_inv = UnicodeCharacterConversion.re_html_inv.exec(inputString); - const m_chr = UnicodeCharacterConversion.re_chr.exec(inputString); - const m_chr_inv = UnicodeCharacterConversion.re_chr_inv.exec(inputString); - - // valid '&#x...' - if (m_hex) { - const codePoint_h = parseInt(m_hex[1], 16); - // Reject surrogates and invalid codepoints - if (!(util.isValidUnicode(codePoint_h))) { - return undefined; - } - return String.fromCodePoint(codePoint_h); - } - - // valid '&#...' - else if (m_dec) { - const codePoint_d = parseInt(m_dec[1], 10); - // Reject surrogates and invalid codepoints - if (!(util.isValidUnicode(codePoint_d))) { - return undefined; - } - return String.fromCodePoint(codePoint_d); - } - - // valid '>', '<',.. - else if (m_nam) { - switch (m_nam[1]) { - case 'gt': return '>'; - case 'lt': return '<'; - case 'quot': return '"'; - case 'amp': return '&'; - case 'apos': return "'"; - default: return undefined; - } - } - // invalid '&...' - else if (m_html_inv) { - return undefined; - } - - // single '&', '' - else if (m_chr_inv) { - return inputString; - } - - // if no matches so far, check for one or more characters ('a','ab', 'ẘ','😎', '😎😎', ) - else if (m_chr) { - return inputString; - } - - return undefined; - } -}; -/** - * @brief function to convert a character, numeric character reference or a unicode value to a character or unicode Codepoint - * if input is a valid single character or Codepoint like 'c','Γ€', 'ሴ', 'ẘ', '😎', the same character or Codepoint is returned (e.g. 'c' -> 'c', '😎' -> '😎') - * if input is a valid multi character/Codepoint string like 'ab', 'abcde', 'πŸ˜ŽπŸ˜†', the same string is returned (e.g. 'abcde' -> 'abcde' , πŸ˜ŽπŸ˜† ->πŸ˜ŽπŸ˜† ) - * if input is a valid numeric character reference in hex or decimal, the corresponding character or unicode Codepoint is returned (e.g. 😎 -> 😎) - * if input is a one of the named character reference > < & " ', the corresponding character is returned ( e.g. '>' -> '>') - * @param inputString the string or stringvalue that will converted - * @return the input character/Codepoint if input is a valid character/Codepoint - * a converted character if input is a valid Unicode value, numeric character reference in hex or decimal, or named character reference - * or undefined if input is null or undefined, half a surrogate pair, or not recognized - */ -export function convertToUnicodeCharacter(inputString: string): string | undefined { - - // null, undefined will later be refused for conversion - if (inputString == null || inputString == undefined) { - return undefined; - } - // &#x followed by 1.-6. hex digits will later be used for conversion - const m_hex = /^&#x([0-9a-f]{1,6});$/i.exec(inputString); - - // &# followed by 1.-6. decimal digits will later be used for conversion - const m_dec = /^&#([0-9]{1,7});$/.exec(inputString); - - // & followed by gt, lt, quot, amp, apos will later be used for conversion - const m_nam = /^&(gt|lt|quot|amp|apos);$/i.exec(inputString); - - // &# followed by anything will later be refused for conversion - const m_html_inv = /^(&#)+(.?)+$/i.exec(inputString); - - // one or more characters except starting & will later be used for conversion - const m_chr = /^(?!&).+$/i.exec(inputString); - - // '&', '&#','&#x' with or without ; will later be refused for conversion - const m_chr_inv = /^((&;?)+|(&#;?)+|(&#x;?)+;?)$|^$/i.exec(inputString); - - // valid '&#x...' - if (m_hex) { - const codePoint_h = parseInt(m_hex[1], 16); - - if (!(util.isValidUnicode(codePoint_h))) { - return undefined; - } - return String.fromCodePoint(codePoint_h); - } - - // valid '&#...' - else if (m_dec) { - const codePoint_d = parseInt(m_dec[1], 10); - - if (!(util.isValidUnicode(codePoint_d))) { - return undefined; - } - return String.fromCodePoint(codePoint_d); - } - - // valid '>', '<',.. - else if (m_nam) { - switch (m_nam[1]) { - case 'gt': return '>'; - case 'lt': return '<'; - case 'quot': return '"'; - case 'amp': return '&'; - case 'apos': return "'"; - default: return undefined; - } - } - - // invalid '&...' - else if (m_html_inv) { - return undefined; - } - - // single '&', '' - else if (m_chr_inv) { - return inputString; - } - - // if no matches so far, check for one or more characters ('a','ab', 'ẘ','😎', '😎😎', ) - else if (m_chr) { - return inputString; - } - - return undefined; -} diff --git a/common/web/types/tests/util/convert-utils.tests.ts b/common/web/types/tests/util/convert-utils.tests.ts deleted file mode 100644 index f6da09fdb4..0000000000 --- a/common/web/types/tests/util/convert-utils.tests.ts +++ /dev/null @@ -1,71 +0,0 @@ -import 'mocha'; -import { assert } from 'chai'; -import { convertUtil } from '@keymanapp/common-types'; - -describe('convert-utils', function () { - describe('convertToUnicodeCharacter', function () { - [ - ["a>b", 'a>b'], - ["<", '<'], - ["a", 'a'], - ["ሴ", 'ሴ'], - ["W̊", "W̊"], - ['😎', '😎'], - ["ab", 'ab'], - ["αˆ΄Π–", 'αˆ΄Π–'], - ["ẘẈ", "ẘẈ"], - ["πŸ˜ŽπŸ˜†", 'πŸ˜ŽπŸ˜†'], - ["aαˆ΄πŸ˜†", 'aαˆ΄πŸ˜†'], - ["U+0061", 'U+0061'], - ["a", 'a'], - ["ሴ", 'ሴ'], - ["ẘ", "ẘ"], - ["😏", '😏'], - ["", '\u0002'], - ["�", undefined], - ["a", 'a'], - ["ሴ", 'ሴ'], - ["ẛ", "αΊ›"], - ["😆", 'πŸ˜†'], - ["😆", 'πŸ˜†'], - ["", '\u0003'], - ["󴉀", '󴉀'], - ["@", undefined], - [">", '>'], - ["<", '<'], - [""", '"'], - ["'", "'"], - [">", undefined], - ["␀", '␀'], - ["␕", '␕'], - ["", ''], - ["", ''], - [undefined, undefined], - [null, undefined], - ["U+", "U+"], - ['&', '&'], - ['&;', '&;'], - ['&&', '&&'], - ['&&;', '&&;'], - ["&#&#", undefined], - ["&#x&#x", undefined], - ["&#", undefined], - ["&#;", undefined], - ["&#x", undefined], - ["&#x;", undefined], - ['&##', undefined], - ['&##;', undefined], - ["􏿿", undefined], - ["􏿿", undefined], - ["�", undefined], - ["�", undefined], - ['Ӓ56', undefined], - - ].forEach(function (values) { - it(('should convert "' + values[0] + '"').padEnd(30, " ") + 'to "' + values[1] + '"', async function () { - const result = convertUtil.convertToUnicodeCharacter(values[0] as string); - assert.equal(result, values[1]); - }); - }); - }); -}); diff --git a/developer/src/kmc-convert/src/keylayout-to-kmn/kmn-file-writer.ts b/developer/src/kmc-convert/src/keylayout-to-kmn/kmn-file-writer.ts index 0f0d29d996..9f63c8fca0 100644 --- a/developer/src/kmc-convert/src/keylayout-to-kmn/kmn-file-writer.ts +++ b/developer/src/kmc-convert/src/keylayout-to-kmn/kmn-file-writer.ts @@ -68,7 +68,7 @@ export class UnicodeCharacterConversion { public static re_dec = /^&#([0-9]{1,7});$/; // &#x followed by 1.-6. hex digits or &# followed by 1.-7. decimal digits - private static re_hexdec = /^&#x?([0-9a-f]{1,7};)/ig; + private static re_hexdec = /^&#x?([0-9a-f]{1,7};)/i; // & followed by gt, lt, quot, amp, apos and ; (>) private static re_nam = /&(gt|lt|quot|amp|apos);/i; @@ -155,7 +155,6 @@ export class UnicodeCharacterConversion { inputString.input = this.unescape_string(inputString.input) ?? ''; const re_hexdec = UnicodeCharacterConversion.re_hexdec.exec(inputString.input); - UnicodeCharacterConversion.re_hexdec.lastIndex = 0; // if the (remaining) input string starts with a hex or dec html entity ( &#x...; or &#...;) we need to convert this part to a character if (re_hexdec) {