@purescraps/purehtml 0.6.0 → 0.7.1

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.
@@ -1,3 +1,6 @@
1
+ import { CheerioAPI, Cheerio } from 'cheerio';
2
+ import { AnyNode } from 'domhandler';
3
+
1
4
  /**
2
5
  * PureHTML backend class
3
6
  */
@@ -18,18 +21,19 @@ declare abstract class PureHTMLNode {
18
21
  abstract text(): string;
19
22
  }
20
23
 
24
+ type CheerioRoot = CheerioAPI;
21
25
  declare class PureHTMLCheerioBackend extends PureHTMLBackend {
22
26
  load(html: string | Buffer): PureHTMLCheerioDocument;
23
27
  }
24
28
  declare class PureHTMLCheerioDocument extends PureHTMLDocument {
25
29
  #private;
26
- constructor(root: cheerio.Root);
30
+ constructor(root: CheerioRoot);
27
31
  $(selector: string): PureHTMLCheerioNode[];
28
32
  root(): PureHTMLCheerioNode;
29
33
  }
30
34
  declare class PureHTMLCheerioNode extends PureHTMLNode {
31
35
  #private;
32
- constructor($: cheerio.Root, el: cheerio.Cheerio);
36
+ constructor($: CheerioRoot, el: Cheerio<AnyNode>);
33
37
  attr(name: string): string | null;
34
38
  attr(): PureHTMLNodeAttributes;
35
39
  find(selector: string): PureHTMLCheerioNode[];
@@ -59,8 +63,8 @@ declare class ConfigFactory {
59
63
  private static detectExpectedType;
60
64
  }
61
65
 
62
- declare const cheerio$1: PureHTMLCheerioBackend;
66
+ declare const cheerio: PureHTMLCheerioBackend;
63
67
 
64
68
  declare function extract<T = unknown>(backend: PureHTMLBackend, $: PureHTMLDocument | string, config: Config<T>, url: string): T;
65
69
 
66
- export { Config, ConfigFactory, PureHTMLBackend, PureHTMLDocument, PureHTMLNode, type PureHTMLNodeAttributes, cheerio$1 as cheerio, extract };
70
+ export { Config, ConfigFactory, PureHTMLBackend, PureHTMLDocument, PureHTMLNode, type PureHTMLNodeAttributes, cheerio, extract };
package/dist/src/index.js CHANGED
@@ -6472,7 +6472,7 @@ var PureHTMLCheerioNode = class _PureHTMLCheerioNode extends PureHTMLNode {
6472
6472
  if (name) {
6473
6473
  return this.#el.attr(name) ?? null;
6474
6474
  }
6475
- return this.#el.attr();
6475
+ return this.#el.attr() ?? {};
6476
6476
  }
6477
6477
  find(selector) {
6478
6478
  return this.#el.find(selector).toArray().map((el) => new _PureHTMLCheerioNode(this.#$, this.#$(el)));
@@ -6524,6 +6524,9 @@ var ConfigWithSelector = class extends Config {
6524
6524
  }
6525
6525
  return matches;
6526
6526
  }
6527
+ toString() {
6528
+ return `ConfigWithSelector(${this.selector})`;
6529
+ }
6527
6530
  };
6528
6531
 
6529
6532
  // src/config/types/primitive.ts
@@ -6589,6 +6592,7 @@ var ArrayConfig = class _ArrayConfig extends ConfigWithSelector {
6589
6592
  alreadyMatched: params.elementAlreadyMatched ?? false,
6590
6593
  includeRoot: false
6591
6594
  });
6595
+ if (!$el) return null;
6592
6596
  const matches = Array.isArray($el) ? $el : [$el];
6593
6597
  const conf = this.items || PrimitiveValueConfig.generate(null, this.transform);
6594
6598
  const extractOpts = { ...params };
@@ -6793,7 +6797,9 @@ var RemoveUrlQueryParam = class _RemoveUrlQueryParam extends Transformer {
6793
6797
  }
6794
6798
  if (args.length === 0) return val;
6795
6799
  const url = new URL(val);
6796
- args.forEach((paramName) => url.searchParams.delete(paramName));
6800
+ for (const paramName of args) {
6801
+ url.searchParams.delete(paramName);
6802
+ }
6797
6803
  return url.toString();
6798
6804
  }
6799
6805
  };
@@ -6878,15 +6884,18 @@ var UrlQueryParamTransformer = class _UrlQueryParamTransformer extends Transform
6878
6884
  if (args.length === 1) {
6879
6885
  return url.searchParams.get(args[0]);
6880
6886
  }
6881
- return args.reduce((acc, arg) => {
6882
- acc[arg] = url.searchParams.get(arg);
6883
- return acc;
6884
- }, {});
6887
+ return args.reduce(
6888
+ (acc, arg) => {
6889
+ acc[arg] = url.searchParams.get(arg);
6890
+ return acc;
6891
+ },
6892
+ {}
6893
+ );
6885
6894
  }
6886
6895
  };
6887
6896
 
6888
6897
  // src/transformers/transformers.ts
6889
- var Transformers = class {
6898
+ var Transformers = class _Transformers {
6890
6899
  static transformers = [
6891
6900
  AttributeTransformer,
6892
6901
  Exists,
@@ -6900,7 +6909,7 @@ var Transformers = class {
6900
6909
  UrlQueryParamTransformer
6901
6910
  ];
6902
6911
  static getByName(name) {
6903
- return this.transformers.find((tr) => tr.getName() === name);
6912
+ return _Transformers.transformers.find((tr) => tr.getName() === name);
6904
6913
  }
6905
6914
  };
6906
6915
 
@@ -6992,15 +7001,18 @@ var ObjectConfig = class _ObjectConfig extends ConfigWithSelector {
6992
7001
  }
6993
7002
  const props = this.properties;
6994
7003
  const keys = Object.keys(props);
6995
- return keys.reduce((acc, key) => {
6996
- const config = props[key];
6997
- acc[key] = config.extract({
6998
- ...params,
6999
- $el,
7000
- property: this.makeProperty(params.property, key)
7001
- });
7002
- return acc;
7003
- }, {});
7004
+ return keys.reduce(
7005
+ (acc, key) => {
7006
+ const config = props[key];
7007
+ acc[key] = config.extract({
7008
+ ...params,
7009
+ $el,
7010
+ property: this.makeProperty(params.property, key)
7011
+ });
7012
+ return acc;
7013
+ },
7014
+ {}
7015
+ );
7004
7016
  }
7005
7017
  makeProperty(property, prop) {
7006
7018
  if (property === rootProp) {
@@ -7020,21 +7032,20 @@ var UnionConfig = class _UnionConfig extends Config {
7020
7032
  const $parent = params.$el;
7021
7033
  for (const config of this.configs) {
7022
7034
  if (config instanceof ConfigWithSelector) {
7023
- const $el = config.getFirstMatch($parent, {
7035
+ const $matches = config.getAllMatches($parent, {
7024
7036
  alreadyMatched: false,
7025
7037
  includeRoot: true
7026
7038
  });
7027
- if (!$el) {
7039
+ if (!$matches || Array.isArray($matches) && $matches.length === 0)
7028
7040
  continue;
7029
- }
7030
7041
  return config.extract({
7031
7042
  ...params,
7032
- $el,
7033
- elementAlreadyMatched: true
7043
+ $el: Array.isArray($matches) ? $matches.length > 1 ? $parent : $matches[0] : $matches,
7044
+ // arrays must be matched again
7045
+ elementAlreadyMatched: !Array.isArray($matches) || $matches.length === 1
7034
7046
  });
7035
- } else {
7036
- return config.extract({ ...params, $el: params.$el });
7037
7047
  }
7048
+ return config.extract({ ...params, $el: params.$el });
7038
7049
  }
7039
7050
  return null;
7040
7051
  }
@@ -7220,7 +7231,7 @@ var ConfigValidator = class {
7220
7231
  var ConfigFactory = class _ConfigFactory {
7221
7232
  static fromYAML(yaml) {
7222
7233
  const plain = (0, import_yaml.parse)(yaml);
7223
- return this.generate(plain);
7234
+ return _ConfigFactory.generate(plain);
7224
7235
  }
7225
7236
  static generate(plain) {
7226
7237
  const validator = new ConfigValidator(plain);
@@ -7247,24 +7258,28 @@ var ConfigFactory = class _ConfigFactory {
7247
7258
  case "constant":
7248
7259
  return ConstantConfig.generate(constant, selector);
7249
7260
  case "object": {
7250
- let propConfigs = void 0;
7261
+ let propConfigs;
7251
7262
  if (properties) {
7252
- propConfigs = Object.keys(properties).reduce((acc, key) => {
7253
- acc[key] = this.generate(properties[key]);
7254
- return acc;
7255
- }, {});
7263
+ propConfigs = Object.keys(properties).reduce(
7264
+ (acc, key) => {
7265
+ acc[key] = _ConfigFactory.generate(properties[key]);
7266
+ return acc;
7267
+ },
7268
+ {}
7269
+ );
7256
7270
  }
7257
7271
  return ObjectConfig.generate(selector, propConfigs);
7258
7272
  }
7259
7273
  case "array":
7260
7274
  return ArrayConfig.generate(
7261
7275
  selector,
7262
- items && this.generate(items),
7276
+ items && _ConfigFactory.generate(items),
7263
7277
  transform
7264
7278
  );
7265
7279
  case "union":
7266
7280
  return UnionConfig.generate(
7267
- union.map((cfg) => this.generate(cfg))
7281
+ // biome-ignore lint/style/noNonNullAssertion: union will be present here. because our schema already validated the input
7282
+ union.map((cfg) => _ConfigFactory.generate(cfg))
7268
7283
  );
7269
7284
  default:
7270
7285
  return PrimitiveValueConfig.generate(selector, transform);