From 6df0bb60f1b4520b9712488636882ba861023a1a Mon Sep 17 00:00:00 2001 From: Vas Sudanagunta Date: Tue, 14 Dec 2021 15:14:03 -0800 Subject: [PATCH] Highlight special `onclosetag` logic for `p` and `br` `Parser.ts` has special `onclosetag` logic for [`p`](https://github.com/fb55/htmlparser2/blob/6445c32b05dc070049eeaaf201bfc123daca3d37/src/Parser.ts#L336) and [`br`](https://github.com/fb55/htmlparser2/blob/6445c32b05dc070049eeaaf201bfc123daca3d37/src/Parser.ts#L340) tags. The updated tests in PR #1046 were unable to expose any behavioral differences, despite the different code paths taken by the existing special logic. So I decided it would be best to at least to clarify with an update to the existing comment in the code, as well as by using the literal values 'p' and 'br' instead of the variable `name` (which is guaranteed to have those values). --- src/Parser.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Parser.ts b/src/Parser.ts index cccd33f9e..bca168af1 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -334,14 +334,15 @@ export class Parser { } } else this.stack.length = pos; } else if (!this.options.xmlMode && name === "p") { - this.emitOpenTag(name); + // Implicit open before close + this.emitOpenTag("p"); this.closeCurrentTag(true); } } else if (!this.options.xmlMode && name === "br") { - // We can't go through `emitOpenTag` here, as `br` would be implicitly closed. - this.cbs.onopentagname?.(name); - this.cbs.onopentag?.(name, {}, true); - this.cbs.onclosetag?.(name, false); + // We can't use `emitOpenTag` for implicit open, as `br` would be implicitly closed. + this.cbs.onopentagname?.("br"); + this.cbs.onopentag?.("br", {}, true); + this.cbs.onclosetag?.("br", false); } // Set `startIndex` for next node