• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nodeplusplus / xregex-parser / 180

pending completion
180

Pull #17

circleci

Unknown Committer
Unknown Commit Message
Pull Request #17: Bump json5 from 2.1.2 to 2.2.3

160 of 160 branches covered (100.0%)

Branch coverage included in aggregate %.

171 of 171 relevant lines covered (100.0%)

8.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/engines/HTML.ts
1
import _ from "lodash";
3✔
2
import cheerio from "cheerio";
3✔
3
import { injectable, inject } from "inversify";
3✔
4
import { ILogger } from "@nodeplusplus/xregex-logger";
5

6
import {
3✔
7
  XParserReserved,
8
  IXParser,
9
  IXParserExecOpts,
10
  IXParserSchemaItem,
11
} from "../types/XParser";
12

13
@injectable()
14
export class HTMLParser implements IXParser {
3✔
15
  @inject("LOGGER") private logger!: ILogger;
3✔
16

17
  public async start() {
18
    this.logger.info("XPARSER:ENGINE.HTML.STARTED");
4✔
19
  }
20
  public async stop() {
21
    this.logger.info("XPARSER:ENGINE.HTML.STOPPED");
4✔
22
  }
23

24
  public async exec<T = any>(
25
    data: any,
26
    opts: IXParserExecOpts
27
  ): Promise<T | T[]> {
28
    if (!data || typeof data !== "string") return "" as any;
15✔
29

30
    if (!opts.schema?.ref && !opts.schema?.selector) {
11✔
31
      throw new Error("XPARSER:ENGINE.HTML.NO_REF_AND_SELECTOR");
2✔
32
    }
33
    const schema = opts.schema as IXParserSchemaItem;
9✔
34

35
    const ref = opts.ref;
9✔
36
    // 1. Return reference data
37
    if (schema.ref) return _.get(opts.ref, schema.ref);
9✔
38

39
    // 2. Return root data
40
    if (schema.selector === XParserReserved.ROOT_SELECTOR) return data as any;
8✔
41

42
    const $ = cheerio.load(data);
7✔
43

44
    // 3.1 Remove redundant elements
45
    if (schema.unselector) {
7✔
46
      const unselector = _.template(schema.unselector)({ ...ref });
1✔
47
      $(unselector).remove();
1✔
48
    }
49
    // 3.2 We want to get raw HTML intead of property
50
    if (!schema.prop) {
7✔
51
      return $(schema.selector)
1✔
52
        .toArray()
53
        .map((e: any) => $.html(e)) as any[];
1✔
54
    }
55
    // 3.3 Count items;
56
    if (schema.prop === XParserReserved.PROP_LENGTH) {
6✔
57
      return $(schema.selector).length as any;
1✔
58
    }
59

60
    const dom = $.root().find(schema.selector as string);
5✔
61
    if (!dom.length) return "" as any;
5✔
62

63
    // 3.4 Fix missing some values of .prop()
64
    if (schema.prop === XParserReserved.PROP_TEXT) return dom.text() as any;
3✔
65

66
    return (dom.prop(schema.prop) || "") as any;
2✔
67
  }
68

69
  public async clean<T = any>(data: any, selectors: string[]): Promise<T> {
70
    if (!Array.isArray(selectors) || !selectors.length) return data;
7✔
71
    if (!data || typeof data !== "string") return data;
4✔
72

73
    const $ = cheerio.load(data);
2✔
74
    $(selectors.join(",")).remove();
2✔
75
    return $.html() as any;
2✔
76
  }
77
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc