@loaders.gl/xml 3.4.0-alpha.1 → 3.4.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/dist/dist.min.js +419 -196
  2. package/dist/es5/html-loader.js +57 -0
  3. package/dist/es5/html-loader.js.map +1 -0
  4. package/dist/es5/index.js +33 -0
  5. package/dist/es5/index.js.map +1 -1
  6. package/dist/es5/lib/{parse-xml.js → parsers/parse-xml.js} +7 -7
  7. package/dist/es5/lib/parsers/parse-xml.js.map +1 -0
  8. package/dist/es5/lib/{parser/xml-parser.js → parsers/streaming-xml-parser.js} +10 -15
  9. package/dist/es5/lib/parsers/streaming-xml-parser.js.map +1 -0
  10. package/dist/es5/lib/xml-utils/uncapitalize.js +32 -0
  11. package/dist/es5/lib/xml-utils/uncapitalize.js.map +1 -0
  12. package/dist/es5/lib/xml-utils/xml-utils.js +23 -0
  13. package/dist/es5/lib/xml-utils/xml-utils.js.map +1 -0
  14. package/dist/es5/sax-ts/sax.js +1 -18
  15. package/dist/es5/sax-ts/sax.js.map +1 -1
  16. package/dist/es5/xml-loader.js +44 -12
  17. package/dist/es5/xml-loader.js.map +1 -1
  18. package/dist/esm/bundle.js +0 -1
  19. package/dist/esm/bundle.js.map +1 -1
  20. package/dist/esm/html-loader.js +27 -0
  21. package/dist/esm/html-loader.js.map +1 -0
  22. package/dist/esm/index.js +3 -3
  23. package/dist/esm/index.js.map +1 -1
  24. package/dist/esm/lib/parsers/parse-xml.js +22 -0
  25. package/dist/esm/lib/parsers/parse-xml.js.map +1 -0
  26. package/dist/esm/lib/{parser/xml-parser.js → parsers/streaming-xml-parser.js} +3 -8
  27. package/dist/esm/lib/parsers/streaming-xml-parser.js.map +1 -0
  28. package/dist/esm/lib/xml-utils/uncapitalize.js +17 -0
  29. package/dist/esm/lib/xml-utils/uncapitalize.js.map +1 -0
  30. package/dist/esm/lib/xml-utils/xml-utils.js +14 -0
  31. package/dist/esm/lib/xml-utils/xml-utils.js.map +1 -0
  32. package/dist/esm/sax-ts/sax.js +1 -19
  33. package/dist/esm/sax-ts/sax.js.map +1 -1
  34. package/dist/esm/xml-loader.js +38 -6
  35. package/dist/esm/xml-loader.js.map +1 -1
  36. package/dist/html-loader.d.ts +11 -0
  37. package/dist/html-loader.d.ts.map +1 -0
  38. package/dist/html-loader.js +40 -0
  39. package/dist/index.d.ts +4 -0
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +11 -1
  42. package/dist/lib/parsers/parse-xml.d.ts +12 -0
  43. package/dist/lib/parsers/parse-xml.d.ts.map +1 -0
  44. package/dist/lib/{parse-xml.js → parsers/parse-xml.js} +7 -8
  45. package/dist/lib/{parser/xml-parser.d.ts → parsers/streaming-xml-parser.d.ts} +6 -2
  46. package/dist/lib/parsers/streaming-xml-parser.d.ts.map +1 -0
  47. package/dist/lib/{parser/xml-parser.js → parsers/streaming-xml-parser.js} +6 -6
  48. package/dist/lib/xml-utils/uncapitalize.d.ts +13 -0
  49. package/dist/lib/xml-utils/uncapitalize.d.ts.map +1 -0
  50. package/dist/lib/xml-utils/uncapitalize.js +32 -0
  51. package/dist/lib/xml-utils/xml-utils.d.ts +11 -0
  52. package/dist/lib/xml-utils/xml-utils.d.ts.map +1 -0
  53. package/dist/lib/xml-utils/xml-utils.js +30 -0
  54. package/dist/sax-ts/sax.js +1 -1
  55. package/dist/xml-loader.d.ts +21 -3
  56. package/dist/xml-loader.d.ts.map +1 -1
  57. package/dist/xml-loader.js +41 -5
  58. package/package.json +5 -5
  59. package/src/html-loader.ts +46 -0
  60. package/src/index.ts +16 -0
  61. package/src/lib/{parse-xml.ts → parsers/parse-xml.ts} +9 -6
  62. package/src/lib/{parser/xml-parser.ts → parsers/streaming-xml-parser.ts} +5 -3
  63. package/src/lib/xml-utils/uncapitalize.ts +31 -0
  64. package/src/lib/xml-utils/xml-utils.ts +27 -0
  65. package/src/xml-loader.ts +63 -6
  66. package/dist/es5/lib/parse-xml.js.map +0 -1
  67. package/dist/es5/lib/parser/xml-parser.js.map +0 -1
  68. package/dist/esm/lib/parse-xml.js +0 -24
  69. package/dist/esm/lib/parse-xml.js.map +0 -1
  70. package/dist/esm/lib/parser/xml-parser.js.map +0 -1
  71. package/dist/lib/parse-xml.d.ts +0 -9
  72. package/dist/lib/parse-xml.d.ts.map +0 -1
  73. package/dist/lib/parser/xml-parser.d.ts.map +0 -1
package/dist/index.d.ts CHANGED
@@ -1,5 +1,9 @@
1
1
  export type { XMLLoaderOptions } from './xml-loader';
2
2
  export { XMLLoader } from './xml-loader';
3
+ export type { HTMLLoaderOptions } from './html-loader';
4
+ export { HTMLLoader } from './html-loader';
3
5
  export type { SAXParserOptions as SAXParserOptions } from './sax-ts/sax';
4
6
  export { SAXParser as SAXParser } from './sax-ts/sax';
7
+ export { convertXMLValueToArray, convertXMLFieldToArrayInPlace } from './lib/xml-utils/xml-utils';
8
+ export { uncapitalize as _uncapitalize, uncapitalizeKeys as _uncapitalizeKeys } from './lib/xml-utils/uncapitalize';
5
9
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,YAAY,EAAC,gBAAgB,EAAC,MAAM,cAAc,CAAC;AACnD,OAAO,EAAC,SAAS,EAAC,MAAM,cAAc,CAAC;AAIvC,YAAY,EAAC,gBAAgB,IAAI,gBAAgB,EAAC,MAAM,cAAc,CAAC;AACvE,OAAO,EAAC,SAAS,IAAI,SAAS,EAAC,MAAM,cAAc,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,YAAY,EAAC,gBAAgB,EAAC,MAAM,cAAc,CAAC;AACnD,OAAO,EAAC,SAAS,EAAC,MAAM,cAAc,CAAC;AAIvC,YAAY,EAAC,iBAAiB,EAAC,MAAM,eAAe,CAAC;AACrD,OAAO,EAAC,UAAU,EAAC,MAAM,eAAe,CAAC;AAIzC,YAAY,EAAC,gBAAgB,IAAI,gBAAgB,EAAC,MAAM,cAAc,CAAC;AACvE,OAAO,EAAC,SAAS,IAAI,SAAS,EAAC,MAAM,cAAc,CAAC;AAIpD,OAAO,EAAC,sBAAsB,EAAE,6BAA6B,EAAC,MAAM,2BAA2B,CAAC;AAIhG,OAAO,EACL,YAAY,IAAI,aAAa,EAC7B,gBAAgB,IAAI,iBAAiB,EACtC,MAAM,8BAA8B,CAAC"}
package/dist/index.js CHANGED
@@ -1,8 +1,18 @@
1
1
  "use strict";
2
2
  // loaders.gl, MIT license
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.SAXParser = exports.XMLLoader = void 0;
4
+ exports._uncapitalizeKeys = exports._uncapitalize = exports.convertXMLFieldToArrayInPlace = exports.convertXMLValueToArray = exports.SAXParser = exports.HTMLLoader = exports.XMLLoader = void 0;
5
5
  var xml_loader_1 = require("./xml-loader");
6
6
  Object.defineProperty(exports, "XMLLoader", { enumerable: true, get: function () { return xml_loader_1.XMLLoader; } });
7
+ var html_loader_1 = require("./html-loader");
8
+ Object.defineProperty(exports, "HTMLLoader", { enumerable: true, get: function () { return html_loader_1.HTMLLoader; } });
7
9
  var sax_1 = require("./sax-ts/sax");
8
10
  Object.defineProperty(exports, "SAXParser", { enumerable: true, get: function () { return sax_1.SAXParser; } });
11
+ // Utilities
12
+ var xml_utils_1 = require("./lib/xml-utils/xml-utils");
13
+ Object.defineProperty(exports, "convertXMLValueToArray", { enumerable: true, get: function () { return xml_utils_1.convertXMLValueToArray; } });
14
+ Object.defineProperty(exports, "convertXMLFieldToArrayInPlace", { enumerable: true, get: function () { return xml_utils_1.convertXMLFieldToArrayInPlace; } });
15
+ // Experimental
16
+ var uncapitalize_1 = require("./lib/xml-utils/uncapitalize");
17
+ Object.defineProperty(exports, "_uncapitalize", { enumerable: true, get: function () { return uncapitalize_1.uncapitalize; } });
18
+ Object.defineProperty(exports, "_uncapitalizeKeys", { enumerable: true, get: function () { return uncapitalize_1.uncapitalizeKeys; } });
@@ -0,0 +1,12 @@
1
+ import type { X2jOptions } from 'fast-xml-parser';
2
+ /** Type for passing through fast-xml-parser options */
3
+ export type FastXMLParserOptions = Partial<X2jOptions>;
4
+ export declare function fastParseXML(text: string, options: FastXMLParserOptions): any;
5
+ /**
6
+ * @todo Build a streaming XML parser based on sax-js
7
+ * @param text
8
+ * @param options
9
+ * @returns
10
+ */
11
+ export declare function parseXMLInBatches(text: string, options?: {}): any;
12
+ //# sourceMappingURL=parse-xml.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-xml.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/parse-xml.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,iBAAiB,CAAC;AAEhD,uDAAuD;AACvD,MAAM,MAAM,oBAAoB,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;AAEvD,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,oBAAoB,GAAG,GAAG,CAU7E;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,KAAK,GAAG,GAAG,CAUjE"}
@@ -1,28 +1,27 @@
1
1
  "use strict";
2
2
  // loaders.gl, MIT license
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.parseXMLInBatches = exports.parseXML = void 0;
5
- const xml_parser_1 = require("../lib/parser/xml-parser");
4
+ exports.parseXMLInBatches = exports.fastParseXML = void 0;
5
+ const streaming_xml_parser_1 = require("./streaming-xml-parser");
6
6
  const fast_xml_parser_1 = require("fast-xml-parser");
7
- function parseXML(text, options) {
7
+ function fastParseXML(text, options) {
8
8
  const parser = new fast_xml_parser_1.XMLParser({
9
9
  ignoreAttributes: false,
10
10
  attributeNamePrefix: '',
11
- // parseAttributeValue: true,
12
- ...options?.xml
11
+ ...options
13
12
  });
14
13
  const parsedXML = parser.parse(text);
15
14
  return parsedXML;
16
15
  }
17
- exports.parseXML = parseXML;
16
+ exports.fastParseXML = fastParseXML;
18
17
  /**
19
18
  * @todo Build a streaming XML parser based on sax-js
20
19
  * @param text
21
20
  * @param options
22
21
  * @returns
23
22
  */
24
- function parseXMLInBatches(text, options) {
25
- const parser = new xml_parser_1.XMLParser({
23
+ function parseXMLInBatches(text, options = {}) {
24
+ const parser = new streaming_xml_parser_1.StreamingXMLParser({
26
25
  ...options,
27
26
  strict: true
28
27
  });
@@ -1,5 +1,9 @@
1
1
  import { SAXParser, SAXParserOptions } from '../../sax-ts/sax';
2
- export declare class XMLParser {
2
+ export type StreamingXMLParserOptions = SAXParserOptions;
3
+ /**
4
+ * StreamingXMLParser builds a JSON object using the events emitted by the SAX parser
5
+ */
6
+ export declare class StreamingXMLParser {
3
7
  readonly parser: SAXParser;
4
8
  result: undefined;
5
9
  previousStates: never[];
@@ -17,4 +21,4 @@ export declare class XMLParser {
17
21
  _openObject(newContainer?: {}): void;
18
22
  _closeObject(): void;
19
23
  }
20
- //# sourceMappingURL=xml-parser.d.ts.map
24
+ //# sourceMappingURL=streaming-xml-parser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"streaming-xml-parser.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/streaming-xml-parser.ts"],"names":[],"mappings":"AAGA,OAAO,EAAC,SAAS,EAAE,gBAAgB,EAAC,MAAM,kBAAkB,CAAC;AAG7D,MAAM,MAAM,yBAAyB,GAAG,gBAAgB,CAAC;AAEzD;;GAEG;AACH,qBAAa,kBAAkB;IAC7B,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC;IAC3B,MAAM,YAAa;IACnB,cAAc,UAAM;IACpB,YAAY;;;OAA6C;gBAG7C,OAAO,EAAE,gBAAgB;IA+CrC,KAAK,IAAI,IAAI;IAMb,KAAK,CAAC,KAAK,KAAA,GAAG,IAAI;IAIlB,KAAK,IAAI,IAAI;IAMb,UAAU,CAAC,KAAK,KAAA,GAAG,IAAI;IAYvB,UAAU,CAAC,YAAY,UAAK,GAAG,IAAI;IAOnC,WAAW,IAAI,IAAI;IAKnB,WAAW,CAAC,YAAY,KAAK,GAAG,IAAI;IAOpC,YAAY,IAAI,IAAI;CAIrB"}
@@ -2,12 +2,12 @@
2
2
  // @ts-nocheck
3
3
  /* eslint-disable */
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.XMLParser = void 0;
6
- // import ClarinetParser, {ClarinetParserOptions} from '../clarinet/clarinet';
5
+ exports.StreamingXMLParser = void 0;
7
6
  const sax_1 = require("../../sax-ts/sax");
8
- // import JSONPath from '../jsonpath/jsonpath';
9
- // XMLParser builds a JSON object using the events emitted by the Clarinet parser
10
- class XMLParser {
7
+ /**
8
+ * StreamingXMLParser builds a JSON object using the events emitted by the SAX parser
9
+ */
10
+ class StreamingXMLParser {
11
11
  // jsonpath: JSONPath = new JSONPath();
12
12
  constructor(options) {
13
13
  this.result = undefined;
@@ -95,4 +95,4 @@ class XMLParser {
95
95
  this.currentState = this.previousStates.pop();
96
96
  }
97
97
  }
98
- exports.XMLParser = XMLParser;
98
+ exports.StreamingXMLParser = StreamingXMLParser;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Uncapitalize first letter of a string
3
+ * @param str
4
+ * @returns
5
+ */
6
+ export declare function uncapitalize(str: string): string;
7
+ /**
8
+ * Recursively uncapitalize all keys in a nested object
9
+ * @param object
10
+ * @returns
11
+ */
12
+ export declare function uncapitalizeKeys(object: any): any;
13
+ //# sourceMappingURL=uncapitalize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uncapitalize.d.ts","sourceRoot":"","sources":["../../../src/lib/xml-utils/uncapitalize.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,GAAG,GAAG,GAAG,CAcjD"}
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ // loaders.gl, MIT license
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.uncapitalizeKeys = exports.uncapitalize = void 0;
5
+ /**
6
+ * Uncapitalize first letter of a string
7
+ * @param str
8
+ * @returns
9
+ */
10
+ function uncapitalize(str) {
11
+ return typeof str === 'string' ? str.charAt(0).toLowerCase() + str.slice(1) : str;
12
+ }
13
+ exports.uncapitalize = uncapitalize;
14
+ /**
15
+ * Recursively uncapitalize all keys in a nested object
16
+ * @param object
17
+ * @returns
18
+ */
19
+ function uncapitalizeKeys(object) {
20
+ if (Array.isArray(object)) {
21
+ return object.map((element) => uncapitalizeKeys(element));
22
+ }
23
+ if (object && typeof object === 'object') {
24
+ const newObject = {};
25
+ for (const [key, value] of Object.entries(object)) {
26
+ newObject[uncapitalize(key)] = uncapitalizeKeys(value);
27
+ }
28
+ return newObject;
29
+ }
30
+ return object;
31
+ }
32
+ exports.uncapitalizeKeys = uncapitalizeKeys;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Extracts a value or array and always return an array
3
+ * Useful since XML parses to object instead of array when only a single value is provided
4
+ */
5
+ export declare function convertXMLValueToArray(xmlValue: unknown): unknown[];
6
+ /**
7
+ * Mutates a field in place, converting it to array
8
+ * Useful since XML parses to object instead of array when only a single value is provided
9
+ */
10
+ export declare function convertXMLFieldToArrayInPlace(xml: any, key: string): void;
11
+ //# sourceMappingURL=xml-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xml-utils.d.ts","sourceRoot":"","sources":["../../../src/lib/xml-utils/xml-utils.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,OAAO,GAAG,OAAO,EAAE,CAWnE;AAED;;;GAGG;AACH,wBAAgB,6BAA6B,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,CAEzE"}
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ // TODO - these utilities could be moved to the XML parser.
3
+ // uncapitalizeKeys could be an XMLLoader option
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.convertXMLFieldToArrayInPlace = exports.convertXMLValueToArray = void 0;
6
+ /**
7
+ * Extracts a value or array and always return an array
8
+ * Useful since XML parses to object instead of array when only a single value is provided
9
+ */
10
+ function convertXMLValueToArray(xmlValue) {
11
+ if (Array.isArray(xmlValue)) {
12
+ return xmlValue;
13
+ }
14
+ if (xmlValue && typeof xmlValue === 'object' && xmlValue['0']) {
15
+ // Error this is an objectified array
16
+ }
17
+ if (xmlValue) {
18
+ return [xmlValue];
19
+ }
20
+ return [];
21
+ }
22
+ exports.convertXMLValueToArray = convertXMLValueToArray;
23
+ /**
24
+ * Mutates a field in place, converting it to array
25
+ * Useful since XML parses to object instead of array when only a single value is provided
26
+ */
27
+ function convertXMLFieldToArrayInPlace(xml, key) {
28
+ xml[key] = convertXMLValueToArray(xml[key]);
29
+ }
30
+ exports.convertXMLFieldToArrayInPlace = convertXMLFieldToArrayInPlace;
@@ -1436,5 +1436,5 @@ class SAXParser extends SAX {
1436
1436
  this.flushBuffers();
1437
1437
  }
1438
1438
  }
1439
- exports.SAXParser = SAXParser;
1440
1439
  SAXParser.ENTITIES = exports.ENTITIES;
1440
+ exports.SAXParser = SAXParser;
@@ -1,10 +1,22 @@
1
1
  import type { LoaderWithParser, LoaderOptions } from '@loaders.gl/loader-utils';
2
2
  import type { SAXParserOptions } from './sax-ts/sax';
3
+ import { FastXMLParserOptions } from './lib/parsers/parse-xml';
3
4
  export type XMLLoaderOptions = LoaderOptions & {
4
- xml?: SAXParserOptions;
5
+ xml?: {
6
+ parser?: 'fast-xml-parser' | string;
7
+ /** XML is typically PascalCase, JavaScript prefects camelCase */
8
+ uncapitalizeKeys?: boolean;
9
+ removeNSPrefix?: boolean;
10
+ textNodeName?: string;
11
+ arrayPaths?: string[];
12
+ };
13
+ /** @deprecated Experimental, passes options to fast-xml-parser, IF it is being used */
14
+ _fastXML?: FastXMLParserOptions;
15
+ /** @deprecated Experimental, passes options to the SAX XML parser, IF it is being used. */
16
+ _sax?: SAXParserOptions;
5
17
  };
6
18
  /**
7
- * Worker loader for the OBJ geometry format
19
+ * Loader for XML files
8
20
  */
9
21
  export declare const XMLLoader: {
10
22
  name: string;
@@ -16,7 +28,13 @@ export declare const XMLLoader: {
16
28
  mimeTypes: string[];
17
29
  testText: typeof testXMLFile;
18
30
  options: {
19
- xml: {};
31
+ xml: {
32
+ parser: string;
33
+ uncapitalizeKeys: boolean;
34
+ removeNSPrefix: boolean;
35
+ textNodeName: string;
36
+ arrayPaths: never[];
37
+ };
20
38
  };
21
39
  parse: (arrayBuffer: ArrayBuffer, options?: XMLLoaderOptions) => Promise<any>;
22
40
  parseTextSync: (text: string, options?: XMLLoaderOptions) => any;
@@ -1 +1 @@
1
- {"version":3,"file":"xml-loader.d.ts","sourceRoot":"","sources":["../src/xml-loader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,gBAAgB,EAAE,aAAa,EAAC,MAAM,0BAA0B,CAAC;AAC9E,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,cAAc,CAAC;AAOnD,MAAM,MAAM,gBAAgB,GAAG,aAAa,GAAG;IAC7C,GAAG,CAAC,EAAE,gBAAgB,CAAC;CACxB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,SAAS;;;;;;;;;;;;yBAYO,WAAW,YAAY,gBAAgB;0BAE5C,MAAM,YAAY,gBAAgB;CACzD,CAAC;AAEF,iBAAS,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAG1C;AAED,eAAO,MAAM,mBAAmB,EAAE,gBAA4B,CAAC"}
1
+ {"version":3,"file":"xml-loader.d.ts","sourceRoot":"","sources":["../src/xml-loader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,gBAAgB,EAAE,aAAa,EAAC,MAAM,0BAA0B,CAAC;AAC9E,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,cAAc,CAAC;AACnD,OAAO,EAAe,oBAAoB,EAAC,MAAM,yBAAyB,CAAC;AAO3E,MAAM,MAAM,gBAAgB,GAAG,aAAa,GAAG;IAC7C,GAAG,CAAC,EAAE;QAEJ,MAAM,CAAC,EAAE,iBAAiB,GAAG,MAAM,CAAC;QACpC,iEAAiE;QACjE,gBAAgB,CAAC,EAAE,OAAO,CAAC;QAC3B,cAAc,CAAC,EAAE,OAAO,CAAC;QACzB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;KACvB,CAAC;IACF,uFAAuF;IACvF,QAAQ,CAAC,EAAE,oBAAoB,CAAC;IAChC,2FAA2F;IAC3F,IAAI,CAAC,EAAE,gBAAgB,CAAC;CACzB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,SAAS;;;;;;;;;;;;;;;;;;yBAkBO,WAAW,YAAY,gBAAgB;0BAE5C,MAAM,YAAY,gBAAgB;CACzD,CAAC;AAEF,iBAAS,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAG1C;AAwCD,eAAO,MAAM,mBAAmB,EAAE,gBAA4B,CAAC"}
@@ -1,12 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports._typecheckXMLLoader = exports.XMLLoader = void 0;
4
- const parse_xml_1 = require("./lib/parse-xml");
4
+ const parse_xml_1 = require("./lib/parsers/parse-xml");
5
+ const uncapitalize_1 = require("./lib/xml-utils/uncapitalize");
5
6
  // __VERSION__ is injected by babel-plugin-version-inline
6
7
  // @ts-ignore TS2304: Cannot find name '__VERSION__'.
7
8
  const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
8
9
  /**
9
- * Worker loader for the OBJ geometry format
10
+ * Loader for XML files
10
11
  */
11
12
  exports.XMLLoader = {
12
13
  name: 'XML',
@@ -18,13 +19,48 @@ exports.XMLLoader = {
18
19
  mimeTypes: ['application/xml', 'text/xml'],
19
20
  testText: testXMLFile,
20
21
  options: {
21
- xml: {}
22
+ xml: {
23
+ parser: 'fast-xml-parser',
24
+ uncapitalizeKeys: false,
25
+ removeNSPrefix: false,
26
+ textNodeName: 'value',
27
+ arrayPaths: []
28
+ }
22
29
  },
23
- parse: async (arrayBuffer, options) => (0, parse_xml_1.parseXML)(new TextDecoder().decode(arrayBuffer), options),
24
- parseTextSync: (text, options) => (0, parse_xml_1.parseXML)(text, options)
30
+ parse: async (arrayBuffer, options) => parseTextSync(new TextDecoder().decode(arrayBuffer), options),
31
+ parseTextSync: (text, options) => parseTextSync(text, options)
25
32
  };
26
33
  function testXMLFile(text) {
27
34
  // TODO - There could be space first.
28
35
  return text.startsWith('<?xml');
29
36
  }
37
+ function parseTextSync(text, options) {
38
+ const xmlOptions = { ...exports.XMLLoader.options.xml, ...options?.xml };
39
+ switch (xmlOptions.parser) {
40
+ case 'fast-xml-parser':
41
+ const fastXMLOptions = {
42
+ // Default FastXML options
43
+ // https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/docs/v4/2.XMLparseOptions.md#allowbooleanattributes
44
+ allowBooleanAttributes: true,
45
+ // https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/docs/v4/2.XMLparseOptions.md#ignoredeclaration
46
+ ignoreDeclaration: true,
47
+ // XMLLoader Options: Map to FastXMLOptions
48
+ // https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/docs/v4/2.XMLparseOptions.md#removensprefix
49
+ removeNSPrefix: xmlOptions.removeNSPrefix,
50
+ // https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/docs/v4/2.XMLparseOptions.md#textnodename
51
+ textNodeName: xmlOptions.textNodeName,
52
+ isArray: (name, jpath, isLeafNode, isAttribute) => {
53
+ const array = Boolean(xmlOptions?.arrayPaths?.some((path) => jpath === path));
54
+ return array;
55
+ },
56
+ // Application overrides
57
+ ...options?._fastXML
58
+ };
59
+ const xml = (0, parse_xml_1.fastParseXML)(text, fastXMLOptions);
60
+ // Note - could be done with FastXML tag processing
61
+ return xmlOptions.uncapitalizeKeys ? (0, uncapitalize_1.uncapitalizeKeys)(xml) : xml;
62
+ default:
63
+ throw new Error(options?.xml?.parser);
64
+ }
65
+ }
30
66
  exports._typecheckXMLLoader = exports.XMLLoader;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loaders.gl/xml",
3
- "version": "3.4.0-alpha.1",
3
+ "version": "3.4.0-alpha.3",
4
4
  "description": "Framework-independent loaders for the XML (eXtensible Markup Language) format",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -33,12 +33,12 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "@babel/runtime": "^7.3.1",
36
- "@loaders.gl/loader-utils": "3.4.0-alpha.1",
37
- "@loaders.gl/schema": "3.4.0-alpha.1",
38
- "fast-xml-parser": "^4.0.11"
36
+ "@loaders.gl/loader-utils": "3.4.0-alpha.3",
37
+ "@loaders.gl/schema": "3.4.0-alpha.3",
38
+ "fast-xml-parser": "^4.1.3"
39
39
  },
40
40
  "devDependencies": {
41
41
  "xmldom": "0.6.0"
42
42
  },
43
- "gitHead": "4085b0323050e4361614471319a1fb4729547bbf"
43
+ "gitHead": "a954528dd1d78a1f128d8f6b07e4baeb7a296924"
44
44
  }
@@ -0,0 +1,46 @@
1
+ // loaders.gl, MIT license
2
+
3
+ import type {LoaderWithParser} from '@loaders.gl/loader-utils';
4
+ import {mergeLoaderOptions} from '@loaders.gl/loader-utils';
5
+ import {XMLLoader, XMLLoaderOptions} from './xml-loader';
6
+
7
+ export type HTMLLoaderOptions = XMLLoaderOptions;
8
+
9
+ /**
10
+ * Loader for HTML files
11
+ * Essentially a copy of the XMLLoader with different mime types, file extensions and content tests.
12
+ * This split enables applications can control whether they want HTML responses to be parsed by the XML loader or not.
13
+ * This loader does not have any additional understanding of the structure of HTML or the document.
14
+ */
15
+ export const HTMLLoader: LoaderWithParser = {
16
+ ...XMLLoader,
17
+ name: 'HTML',
18
+ id: 'html',
19
+ extensions: ['html', 'htm'],
20
+ mimeTypes: ['text/html'],
21
+ testText: testHTMLFile,
22
+ parse: async (arrayBuffer: ArrayBuffer, options?: XMLLoaderOptions) =>
23
+ parseTextSync(new TextDecoder().decode(arrayBuffer), options),
24
+ parseTextSync: (text: string, options?: XMLLoaderOptions) => parseTextSync(text, options)
25
+ };
26
+
27
+ function testHTMLFile(text: string): boolean {
28
+ // TODO - There could be space first.
29
+ return text.startsWith('<html');
30
+ }
31
+
32
+ function parseTextSync(text: string, options?: XMLLoaderOptions): any {
33
+ // fast-xml-parser can recognize HTML entities
34
+ // https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/docs/v4/2.XMLparseOptions.md#htmlentities
35
+ // https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/docs/v4/5.Entities.md
36
+ options = mergeLoaderOptions(options, {
37
+ xml: {
38
+ parser: 'fast-xml-parser'
39
+ },
40
+ _fastXML: {
41
+ htmlEntities: true
42
+ }
43
+ });
44
+
45
+ return XMLLoader.parseTextSync(text, options);
46
+ }
package/src/index.ts CHANGED
@@ -5,7 +5,23 @@
5
5
  export type {XMLLoaderOptions} from './xml-loader';
6
6
  export {XMLLoader} from './xml-loader';
7
7
 
8
+ // HTMLLoader
9
+
10
+ export type {HTMLLoaderOptions} from './html-loader';
11
+ export {HTMLLoader} from './html-loader';
12
+
8
13
  // SAX
9
14
 
10
15
  export type {SAXParserOptions as SAXParserOptions} from './sax-ts/sax';
11
16
  export {SAXParser as SAXParser} from './sax-ts/sax';
17
+
18
+ // Utilities
19
+
20
+ export {convertXMLValueToArray, convertXMLFieldToArrayInPlace} from './lib/xml-utils/xml-utils';
21
+
22
+ // Experimental
23
+
24
+ export {
25
+ uncapitalize as _uncapitalize,
26
+ uncapitalizeKeys as _uncapitalizeKeys
27
+ } from './lib/xml-utils/uncapitalize';
@@ -1,14 +1,17 @@
1
1
  // loaders.gl, MIT license
2
2
 
3
- import {XMLParser} from '../lib/parser/xml-parser';
3
+ import {StreamingXMLParser} from './streaming-xml-parser';
4
4
  import {XMLParser as FastXMLParser} from 'fast-xml-parser';
5
+ import type {X2jOptions} from 'fast-xml-parser';
5
6
 
6
- export function parseXML(text: string, options): any {
7
+ /** Type for passing through fast-xml-parser options */
8
+ export type FastXMLParserOptions = Partial<X2jOptions>;
9
+
10
+ export function fastParseXML(text: string, options: FastXMLParserOptions): any {
7
11
  const parser = new FastXMLParser({
8
12
  ignoreAttributes: false,
9
13
  attributeNamePrefix: '',
10
- // parseAttributeValue: true,
11
- ...options?.xml
14
+ ...options
12
15
  });
13
16
 
14
17
  const parsedXML = parser.parse(text);
@@ -22,8 +25,8 @@ export function parseXML(text: string, options): any {
22
25
  * @param options
23
26
  * @returns
24
27
  */
25
- export function parseXMLInBatches(text: string, options): any {
26
- const parser = new XMLParser({
28
+ export function parseXMLInBatches(text: string, options = {}): any {
29
+ const parser = new StreamingXMLParser({
27
30
  ...options,
28
31
  strict: true
29
32
  });
@@ -1,13 +1,15 @@
1
1
  // @ts-nocheck
2
2
  /* eslint-disable */
3
3
 
4
- // import ClarinetParser, {ClarinetParserOptions} from '../clarinet/clarinet';
5
4
  import {SAXParser, SAXParserOptions} from '../../sax-ts/sax';
6
5
  // import JSONPath from '../jsonpath/jsonpath';
7
6
 
8
- // XMLParser builds a JSON object using the events emitted by the Clarinet parser
7
+ export type StreamingXMLParserOptions = SAXParserOptions;
9
8
 
10
- export class XMLParser {
9
+ /**
10
+ * StreamingXMLParser builds a JSON object using the events emitted by the SAX parser
11
+ */
12
+ export class StreamingXMLParser {
11
13
  readonly parser: SAXParser;
12
14
  result = undefined;
13
15
  previousStates = [];
@@ -0,0 +1,31 @@
1
+ // loaders.gl, MIT license
2
+
3
+ /**
4
+ * Uncapitalize first letter of a string
5
+ * @param str
6
+ * @returns
7
+ */
8
+ export function uncapitalize(str: string): string {
9
+ return typeof str === 'string' ? str.charAt(0).toLowerCase() + str.slice(1) : str;
10
+ }
11
+
12
+ /**
13
+ * Recursively uncapitalize all keys in a nested object
14
+ * @param object
15
+ * @returns
16
+ */
17
+ export function uncapitalizeKeys(object: any): any {
18
+ if (Array.isArray(object)) {
19
+ return object.map((element) => uncapitalizeKeys(element));
20
+ }
21
+
22
+ if (object && typeof object === 'object') {
23
+ const newObject = {};
24
+ for (const [key, value] of Object.entries(object)) {
25
+ newObject[uncapitalize(key)] = uncapitalizeKeys(value);
26
+ }
27
+ return newObject;
28
+ }
29
+
30
+ return object;
31
+ }
@@ -0,0 +1,27 @@
1
+ // TODO - these utilities could be moved to the XML parser.
2
+ // uncapitalizeKeys could be an XMLLoader option
3
+
4
+ /**
5
+ * Extracts a value or array and always return an array
6
+ * Useful since XML parses to object instead of array when only a single value is provided
7
+ */
8
+ export function convertXMLValueToArray(xmlValue: unknown): unknown[] {
9
+ if (Array.isArray(xmlValue)) {
10
+ return xmlValue;
11
+ }
12
+ if (xmlValue && typeof xmlValue === 'object' && xmlValue['0']) {
13
+ // Error this is an objectified array
14
+ }
15
+ if (xmlValue) {
16
+ return [xmlValue];
17
+ }
18
+ return [];
19
+ }
20
+
21
+ /**
22
+ * Mutates a field in place, converting it to array
23
+ * Useful since XML parses to object instead of array when only a single value is provided
24
+ */
25
+ export function convertXMLFieldToArrayInPlace(xml: any, key: string): void {
26
+ xml[key] = convertXMLValueToArray(xml[key]);
27
+ }