File size: 4,100 Bytes
74411f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Bayan Chrome Extension — Renderer
 *
 * Functions reused from: src/js/renderer.js
 * All functions preserve original behavior exactly.
 * Source line references documented per Phase 0 audit.
 */

/**
 * Escapes HTML special characters to prevent XSS.
 * Source: src/js/renderer.js L9-18 (direct copy)
 *
 * @param {string} text - Text to escape
 * @returns {string} Escaped text
 */
function escapeHtml(text) {
  const map = {
    '&': '&',
    '<': '&lt;',
    '>': '&gt;',
    '"': '&quot;',
    "'": '&#039;',
  };
  return text.replace(/[&<>"']/g, (c) => map[c]);
}

/**
 * Sorts suggestions by start offset.
 * Source: src/js/renderer.js L25-27 (direct copy)
 *
 * @param {Array} suggestions - Array of suggestions with start/end offsets
 * @returns {Array} Sorted suggestions
 */
function sortSuggestions(suggestions) {
  return [...suggestions].sort((a, b) => a.start - b.start);
}

/**
 * Gets CSS class for suggestion type.
 * Source: src/js/renderer.js L138-145 (direct copy)
 *
 * @param {string} type - Suggestion type (spelling, grammar, punctuation)
 * @returns {string} CSS class name
 */
function getErrorClass(type) {
  const classes = {
    spelling: 'bayan-spelling-error',
    grammar: 'bayan-grammar-error',
    punctuation: 'bayan-punctuation-suggestion',
  };
  return classes[type] || 'bayan-spelling-error';
}

/**
 * Creates a segment tree of text ranges with their suggestions.
 * Handles overlapping ranges by merging them.
 * Source: src/js/renderer.js L36-131 (direct copy)
 *
 * @param {string} text - Original text
 * @param {Array} suggestions - Array of suggestions with start/end offsets
 * @returns {Array} Array of segments with position and suggestion info
 */
function createSegments(text, suggestions) {
  const sorted = sortSuggestions(suggestions);
  const finalSegments = [];
  let segStart = 0;

  sorted.forEach((suggestion) => {
    const { start, end } = suggestion;

    // Add text before suggestion
    if (segStart < start) {
      finalSegments.push({
        type: 'text',
        text: text.slice(segStart, start),
        suggestions: [],
      });
    }

    // Add suggested text
    finalSegments.push({
      type: 'suggestion',
      text: text.slice(start, end),
      suggestion: suggestion,
    });

    segStart = end;
  });

  // Add remaining text
  if (segStart < text.length) {
    finalSegments.push({
      type: 'text',
      text: text.slice(segStart),
      suggestions: [],
    });
  }

  return finalSegments;
}

/**
 * Renders text with highlighted suggestions as HTML string.
 * Source: src/js/renderer.js L153-191 (adapted for extension CSS classes)
 *
 * @param {string} text - Original text
 * @param {Array} suggestions - Array of suggestions with start/end offsets
 * @returns {string} Safe HTML string with highlights
 */
function renderHighlightedText(text, suggestions) {
  if (!text || text.length === 0) return '';
  if (!suggestions || suggestions.length === 0) return escapeHtml(text);

  const segments = createSegments(text, suggestions);
  let html = '';

  segments.forEach((segment) => {
    if (segment.type === 'text') {
      html += escapeHtml(segment.text);
    } else if (segment.type === 'suggestion') {
      const { suggestion } = segment;
      const errorClass = getErrorClass(suggestion.type);
      const escapedText = escapeHtml(segment.text);
      const sid = suggestion.id || '';

      html += `<span class="${errorClass}" data-suggestion-id="${sid}" data-original="${escapeHtml(
        suggestion.original
      )}" data-correction="${escapeHtml(
        suggestion.correction
      )}" data-type="${suggestion.type}" title="${suggestion.type}: ${escapeHtml(
        suggestion.correction
      )}">${escapedText}</span>`;
    }
  });

  return html;
}

/**
 * Main renderer function.
 * Source: src/js/renderer.js L201-204 (direct copy)
 *
 * @param {Object} input - { text, suggestions }
 * @returns {string} Safe HTML with highlights
 */
function bayanRender(input) {
  const { text = '', suggestions = [] } = input;
  return renderHighlightedText(text, suggestions);
}