diff --git a/common/web/keyboard-processor/src/text/stringDivergence.ts b/common/web/keyboard-processor/src/text/stringDivergence.ts index 31a94d8bdb9..f45ea9f3dfa 100644 --- a/common/web/keyboard-processor/src/text/stringDivergence.ts +++ b/common/web/keyboard-processor/src/text/stringDivergence.ts @@ -1,4 +1,4 @@ -import { util } from '@keymanapp/common-types'; +import { Uni_IsSurrogate1, Uni_IsSurrogate2 } from '@keymanapp/common-types'; /** * Returns the index for the code point divergence point between two strings, as measured in code @@ -72,8 +72,8 @@ export function findCommonSubstringEndIndex(str1: string, str2: string, commonSu const divergentChar1 = str1.charCodeAt(index); const divergentChar2 = str2.charCodeAt(index + offset); - const commonSurrogateChecker = commonSuffix ? util.Uni_IsSurrogate2 : util.Uni_IsSurrogate1; - const divergentSurrogateChecker = commonSuffix ? util.Uni_IsSurrogate1 : util.Uni_IsSurrogate2; + const commonSurrogateChecker = commonSuffix ? Uni_IsSurrogate2 : Uni_IsSurrogate1; + const divergentSurrogateChecker = commonSuffix ? Uni_IsSurrogate1 : Uni_IsSurrogate2; // If the last common character if of the direction-appropriate surrogate type (for // comprising a potential split surrogate pair representing a non-BMP char)... diff --git a/common/web/keyboard-processor/tests/dom/cases/domKeyboardLoader.spec.ts b/common/web/keyboard-processor/tests/dom/cases/domKeyboardLoader.spec.ts index 560608504d9..1965adc8534 100644 --- a/common/web/keyboard-processor/tests/dom/cases/domKeyboardLoader.spec.ts +++ b/common/web/keyboard-processor/tests/dom/cases/domKeyboardLoader.spec.ts @@ -1,11 +1,13 @@ import { assert } from 'chai'; import { DOMKeyboardLoader } from '@keymanapp/keyboard-processor/dom-keyboard-loader'; -import { extendString, KeyboardHarness, Keyboard, KeyboardInterface, MinimalKeymanGlobal, Mock, DeviceSpec } from '@keymanapp/keyboard-processor'; +import { extendString, KeyboardHarness, Keyboard, KeyboardInterface, MinimalKeymanGlobal, Mock, DeviceSpec, KeyboardKeymanGlobal } from '@keymanapp/keyboard-processor'; -type WindowKey = keyof typeof window; -const keyman_window = 'keyman' as WindowKey; -const KeymanWeb = 'KeymanWeb' as WindowKey; +declare let window: typeof globalThis; +// KeymanEngine from the web/ folder... when available. +// At this level, though... we just mock it. +declare let keyman: KeyboardKeymanGlobal; +declare let KeymanWeb: KeyboardInterface; // Note: rule processing tests will fail if string extensions are not established beforehand. extendString(); @@ -19,8 +21,8 @@ const device: DeviceSpec = { describe('Keyboard loading in DOM', function() { afterEach(() => { - if (window[KeymanWeb]) { - (window[KeymanWeb] as KeyboardInterface).uninstall(); + if (KeymanWeb) { + KeymanWeb.uninstall(); } }) @@ -33,8 +35,10 @@ describe('Keyboard loading in DOM', function() { assert.equal(keyboard.id, 'Keyboard_khmer_angkor'); assert.isTrue(keyboard.isChiral); assert.isFalse(keyboard.isCJK); - assert.isOk(window[KeymanWeb]); - assert.isOk(window[keyman_window]); + assert.isOk(KeymanWeb); + assert.isOk(keyman); + assert.isOk(keyman.osk); + assert.isOk(keyman.osk.keyCodes); // Should be cleared post-keyboard-load. assert.isNotOk(harness.loadedKeyboard); @@ -50,8 +54,10 @@ describe('Keyboard loading in DOM', function() { assert.equal(keyboard.id, 'Keyboard_khmer_angkor'); assert.isTrue(keyboard.isChiral); assert.isFalse(keyboard.isCJK); - assert.isOk(window[KeymanWeb]); - assert.isOk(window[keyman_window]); + assert.isOk(KeymanWeb); + assert.isOk(keyman); + assert.isOk(keyman.osk); + assert.isOk(keyman.osk.keyCodes); // TODO: verify actual rule processing. const nullKeyEvent = keyboard.constructNullKeyEvent(device); @@ -59,8 +65,10 @@ describe('Keyboard loading in DOM', function() { const result = harness.processKeystroke(mock, nullKeyEvent); assert.isOk(result); - assert.isOk(window[KeymanWeb]); - assert.isOk(window[keyman_window]); + assert.isOk(KeymanWeb); + assert.isOk(keyman); + assert.isOk(keyman.osk); + assert.isOk(keyman.osk.keyCodes); // Should be cleared post-keyboard-load. assert.isNotOk(harness.loadedKeyboard); diff --git a/common/web/types/package.json b/common/web/types/package.json index 9327e485900..4d41b42e6e1 100644 --- a/common/web/types/package.json +++ b/common/web/types/package.json @@ -12,6 +12,10 @@ ".": { "es6-bundling": "./src/main.ts", "default": "./build/src/main.js" + }, + "./utils": { + "es6-bundling": "./src/util/index.ts", + "default": "./build/src/util/index.js" } }, "files": [ diff --git a/common/web/types/src/kmx/element-string.ts b/common/web/types/src/kmx/element-string.ts index b26878f459e..1b24c3eb450 100644 --- a/common/web/types/src/kmx/element-string.ts +++ b/common/web/types/src/kmx/element-string.ts @@ -1,7 +1,8 @@ import { constants } from '@keymanapp/ldml-keyboard-constants'; import { DependencySections, StrsItem, UsetItem } from './kmx-plus.js'; import { ElementParser, ElementSegment, ElementType } from '../ldml-keyboard/pattern-parser.js'; -import { MATCH_HEX_ESCAPE, unescapeOneQuadString } from '../util/util.js'; +import { MATCH_HEX_ESCAPE } from '../util/consts.js'; +import { unescapeOneQuadString } from '../util/util.js'; export enum ElemElementFlags { none = 0, diff --git a/common/web/types/src/ldml-keyboard/pattern-parser.ts b/common/web/types/src/ldml-keyboard/pattern-parser.ts index 8901de6dddf..3e56096ec67 100644 --- a/common/web/types/src/ldml-keyboard/pattern-parser.ts +++ b/common/web/types/src/ldml-keyboard/pattern-parser.ts @@ -3,7 +3,8 @@ */ import { constants } from "@keymanapp/ldml-keyboard-constants"; -import { MATCH_QUAD_ESCAPE, isOneChar, unescapeOneQuadString, unescapeString, hexQuad } from "../util/util.js"; +import { MATCH_QUAD_ESCAPE } from "../util/consts.js"; +import { isOneChar, unescapeOneQuadString, unescapeString, hexQuad } from "../util/util.js"; /** * Helper function for extracting matched items diff --git a/common/web/types/src/main.ts b/common/web/types/src/main.ts index 3d18812876c..92e3397c486 100644 --- a/common/web/types/src/main.ts +++ b/common/web/types/src/main.ts @@ -50,7 +50,7 @@ export { KeymanDeveloperProject, KeymanDeveloperProjectFile, KeymanDeveloperProj export * as KpsFile from './package/kps-file.js'; export * as KmpJsonFile from './package/kmp-json-file.js'; -export * as util from './util/util.js'; +export { Uni_IsSurrogate1, Uni_IsSurrogate2 } from './util/util.js'; export * as KeymanFileTypes from './util/file-types.js'; diff --git a/common/web/types/src/util/consts.ts b/common/web/types/src/util/consts.ts new file mode 100644 index 00000000000..271b3aa6bc7 --- /dev/null +++ b/common/web/types/src/util/consts.ts @@ -0,0 +1,12 @@ +// TODO-LDML: #7569 the below regex works, but captures more than it should +// (it would include \u{fffffffffffffffff } which +// is overlong and has a space at the end.) The second regex does not work yet. +export const MATCH_HEX_ESCAPE = /\\u{([0-9a-fA-F ]{1,})}/g; +// const MATCH_HEX_ESCAPE = /\\u{((?:(?:[0-9a-fA-F]{1,5})|(?:10[0-9a-fA-F]{4})(?: (?!}))?)+)}/g; + +/** regex for single quad escape such as \u0127 or \U00000000 */ +export const CONTAINS_QUAD_ESCAPE = /(?:\\u([0-9a-fA-F]{4})|\\U([0-9a-fA-F]{8}))/; + +/** regex for single quad escape such as \u0127 */ +export const MATCH_QUAD_ESCAPE = new RegExp(CONTAINS_QUAD_ESCAPE, 'g'); + diff --git a/common/web/types/src/util/index.ts b/common/web/types/src/util/index.ts new file mode 100644 index 00000000000..0833d3dd06d --- /dev/null +++ b/common/web/types/src/util/index.ts @@ -0,0 +1,2 @@ +export * from './consts.js'; +export * from './util.js'; diff --git a/common/web/types/src/util/util.ts b/common/web/types/src/util/util.ts index 7835743e62e..c5f082328a7 100644 --- a/common/web/types/src/util/util.ts +++ b/common/web/types/src/util/util.ts @@ -1,3 +1,4 @@ +import { MATCH_HEX_ESCAPE, MATCH_QUAD_ESCAPE } from './consts.js'; /** * xml2js will not place single-entry objects into arrays. Easiest way to fix * this is to box them ourselves as needed. Ensures that o.x is an array. @@ -16,18 +17,6 @@ export function boxXmlArray(o: any, x: string): void { } } -// TODO-LDML: #7569 the below regex works, but captures more than it should -// (it would include \u{fffffffffffffffff } which -// is overlong and has a space at the end.) The second regex does not work yet. -export const MATCH_HEX_ESCAPE = /\\u{([0-9a-fA-F ]{1,})}/g; -// const MATCH_HEX_ESCAPE = /\\u{((?:(?:[0-9a-fA-F]{1,5})|(?:10[0-9a-fA-F]{4})(?: (?!}))?)+)}/g; - -/** regex for single quad escape such as \u0127 or \U00000000 */ -export const CONTAINS_QUAD_ESCAPE = /(?:\\u([0-9a-fA-F]{4})|\\U([0-9a-fA-F]{8}))/; - -/** regex for single quad escape such as \u0127 */ -export const MATCH_QUAD_ESCAPE = new RegExp(CONTAINS_QUAD_ESCAPE, 'g'); - export class UnescapeError extends Error { } diff --git a/developer/src/kmc-ldml/src/compiler/empty-compiler.ts b/developer/src/kmc-ldml/src/compiler/empty-compiler.ts index 911e56d8961..7d39545ce30 100644 --- a/developer/src/kmc-ldml/src/compiler/empty-compiler.ts +++ b/developer/src/kmc-ldml/src/compiler/empty-compiler.ts @@ -1,6 +1,7 @@ import { SectionIdent, constants } from '@keymanapp/ldml-keyboard-constants'; import { SectionCompiler } from "./section-compiler.js"; -import { LDMLKeyboard, KMXPlus, CompilerCallbacks, util, MarkerParser } from "@keymanapp/common-types"; +import { LDMLKeyboard, KMXPlus, CompilerCallbacks, MarkerParser } from "@keymanapp/common-types"; +import * as util from '@keymanapp/common-types/utils'; import { VarsCompiler } from './vars.js'; import { CompilerMessages } from './messages.js'; diff --git a/developer/src/kmc-ldml/src/compiler/messages.ts b/developer/src/kmc-ldml/src/compiler/messages.ts index aefd106ae44..4e1794dae45 100644 --- a/developer/src/kmc-ldml/src/compiler/messages.ts +++ b/developer/src/kmc-ldml/src/compiler/messages.ts @@ -1,4 +1,5 @@ -import { util, CompilerErrorNamespace, CompilerErrorSeverity, CompilerMessageSpec as m, CompilerMessageDef as def } from "@keymanapp/common-types"; +import { CompilerErrorNamespace, CompilerErrorSeverity, CompilerMessageSpec as m, CompilerMessageDef as def } from "@keymanapp/common-types"; +import * as util from '@keymanapp/common-types/utils'; // const SevInfo = CompilerErrorSeverity.Info | CompilerErrorNamespace.LdmlKeyboardCompiler; const SevHint = CompilerErrorSeverity.Hint | CompilerErrorNamespace.LdmlKeyboardCompiler; const SevWarn = CompilerErrorSeverity.Warn | CompilerErrorNamespace.LdmlKeyboardCompiler; diff --git a/developer/src/kmc-ldml/src/compiler/tran.ts b/developer/src/kmc-ldml/src/compiler/tran.ts index 7ddffd47471..64bc7a7c397 100644 --- a/developer/src/kmc-ldml/src/compiler/tran.ts +++ b/developer/src/kmc-ldml/src/compiler/tran.ts @@ -1,5 +1,6 @@ import { constants, SectionIdent } from "@keymanapp/ldml-keyboard-constants"; -import { KMXPlus, LDMLKeyboard, CompilerCallbacks, VariableParser, MarkerParser, util } from '@keymanapp/common-types'; +import { KMXPlus, LDMLKeyboard, CompilerCallbacks, VariableParser, MarkerParser } from '@keymanapp/common-types'; +import * as util from '@keymanapp/common-types/utils'; import { SectionCompiler } from "./section-compiler.js"; import Bksp = KMXPlus.Bksp;