|
| 1 | +# Normalize Empty Content |
| 2 | + |
| 3 | +**Status:** Proposed |
| 4 | +**Type:** Bug Fix |
| 5 | +**Impact:** Breaking (Validation behavior) |
| 6 | +**Effort:** ~3.5 hours |
| 7 | + |
| 8 | +## Quick Summary |
| 9 | + |
| 10 | +Fix the rich text editor to save empty content as `""` instead of `<p></p>`, ensuring correct validation and consistent data representation. |
| 11 | + |
| 12 | +## Problem |
| 13 | + |
| 14 | +When users delete all content, the editor currently saves `<p></p>` to the database. This breaks: |
| 15 | + |
| 16 | +- Required field validation (incorrectly passes) |
| 17 | +- Empty checks (`if (value)` returns true) |
| 18 | +- Data consistency (semantically empty ≠ empty string) |
| 19 | + |
| 20 | +## Solution |
| 21 | + |
| 22 | +Use Tiptap's `editor.isEmpty` property to return `""` when content is empty, and normalize comparison logic to treat `""` and `<p></p>` as equivalent. |
| 23 | + |
| 24 | +## Files |
| 25 | + |
| 26 | +- [`proposal.md`](./proposal.md) - Problem statement, scope, and impact |
| 27 | +- [`design.md`](./design.md) - Technical design, decisions, and architecture |
| 28 | +- [`tasks.md`](./tasks.md) - Implementation checklist and effort estimate |
| 29 | + |
| 30 | +## Key Decisions |
| 31 | + |
| 32 | +1. **Trust Tiptap's isEmpty** - Use built-in check, no custom logic |
| 33 | +2. **Normalize at persistence** - Convert to `""` when saving, not loading |
| 34 | +3. **Lazy migration** - Existing `<p></p>` records normalize on next edit |
| 35 | +4. **No version bump** - This Tiptap version is unreleased |
| 36 | + |
| 37 | +## Changes Required |
| 38 | + |
| 39 | +### Editor.tsx (line 237-240) |
| 40 | + |
| 41 | +```typescript |
| 42 | +// Before: |
| 43 | +const html = editor.getHTML(); |
| 44 | + |
| 45 | +// After: |
| 46 | +const html = editor.isEmpty ? "" : editor.getHTML(); |
| 47 | +``` |
| 48 | + |
| 49 | +### EditorWrapper.tsx (line 48-60) |
| 50 | + |
| 51 | +```typescript |
| 52 | +// Add normalization in comparison: |
| 53 | +const normalizeEmpty = (val?: string) => (!val || val === "<p></p>" ? "" : val); |
| 54 | + |
| 55 | +const current = normalizeEmpty(stringAttribute.value); |
| 56 | +const incoming = normalizeEmpty(html); |
| 57 | +if (current !== incoming) { |
| 58 | + stringAttribute.setValue(incoming); |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +## Testing |
| 63 | + |
| 64 | +- ✓ Unit tests for empty content behavior |
| 65 | +- ✓ E2E test for persistence |
| 66 | +- ✓ Manual validation testing |
| 67 | + |
| 68 | +## Breaking Change |
| 69 | + |
| 70 | +**Required field validation will now correctly reject empty content.** |
| 71 | + |
| 72 | +Previously: `<p></p>` passed validation ❌ |
| 73 | +After fix: `""` fails validation ✓ |
| 74 | + |
| 75 | +This is desired behavior (bug fix), but may affect existing forms. |
| 76 | + |
| 77 | +## Next Steps |
| 78 | + |
| 79 | +1. Review proposal and design |
| 80 | +2. Approve or request changes |
| 81 | +3. Implement following tasks.md |
| 82 | +4. Test thoroughly |
| 83 | +5. Update CHANGELOG |
| 84 | +6. Merge to main branch |
0 commit comments