From 5dc30bf30820594fb5488e42c7aa33a6d0ea326d 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..3948e317 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/');