@purescraps/purehtml 0.7.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 };
@@ -6997,15 +7001,18 @@ var ObjectConfig = class _ObjectConfig extends ConfigWithSelector {
6997
7001
  }
6998
7002
  const props = this.properties;
6999
7003
  const keys = Object.keys(props);
7000
- return keys.reduce((acc, key) => {
7001
- const config = props[key];
7002
- acc[key] = config.extract({
7003
- ...params,
7004
- $el,
7005
- property: this.makeProperty(params.property, key)
7006
- });
7007
- return acc;
7008
- }, {});
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
+ );
7009
7016
  }
7010
7017
  makeProperty(property, prop) {
7011
7018
  if (property === rootProp) {
@@ -7224,7 +7231,7 @@ var ConfigValidator = class {
7224
7231
  var ConfigFactory = class _ConfigFactory {
7225
7232
  static fromYAML(yaml) {
7226
7233
  const plain = (0, import_yaml.parse)(yaml);
7227
- return this.generate(plain);
7234
+ return _ConfigFactory.generate(plain);
7228
7235
  }
7229
7236
  static generate(plain) {
7230
7237
  const validator = new ConfigValidator(plain);
@@ -7251,24 +7258,28 @@ var ConfigFactory = class _ConfigFactory {
7251
7258
  case "constant":
7252
7259
  return ConstantConfig.generate(constant, selector);
7253
7260
  case "object": {
7254
- let propConfigs = void 0;
7261
+ let propConfigs;
7255
7262
  if (properties) {
7256
- propConfigs = Object.keys(properties).reduce((acc, key) => {
7257
- acc[key] = this.generate(properties[key]);
7258
- return acc;
7259
- }, {});
7263
+ propConfigs = Object.keys(properties).reduce(
7264
+ (acc, key) => {
7265
+ acc[key] = _ConfigFactory.generate(properties[key]);
7266
+ return acc;
7267
+ },
7268
+ {}
7269
+ );
7260
7270
  }
7261
7271
  return ObjectConfig.generate(selector, propConfigs);
7262
7272
  }
7263
7273
  case "array":
7264
7274
  return ArrayConfig.generate(
7265
7275
  selector,
7266
- items && this.generate(items),
7276
+ items && _ConfigFactory.generate(items),
7267
7277
  transform
7268
7278
  );
7269
7279
  case "union":
7270
7280
  return UnionConfig.generate(
7271
- 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))
7272
7283
  );
7273
7284
  default:
7274
7285
  return PrimitiveValueConfig.generate(selector, transform);