From 15e3e9ee3bdb2e90c74d56a12936a4ca6e36553d Mon Sep 17 00:00:00 2001 From: zhaoyi Date: Tue, 28 Jun 2022 11:47:34 +0800 Subject: [PATCH] fix: escape html tags in order to solve html reporter rendering problems --- packages/html-reporter/src/index.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packages/html-reporter/src/index.ts b/packages/html-reporter/src/index.ts index d15fac64..6713e3f7 100644 --- a/packages/html-reporter/src/index.ts +++ b/packages/html-reporter/src/index.ts @@ -4,6 +4,23 @@ import {IReporter, JsonReporter} from "@jscpd/finder"; import {copySync, writeFileSync, readFileSync} from "fs-extra"; import {green, red} from "colors/safe"; +function escapeHtml(value) { + if (typeof value !== 'string') { + return value; + } + return value.replace(/[&<>`"'\/]/g, function(result) { + return { + '&': '&', + '<': '<', + '>': '>', + '`': '`', + '"': '"', + "'": ''', + '/': '/', + }[result]; + }) +} + export default class HtmlReporter implements IReporter { constructor(private options: IOptions) { } @@ -11,6 +28,9 @@ export default class HtmlReporter implements IReporter { public report(clones: IClone[], statistic: IStatistic): void { const jsonReporter = new JsonReporter(this.options); const json = jsonReporter.generateJson(clones, statistic); + json.duplicates.forEach(item => { + item.fragment = escapeHtml(item.fragment); + }); if (this.options.output) { const src = join(__dirname, '../html/'); const destination = join(this.options.output, 'html/');