@purescraps/purehtml 0.3.1 → 0.4.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,9 +1,49 @@
1
+ /**
2
+ * PureHTML backend class
3
+ */
4
+ declare abstract class PureHTMLBackend {
5
+ abstract load(html: Buffer | string): PureHTMLDocument;
6
+ }
7
+ declare abstract class PureHTMLDocument {
8
+ abstract root(): PureHTMLNode;
9
+ abstract $(selector: string): PureHTMLNode[];
10
+ }
11
+ type PureHTMLNodeAttributes = Record<string, string | null>;
12
+ declare abstract class PureHTMLNode {
13
+ abstract attr(name: string): string | null;
14
+ abstract attr(): PureHTMLNodeAttributes;
15
+ abstract find(selector: string): PureHTMLNode[];
16
+ abstract html(): string | null;
17
+ abstract is(selector: string): boolean;
18
+ abstract text(): string;
19
+ }
20
+
21
+ declare class PureHTMLCheerioBackend extends PureHTMLBackend {
22
+ load(html: string | Buffer): PureHTMLCheerioDocument;
23
+ }
24
+ declare class PureHTMLCheerioDocument extends PureHTMLDocument {
25
+ #private;
26
+ constructor(root: cheerio.Root);
27
+ $(selector: string): PureHTMLCheerioNode[];
28
+ root(): PureHTMLCheerioNode;
29
+ }
30
+ declare class PureHTMLCheerioNode extends PureHTMLNode {
31
+ #private;
32
+ constructor($: cheerio.Root, el: cheerio.Cheerio);
33
+ attr(name: string): string | null;
34
+ attr(): PureHTMLNodeAttributes;
35
+ find(selector: string): PureHTMLCheerioNode[];
36
+ html(): string | null;
37
+ is(selector: string): boolean;
38
+ text(): string;
39
+ }
40
+
1
41
  declare const rootProp: unique symbol;
2
42
  type Property = typeof rootProp | string;
3
43
 
4
44
  interface ExtractParams {
5
- $: cheerio.Root;
6
- $el: cheerio.Cheerio;
45
+ $: PureHTMLDocument;
46
+ $el: PureHTMLNode;
7
47
  url: string;
8
48
  property: Property;
9
49
  }
@@ -19,6 +59,8 @@ declare class ConfigFactory {
19
59
  private static detectExpectedType;
20
60
  }
21
61
 
22
- declare function extract<T = unknown>($: cheerio.Root | string, config: Config<T>, url: string): T;
62
+ declare const cheerio$1: PureHTMLCheerioBackend;
63
+
64
+ declare function extract<T = unknown>(backend: PureHTMLBackend, $: PureHTMLDocument | string, config: Config<T>, url: string): T;
23
65
 
24
- export { Config, ConfigFactory, extract };
66
+ export { Config, ConfigFactory, PureHTMLBackend, PureHTMLDocument, PureHTMLNode, type PureHTMLNodeAttributes, cheerio$1 as cheerio, extract };
package/dist/src/index.js CHANGED
@@ -6787,11 +6787,72 @@ var src_exports = {};
6787
6787
  __export(src_exports, {
6788
6788
  Config: () => Config,
6789
6789
  ConfigFactory: () => ConfigFactory,
6790
+ PureHTMLBackend: () => PureHTMLBackend,
6791
+ PureHTMLDocument: () => PureHTMLDocument,
6792
+ PureHTMLNode: () => PureHTMLNode,
6793
+ cheerio: () => cheerio,
6790
6794
  extract: () => extract
6791
6795
  });
6792
6796
  module.exports = __toCommonJS(src_exports);
6797
+
6798
+ // src/backends/cheerio.ts
6793
6799
  var import_cheerio = require("cheerio");
6794
6800
 
6801
+ // src/core/backend.ts
6802
+ var PureHTMLBackend = class {
6803
+ };
6804
+ var PureHTMLDocument = class {
6805
+ };
6806
+ var PureHTMLNode = class {
6807
+ };
6808
+
6809
+ // src/backends/cheerio.ts
6810
+ var PureHTMLCheerioBackend = class extends PureHTMLBackend {
6811
+ load(html) {
6812
+ return new PureHTMLCheerioDocument((0, import_cheerio.load)(html));
6813
+ }
6814
+ };
6815
+ var PureHTMLCheerioDocument = class extends PureHTMLDocument {
6816
+ #root;
6817
+ constructor(root) {
6818
+ super();
6819
+ this.#root = root;
6820
+ }
6821
+ $(selector) {
6822
+ return this.#root(selector).toArray().map((el) => new PureHTMLCheerioNode(this.#root, this.#root(el)));
6823
+ }
6824
+ root() {
6825
+ return new PureHTMLCheerioNode(this.#root, this.#root.root());
6826
+ }
6827
+ };
6828
+ var PureHTMLCheerioNode = class _PureHTMLCheerioNode extends PureHTMLNode {
6829
+ #$;
6830
+ #el;
6831
+ constructor($, el) {
6832
+ super();
6833
+ this.#$ = $;
6834
+ this.#el = el;
6835
+ }
6836
+ attr(name) {
6837
+ if (name) {
6838
+ return this.#el.attr(name) ?? null;
6839
+ }
6840
+ return this.#el.attr();
6841
+ }
6842
+ find(selector) {
6843
+ return this.#el.find(selector).toArray().map((el) => new _PureHTMLCheerioNode(this.#$, this.#$(el)));
6844
+ }
6845
+ html() {
6846
+ return this.#el.html();
6847
+ }
6848
+ is(selector) {
6849
+ return this.#el.is(selector);
6850
+ }
6851
+ text() {
6852
+ return this.#el.text();
6853
+ }
6854
+ };
6855
+
6795
6856
  // src/core/property.ts
6796
6857
  var rootProp = Symbol("rootProp");
6797
6858
 
@@ -6809,17 +6870,24 @@ var Config = class {
6809
6870
  // src/config/types/with-selector.ts
6810
6871
  var ConfigWithSelector = class extends Config {
6811
6872
  selector = null;
6812
- getSelectorMatches($, alreadyMatched) {
6873
+ getAllMatches($, { alreadyMatched, includeRoot }) {
6813
6874
  if (alreadyMatched) {
6814
6875
  return $;
6815
6876
  }
6816
6877
  if (this.selector === null) {
6817
6878
  return $;
6818
6879
  }
6819
- if ($.is(this.selector)) {
6880
+ if (includeRoot && $.is(this.selector)) {
6820
6881
  return $;
6821
6882
  }
6822
- return $.find(this.selector);
6883
+ return $.find(this.selector) ?? null;
6884
+ }
6885
+ getFirstMatch($, params) {
6886
+ const matches = this.getAllMatches($, params);
6887
+ if (Array.isArray(matches)) {
6888
+ return matches[0] ?? null;
6889
+ }
6890
+ return matches;
6823
6891
  }
6824
6892
  };
6825
6893
 
@@ -6831,11 +6899,11 @@ var PrimitiveValueConfig = class _PrimitiveValueConfig extends ConfigWithSelecto
6831
6899
  }
6832
6900
  extract(params) {
6833
6901
  let val = null;
6834
- const $el = this.getSelectorMatches(
6835
- params.$el,
6836
- params.elementAlreadyMatched ?? false
6837
- );
6838
- if ($el.length > 0) {
6902
+ const $el = this.getFirstMatch(params.$el, {
6903
+ alreadyMatched: params.elementAlreadyMatched ?? false,
6904
+ includeRoot: false
6905
+ });
6906
+ if ($el) {
6839
6907
  val = $el.text();
6840
6908
  }
6841
6909
  if (!this.transform) {
@@ -6882,16 +6950,17 @@ var ArrayConfig = class _ArrayConfig extends ConfigWithSelector {
6882
6950
  super();
6883
6951
  }
6884
6952
  extract(params) {
6885
- const $el = this.getSelectorMatches(
6886
- params.$el,
6887
- params.elementAlreadyMatched ?? false
6888
- );
6953
+ const $el = this.getAllMatches(params.$el, {
6954
+ alreadyMatched: params.elementAlreadyMatched ?? false,
6955
+ includeRoot: false
6956
+ });
6957
+ const matches = Array.isArray($el) ? $el : [$el];
6889
6958
  const conf = this.items || PrimitiveValueConfig.generate(null, this.transform);
6890
6959
  const extractOpts = { ...params };
6891
- return $el.toArray().map((el, i) => {
6960
+ return matches.map((el, i) => {
6892
6961
  return conf.extract({
6893
6962
  ...extractOpts,
6894
- $el: params.$(el),
6963
+ $el: el,
6895
6964
  property: this.makeProperty(params.property, i)
6896
6965
  });
6897
6966
  });
@@ -6929,17 +6998,19 @@ var AttributeTransformer = class _AttributeTransformer extends Transformer {
6929
6998
  }
6930
6999
  transform({ $el }) {
6931
7000
  const { args } = this;
6932
- switch (args.length) {
6933
- case 0:
6934
- return $el.attr();
6935
- case 1:
6936
- return _AttributeTransformer.getAttrValue($el, args[0]);
6937
- default:
6938
- return args.reduce((acc, arg) => {
6939
- acc[arg] = _AttributeTransformer.getAttrValue($el, arg);
6940
- return acc;
6941
- }, {});
7001
+ if (!$el) {
7002
+ return null;
7003
+ }
7004
+ if (args.length === 0) {
7005
+ return $el.attr();
6942
7006
  }
7007
+ if (args.length === 1) {
7008
+ return _AttributeTransformer.getAttrValue($el, args[0]);
7009
+ }
7010
+ return args.reduce((acc, arg) => {
7011
+ acc[arg] = _AttributeTransformer.getAttrValue($el, arg);
7012
+ return acc;
7013
+ }, {});
6943
7014
  }
6944
7015
  static getAttrValue($el, attr) {
6945
7016
  const val = $el.attr(attr);
@@ -6962,7 +7033,7 @@ var Exists = class extends Transformer {
6962
7033
  return BOOLEAN;
6963
7034
  }
6964
7035
  transform({ $el }) {
6965
- return $el.length > 0;
7036
+ return !!$el;
6966
7037
  }
6967
7038
  };
6968
7039
 
@@ -6978,7 +7049,7 @@ var HTMLTransformer = class extends Transformer {
6978
7049
  return STRING;
6979
7050
  }
6980
7051
  transform({ $el }) {
6981
- return $el.html();
7052
+ return $el?.html();
6982
7053
  }
6983
7054
  };
6984
7055
 
@@ -7012,22 +7083,6 @@ var Length = class extends Transformer {
7012
7083
  }
7013
7084
  };
7014
7085
 
7015
- // src/transformers/match-count.ts
7016
- var MatchCount = class extends Transformer {
7017
- static getName() {
7018
- return "match-count";
7019
- }
7020
- inputType() {
7021
- return STRING;
7022
- }
7023
- outputType() {
7024
- return NUMBER;
7025
- }
7026
- transform({ $el }) {
7027
- return $el.length;
7028
- }
7029
- };
7030
-
7031
7086
  // src/transformers/number.ts
7032
7087
  var NumberTransformer = class extends Transformer {
7033
7088
  static getName() {
@@ -7043,6 +7098,9 @@ var NumberTransformer = class extends Transformer {
7043
7098
  if (typeof val === "string") {
7044
7099
  return Number(val);
7045
7100
  }
7101
+ if (val === null) {
7102
+ return null;
7103
+ }
7046
7104
  throw new InvalidParseInputError(
7047
7105
  property,
7048
7106
  `NumberTransformer.transform: invalid value type: ${typeof val}. value=${JSON.stringify(
@@ -7137,13 +7195,6 @@ var UrlQueryParamTransformer = class _UrlQueryParamTransformer extends Transform
7137
7195
  return acc;
7138
7196
  }, {});
7139
7197
  }
7140
- static getAttrValue($el, attr) {
7141
- const val = $el.attr(attr);
7142
- if (val === void 0) {
7143
- return null;
7144
- }
7145
- return val;
7146
- }
7147
7198
  };
7148
7199
 
7149
7200
  // src/transformers/transformers.ts
@@ -7153,7 +7204,6 @@ var Transformers = class {
7153
7204
  Exists,
7154
7205
  HTMLTransformer,
7155
7206
  Length,
7156
- MatchCount,
7157
7207
  NumberTransformer,
7158
7208
  ResolveTransformer,
7159
7209
  Trim,
@@ -7203,16 +7253,27 @@ var TransformerFactory = class _TransformerFactory {
7203
7253
  };
7204
7254
 
7205
7255
  // src/config/types/constant.ts
7206
- var ConstantConfig = class _ConstantConfig extends Config {
7207
- constructor(val) {
7256
+ var ConstantConfig = class _ConstantConfig extends ConfigWithSelector {
7257
+ constructor(val, selector) {
7208
7258
  super();
7209
7259
  this.val = val;
7260
+ this.selector = selector;
7210
7261
  }
7211
- extract() {
7262
+ extract(params) {
7263
+ if (this.selector) {
7264
+ const matches = this.getAllMatches(params.$el, {
7265
+ alreadyMatched: false,
7266
+ includeRoot: true
7267
+ });
7268
+ if (Array.isArray(matches) && matches.length === 0) {
7269
+ return null;
7270
+ }
7271
+ return this.val;
7272
+ }
7212
7273
  return this.val;
7213
7274
  }
7214
- static generate(val) {
7215
- return new _ConstantConfig(val);
7275
+ static generate(val, selector) {
7276
+ return new _ConstantConfig(val, selector);
7216
7277
  }
7217
7278
  };
7218
7279
 
@@ -7232,11 +7293,11 @@ var ObjectConfig = class _ObjectConfig extends ConfigWithSelector {
7232
7293
  }
7233
7294
  extract(params) {
7234
7295
  const $parent = params.$el;
7235
- const $el = this.getSelectorMatches(
7236
- $parent,
7237
- params.elementAlreadyMatched ?? false
7238
- );
7239
- if ($el.length === 0) {
7296
+ const $el = this.getFirstMatch($parent, {
7297
+ alreadyMatched: params.elementAlreadyMatched ?? false,
7298
+ includeRoot: false
7299
+ });
7300
+ if (!$el) {
7240
7301
  return null;
7241
7302
  }
7242
7303
  const props = this.properties;
@@ -7269,8 +7330,11 @@ var UnionConfig = class _UnionConfig extends Config {
7269
7330
  const $parent = params.$el;
7270
7331
  for (const config of this.configs) {
7271
7332
  if (config instanceof ConfigWithSelector) {
7272
- const $el = config.getSelectorMatches($parent, false);
7273
- if ($el.length === 0) {
7333
+ const $el = config.getFirstMatch($parent, {
7334
+ alreadyMatched: false,
7335
+ includeRoot: true
7336
+ });
7337
+ if (!$el) {
7274
7338
  continue;
7275
7339
  }
7276
7340
  return config.extract({
@@ -7279,7 +7343,7 @@ var UnionConfig = class _UnionConfig extends Config {
7279
7343
  elementAlreadyMatched: true
7280
7344
  });
7281
7345
  } else {
7282
- return config.extract({ ...params, $el: params.$("") });
7346
+ return config.extract({ ...params, $el: params.$el });
7283
7347
  }
7284
7348
  }
7285
7349
  return null;
@@ -7437,7 +7501,7 @@ var oneOf = [
7437
7501
  {
7438
7502
  $comment: "Constant",
7439
7503
  type: "object",
7440
- properties: { constant: {} },
7504
+ properties: { selector: { type: "string" }, constant: {} },
7441
7505
  required: ["constant"],
7442
7506
  additionalProperties: false
7443
7507
  }
@@ -7491,7 +7555,7 @@ var ConfigFactory = class _ConfigFactory {
7491
7555
  const transform = transformOrig ? _ConfigFactory.generateTransform(transformOrig) : void 0;
7492
7556
  switch (expectedType) {
7493
7557
  case "constant":
7494
- return ConstantConfig.generate(constant);
7558
+ return ConstantConfig.generate(constant, selector);
7495
7559
  case "object": {
7496
7560
  let propConfigs = void 0;
7497
7561
  if (properties) {
@@ -7563,10 +7627,11 @@ var ConfigFactory = class _ConfigFactory {
7563
7627
  };
7564
7628
 
7565
7629
  // src/index.ts
7566
- function extract($, config, url) {
7630
+ var cheerio = new PureHTMLCheerioBackend();
7631
+ function extract(backend, $, config, url) {
7567
7632
  let $root = $;
7568
7633
  if (typeof $root === "string") {
7569
- $root = (0, import_cheerio.load)($root);
7634
+ $root = backend.load($root);
7570
7635
  }
7571
7636
  return config.extract({
7572
7637
  $: $root,
@@ -7579,6 +7644,10 @@ function extract($, config, url) {
7579
7644
  0 && (module.exports = {
7580
7645
  Config,
7581
7646
  ConfigFactory,
7647
+ PureHTMLBackend,
7648
+ PureHTMLDocument,
7649
+ PureHTMLNode,
7650
+ cheerio,
7582
7651
  extract
7583
7652
  });
7584
7653
  /*! Bundled license information: