Skip to content

Commit

Permalink
Don't treat an empty paragraph as literal
Browse files Browse the repository at this point in the history
`Paragraph#isLiteral()` would return `true` for empty paragraphs, which
caused formatters to render a literal section with a label containing
`"null"`.

Fixes #1163

Thanks to @gpr19 for reporting
  • Loading branch information
martijnversluis committed May 17, 2024
1 parent 6a5ed50 commit 0a4fad6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/chord_sheet/paragraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,23 @@ class Paragraph {
* @returns {boolean}
*/
isLiteral() {
return this
.lines
.every((line) => line.items.every((item) => {
if (item instanceof Literal) {
return true;
}
const { lines } = this;

if (lines.length === 0) {
return false;
}

return lines.every((line) => line.items.every((item) => {
if (item instanceof Literal) {
return true;
}

if (item instanceof Tag && (item as Tag).isSectionDelimiter()) {
return true;
}
if (item instanceof Tag && (item as Tag).isSectionDelimiter()) {
return true;
}

return false;
}));
return false;
}));
}

/**
Expand Down
8 changes: 8 additions & 0 deletions test/chord_sheet/paragraph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ describe('Paragraph', () => {
expect(paragraph.isLiteral()).toBe(false);
});
});

describe('when the paragraph is empty', () => {
it('returns false', () => {
const paragraph = createParagraph([]);

expect(paragraph.isLiteral()).toBe(false);
});
});
});

describe('#contents', () => {
Expand Down
28 changes: 28 additions & 0 deletions test/formatter/html_table_formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,34 @@ describe('HtmlTableFormatter', () => {
expect(new HtmlTableFormatter({ key: 'Eb' }).format(exampleSongSymbol)).toEqual(expectedChordSheet);
});

it('correctly renders blank lines', () => {
const song = createSongFromAst([
[chordLyricsPair('C', 'Whisper words of wisdom')],
[],
[],
]);

const expectedOutput = html`
<div class="chord-sheet">
<div class="paragraph">
<table class="row">
<tr>
<td class="chord">C</td>
</tr>
<tr>
<td class="lyrics">Whisper words of wisdom</td>
</tr>
</table>
</div>
<div class="paragraph"></div>
<div class="paragraph"></div>
</div>
`;

const output = new HtmlTableFormatter().format(song);
expect(output).toEqual(expectedOutput);
});

describe('with option useUnicodeModifiers:true', () => {
it('replaces # with unicode sharp', () => {
const songWithSharps = createSongFromAst([
Expand Down

0 comments on commit 0a4fad6

Please sign in to comment.