From 6d8a2ff972923ff54607d0fc19fef6c754185e62 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Mon, 24 Aug 2020 18:08:43 +0800 Subject: [PATCH] Improve Coverage (#540) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feedhandler: overcome lint warning * test(WritableStream): cover else branch * test(Parser): cover (open-implies-close case of) `onclosetag` else branch with non-`br` void close tag in non-XML mode * refactor: improve else coverage by avoiding `openImpliesClose` optional chaining operator (check for existence already made upon entering block) * refactor: improve coverage by avoiding null check for `endIndex` (since by calling `_updatePosition`, it will never be `null`). Cast endIndex as number to avoid TS believing it could be null. * test(index): check APIs * Move test case to event Co-authored-by: Felix Böhm --- src/FeedHandler.ts | 2 +- src/Parser.ts | 6 ++-- src/WritableStream.spec.ts | 1 + .../Events/35-non-br-void-close-tag.json | 33 +++++++++++++++++++ src/index.spec.ts | 12 ++++++- 5 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 src/__fixtures__/Events/35-non-br-void-close-tag.json diff --git a/src/FeedHandler.ts b/src/FeedHandler.ts index 8337281f1..e47441c46 100644 --- a/src/FeedHandler.ts +++ b/src/FeedHandler.ts @@ -179,7 +179,7 @@ function addConditionally( recurse = false ) { const tmp = fetch(what, where, recurse); - if (tmp) obj[prop] = tmp as any; + if (tmp) obj[prop] = tmp as unknown as T[keyof T]; } function isValidFeed(value: string) { diff --git a/src/Parser.ts b/src/Parser.ts index 49cf965c8..be0de5837 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -225,9 +225,7 @@ export class Parser extends EventEmitter { //Tokenizer event handlers ontext(data: string) { this._updatePosition(1); - if (this.endIndex !== null) { - this.endIndex--; - } + (this.endIndex as number)--; this._cbs.ontext?.(data); } @@ -242,7 +240,7 @@ export class Parser extends EventEmitter { ) { for ( let el; - openImpliesClose[name]?.has( + openImpliesClose[name].has( (el = this._stack[this._stack.length - 1]) ); this.onclosetag(el) diff --git a/src/WritableStream.spec.ts b/src/WritableStream.spec.ts index d0d31f643..23e97acdc 100644 --- a/src/WritableStream.spec.ts +++ b/src/WritableStream.spec.ts @@ -7,6 +7,7 @@ describe("WritableStream", () => { stream.write(Buffer.from([0xe2, 0x82])); stream.write(Buffer.from([0xac])); + stream.write(''); stream.end(); expect(ontext).toBeCalledWith("€"); diff --git a/src/__fixtures__/Events/35-non-br-void-close-tag.json b/src/__fixtures__/Events/35-non-br-void-close-tag.json new file mode 100644 index 000000000..edc473b36 --- /dev/null +++ b/src/__fixtures__/Events/35-non-br-void-close-tag.json @@ -0,0 +1,33 @@ +{ + "name": "open-implies-close case of (non-br) void close tag in non-XML mode", + "options": { + "parser": { "lowerCaseAttributeNames": true } + }, + "html": "", + "expected": [ + { + "event": "opentagname", + "data": ["select"] + }, + { + "event": "opentag", + "data": ["select", {}] + }, + { + "event": "closetag", + "data": ["select"] + }, + { + "event": "opentagname", + "data": ["input"] + }, + { + "event": "opentag", + "data": ["input", {}] + }, + { + "event": "closetag", + "data": ["input"] + } + ] +} diff --git a/src/index.spec.ts b/src/index.spec.ts index 1f0dc0405..f257fa83c 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -1,4 +1,7 @@ -import { parseDOM, createDomStream } from "."; +import { + parseDOM, createDomStream, DomHandler, DefaultHandler, RssHandler +} from "."; +import { FeedHandler } from "./FeedHandler"; import { Element } from "domhandler"; // Add an `attributes` prop to the Element for now, to make it possible for Jest to render DOM nodes. @@ -33,4 +36,11 @@ describe("Index", () => { domStream.end(); }); + + describe("API", () => { + it("should export the appropriate APIs", () => { + expect(RssHandler).toEqual(FeedHandler); + expect(DomHandler).toEqual(DefaultHandler); + }); + }); });