Skip to content

Commit 1be5684

Browse files
authored
fix: revert "fix(editor): preserve button href through HTML round-trip" (#3376)
1 parent 16b9407 commit 1be5684

3 files changed

Lines changed: 8 additions & 71 deletions

File tree

.changeset/little-vans-switch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-email/editor": patch
3+
---
4+
5+
Do not preserve button href through HTML round-trip
Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
1-
import { Editor } from '@tiptap/core';
21
import { render } from 'react-email';
3-
import { afterEach, describe, expect, it } from 'vitest';
42
import { DEFAULT_STYLES } from '../utils/default-styles';
53
import { Button } from './button';
6-
import { StarterKit } from './index';
74

85
const buttonStyle = { ...DEFAULT_STYLES.reset, ...DEFAULT_STYLES.button };
96

107
describe('EditorButton Node', () => {
11-
let editor: Editor | null = null;
12-
13-
afterEach(() => {
14-
editor?.destroy();
15-
editor = null;
16-
});
17-
188
it('renders React Email properly', async () => {
199
const Component = Button.config.renderToReactEmail;
2010
expect(Component).toBeDefined();
@@ -38,60 +28,4 @@ describe('EditorButton Node', () => {
3828
),
3929
).toMatchSnapshot();
4030
});
41-
42-
it('preserves href through HTML round-trip', () => {
43-
editor = new Editor({
44-
extensions: [StarterKit],
45-
content: {
46-
type: 'doc',
47-
content: [
48-
{
49-
type: 'container',
50-
content: [
51-
{
52-
type: 'button',
53-
attrs: { href: 'https://example.com' },
54-
content: [{ type: 'text', text: 'Click me' }],
55-
},
56-
],
57-
},
58-
],
59-
},
60-
});
61-
62-
const html = editor.getHTML();
63-
expect(html).toContain('data-href="https://example.com"');
64-
expect(html).toContain('href="https://example.com"');
65-
66-
editor.commands.setContent(html);
67-
68-
const json = editor.getJSON();
69-
const findButton = (nodes: typeof json.content): typeof json | undefined =>
70-
nodes?.reduce<typeof json | undefined>(
71-
(found, n) =>
72-
found ?? (n.type === 'button' ? n : findButton(n.content ?? [])),
73-
undefined,
74-
);
75-
const buttonNode = findButton(json.content ?? []);
76-
expect(buttonNode?.attrs?.href).toBe('https://example.com');
77-
});
78-
79-
it('parses data-href back to href when href attribute is missing', () => {
80-
editor = new Editor({ extensions: [StarterKit] });
81-
82-
const htmlWithOnlyDataHref =
83-
'<div class="align-left"><a class="node-button button" data-id="react-email-button" data-href="https://restored.example.com">Restore me</a></div>';
84-
85-
editor.commands.setContent(htmlWithOnlyDataHref);
86-
87-
const json = editor.getJSON();
88-
const findButton = (nodes: typeof json.content): typeof json | undefined =>
89-
nodes?.reduce<typeof json | undefined>(
90-
(found, n) =>
91-
found ?? (n.type === 'button' ? n : findButton(n.content ?? [])),
92-
undefined,
93-
);
94-
const buttonNode = findButton(json.content ?? []);
95-
expect(buttonNode?.attrs?.href).toBe('https://restored.example.com');
96-
});
9731
});

packages/editor/src/extensions/button.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,13 @@ export const Button = EmailNode.create<EditorButtonOptions>({
4949
}
5050
const element = node as HTMLElement;
5151
const attrs: Record<string, string> = {};
52+
53+
// Preserve all attributes
5254
Array.from(element.attributes).forEach((attr) => {
5355
attrs[attr.name] = attr.value;
5456
});
5557

56-
return {
57-
...attrs,
58-
href: attrs['data-href'] ?? attrs.href,
59-
};
58+
return attrs;
6059
},
6160
},
6261
];
@@ -74,7 +73,6 @@ export const Button = EmailNode.create<EditorButtonOptions>({
7473
class: `node-button ${HTMLAttributes?.class}`,
7574
style: HTMLAttributes?.style,
7675
'data-id': 'react-email-button',
77-
href: HTMLAttributes?.href,
7876
'data-href': HTMLAttributes?.href,
7977
}),
8078
0,

0 commit comments

Comments
 (0)