Skip to content

Commit 6036b90

Browse files
authored
fix(newlines). Remove newline chars in text nodes (\n) when parsing HTML (#478)
Fixes #333
1 parent 6969f5c commit 6036b90

File tree

4 files changed

+93
-238
lines changed

4 files changed

+93
-238
lines changed

src/js/parsers/section.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ const SKIPPABLE_ELEMENT_TAG_NAMES = [
4444
'style', 'head', 'title', 'meta'
4545
].map(normalizeTagName);
4646

47+
const NEWLINES = /\n/g;
48+
function sanitize(text) {
49+
text = text.replace(NEWLINES, '');
50+
return text;
51+
}
52+
4753
/**
4854
* parses an element into a section, ignoring any non-markup
4955
* elements contained within
@@ -167,7 +173,7 @@ class SectionParser {
167173

168174
parseTextNode(textNode) {
169175
let { state } = this;
170-
state.text += textNode.textContent;
176+
state.text += sanitize(textNode.textContent);
171177
}
172178

173179
_updateStateFromElement(element) {

tests/helpers/post-abstract.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,7 @@ function parseTextIntoMarkers(text, builder) {
113113
markers = markers.concat( parseTextIntoMarkers(piece, builder) );
114114
}
115115
});
116-
} else if (text.indexOf('*') === -1) {
117-
if (text.length) {
118-
markers.push(builder.marker(text));
119-
}
120-
} else {
116+
} else if (text.indexOf('*') !== -1) {
121117
let markup = builder.markup('b');
122118

123119
let startIndex = text.indexOf('*');
@@ -133,6 +129,10 @@ function parseTextIntoMarkers(text, builder) {
133129
markers.push(builder.marker(piece, markups));
134130
}
135131
});
132+
} else {
133+
if (text.length) {
134+
markers.push(builder.marker(text));
135+
}
136136
}
137137

138138
return markers;

0 commit comments

Comments
 (0)