Skip to content

Commit

Permalink
fix: escape html tags in order to solve html reporter rendering problems
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoyi committed Jun 28, 2022
1 parent d5d8a40 commit 5dc30bf
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/html-reporter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,33 @@ 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 {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'`': '&#x60;',
'"': '&quot;',
"'": '&#x27;',
'/': '&#x2f;',
}[result];
})
}

export default class HtmlReporter implements IReporter {
constructor(private options: IOptions) {
}

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/');
Expand Down

0 comments on commit 5dc30bf

Please sign in to comment.