@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.
- package/README.md +2 -6
- package/dist/examples/simple.js +135 -75
- package/dist/examples/simple.js.map +1 -1
- package/dist/src/index.d.ts +46 -4
- package/dist/src/index.js +136 -67
- package/dist/src/index.js.map +1 -1
- package/package.json +2 -1
- package/src/config-schema.json +1 -1
package/README.md
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Typescript Implementation of the PureHTML
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
You need to install [NodeJS](https://nodejs.org) first, then follow the steps explained in the documentation.
|
|
6
|
-
|
|
7
|
-
See [https://purescraps.github.io/purehtml/](https://purescraps.github.io/purehtml/) for documentation and playground.
|
|
3
|
+
Supported platforms: browser, node.js and -probably- any other platform that supports executing TypeScript.
|
package/dist/examples/simple.js
CHANGED
|
@@ -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/
|
|
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
|
-
|
|
6862
|
+
getAllMatches($, { alreadyMatched, includeRoot }) {
|
|
6809
6863
|
if (alreadyMatched) {
|
|
6810
|
-
return
|
|
6864
|
+
return $;
|
|
6811
6865
|
}
|
|
6812
6866
|
if (this.selector === null) {
|
|
6813
|
-
return
|
|
6867
|
+
return $;
|
|
6814
6868
|
}
|
|
6815
|
-
if (
|
|
6816
|
-
return
|
|
6869
|
+
if (includeRoot && $.is(this.selector)) {
|
|
6870
|
+
return $;
|
|
6817
6871
|
}
|
|
6818
|
-
return
|
|
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;
|
|
6878
|
+
}
|
|
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.
|
|
6831
|
-
params
|
|
6832
|
-
|
|
6833
|
-
);
|
|
6834
|
-
if ($el
|
|
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.
|
|
6882
|
-
params
|
|
6883
|
-
|
|
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
|
|
6949
|
+
return matches.map((el, i) => {
|
|
6888
6950
|
return conf.extract({
|
|
6889
6951
|
...extractOpts,
|
|
6890
|
-
$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
|
-
|
|
6929
|
-
|
|
6930
|
-
|
|
6931
|
-
|
|
6932
|
-
|
|
6933
|
-
|
|
6934
|
-
|
|
6935
|
-
|
|
6936
|
-
return acc;
|
|
6937
|
-
}, {});
|
|
6990
|
+
if (!$el) {
|
|
6991
|
+
return null;
|
|
6992
|
+
}
|
|
6993
|
+
if (args.length === 0) {
|
|
6994
|
+
return $el.attr();
|
|
6995
|
+
}
|
|
6996
|
+
if (args.length === 1) {
|
|
6997
|
+
return _AttributeTransformer.getAttrValue($el, args[0]);
|
|
6938
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
|
|
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
|
|
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() {
|
|
@@ -7039,6 +7087,9 @@ var NumberTransformer = class extends Transformer {
|
|
|
7039
7087
|
if (typeof val === "string") {
|
|
7040
7088
|
return Number(val);
|
|
7041
7089
|
}
|
|
7090
|
+
if (val === null) {
|
|
7091
|
+
return null;
|
|
7092
|
+
}
|
|
7042
7093
|
throw new InvalidParseInputError(
|
|
7043
7094
|
property,
|
|
7044
7095
|
`NumberTransformer.transform: invalid value type: ${typeof val}. value=${JSON.stringify(
|
|
@@ -7133,13 +7184,6 @@ var UrlQueryParamTransformer = class _UrlQueryParamTransformer extends Transform
|
|
|
7133
7184
|
return acc;
|
|
7134
7185
|
}, {});
|
|
7135
7186
|
}
|
|
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
7187
|
};
|
|
7144
7188
|
|
|
7145
7189
|
// src/transformers/transformers.ts
|
|
@@ -7149,7 +7193,6 @@ var Transformers = class {
|
|
|
7149
7193
|
Exists,
|
|
7150
7194
|
HTMLTransformer,
|
|
7151
7195
|
Length,
|
|
7152
|
-
MatchCount,
|
|
7153
7196
|
NumberTransformer,
|
|
7154
7197
|
ResolveTransformer,
|
|
7155
7198
|
Trim,
|
|
@@ -7199,16 +7242,27 @@ var TransformerFactory = class _TransformerFactory {
|
|
|
7199
7242
|
};
|
|
7200
7243
|
|
|
7201
7244
|
// src/config/types/constant.ts
|
|
7202
|
-
var ConstantConfig = class _ConstantConfig extends
|
|
7203
|
-
constructor(val) {
|
|
7245
|
+
var ConstantConfig = class _ConstantConfig extends ConfigWithSelector {
|
|
7246
|
+
constructor(val, selector) {
|
|
7204
7247
|
super();
|
|
7205
7248
|
this.val = val;
|
|
7249
|
+
this.selector = selector;
|
|
7206
7250
|
}
|
|
7207
|
-
extract() {
|
|
7251
|
+
extract(params) {
|
|
7252
|
+
if (this.selector) {
|
|
7253
|
+
const matches = this.getAllMatches(params.$el, {
|
|
7254
|
+
alreadyMatched: false,
|
|
7255
|
+
includeRoot: true
|
|
7256
|
+
});
|
|
7257
|
+
if (Array.isArray(matches) && matches.length === 0) {
|
|
7258
|
+
return null;
|
|
7259
|
+
}
|
|
7260
|
+
return this.val;
|
|
7261
|
+
}
|
|
7208
7262
|
return this.val;
|
|
7209
7263
|
}
|
|
7210
|
-
static generate(val) {
|
|
7211
|
-
return new _ConstantConfig(val);
|
|
7264
|
+
static generate(val, selector) {
|
|
7265
|
+
return new _ConstantConfig(val, selector);
|
|
7212
7266
|
}
|
|
7213
7267
|
};
|
|
7214
7268
|
|
|
@@ -7228,11 +7282,11 @@ var ObjectConfig = class _ObjectConfig extends ConfigWithSelector {
|
|
|
7228
7282
|
}
|
|
7229
7283
|
extract(params) {
|
|
7230
7284
|
const $parent = params.$el;
|
|
7231
|
-
const $el = this.
|
|
7232
|
-
|
|
7233
|
-
|
|
7234
|
-
);
|
|
7235
|
-
if (
|
|
7285
|
+
const $el = this.getFirstMatch($parent, {
|
|
7286
|
+
alreadyMatched: params.elementAlreadyMatched ?? false,
|
|
7287
|
+
includeRoot: false
|
|
7288
|
+
});
|
|
7289
|
+
if (!$el) {
|
|
7236
7290
|
return null;
|
|
7237
7291
|
}
|
|
7238
7292
|
const props = this.properties;
|
|
@@ -7265,8 +7319,11 @@ var UnionConfig = class _UnionConfig extends Config {
|
|
|
7265
7319
|
const $parent = params.$el;
|
|
7266
7320
|
for (const config2 of this.configs) {
|
|
7267
7321
|
if (config2 instanceof ConfigWithSelector) {
|
|
7268
|
-
const $el = config2.
|
|
7269
|
-
|
|
7322
|
+
const $el = config2.getFirstMatch($parent, {
|
|
7323
|
+
alreadyMatched: false,
|
|
7324
|
+
includeRoot: true
|
|
7325
|
+
});
|
|
7326
|
+
if (!$el) {
|
|
7270
7327
|
continue;
|
|
7271
7328
|
}
|
|
7272
7329
|
return config2.extract({
|
|
@@ -7275,7 +7332,7 @@ var UnionConfig = class _UnionConfig extends Config {
|
|
|
7275
7332
|
elementAlreadyMatched: true
|
|
7276
7333
|
});
|
|
7277
7334
|
} else {
|
|
7278
|
-
return config2.extract({ ...params, $el: params.$
|
|
7335
|
+
return config2.extract({ ...params, $el: params.$el });
|
|
7279
7336
|
}
|
|
7280
7337
|
}
|
|
7281
7338
|
return null;
|
|
@@ -7433,7 +7490,7 @@ var oneOf = [
|
|
|
7433
7490
|
{
|
|
7434
7491
|
$comment: "Constant",
|
|
7435
7492
|
type: "object",
|
|
7436
|
-
properties: { constant: {} },
|
|
7493
|
+
properties: { selector: { type: "string" }, constant: {} },
|
|
7437
7494
|
required: ["constant"],
|
|
7438
7495
|
additionalProperties: false
|
|
7439
7496
|
}
|
|
@@ -7487,7 +7544,7 @@ var ConfigFactory = class _ConfigFactory {
|
|
|
7487
7544
|
const transform = transformOrig ? _ConfigFactory.generateTransform(transformOrig) : void 0;
|
|
7488
7545
|
switch (expectedType) {
|
|
7489
7546
|
case "constant":
|
|
7490
|
-
return ConstantConfig.generate(constant);
|
|
7547
|
+
return ConstantConfig.generate(constant, selector);
|
|
7491
7548
|
case "object": {
|
|
7492
7549
|
let propConfigs = void 0;
|
|
7493
7550
|
if (properties) {
|
|
@@ -7559,10 +7616,11 @@ var ConfigFactory = class _ConfigFactory {
|
|
|
7559
7616
|
};
|
|
7560
7617
|
|
|
7561
7618
|
// src/index.ts
|
|
7562
|
-
|
|
7563
|
-
|
|
7619
|
+
var cheerio = new PureHTMLCheerioBackend();
|
|
7620
|
+
function extract(backend, $, config2, url) {
|
|
7621
|
+
let $root = $;
|
|
7564
7622
|
if (typeof $root === "string") {
|
|
7565
|
-
$root =
|
|
7623
|
+
$root = backend.load($root);
|
|
7566
7624
|
}
|
|
7567
7625
|
return config2.extract({
|
|
7568
7626
|
$: $root,
|
|
@@ -7577,8 +7635,10 @@ var html = (0, import_fs.readFileSync)("examples/simple.html").toString();
|
|
|
7577
7635
|
var config = ConfigFactory.fromYAML(
|
|
7578
7636
|
(0, import_fs.readFileSync)("examples/simple.yaml").toString()
|
|
7579
7637
|
);
|
|
7580
|
-
|
|
7581
|
-
|
|
7638
|
+
console.log(
|
|
7639
|
+
"Simple parse result:",
|
|
7640
|
+
extract(cheerio, html, config, "https://example.com")
|
|
7641
|
+
);
|
|
7582
7642
|
/*! Bundled license information:
|
|
7583
7643
|
|
|
7584
7644
|
uri-js/dist/es5/uri.all.js:
|