Merge pull request #2428 from thelounge/xpaw/fix-merge-sort

Fix merge() in parser not filling unstyled text correctly
This commit is contained in:
Jérémie Astori 2018-05-08 01:39:56 -04:00 committed by GitHub
commit f54fef4210
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 7 deletions

View File

@ -25,13 +25,9 @@ function sortParts(a, b) {
// "o", and the second resulting part will contain "b" and "ar". "o" and "b"
// fragments will contain duplicate styling attributes.
function merge(textParts, styleFragments, cleanText) {
// Every section of the original text that has not been captured in a "part"
// is filled with "text" parts, dummy objects with start/end but no extra
// metadata.
const allParts = textParts
.sort(sortParts) // Sort all parts identified based on their position in the original text
.concat(fill(textParts, cleanText))
.sort(sortParts) // Sort them again after filling in unstyled text
// Remove overlapping parts
textParts = textParts
.sort(sortParts)
.reduce((prev, curr) => {
const intersection = prev.some((p) => anyIntersection(p, curr));
@ -42,6 +38,13 @@ function merge(textParts, styleFragments, cleanText) {
return prev.concat([curr]);
}, []);
// Every section of the original text that has not been captured in a "part"
// is filled with "text" parts, dummy objects with start/end but no extra
// metadata.
const allParts = textParts
.concat(fill(textParts, cleanText))
.sort(sortParts); // Sort all parts identified based on their position in the original text
// Distribute the style fragments within the text parts
return allParts.map((textPart) => {
textPart.fragments = styleFragments

View File

@ -60,4 +60,67 @@ describe("merge", () => {
expect(actual).to.deep.equal(expected);
});
it("should not drop clean text", () => {
const textParts = [{
start: 0,
end: 52,
link: "https://github.com/xPaw/PHP-Source-Query/runs/175079",
}];
const styleFragments = [{
bold: false,
textColor: undefined,
bgColor: undefined,
hexColor: undefined,
hexBgColor: undefined,
italic: false,
underline: false,
strikethrough: false,
monospace: false,
text: "https://github.com/xPaw/PHP-Source-Query/runs/175079 here's some text",
start: 0,
end: 69,
}];
const expected = [{
link: "https://github.com/xPaw/PHP-Source-Query/runs/175079",
start: 0,
end: 52,
fragments: [{
bold: false,
textColor: undefined,
bgColor: undefined,
hexColor: undefined,
hexBgColor: undefined,
italic: false,
underline: false,
strikethrough: false,
monospace: false,
text: "https://github.com/xPaw/PHP-Source-Query/runs/175079",
start: 0,
end: 52,
}],
}, {
start: 52,
end: 69,
fragments: [{
bold: false,
textColor: undefined,
bgColor: undefined,
hexColor: undefined,
hexBgColor: undefined,
italic: false,
underline: false,
strikethrough: false,
monospace: false,
text: " here's some text",
start: 52,
end: 69,
}],
}];
const actual = merge(textParts, styleFragments, styleFragments.map((fragment) => fragment.text).join(""));
expect(actual).to.deep.equal(expected);
});
});