youssefreda9 commited on
Commit
d061c20
·
1 Parent(s): 75190ee

Fix renderer.js null safety, rename duplicate walkTextNodes in format.js to avoid conflict

Browse files
Files changed (2) hide show
  1. src/js/format.js +5 -5
  2. src/js/renderer.js +1 -0
src/js/format.js CHANGED
@@ -1,4 +1,4 @@
1
- // src/js/format.js
2
  // Rich text formatting commands for the editor
3
 
4
  /**
@@ -357,16 +357,16 @@ function convertToHindiNumerals() {
357
  if (!editor) return;
358
  pushUndoState();
359
  const map = {'0':'٠','1':'١','2':'٢','3':'٣','4':'٤','5':'٥','6':'٦','7':'٧','8':'٨','9':'٩'};
360
- walkTextNodes(editor, text => text.replace(/[0-9]/g, d => map[d]));
361
  }
362
  function convertToArabicNumerals() {
363
  const editor = getEditorElement();
364
  if (!editor) return;
365
  pushUndoState();
366
  const map = {'٠':'0','١':'1','٢':'2','٣':'3','٤':'4','٥':'5','٦':'6','٧':'7','٨':'8','٩':'9'};
367
- walkTextNodes(editor, text => text.replace(/[٠-٩]/g, d => map[d]));
368
  }
369
- function walkTextNodes(root, fn) {
370
  const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, null, false);
371
  let node;
372
  while (node = walker.nextNode()) {
@@ -380,7 +380,7 @@ function cleanupText() {
380
  const editor = getEditorElement();
381
  if (!editor) return;
382
  pushUndoState();
383
- walkTextNodes(editor, text => {
384
  return text
385
  .replace(/[\u064B-\u065F\u0670\u06D6-\u06ED]/g, '') // Remove diacritics
386
  .replace(/\u200C/g, '') // Remove ZWNJ
 
1
+ // src/js/format.js
2
  // Rich text formatting commands for the editor
3
 
4
  /**
 
357
  if (!editor) return;
358
  pushUndoState();
359
  const map = {'0':'٠','1':'١','2':'٢','3':'٣','4':'٤','5':'٥','6':'٦','7':'٧','8':'٨','9':'٩'};
360
+ fmtWalkTextNodes(editor, text => text.replace(/[0-9]/g, d => map[d]));
361
  }
362
  function convertToArabicNumerals() {
363
  const editor = getEditorElement();
364
  if (!editor) return;
365
  pushUndoState();
366
  const map = {'٠':'0','١':'1','٢':'2','٣':'3','٤':'4','٥':'5','٦':'6','٧':'7','٨':'8','٩':'9'};
367
+ fmtWalkTextNodes(editor, text => text.replace(/[٠-٩]/g, d => map[d]));
368
  }
369
+ function fmtWalkTextNodes(root, fn) {
370
  const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, null, false);
371
  let node;
372
  while (node = walker.nextNode()) {
 
380
  const editor = getEditorElement();
381
  if (!editor) return;
382
  pushUndoState();
383
+ fmtWalkTextNodes(editor, text => {
384
  return text
385
  .replace(/[\u064B-\u065F\u0670\u06D6-\u06ED]/g, '') // Remove diacritics
386
  .replace(/\u200C/g, '') // Remove ZWNJ
src/js/renderer.js CHANGED
@@ -288,6 +288,7 @@ function overlaySuggestions(editor, suggestions) {
288
  const localEnd = Math.min(tn.node.length, end - tn.start);
289
 
290
  // Split the text node
 
291
  const textContent = tn.node.textContent;
292
  const beforeText = textContent.slice(0, localStart);
293
  const errorText = textContent.slice(localStart, localEnd);
 
288
  const localEnd = Math.min(tn.node.length, end - tn.start);
289
 
290
  // Split the text node
291
+ if (!tn.node || !tn.node.textContent) return;
292
  const textContent = tn.node.textContent;
293
  const beforeText = textContent.slice(0, localStart);
294
  const errorText = textContent.slice(localStart, localEnd);