Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/core/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ export function createEmptyDoc(): string {
* Strips all tags and checks whether any visible text remains.
*/
export function isContentEmpty(html: string): boolean {
const stripped = html.replace(/<[^>]*>/g, '').trim();
return stripped.length === 0;
let i = 0;
let stripped = '';
while (i < html.length) {
const lt = html.indexOf('<', i);
if (lt === -1) {
stripped += html.slice(i);
break;
}
stripped += html.slice(i, lt);
const gt = html.indexOf('>', lt + 1);
if (gt === -1) {
stripped += html.slice(lt);
break;
}
i = gt + 1;
}
return stripped.trim().length === 0;
}
16 changes: 16 additions & 0 deletions tests/unit/core/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,21 @@ describe('model', () => {
it('returns true for empty string', () => {
expect(isContentEmpty('')).toBe(true);
});

it('returns false when < is unclosed and there is visible text after it', () => {
expect(isContentEmpty('< world')).toBe(false);
});

it('returns false for a bare unclosed tag (no closing >)', () => {
expect(isContentEmpty('<foo')).toBe(false);
});

it('returns true for paragraph with only HTML ASCII whitespace between tags', () => {
expect(isContentEmpty('<p>\n\t\r\f </p>')).toBe(true);
});

it('returns true for a string with no tags that is only whitespace', () => {
expect(isContentEmpty(' \n ')).toBe(true);
});
});
});
Loading