File size: 8,538 Bytes
012d0ac | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | # Example Walkthrough: Multiple Duplicates Rendering
## Example Input
```
Text: "ذهبو الى المدرسة ثم ذهبو الى البيت ثم ذهبو مرة اخرى"
```
## Step 1: Backend Analysis
When this text is sent to `/api/analyze`, the backend returns:
```json
{
"original": "ذهبو الى المدرسة ثم ذهبو الى البيت ثم ذهبو مرة اخرى",
"corrected": "ذهبوا الى المدرسة ثم ذهبوا الى البيت ثم ذهبوا مرة اخرى",
"suggestions": [
{
"start": 0,
"end": 4,
"original": "ذهبو",
"correction": "ذهبوا",
"type": "spelling"
},
{
"start": 20,
"end": 24,
"original": "ذهبو",
"correction": "ذهبوا",
"type": "spelling"
},
{
"start": 38,
"end": 42,
"original": "ذهبو",
"correction": "ذهبوا",
"type": "spelling"
}
],
"status": "success"
}
```
## Step 2: Offset Verification
Character positions in the text:
```
Position: 0 5 10 15 20 25 30 35 40 45 50
Text: ذ ــ ـ م ذ ــ ـ ب ذ ــ ى
ذهبو الى المدرسة ثم ذهبو الى البيت ثم ذهبو مرة اخرى
^^^^ ^^^^ ^^^^
0-4 20-24 38-42
```
## Step 3: Renderer Processing
### 3a. Sort Suggestions (already sorted)
```javascript
sorted = [
{start: 0, end: 4, ...}, // First occurrence
{start: 20, end: 24, ...}, // Second occurrence
{start: 38, end: 42, ...} // Third occurrence
]
```
### 3b. Create Segments
The text is split into segments at suggestion boundaries:
```javascript
segments = [
{type: 'suggestion', text: 'ذهبو', suggestion: {...}}, // [0:4]
{type: 'text', text: ' الى المدرسة ثم '}, // [4:20]
{type: 'suggestion', text: 'ذهبو', suggestion: {...}}, // [20:24]
{type: 'text', text: ' الى البيت ثم '}, // [24:38]
{type: 'suggestion', text: 'ذهبو', suggestion: {...}}, // [38:42]
{type: 'text', text: ' مرة اخرى'} // [42:51]
]
```
### 3c. Render Each Segment
**Segment 1** (suggestion):
```html
<span class="spelling-error"
data-suggestion-id="0"
data-original="ذهبو"
data-correction="ذهبوا"
data-type="spelling"
title="spelling: ذهبوا">
ذهبو
</span>
```
**Segment 2** (text): `" الى المدرسة ثم "`
**Segment 3** (suggestion):
```html
<span class="spelling-error"
data-suggestion-id="1"
data-original="ذهبو"
data-correction="ذهبوا"
data-type="spelling"
title="spelling: ذهبوا">
ذهبو
</span>
```
**Segment 4** (text): `" الى البيت ثم "`
**Segment 5** (suggestion):
```html
<span class="spelling-error"
data-suggestion-id="2"
data-original="ذهبو"
data-correction="ذهبوا"
data-type="spelling"
title="spelling: ذهبوا">
ذهبو
</span>
```
**Segment 6** (text): `" مرة اخرى"`
## Step 4: Final Rendered Output
Complete HTML:
```html
<span class="spelling-error" data-suggestion-id="0" data-original="ذهبو" data-correction="ذهبوا" data-type="spelling" title="spelling: ذهبوا">ذهبو</span> الى المدرسة ثم <span class="spelling-error" data-suggestion-id="1" data-original="ذهبو" data-correction="ذهبوا" data-type="spelling" title="spelling: ذهبوا">ذهبو</span> الى البيت ثم <span class="spelling-error" data-suggestion-id="2" data-original="ذهبو" data-correction="ذهبوا" data-type="spelling" title="spelling: ذهبوا">ذهبو</span> مرة اخرى
```
Visual representation:
```
[ذهبو] الى المدرسة ثم [ذهبو] الى البيت ثم [ذهبو] مرة اخرى
(red) (red) (red)
```
## Step 5: User Interaction
### User clicks on first "ذهبو" (red underline)
1. Click event fired on `<span role="0">`
2. Tooltip appears showing:
- Error type: "خطأ إملائي"
- Correction: "ذهبوا"
3. User can click to apply correction
### When user applies correction:
**Before**:
```
ذهبو الى المدرسة ثم ذهبو الى البيت ثم ذهبو مرة اخرى
```
**After** (1st corrected):
```
ذهبوا الى المدرسة ثم ذهبو الى البيت ثم ذهبو مرة اخرى
```
**Key**: Only the clicked occurrence changes. The other two remain.
**Important**:
- ✅ Cursor position is preserved
- ✅ Selection is preserved
- ✅ Text is re-analyzed immediately
- ✅ New highlights appear with updated offsets
## Advantages Over Old System
### Old System (replace-based)
```javascript
// Try to find and replace first occurrence
text.replace("ذهبو", "ذهبوا")
// Result: ALL occurrences get replaced (or just first depending on regex)
// Problem: Can't target specific occurrence
// Problem: Duplicate words fail silently
```
### New System (offset-based)
```javascript
// Render all occurrences with specific offsets
render({
text: "ذهبو الى ... ذهبو الى ... ذهبو مرة",
suggestions: [
{start: 0, end: 4, ...}, // Exact position 1
{start: 20, end: 24, ...}, // Exact position 2
{start: 38, end: 42, ...} // Exact position 3
]
})
// Result: 3 independent spans, each clickable/correctable
// Advantage: Precise, no ambiguity, no silent failures
```
## Code Flow Diagram
```
User Text Input
↓
┌─────────────────────────────────┐
│ saveSelection() from selection.js│
│ Stores: {selectionStart, end} │
└────────────────┬────────────────┘
↓
┌─────────────────────────────────┐
│ analyzeText() from editor.js │
│ Calls: POST /api/analyze │
└────────────────┬────────────────┘
↓
API Response
{original, corrected,
suggestions with offsets}
↓
┌─────────────────────────────────┐
│ render() from renderer.js │
│ Input: text + suggestions[] │
│ - Sort by offset │
│ - Create segments │
│ - Escape HTML │
│ - Generate spans with metadata │
│ Output: Safe HTML string │
└────────────────┬────────────────┘
↓
┌─────────────────────────────────┐
│ setEditorHTML() from selection.js│
│ Updates DOM │
└────────────────┬────────────────┘
↓
┌─────────────────────────────────┐
│ restoreSelection() from selection│
│ Restores {selectionStart, end} │
│ Cursor back where it was! │
└──────────────────────────────────┘
```
## Per-Occurrence Data
Each `<span>` carries its own suggestion:
**Span 1**:
- `data-suggestion-id="0"` - First in suggestions array
- `data-original="ذهبو"`
- `data-correction="ذهبوا"`
- Spans characters [0:4]
**Span 2**:
- `data-suggestion-id="1"` - Second in suggestions array
- `data-original="ذهبو"` (same original, different position)
- `data-correction="ذهبوا"` (same correction)
- Spans characters [20:24]
**Span 3**:
- `data-suggestion-id="2"` - Third in suggestions array
- `data-original="ذهبو"` (same original, different position)
- `data-correction="ذهبوا"` (same correction)
- Spans characters [38:42]
**Result**: No confusion. Each occurrence is independent.
---
## Summary
✅ **All 3 occurrences are highlighted**
✅ **Each is independently identifiable**
✅ **User can click any one to see/apply correction**
✅ **Cursor/selection preserved through re-renders**
✅ **XSS-safe (all user content escaped)**
✅ **No regex or string replacement needed**
|