Skip to content
Closed
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
2 changes: 1 addition & 1 deletion lib/export/html-parser/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const formatAttributes = (attributes: ElementAttribute[]) => {
return attributes.reduce((attrs, attribute) => {
const { key, value } = attribute;
if (value === null) return `${attrs} ${key}`;
if (key === 'style' && !value) return '';
if (key === 'style' && !value) return attrs;

const quoteEscape = value.indexOf("'") !== -1;
const quote = quoteEscape ? '"' : "'";
Expand Down
80 changes: 80 additions & 0 deletions tests/export/stringify.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { describe, expect, it } from 'vitest';
import { formatAttributes, toHTML } from '@/lib/export/html-parser/stringify';
import type { AST, ElementAttribute } from '@/lib/export/html-parser/types';

describe('formatAttributes', () => {
it('formats a value attribute with single quotes', () => {
const attrs: ElementAttribute[] = [{ key: 'id', value: 'main' }];
expect(formatAttributes(attrs)).toBe(" id='main'");
});

it('renders a null value as a boolean attribute', () => {
const attrs: ElementAttribute[] = [{ key: 'disabled', value: null }];
expect(formatAttributes(attrs)).toBe(' disabled');
});

it('switches to double quotes when the value contains a single quote', () => {
const attrs: ElementAttribute[] = [{ key: 'data-x', value: "it's" }];
expect(formatAttributes(attrs)).toBe(' data-x="it\'s"');
});

it('omits an empty style attribute', () => {
const attrs: ElementAttribute[] = [{ key: 'style', value: '' }];
expect(formatAttributes(attrs)).toBe('');
});

it('keeps attributes accumulated before an empty style attribute (regression #682)', () => {
const attrs: ElementAttribute[] = [
{ key: 'id', value: 'a' },
{ key: 'style', value: '' },
{ key: 'class', value: 'b' },
];
// Previously the empty-style branch returned '' and wiped `id`.
expect(formatAttributes(attrs)).toBe(" id='a' class='b'");
});

it('preserves a non-empty style attribute', () => {
const attrs: ElementAttribute[] = [
{ key: 'id', value: 'a' },
{ key: 'style', value: 'color:red' },
];
expect(formatAttributes(attrs)).toBe(" id='a' style='color:red'");
});
});

describe('toHTML', () => {
it('serializes an element whose first attribute precedes an empty style (regression #682)', () => {
const tree: AST[] = [
{
type: 'element',
tagName: 'div',
attributes: [
{ key: 'id', value: 'box' },
{ key: 'style', value: '' },
],
children: [{ type: 'text', content: 'hi' }],
},
];
expect(toHTML(tree)).toBe("<div id='box'>hi</div>");
});

it('serializes void tags without a closing tag', () => {
const tree: AST[] = [
{
type: 'element',
tagName: 'br',
attributes: [],
children: [],
},
];
expect(toHTML(tree)).toBe('<br>');
});

it('passes through text and comment nodes', () => {
const tree: AST[] = [
{ type: 'text', content: 'a' },
{ type: 'comment', content: ' note ' },
];
expect(toHTML(tree)).toBe('a<!-- note -->');
});
});
Loading