Skip to content

Commit

Permalink
YellowBox: Fix Off-By-1 Content Rendering Bug
Browse files Browse the repository at this point in the history
Summary:
Fixes an off-by-one bug that was causing trailing single characters to be truncated from YellowBox.

This snuck in during the revamp of YellowBox. Whoops!

@public

Reviewed By: TheSavior

Differential Revision: D8607388

fbshipit-source-id: d0efac4dec361456ef58347a60eb1486a888624a
  • Loading branch information
yungsters authored and facebook-github-bot committed Jun 26, 2018
1 parent 8b6f1c3 commit 537a995
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Libraries/YellowBox/Data/YellowBoxCategory.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const YellowBoxCategory = {
0,
);

if (lastOffset < content.length - 1) {
if (lastOffset < content.length) {
const lastPart = content.substr(lastOffset);
elements.push(<Text key="-1">{lastPart}</Text>);
}
Expand Down
85 changes: 85 additions & 0 deletions Libraries/YellowBox/Data/__tests__/YellowBoxCategory-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,89 @@ describe('YellowBoxCategory', () => {
},
});
});

it('renders content with no substitutions', () => {
expect(
YellowBoxCategory.render(
{content: 'A', substitutions: []},
{fontWeight: 'bold'},
),
).toMatchSnapshot();
});

it('renders a single substitution', () => {
expect(
YellowBoxCategory.render(
{
content: '"A"',
substitutions: [
{
length: 3,
offset: 0,
},
],
},
{fontWeight: 'bold'},
),
).toMatchSnapshot();
});

it('renders multiple substitutions', () => {
expect(
YellowBoxCategory.render(
{
content: '"A" "B" "C"',
substitutions: [
{
length: 3,
offset: 0,
},
{
length: 3,
offset: 4,
},
{
length: 3,
offset: 8,
},
],
},
{fontWeight: 'bold'},
),
).toMatchSnapshot();
});

it('renders substitutions with leading content', () => {
expect(
YellowBoxCategory.render(
{
content: '!"A"',
substitutions: [
{
length: 3,
offset: 1,
},
],
},
{fontWeight: 'bold'},
),
).toMatchSnapshot();
});

it('renders substitutions with trailing content', () => {
expect(
YellowBoxCategory.render(
{
content: '"A"!',
substitutions: [
{
length: 3,
offset: 0,
},
],
},
{fontWeight: 'bold'},
),
).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`YellowBoxCategory renders a single substitution 1`] = `
Array [
<Component
style={
Object {
"fontWeight": "bold",
}
}
>
"A"
</Component>,
]
`;

exports[`YellowBoxCategory renders content with no substitutions 1`] = `
Array [
<Component>
A
</Component>,
]
`;

exports[`YellowBoxCategory renders multiple substitutions 1`] = `
Array [
<Component
style={
Object {
"fontWeight": "bold",
}
}
>
"A"
</Component>,
<Component>
</Component>,
<Component
style={
Object {
"fontWeight": "bold",
}
}
>
"B"
</Component>,
<Component>
</Component>,
<Component
style={
Object {
"fontWeight": "bold",
}
}
>
"C"
</Component>,
]
`;

exports[`YellowBoxCategory renders substitutions with leading content 1`] = `
Array [
<Component>
!
</Component>,
<Component
style={
Object {
"fontWeight": "bold",
}
}
>
"A"
</Component>,
]
`;

exports[`YellowBoxCategory renders substitutions with trailing content 1`] = `
Array [
<Component
style={
Object {
"fontWeight": "bold",
}
}
>
"A"
</Component>,
<Component>
!
</Component>,
]
`;

0 comments on commit 537a995

Please sign in to comment.