-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): unified xml parser reader test
Fixes: #12208
- Loading branch information
Showing
20 changed files
with
3,632 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
developer/src/common/web/utils/test/fixtures/xml/disp_maximal.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
35 changes: 35 additions & 0 deletions
35
developer/src/common/web/utils/test/fixtures/xml/disp_maximal.xml.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
developer/src/common/web/utils/test/fixtures/xml/error_invalid_package_file.kps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
developer/src/common/web/utils/test/fixtures/xml/k_020_fr-test.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.