Fix crash on markdown emoji with hardbreak parser (#5547) (#5549)

(cherry picked from commit 90cce88358)

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
This commit is contained in:
Mattermost Build 2021-07-20 18:00:11 +02:00 committed by GitHub
parent 44708f5bba
commit ad249e9fe4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 181 additions and 2 deletions

View file

@ -31,3 +31,164 @@ exports[`MarkdownEmoji should match snapshot 1`] = `
</Unknown>
</Unknown>
`;
exports[`MarkdownEmoji should render with hardbreaks 1`] = `
<Unknown
context={Array []}
first={true}
last={true}
literal={null}
nodeKey="22"
>
<Unknown
context={
Array [
"paragraph",
]
}
emojiName="fire"
literal=":fire:"
nodeKey="4"
>
<Unknown
context={
Array [
"paragraph",
"emoji",
]
}
literal=":fire:"
nodeKey="3"
/>
</Unknown>
<Unknown
context={
Array [
"paragraph",
]
}
literal=" "
nodeKey="5"
/>
<Unknown
context={
Array [
"paragraph",
]
}
emojiName="fire"
literal=":fire:"
nodeKey="8"
>
<Unknown
context={
Array [
"paragraph",
"emoji",
]
}
literal=":fire:"
nodeKey="7"
/>
</Unknown>
<Unknown
context={
Array [
"paragraph",
]
}
literal=""
nodeKey="9"
/>
<Unknown
context={
Array [
"paragraph",
]
}
literal={null}
nodeKey="10"
/>
<Unknown
context={
Array [
"paragraph",
]
}
emojiName="fire"
literal=":fire:"
nodeKey="13"
>
<Unknown
context={
Array [
"paragraph",
"emoji",
]
}
literal=":fire:"
nodeKey="12"
/>
</Unknown>
<Unknown
context={
Array [
"paragraph",
]
}
literal=" "
nodeKey="14"
/>
<Unknown
context={
Array [
"paragraph",
]
}
emojiName="fire"
literal=":fire:"
nodeKey="17"
>
<Unknown
context={
Array [
"paragraph",
"emoji",
]
}
literal=":fire:"
nodeKey="16"
/>
</Unknown>
<Unknown
context={
Array [
"paragraph",
]
}
literal=" "
nodeKey="18"
/>
<Unknown
context={
Array [
"paragraph",
]
}
emojiName="fire"
literal=":fire:"
nodeKey="21"
>
<Unknown
context={
Array [
"paragraph",
"emoji",
]
}
literal=":fire:"
nodeKey="20"
/>
</Unknown>
</Unknown>
`;

View file

@ -39,6 +39,7 @@ export default class MarkdownEmoji extends PureComponent {
paragraph: this.renderParagraph,
document: this.renderParagraph,
text: this.renderText,
hardbreak: this.renderNewLine,
},
});
};
@ -73,10 +74,14 @@ export default class MarkdownEmoji extends PureComponent {
renderText = ({context, literal}) => {
const style = this.computeTextStyle(this.props.baseTextStyle, context);
return <Text style={style}>{literal}</Text>;
};
renderNewLine = ({context}) => {
const style = this.computeTextStyle(this.props.baseTextStyle, context);
return <Text style={style}>{'\n'}</Text>;
}
renderEditedIndicator = ({context}) => {
let spacer = '';
if (context[0] === 'paragraph') {
@ -101,7 +106,7 @@ export default class MarkdownEmoji extends PureComponent {
};
render() {
const ast = this.parser.parse(this.props.value);
const ast = this.parser.parse(this.props.value.replace(/\n*$/, ''));
if (this.props.isEdited) {
const editIndicatorNode = new Node('edited_indicator');

View file

@ -24,4 +24,17 @@ describe('MarkdownEmoji', () => {
expect(wrapper.getElement()).toMatchSnapshot();
});
test('should render with hardbreaks', () => {
const wrapper = shallow(
<MarkdownEmoji
{...baseProps}
value={`:fire: :fire:
:fire: :fire: :fire:
`}
/>,
);
expect(wrapper.getElement()).toMatchSnapshot();
});
});