@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.
- package/dist/examples/simple.js +132 -75
- package/dist/examples/simple.js.map +1 -1
- package/dist/src/index.d.ts +46 -4
- package/dist/src/index.js +133 -67
- package/dist/src/index.js.map +1 -1
- package/package.json +1 -1
- package/src/config-schema.json +1 -1
package/dist/src/index.d.ts
CHANGED
|
@@ -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
|
-
$:
|
|
6
|
-
$el:
|
|
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
|
|
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
|
-
|
|
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.
|
|
6835
|
-
params
|
|
6836
|
-
|
|
6837
|
-
);
|
|
6838
|
-
if ($el
|
|
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.
|
|
6886
|
-
params
|
|
6887
|
-
|
|
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
|
|
6960
|
+
return matches.map((el, i) => {
|
|
6892
6961
|
return conf.extract({
|
|
6893
6962
|
...extractOpts,
|
|
6894
|
-
$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
|
-
|
|
6933
|
-
|
|
6934
|
-
|
|
6935
|
-
|
|
6936
|
-
|
|
6937
|
-
|
|
6938
|
-
|
|
6939
|
-
|
|
6940
|
-
return acc;
|
|
6941
|
-
}, {});
|
|
7001
|
+
if (!$el) {
|
|
7002
|
+
return null;
|
|
7003
|
+
}
|
|
7004
|
+
if (args.length === 0) {
|
|
7005
|
+
return $el.attr();
|
|
7006
|
+
}
|
|
7007
|
+
if (args.length === 1) {
|
|
7008
|
+
return _AttributeTransformer.getAttrValue($el, args[0]);
|
|
6942
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
|
|
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
|
|
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() {
|
|
@@ -7137,13 +7192,6 @@ var UrlQueryParamTransformer = class _UrlQueryParamTransformer extends Transform
|
|
|
7137
7192
|
return acc;
|
|
7138
7193
|
}, {});
|
|
7139
7194
|
}
|
|
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
7195
|
};
|
|
7148
7196
|
|
|
7149
7197
|
// src/transformers/transformers.ts
|
|
@@ -7153,7 +7201,6 @@ var Transformers = class {
|
|
|
7153
7201
|
Exists,
|
|
7154
7202
|
HTMLTransformer,
|
|
7155
7203
|
Length,
|
|
7156
|
-
MatchCount,
|
|
7157
7204
|
NumberTransformer,
|
|
7158
7205
|
ResolveTransformer,
|
|
7159
7206
|
Trim,
|
|
@@ -7203,16 +7250,27 @@ var TransformerFactory = class _TransformerFactory {
|
|
|
7203
7250
|
};
|
|
7204
7251
|
|
|
7205
7252
|
// src/config/types/constant.ts
|
|
7206
|
-
var ConstantConfig = class _ConstantConfig extends
|
|
7207
|
-
constructor(val) {
|
|
7253
|
+
var ConstantConfig = class _ConstantConfig extends ConfigWithSelector {
|
|
7254
|
+
constructor(val, selector) {
|
|
7208
7255
|
super();
|
|
7209
7256
|
this.val = val;
|
|
7257
|
+
this.selector = selector;
|
|
7210
7258
|
}
|
|
7211
|
-
extract() {
|
|
7259
|
+
extract(params) {
|
|
7260
|
+
if (this.selector) {
|
|
7261
|
+
const matches = this.getAllMatches(params.$el, {
|
|
7262
|
+
alreadyMatched: false,
|
|
7263
|
+
includeRoot: true
|
|
7264
|
+
});
|
|
7265
|
+
if (Array.isArray(matches) && matches.length === 0) {
|
|
7266
|
+
return null;
|
|
7267
|
+
}
|
|
7268
|
+
return this.val;
|
|
7269
|
+
}
|
|
7212
7270
|
return this.val;
|
|
7213
7271
|
}
|
|
7214
|
-
static generate(val) {
|
|
7215
|
-
return new _ConstantConfig(val);
|
|
7272
|
+
static generate(val, selector) {
|
|
7273
|
+
return new _ConstantConfig(val, selector);
|
|
7216
7274
|
}
|
|
7217
7275
|
};
|
|
7218
7276
|
|
|
@@ -7232,11 +7290,11 @@ var ObjectConfig = class _ObjectConfig extends ConfigWithSelector {
|
|
|
7232
7290
|
}
|
|
7233
7291
|
extract(params) {
|
|
7234
7292
|
const $parent = params.$el;
|
|
7235
|
-
const $el = this.
|
|
7236
|
-
|
|
7237
|
-
|
|
7238
|
-
);
|
|
7239
|
-
if (
|
|
7293
|
+
const $el = this.getFirstMatch($parent, {
|
|
7294
|
+
alreadyMatched: params.elementAlreadyMatched ?? false,
|
|
7295
|
+
includeRoot: false
|
|
7296
|
+
});
|
|
7297
|
+
if (!$el) {
|
|
7240
7298
|
return null;
|
|
7241
7299
|
}
|
|
7242
7300
|
const props = this.properties;
|
|
@@ -7269,8 +7327,11 @@ var UnionConfig = class _UnionConfig extends Config {
|
|
|
7269
7327
|
const $parent = params.$el;
|
|
7270
7328
|
for (const config of this.configs) {
|
|
7271
7329
|
if (config instanceof ConfigWithSelector) {
|
|
7272
|
-
const $el = config.
|
|
7273
|
-
|
|
7330
|
+
const $el = config.getFirstMatch($parent, {
|
|
7331
|
+
alreadyMatched: false,
|
|
7332
|
+
includeRoot: true
|
|
7333
|
+
});
|
|
7334
|
+
if (!$el) {
|
|
7274
7335
|
continue;
|
|
7275
7336
|
}
|
|
7276
7337
|
return config.extract({
|
|
@@ -7279,7 +7340,7 @@ var UnionConfig = class _UnionConfig extends Config {
|
|
|
7279
7340
|
elementAlreadyMatched: true
|
|
7280
7341
|
});
|
|
7281
7342
|
} else {
|
|
7282
|
-
return config.extract({ ...params, $el: params.$
|
|
7343
|
+
return config.extract({ ...params, $el: params.$el });
|
|
7283
7344
|
}
|
|
7284
7345
|
}
|
|
7285
7346
|
return null;
|
|
@@ -7437,7 +7498,7 @@ var oneOf = [
|
|
|
7437
7498
|
{
|
|
7438
7499
|
$comment: "Constant",
|
|
7439
7500
|
type: "object",
|
|
7440
|
-
properties: { constant: {} },
|
|
7501
|
+
properties: { selector: { type: "string" }, constant: {} },
|
|
7441
7502
|
required: ["constant"],
|
|
7442
7503
|
additionalProperties: false
|
|
7443
7504
|
}
|
|
@@ -7491,7 +7552,7 @@ var ConfigFactory = class _ConfigFactory {
|
|
|
7491
7552
|
const transform = transformOrig ? _ConfigFactory.generateTransform(transformOrig) : void 0;
|
|
7492
7553
|
switch (expectedType) {
|
|
7493
7554
|
case "constant":
|
|
7494
|
-
return ConstantConfig.generate(constant);
|
|
7555
|
+
return ConstantConfig.generate(constant, selector);
|
|
7495
7556
|
case "object": {
|
|
7496
7557
|
let propConfigs = void 0;
|
|
7497
7558
|
if (properties) {
|
|
@@ -7563,10 +7624,11 @@ var ConfigFactory = class _ConfigFactory {
|
|
|
7563
7624
|
};
|
|
7564
7625
|
|
|
7565
7626
|
// src/index.ts
|
|
7566
|
-
|
|
7627
|
+
var cheerio = new PureHTMLCheerioBackend();
|
|
7628
|
+
function extract(backend, $, config, url) {
|
|
7567
7629
|
let $root = $;
|
|
7568
7630
|
if (typeof $root === "string") {
|
|
7569
|
-
$root =
|
|
7631
|
+
$root = backend.load($root);
|
|
7570
7632
|
}
|
|
7571
7633
|
return config.extract({
|
|
7572
7634
|
$: $root,
|
|
@@ -7579,6 +7641,10 @@ function extract($, config, url) {
|
|
|
7579
7641
|
0 && (module.exports = {
|
|
7580
7642
|
Config,
|
|
7581
7643
|
ConfigFactory,
|
|
7644
|
+
PureHTMLBackend,
|
|
7645
|
+
PureHTMLDocument,
|
|
7646
|
+
PureHTMLNode,
|
|
7647
|
+
cheerio,
|
|
7582
7648
|
extract
|
|
7583
7649
|
});
|
|
7584
7650
|
/*! Bundled license information:
|