Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(web): improve auto-test references to globals in #11881 #11993

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions common/web/keyboard-processor/src/text/stringDivergence.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)...
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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();
}
})

Expand All @@ -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);
Expand All @@ -50,17 +54,21 @@ 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);
const mock = new Mock();
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);
Expand Down
4 changes: 4 additions & 0 deletions common/web/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
3 changes: 2 additions & 1 deletion common/web/types/src/kmx/element-string.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 2 additions & 1 deletion common/web/types/src/ldml-keyboard/pattern-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion common/web/types/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
12 changes: 12 additions & 0 deletions common/web/types/src/util/consts.ts
Original file line number Diff line number Diff line change
@@ -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');

2 changes: 2 additions & 0 deletions common/web/types/src/util/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './consts.js';
export * from './util.js';
13 changes: 1 addition & 12 deletions common/web/types/src/util/util.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 {
}

Expand Down
3 changes: 2 additions & 1 deletion developer/src/kmc-ldml/src/compiler/empty-compiler.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
3 changes: 2 additions & 1 deletion developer/src/kmc-ldml/src/compiler/messages.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 2 additions & 1 deletion developer/src/kmc-ldml/src/compiler/tran.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading