Skip to content

Commit 7841fc5

Browse files
author
Dolicraft
committed
Fix Dolibarr#38919 keep line breaks around bare < and > in description fields
When MAIN_RESTRICTHTML_ONLY_VALID_HTML is on, dol_htmlwithnojs() wrapped a plain text in <div> and ran it through DOMDocument::loadHTML. A lone < or > in the user text (e.g. 'from EUR2.00 > EUR2.50') was interpreted as a tag boundary, the chunk between them was treated as an unclosed element and the saveHTML() output lost the line breaks. Pre-escape lone < and > to &lt;/&gt; in the text-only branch before the DOM round-trip, so the parser keeps the content intact. Signed-off-by: Dolicraft <contact@dolicraft.com>
1 parent 5941291 commit 7841fc5

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

htdocs/core/lib/functions.lib.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7470,7 +7470,11 @@ function dol_htmlwithnojs($stringtoencode, $nouseofiframesandbox = 0, $check = '
74707470
if (dol_textishtml($out)) {
74717471
$out = '<?xml encoding="UTF-8"><html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body><div class="tricktoremove">'.$out.'</div></body></html>';
74727472
} else {
7473-
$out = '<?xml encoding="UTF-8"><html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body><div class="tricktoremove">'.dol_nl2br($out).'</div></body></html>';
7473+
// Pre-escape lone < and > that are not the start of a real tag, otherwise DOMDocument
7474+
// eats everything between them (e.g. "from EUR2.00 > EUR2.50" gets stripped on save).
7475+
$plainout = preg_replace('/<(?![a-zA-Z\/!?])/', '&lt;', $out);
7476+
$plainout = preg_replace('/(^|[^a-zA-Z0-9"\'\/])>/', '$1&gt;', $plainout);
7477+
$out = '<?xml encoding="UTF-8"><html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body><div class="tricktoremove">'.dol_nl2br($plainout).'</div></body></html>';
74747478
}
74757479

74767480
$dom->loadHTML($out, LIBXML_HTML_NODEFDTD | LIBXML_ERR_NONE | LIBXML_HTML_NOIMPLIED | LIBXML_NONET | LIBXML_NOWARNING | LIBXML_NOERROR | LIBXML_NOXMLDECL);

0 commit comments

Comments
 (0)