From 417273187ca00259b67391c85f1beef4462b89da Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 27 Feb 2024 10:55:03 +0400 Subject: [PATCH] Remove repeating br tags from html before render (#288) --- src/flat_layout/flat_html.ts | 15 +++++++++++++-- tests/flat_html.test.ts | 16 +++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/flat_layout/flat_html.ts b/src/flat_layout/flat_html.ts index a283413..16034f2 100644 --- a/src/flat_layout/flat_html.ts +++ b/src/flat_layout/flat_html.ts @@ -5,6 +5,7 @@ import { FlatQuestion } from './flat_question'; import { FlatRepository } from './flat_repository'; import { IPdfBrick } from '../pdf_render/pdf_brick'; import { SurveyHelper } from '../helper_survey'; +import { replace } from 'lodash'; export type IHTMLRenderType = 'auto' | 'standard' | 'image'; export class FlatHTML extends FlatQuestion { @@ -21,6 +22,16 @@ export class FlatHTML extends FlatQuestion { } return 'standard'; } + + private static correctHtmlRules: [{ searchRegExp: RegExp, replaceString: string }] = [ + { searchRegExp: /(<\/?br\s*?\/?\s*?>\s*){2,}/, replaceString: '
' } + ] + protected correctHtml(html: string): string { + FlatHTML.correctHtmlRules.forEach((rule) => { + html = html.replace(rule.searchRegExp, rule.replaceString); + }); + return html; + } public async generateFlatsContent(point: IPoint): Promise { let renderAs: IHTMLRenderType = this.question.renderAs; if (renderAs === 'auto') renderAs = this.controller.htmlRenderAs; @@ -29,12 +40,12 @@ export class FlatHTML extends FlatQuestion { if (renderAs === 'image') { const width: number = SurveyHelper.getPageAvailableWidth(this.controller); const { url, aspect }: { url: string, aspect: number } = - await SurveyHelper.htmlToImage(html, width, this.controller); + await SurveyHelper.htmlToImage(html, width, this.controller); const height: number = width / aspect; return [await SurveyHelper.createImageFlat(point, this.question, this.controller, { link: url, width, height })]; } return [SurveyHelper.splitHtmlRect(this.controller, await SurveyHelper.createHTMLFlat( - point, this.question, this.controller, html))]; + point, this.question, this.controller, this.correctHtml(html)))]; } } diff --git a/tests/flat_html.test.ts b/tests/flat_html.test.ts index 9ff88db..8d39541 100644 --- a/tests/flat_html.test.ts +++ b/tests/flat_html.test.ts @@ -2,7 +2,7 @@ return {}; }; -import { Question } from 'survey-core'; +import { Question, QuestionHtmlModel } from 'survey-core'; import { SurveyPDF } from '../src/survey'; import { IPoint, IRect, DocController, IDocOptions } from '../src/doc_controller'; import { FlatSurvey } from '../src/flat_layout/flat_survey'; @@ -77,4 +77,18 @@ test('Check createHTMLRect method with long html', async () => { controller.helperDoc.addPage(); actualRect = SurveyHelper.createHTMLRect(descPoint, controller, margins, result); expect(actualRect.yBot - actualRect.yTop).toBeCloseTo(427.2, 8); +}); + +test('Check correctHtml method with multiple br tags', async () => { + let survey: SurveyPDF = new SurveyPDF({}, TestHelper.defaultOptions); + SurveyHelper.shouldConvertImageToPng = false; + let controller: DocController = new DocController(TestHelper.defaultOptions); + const htmlFlat = new FlatHTML(survey, new QuestionHtmlModel('q1'), controller); + expect(htmlFlat['correctHtml']('Test
')).toEqual('Test
'); + expect(htmlFlat['correctHtml']('Test

')).toEqual('Test
'); + expect(htmlFlat['correctHtml']('Test

')).toEqual('Test
'); + expect(htmlFlat['correctHtml']('
Test
')).toEqual('
Test
'); + expect(htmlFlat['correctHtml']('

')).toEqual('
'); + expect(htmlFlat['correctHtml']('

')).toEqual('
'); + expect(htmlFlat['correctHtml']('

')).toEqual('
'); }); \ No newline at end of file