Skip to content

Commit 46995de

Browse files
henry40408Exilz
authored andcommitted
fix(HTML): not to strip line breaks in pre-formatted tags
1 parent 2b9fb11 commit 46995de

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/HTML.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
IGNORED_TAGS,
1010
TEXT_TAGS_IGNORING_ASSOCIATION,
1111
STYLESETS,
12-
TextOnlyPropTypes
12+
TextOnlyPropTypes,
13+
PREFORMATTED_TAGS
1314
} from './HTMLUtils';
1415
import { generateDefaultBlockStyles, generateDefaultTextStyles } from './HTMLDefaultStyles';
1516
import htmlparser2 from 'htmlparser2';
@@ -274,10 +275,20 @@ export default class HTML extends PureComponent {
274275
// This is blank, don't render an useless additional component
275276
return false;
276277
}
278+
279+
if (
280+
node.parent &&
281+
node.parent.name &&
282+
PREFORMATTED_TAGS.indexOf(node.parent.name) === -1
283+
) {
284+
// Remove line breaks in non-pre-formatted tags
285+
data = data.replace(/(\r\n|\n|\r)/gm, '');
286+
}
287+
277288
// Text without tags, these can be mapped to the Text wrapper
278289
return {
279290
wrapper: 'Text',
280-
data: data.replace(/(\r\n|\n|\r)/gm, ''), // remove linebreaks
291+
data: data,
281292
attribs: attribs || {},
282293
parent,
283294
parentTag: parent && parent.name,

src/HTMLUtils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ export const TEXT_TAGS = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'figcaption', 'p',
2020
'dfn', 'i', 'kbd', 'mark', 'q', 'rt', 's', 'samp', 'small', 'big', 'span', 'strong', 'sub', 'sup', 'time', 'u', 'var', 'wbr',
2121
'del', 'ins', 'blink', 'font', 'em', 'bold', 'br'];
2222

23+
// Text in these tags should not be stripped from line breaks
24+
export const PREFORMATTED_TAGS = ['pre'];
25+
2326
// These tags can either be mapped to View or Text wrappers, depending solely on their children
2427
export const MIXED_TAGS = ['a'];
2528

0 commit comments

Comments
 (0)