@purescraps/purehtml 0.3.1 → 0.4.0

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.
@@ -6782,12 +6782,66 @@ var require_ajv = __commonJS({
6782
6782
  });
6783
6783
 
6784
6784
  // examples/simple.ts
6785
- var import_cheerio2 = require("cheerio");
6786
6785
  var import_fs = require("fs");
6787
6786
 
6788
- // src/index.ts
6787
+ // src/backends/cheerio.ts
6789
6788
  var import_cheerio = require("cheerio");
6790
6789
 
6790
+ // src/core/backend.ts
6791
+ var PureHTMLBackend = class {
6792
+ };
6793
+ var PureHTMLDocument = class {
6794
+ };
6795
+ var PureHTMLNode = class {
6796
+ };
6797
+
6798
+ // src/backends/cheerio.ts
6799
+ var PureHTMLCheerioBackend = class extends PureHTMLBackend {
6800
+ load(html2) {
6801
+ return new PureHTMLCheerioDocument((0, import_cheerio.load)(html2));
6802
+ }
6803
+ };
6804
+ var PureHTMLCheerioDocument = class extends PureHTMLDocument {
6805
+ #root;
6806
+ constructor(root) {
6807
+ super();
6808
+ this.#root = root;
6809
+ }
6810
+ $(selector) {
6811
+ return this.#root(selector).toArray().map((el) => new PureHTMLCheerioNode(this.#root, this.#root(el)));
6812
+ }
6813
+ root() {
6814
+ return new PureHTMLCheerioNode(this.#root, this.#root.root());
6815
+ }
6816
+ };
6817
+ var PureHTMLCheerioNode = class _PureHTMLCheerioNode extends PureHTMLNode {
6818
+ #$;
6819
+ #el;
6820
+ constructor($, el) {
6821
+ super();
6822
+ this.#$ = $;
6823
+ this.#el = el;
6824
+ }
6825
+ attr(name) {
6826
+ if (name) {
6827
+ return this.#el.attr(name) ?? null;
6828
+ }
6829
+ return this.#el.attr();
6830
+ }
6831
+ find(selector) {
6832
+ return this.#el.find(selector).toArray().map((el) => new _PureHTMLCheerioNode(this.#$, this.#$(el)));
6833
+ }
6834
+ html() {
6835
+ return this.#el.html();
6836
+ }
6837
+ is(selector) {
6838
+ return this.#el.is(selector);
6839
+ }
6840
+ text() {
6841
+ return this.#el.text();
6842
+ }
6843
+ };
6844
+
6791
6845
  // src/core/property.ts
6792
6846
  var rootProp = Symbol("rootProp");
6793
6847
 
@@ -6805,17 +6859,24 @@ var Config = class {
6805
6859
  // src/config/types/with-selector.ts
6806
6860
  var ConfigWithSelector = class extends Config {
6807
6861
  selector = null;
6808
- getSelectorMatches($2, alreadyMatched) {
6862
+ getAllMatches($, { alreadyMatched, includeRoot }) {
6809
6863
  if (alreadyMatched) {
6810
- return $2;
6864
+ return $;
6811
6865
  }
6812
6866
  if (this.selector === null) {
6813
- return $2;
6867
+ return $;
6868
+ }
6869
+ if (includeRoot && $.is(this.selector)) {
6870
+ return $;
6814
6871
  }
6815
- if ($2.is(this.selector)) {
6816
- return $2;
6872
+ return $.find(this.selector) ?? null;
6873
+ }
6874
+ getFirstMatch($, params) {
6875
+ const matches = this.getAllMatches($, params);
6876
+ if (Array.isArray(matches)) {
6877
+ return matches[0] ?? null;
6817
6878
  }
6818
- return $2.find(this.selector);
6879
+ return matches;
6819
6880
  }
6820
6881
  };
6821
6882
 
@@ -6827,11 +6888,11 @@ var PrimitiveValueConfig = class _PrimitiveValueConfig extends ConfigWithSelecto
6827
6888
  }
6828
6889
  extract(params) {
6829
6890
  let val = null;
6830
- const $el = this.getSelectorMatches(
6831
- params.$el,
6832
- params.elementAlreadyMatched ?? false
6833
- );
6834
- if ($el.length > 0) {
6891
+ const $el = this.getFirstMatch(params.$el, {
6892
+ alreadyMatched: params.elementAlreadyMatched ?? false,
6893
+ includeRoot: false
6894
+ });
6895
+ if ($el) {
6835
6896
  val = $el.text();
6836
6897
  }
6837
6898
  if (!this.transform) {
@@ -6878,16 +6939,17 @@ var ArrayConfig = class _ArrayConfig extends ConfigWithSelector {
6878
6939
  super();
6879
6940
  }
6880
6941
  extract(params) {
6881
- const $el = this.getSelectorMatches(
6882
- params.$el,
6883
- params.elementAlreadyMatched ?? false
6884
- );
6942
+ const $el = this.getAllMatches(params.$el, {
6943
+ alreadyMatched: params.elementAlreadyMatched ?? false,
6944
+ includeRoot: false
6945
+ });
6946
+ const matches = Array.isArray($el) ? $el : [$el];
6885
6947
  const conf = this.items || PrimitiveValueConfig.generate(null, this.transform);
6886
6948
  const extractOpts = { ...params };
6887
- return $el.toArray().map((el, i) => {
6949
+ return matches.map((el, i) => {
6888
6950
  return conf.extract({
6889
6951
  ...extractOpts,
6890
- $el: params.$(el),
6952
+ $el: el,
6891
6953
  property: this.makeProperty(params.property, i)
6892
6954
  });
6893
6955
  });
@@ -6925,17 +6987,19 @@ var AttributeTransformer = class _AttributeTransformer extends Transformer {
6925
6987
  }
6926
6988
  transform({ $el }) {
6927
6989
  const { args } = this;
6928
- switch (args.length) {
6929
- case 0:
6930
- return $el.attr();
6931
- case 1:
6932
- return _AttributeTransformer.getAttrValue($el, args[0]);
6933
- default:
6934
- return args.reduce((acc, arg) => {
6935
- acc[arg] = _AttributeTransformer.getAttrValue($el, arg);
6936
- return acc;
6937
- }, {});
6990
+ if (!$el) {
6991
+ return null;
6992
+ }
6993
+ if (args.length === 0) {
6994
+ return $el.attr();
6938
6995
  }
6996
+ if (args.length === 1) {
6997
+ return _AttributeTransformer.getAttrValue($el, args[0]);
6998
+ }
6999
+ return args.reduce((acc, arg) => {
7000
+ acc[arg] = _AttributeTransformer.getAttrValue($el, arg);
7001
+ return acc;
7002
+ }, {});
6939
7003
  }
6940
7004
  static getAttrValue($el, attr) {
6941
7005
  const val = $el.attr(attr);
@@ -6958,7 +7022,7 @@ var Exists = class extends Transformer {
6958
7022
  return BOOLEAN;
6959
7023
  }
6960
7024
  transform({ $el }) {
6961
- return $el.length > 0;
7025
+ return !!$el;
6962
7026
  }
6963
7027
  };
6964
7028
 
@@ -6974,7 +7038,7 @@ var HTMLTransformer = class extends Transformer {
6974
7038
  return STRING;
6975
7039
  }
6976
7040
  transform({ $el }) {
6977
- return $el.html();
7041
+ return $el?.html();
6978
7042
  }
6979
7043
  };
6980
7044
 
@@ -7008,22 +7072,6 @@ var Length = class extends Transformer {
7008
7072
  }
7009
7073
  };
7010
7074
 
7011
- // src/transformers/match-count.ts
7012
- var MatchCount = class extends Transformer {
7013
- static getName() {
7014
- return "match-count";
7015
- }
7016
- inputType() {
7017
- return STRING;
7018
- }
7019
- outputType() {
7020
- return NUMBER;
7021
- }
7022
- transform({ $el }) {
7023
- return $el.length;
7024
- }
7025
- };
7026
-
7027
7075
  // src/transformers/number.ts
7028
7076
  var NumberTransformer = class extends Transformer {
7029
7077
  static getName() {
@@ -7133,13 +7181,6 @@ var UrlQueryParamTransformer = class _UrlQueryParamTransformer extends Transform
7133
7181
  return acc;
7134
7182
  }, {});
7135
7183
  }
7136
- static getAttrValue($el, attr) {
7137
- const val = $el.attr(attr);
7138
- if (val === void 0) {
7139
- return null;
7140
- }
7141
- return val;
7142
- }
7143
7184
  };
7144
7185
 
7145
7186
  // src/transformers/transformers.ts
@@ -7149,7 +7190,6 @@ var Transformers = class {
7149
7190
  Exists,
7150
7191
  HTMLTransformer,
7151
7192
  Length,
7152
- MatchCount,
7153
7193
  NumberTransformer,
7154
7194
  ResolveTransformer,
7155
7195
  Trim,
@@ -7199,16 +7239,27 @@ var TransformerFactory = class _TransformerFactory {
7199
7239
  };
7200
7240
 
7201
7241
  // src/config/types/constant.ts
7202
- var ConstantConfig = class _ConstantConfig extends Config {
7203
- constructor(val) {
7242
+ var ConstantConfig = class _ConstantConfig extends ConfigWithSelector {
7243
+ constructor(val, selector) {
7204
7244
  super();
7205
7245
  this.val = val;
7246
+ this.selector = selector;
7206
7247
  }
7207
- extract() {
7248
+ extract(params) {
7249
+ if (this.selector) {
7250
+ const matches = this.getAllMatches(params.$el, {
7251
+ alreadyMatched: false,
7252
+ includeRoot: true
7253
+ });
7254
+ if (Array.isArray(matches) && matches.length === 0) {
7255
+ return null;
7256
+ }
7257
+ return this.val;
7258
+ }
7208
7259
  return this.val;
7209
7260
  }
7210
- static generate(val) {
7211
- return new _ConstantConfig(val);
7261
+ static generate(val, selector) {
7262
+ return new _ConstantConfig(val, selector);
7212
7263
  }
7213
7264
  };
7214
7265
 
@@ -7228,11 +7279,11 @@ var ObjectConfig = class _ObjectConfig extends ConfigWithSelector {
7228
7279
  }
7229
7280
  extract(params) {
7230
7281
  const $parent = params.$el;
7231
- const $el = this.getSelectorMatches(
7232
- $parent,
7233
- params.elementAlreadyMatched ?? false
7234
- );
7235
- if ($el.length === 0) {
7282
+ const $el = this.getFirstMatch($parent, {
7283
+ alreadyMatched: params.elementAlreadyMatched ?? false,
7284
+ includeRoot: false
7285
+ });
7286
+ if (!$el) {
7236
7287
  return null;
7237
7288
  }
7238
7289
  const props = this.properties;
@@ -7265,8 +7316,11 @@ var UnionConfig = class _UnionConfig extends Config {
7265
7316
  const $parent = params.$el;
7266
7317
  for (const config2 of this.configs) {
7267
7318
  if (config2 instanceof ConfigWithSelector) {
7268
- const $el = config2.getSelectorMatches($parent, false);
7269
- if ($el.length === 0) {
7319
+ const $el = config2.getFirstMatch($parent, {
7320
+ alreadyMatched: false,
7321
+ includeRoot: true
7322
+ });
7323
+ if (!$el) {
7270
7324
  continue;
7271
7325
  }
7272
7326
  return config2.extract({
@@ -7275,7 +7329,7 @@ var UnionConfig = class _UnionConfig extends Config {
7275
7329
  elementAlreadyMatched: true
7276
7330
  });
7277
7331
  } else {
7278
- return config2.extract({ ...params, $el: params.$("") });
7332
+ return config2.extract({ ...params, $el: params.$el });
7279
7333
  }
7280
7334
  }
7281
7335
  return null;
@@ -7433,7 +7487,7 @@ var oneOf = [
7433
7487
  {
7434
7488
  $comment: "Constant",
7435
7489
  type: "object",
7436
- properties: { constant: {} },
7490
+ properties: { selector: { type: "string" }, constant: {} },
7437
7491
  required: ["constant"],
7438
7492
  additionalProperties: false
7439
7493
  }
@@ -7487,7 +7541,7 @@ var ConfigFactory = class _ConfigFactory {
7487
7541
  const transform = transformOrig ? _ConfigFactory.generateTransform(transformOrig) : void 0;
7488
7542
  switch (expectedType) {
7489
7543
  case "constant":
7490
- return ConstantConfig.generate(constant);
7544
+ return ConstantConfig.generate(constant, selector);
7491
7545
  case "object": {
7492
7546
  let propConfigs = void 0;
7493
7547
  if (properties) {
@@ -7559,10 +7613,11 @@ var ConfigFactory = class _ConfigFactory {
7559
7613
  };
7560
7614
 
7561
7615
  // src/index.ts
7562
- function extract($2, config2, url) {
7563
- let $root = $2;
7616
+ var cheerio = new PureHTMLCheerioBackend();
7617
+ function extract(backend, $, config2, url) {
7618
+ let $root = $;
7564
7619
  if (typeof $root === "string") {
7565
- $root = (0, import_cheerio.load)($root);
7620
+ $root = backend.load($root);
7566
7621
  }
7567
7622
  return config2.extract({
7568
7623
  $: $root,
@@ -7577,8 +7632,10 @@ var html = (0, import_fs.readFileSync)("examples/simple.html").toString();
7577
7632
  var config = ConfigFactory.fromYAML(
7578
7633
  (0, import_fs.readFileSync)("examples/simple.yaml").toString()
7579
7634
  );
7580
- var $ = (0, import_cheerio2.load)(html);
7581
- console.log("Simple parse result:", extract($, config, "https://example.com"));
7635
+ console.log(
7636
+ "Simple parse result:",
7637
+ extract(cheerio, html, config, "https://example.com")
7638
+ );
7582
7639
  /*! Bundled license information:
7583
7640
 
7584
7641
  uri-js/dist/es5/uri.all.js: