Skip to content

Commit

Permalink
feat(common): unified xml parser reader test
Browse files Browse the repository at this point in the history
Fixes: #12208
  • Loading branch information
srl295 committed Sep 27, 2024
1 parent 7e631c1 commit 609faf8
Show file tree
Hide file tree
Showing 20 changed files with 3,632 additions and 9 deletions.
2 changes: 1 addition & 1 deletion developer/src/common/web/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ export { CommonTypesMessages } from './common-messages.js';

export * as xml2js from './deps/xml2js/xml2js.js';

export { KeymanXMLParser, KeymanXMLGenerator } from './xml-utils.js';
export { KeymanXMLOptions, KeymanXMLWriter, KeymanXMLReader } from './xml-utils.js';
94 changes: 91 additions & 3 deletions developer/src/common/web/utils/src/xml-utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,99 @@
import { xml2js } from "./index.js";

export class KeymanXMLOptions {
type: 'keyboard3' // LDML <keyboard3>
| 'keyboard3-test' // LDML <keyboardTest3>
| 'kps' // <Package>
| 'kvks' // <visualkeyboard>
| 'kpj' // // <KeymanDeveloperProject>
;
}

/** wrapper for XML parsing support */
export class KeymanXMLParser {
export class KeymanXMLReader {
public constructor(public options: KeymanXMLOptions) {
}

public parse(data: string): Object {
const parser = this.parser();
let a: any;
parser.parseString(data, (e: unknown, r: unknown) => { if (e) throw e; a = r; });
return a;
}

public parser() {
const { type } = this.options;
switch (type) {
case 'keyboard3':
return new xml2js.Parser({
explicitArray: false,
mergeAttrs: true,
includeWhiteChars: false,
emptyTag: {} as any
// Why "as any"? xml2js is broken:
// https://github.com/Leonidas-from-XIV/node-xml2js/issues/648 means
// that an old version of `emptyTag` is used which doesn't support
// functions, but DefinitelyTyped is requiring use of function or a
// string. See also notes at
// https://github.com/DefinitelyTyped/DefinitelyTyped/pull/59259#issuecomment-1254405470
// An alternative fix would be to pull xml2js directly from github
// rather than using the version tagged on npmjs.com.
});
case 'keyboard3-test':
return new xml2js.Parser({
// explicitArray: false,
preserveChildrenOrder: true, // needed for test data
explicitChildren: true, // needed for test data
// mergeAttrs: true,
// includeWhiteChars: false,
// emptyTag: {} as any
// Why "as any"? xml2js is broken:
// https://github.com/Leonidas-from-XIV/node-xml2js/issues/648 means
// that an old version of `emptyTag` is used which doesn't support
// functions, but DefinitelyTyped is requiring use of function or a
// string. See also notes at
// https://github.com/DefinitelyTyped/DefinitelyTyped/pull/59259#issuecomment-1254405470
// An alternative fix would be to pull xml2js directly from github
// rather than using the version tagged on npmjs.com.
});
case 'kps':
return new xml2js.Parser({
explicitArray: false
});
case 'kpj':
return new xml2js.Parser({
explicitArray: false,
mergeAttrs: false,
includeWhiteChars: false,
normalize: false,
emptyTag: ''
});
case 'kvks':
return new xml2js.Parser({
explicitArray: false,
mergeAttrs: false,
includeWhiteChars: true,
normalize: false,
emptyTag: {} as any
// Why "as any"? xml2js is broken:
// https://github.com/Leonidas-from-XIV/node-xml2js/issues/648 means
// that an old version of `emptyTag` is used which doesn't support
// functions, but DefinitelyTyped is requiring use of function or a
// string. See also notes at
// https://github.com/DefinitelyTyped/DefinitelyTyped/pull/59259#issuecomment-1254405470
// An alternative fix would be to pull xml2js directly from github
// rather than using the version tagged on npmjs.com.
});
default:
/* c8 ignore next 1 */
throw Error(`Internal error: unhandled XML type ${type}`);
}
}
}

/** wrapper for XML generation support */
export class KeymanXMLGenerator {

export class KeymanXMLWriter {
constructor(public options: KeymanXMLOptions) {
}
}

16 changes: 16 additions & 0 deletions developer/src/common/web/utils/test/fixtures/xml/disp_maximal.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>

<keyboard3 xmlns="https://schemas.unicode.org/cldr/45/keyboard3" locale="mt" conformsTo="45">
<info name="disp-maximal"/>

<displays>
<display keyId="g" display="(g)"/>
<display output="f" display="(f)"/> <!-- Note: in opposite lexical order, as the compiler will sort -->
<display output="${eee}" display="(${eee})"/>
<displayOptions baseCharacter="x" />
</displays>

<variables>
<string id="eee" value="e" />
</variables>
</keyboard3>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"keyboard3": {
"xmlns": "https://schemas.unicode.org/cldr/45/keyboard3",
"locale": "mt",
"conformsTo": "45",
"info": {
"name": "disp-maximal"
},
"displays": {
"display": [
{
"keyId": "g",
"display": "(g)"
},
{
"output": "f",
"display": "(f)"
},
{
"output": "${eee}",
"display": "(${eee})"
}
],
"displayOptions": {
"baseCharacter": "x"
}
},
"variables": {
"string": {
"id": "eee",
"value": "e"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<Package>
<System>
<KeymanDeveloperVersion>15.0.266.0</KeymanDeveloperVersion>
<FileVersion>7.0</FileVersion>
</System>
<Info>
<Name URL="">SENĆOŦEN (Saanich Dialect) Keyboard</Name>
<!-- error_invalid_package_file -->
<Copyright URL="">© 2019 National Research Council Canada & this test</Copyright>
<Author URL="mailto:[email protected]">Eddie Antonio Santos</Author>
<Version>1.0</Version>
</Info>
<Files>
<File>
<Name>basic.kmx</Name>
<Description>Keyboard Basic</Description>
<CopyLocation>0</CopyLocation>
<FileType>.kmx</FileType>
</File>
</Files>
<Keyboards>
<Keyboard>
<Name>Basic</Name>
<ID>basic</ID>
<Version>1.0</Version>
<Languages>
<Language ID="KM">Khmer</Language>
</Languages>
</Keyboard>
</Keyboards>
</Package>
23 changes: 23 additions & 0 deletions developer/src/common/web/utils/test/fixtures/xml/k_020_fr-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Note: similar to, but slightly ahead of, fr-t-k0-azerty -->
<!DOCTYPE keyboardTest3 SYSTEM "../../../../../resources/standards-data/ldml-keyboards/45/dtd/ldmlKeyboardTest3.dtd">
<keyboardTest3 conformsTo="techpreview">
<info keyboard="k_020_fr.xml" author="Team Keyboard" name="fr-test-updated" />
<repertoire name="simple-repertoire" chars="[a b c d e \u0022]" type="simple" /> <!-- verify that these outputs are all available from simple keys on any layer, for all form factors -->
<repertoire name="chars-repertoire" chars="[á é ó]" type="gesture" /> <!-- verify that these outputs are all available from simple or gesture keys on any layer, for touch -->
<tests name="key-tests-updated">
<test name="key-test">
<startContext to="abc\u0022..."/>
<!-- tests by pressing key ids -->
<keystroke key="s"/>
<check result="abc\u0022...s" />
<keystroke key="t"/>
<check result="abc\u0022...st" />
<keystroke key="u"/>
<check result="abc\u0022...stu" />
<!-- tests by specifying 'to' output char -->
<emit to="v"/>
<check result="abc\u0022...stuv" />
</test>
</tests>
</keyboardTest3>
Loading

0 comments on commit 609faf8

Please sign in to comment.