@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.
- package/dist/examples/simple.js +50 -35
- package/dist/examples/simple.js.map +1 -1
- package/dist/src/index.d.ts +8 -4
- package/dist/src/index.js +47 -32
- package/dist/src/index.js.map +1 -1
- package/package.json +18 -17
package/dist/examples/simple.js
CHANGED
|
@@ -6417,7 +6417,7 @@ var require_ajv = __commonJS({
|
|
|
6417
6417
|
});
|
|
6418
6418
|
|
|
6419
6419
|
// examples/simple.ts
|
|
6420
|
-
var
|
|
6420
|
+
var import_node_fs = require("fs");
|
|
6421
6421
|
|
|
6422
6422
|
// src/backends/cheerio.ts
|
|
6423
6423
|
var import_cheerio = require("cheerio");
|
|
@@ -6461,7 +6461,7 @@ var PureHTMLCheerioNode = class _PureHTMLCheerioNode extends PureHTMLNode {
|
|
|
6461
6461
|
if (name) {
|
|
6462
6462
|
return this.#el.attr(name) ?? null;
|
|
6463
6463
|
}
|
|
6464
|
-
return this.#el.attr();
|
|
6464
|
+
return this.#el.attr() ?? {};
|
|
6465
6465
|
}
|
|
6466
6466
|
find(selector) {
|
|
6467
6467
|
return this.#el.find(selector).toArray().map((el) => new _PureHTMLCheerioNode(this.#$, this.#$(el)));
|
|
@@ -6513,6 +6513,9 @@ var ConfigWithSelector = class extends Config {
|
|
|
6513
6513
|
}
|
|
6514
6514
|
return matches;
|
|
6515
6515
|
}
|
|
6516
|
+
toString() {
|
|
6517
|
+
return `ConfigWithSelector(${this.selector})`;
|
|
6518
|
+
}
|
|
6516
6519
|
};
|
|
6517
6520
|
|
|
6518
6521
|
// src/config/types/primitive.ts
|
|
@@ -6578,6 +6581,7 @@ var ArrayConfig = class _ArrayConfig extends ConfigWithSelector {
|
|
|
6578
6581
|
alreadyMatched: params.elementAlreadyMatched ?? false,
|
|
6579
6582
|
includeRoot: false
|
|
6580
6583
|
});
|
|
6584
|
+
if (!$el) return null;
|
|
6581
6585
|
const matches = Array.isArray($el) ? $el : [$el];
|
|
6582
6586
|
const conf = this.items || PrimitiveValueConfig.generate(null, this.transform);
|
|
6583
6587
|
const extractOpts = { ...params };
|
|
@@ -6782,7 +6786,9 @@ var RemoveUrlQueryParam = class _RemoveUrlQueryParam extends Transformer {
|
|
|
6782
6786
|
}
|
|
6783
6787
|
if (args.length === 0) return val;
|
|
6784
6788
|
const url = new URL(val);
|
|
6785
|
-
|
|
6789
|
+
for (const paramName of args) {
|
|
6790
|
+
url.searchParams.delete(paramName);
|
|
6791
|
+
}
|
|
6786
6792
|
return url.toString();
|
|
6787
6793
|
}
|
|
6788
6794
|
};
|
|
@@ -6867,15 +6873,18 @@ var UrlQueryParamTransformer = class _UrlQueryParamTransformer extends Transform
|
|
|
6867
6873
|
if (args.length === 1) {
|
|
6868
6874
|
return url.searchParams.get(args[0]);
|
|
6869
6875
|
}
|
|
6870
|
-
return args.reduce(
|
|
6871
|
-
acc
|
|
6872
|
-
|
|
6873
|
-
|
|
6876
|
+
return args.reduce(
|
|
6877
|
+
(acc, arg) => {
|
|
6878
|
+
acc[arg] = url.searchParams.get(arg);
|
|
6879
|
+
return acc;
|
|
6880
|
+
},
|
|
6881
|
+
{}
|
|
6882
|
+
);
|
|
6874
6883
|
}
|
|
6875
6884
|
};
|
|
6876
6885
|
|
|
6877
6886
|
// src/transformers/transformers.ts
|
|
6878
|
-
var Transformers = class {
|
|
6887
|
+
var Transformers = class _Transformers {
|
|
6879
6888
|
static transformers = [
|
|
6880
6889
|
AttributeTransformer,
|
|
6881
6890
|
Exists,
|
|
@@ -6889,7 +6898,7 @@ var Transformers = class {
|
|
|
6889
6898
|
UrlQueryParamTransformer
|
|
6890
6899
|
];
|
|
6891
6900
|
static getByName(name) {
|
|
6892
|
-
return
|
|
6901
|
+
return _Transformers.transformers.find((tr) => tr.getName() === name);
|
|
6893
6902
|
}
|
|
6894
6903
|
};
|
|
6895
6904
|
|
|
@@ -6981,15 +6990,18 @@ var ObjectConfig = class _ObjectConfig extends ConfigWithSelector {
|
|
|
6981
6990
|
}
|
|
6982
6991
|
const props = this.properties;
|
|
6983
6992
|
const keys = Object.keys(props);
|
|
6984
|
-
return keys.reduce(
|
|
6985
|
-
|
|
6986
|
-
|
|
6987
|
-
|
|
6988
|
-
|
|
6989
|
-
|
|
6990
|
-
|
|
6991
|
-
|
|
6992
|
-
|
|
6993
|
+
return keys.reduce(
|
|
6994
|
+
(acc, key) => {
|
|
6995
|
+
const config2 = props[key];
|
|
6996
|
+
acc[key] = config2.extract({
|
|
6997
|
+
...params,
|
|
6998
|
+
$el,
|
|
6999
|
+
property: this.makeProperty(params.property, key)
|
|
7000
|
+
});
|
|
7001
|
+
return acc;
|
|
7002
|
+
},
|
|
7003
|
+
{}
|
|
7004
|
+
);
|
|
6993
7005
|
}
|
|
6994
7006
|
makeProperty(property, prop) {
|
|
6995
7007
|
if (property === rootProp) {
|
|
@@ -7009,21 +7021,20 @@ var UnionConfig = class _UnionConfig extends Config {
|
|
|
7009
7021
|
const $parent = params.$el;
|
|
7010
7022
|
for (const config2 of this.configs) {
|
|
7011
7023
|
if (config2 instanceof ConfigWithSelector) {
|
|
7012
|
-
const $
|
|
7024
|
+
const $matches = config2.getAllMatches($parent, {
|
|
7013
7025
|
alreadyMatched: false,
|
|
7014
7026
|
includeRoot: true
|
|
7015
7027
|
});
|
|
7016
|
-
if (!$
|
|
7028
|
+
if (!$matches || Array.isArray($matches) && $matches.length === 0)
|
|
7017
7029
|
continue;
|
|
7018
|
-
}
|
|
7019
7030
|
return config2.extract({
|
|
7020
7031
|
...params,
|
|
7021
|
-
$el,
|
|
7022
|
-
|
|
7032
|
+
$el: Array.isArray($matches) ? $matches.length > 1 ? $parent : $matches[0] : $matches,
|
|
7033
|
+
// arrays must be matched again
|
|
7034
|
+
elementAlreadyMatched: !Array.isArray($matches) || $matches.length === 1
|
|
7023
7035
|
});
|
|
7024
|
-
} else {
|
|
7025
|
-
return config2.extract({ ...params, $el: params.$el });
|
|
7026
7036
|
}
|
|
7037
|
+
return config2.extract({ ...params, $el: params.$el });
|
|
7027
7038
|
}
|
|
7028
7039
|
return null;
|
|
7029
7040
|
}
|
|
@@ -7209,7 +7220,7 @@ var ConfigValidator = class {
|
|
|
7209
7220
|
var ConfigFactory = class _ConfigFactory {
|
|
7210
7221
|
static fromYAML(yaml) {
|
|
7211
7222
|
const plain = (0, import_yaml.parse)(yaml);
|
|
7212
|
-
return
|
|
7223
|
+
return _ConfigFactory.generate(plain);
|
|
7213
7224
|
}
|
|
7214
7225
|
static generate(plain) {
|
|
7215
7226
|
const validator = new ConfigValidator(plain);
|
|
@@ -7236,24 +7247,28 @@ var ConfigFactory = class _ConfigFactory {
|
|
|
7236
7247
|
case "constant":
|
|
7237
7248
|
return ConstantConfig.generate(constant, selector);
|
|
7238
7249
|
case "object": {
|
|
7239
|
-
let propConfigs
|
|
7250
|
+
let propConfigs;
|
|
7240
7251
|
if (properties) {
|
|
7241
|
-
propConfigs = Object.keys(properties).reduce(
|
|
7242
|
-
acc
|
|
7243
|
-
|
|
7244
|
-
|
|
7252
|
+
propConfigs = Object.keys(properties).reduce(
|
|
7253
|
+
(acc, key) => {
|
|
7254
|
+
acc[key] = _ConfigFactory.generate(properties[key]);
|
|
7255
|
+
return acc;
|
|
7256
|
+
},
|
|
7257
|
+
{}
|
|
7258
|
+
);
|
|
7245
7259
|
}
|
|
7246
7260
|
return ObjectConfig.generate(selector, propConfigs);
|
|
7247
7261
|
}
|
|
7248
7262
|
case "array":
|
|
7249
7263
|
return ArrayConfig.generate(
|
|
7250
7264
|
selector,
|
|
7251
|
-
items &&
|
|
7265
|
+
items && _ConfigFactory.generate(items),
|
|
7252
7266
|
transform
|
|
7253
7267
|
);
|
|
7254
7268
|
case "union":
|
|
7255
7269
|
return UnionConfig.generate(
|
|
7256
|
-
union.
|
|
7270
|
+
// biome-ignore lint/style/noNonNullAssertion: union will be present here. because our schema already validated the input
|
|
7271
|
+
union.map((cfg) => _ConfigFactory.generate(cfg))
|
|
7257
7272
|
);
|
|
7258
7273
|
default:
|
|
7259
7274
|
return PrimitiveValueConfig.generate(selector, transform);
|
|
@@ -7321,9 +7336,9 @@ function extract(backend, $, config2, url) {
|
|
|
7321
7336
|
}
|
|
7322
7337
|
|
|
7323
7338
|
// examples/simple.ts
|
|
7324
|
-
var html = (0,
|
|
7339
|
+
var html = (0, import_node_fs.readFileSync)("examples/simple.html").toString();
|
|
7325
7340
|
var config = ConfigFactory.fromYAML(
|
|
7326
|
-
(0,
|
|
7341
|
+
(0, import_node_fs.readFileSync)("examples/simple.yaml").toString()
|
|
7327
7342
|
);
|
|
7328
7343
|
console.log(
|
|
7329
7344
|
"Simple parse result:",
|