0

我最近在使用Paul Schreiber 的 unescapeHtml 字符串方法将一大串转义 HTML 插入 CKEditor 实例时,遇到了 Firefox 对单个 XML 节点的 4096 字节 (4KB) 限制。这种方法在 Firefox 3.5.3 中无法处理大小超过 4KB 的字符串,因为一旦达到每个节点的 4096 字节限制,浏览器就会将内容拆分为多个节点。使用此 unescapeHtml 方法时,Firefox 3.5.3 中仅返回前 4096 个字节,而不是返回完整的字符串。

绕过这个 4KB 节点限制的最直接和最有效的方法是什么?

4

1 回答 1

0

我将提供解决节点大小限制问题的第一种方法。我修改了 unescapeHtml 代码,通过创建一个循环遍历每个 childNode 并将 nodeValues 附加到结果的循环来处理 Firefox 将数据拆分为多个节点。然后该函数在Matt Thommes 的简洁删除所有子节点代码的帮助下清理节点

我的代码如下:

 //htmlToCKEditor
 // Arguments:
 //  1. contentCK - Escaped content (likely HTML) to be inserted into CKEditor. (string)
 // 2. targetCK- The ID of the target CKEditor instance. (string)
 // Creates a temporary element, inserts the escaped content into the innerHTML,
 // loops over every childNode, appends each nodeValue to the result string,
 // removes the temporary element's child nodes, sets the data of the CKEditor to the new unescaped value.
 function htmlToCKEditor (contentCK,targetCK) {

  // Return false if there's no target CKEditor instance
  if (!(targetCK.match(/\w+/))){
   return false;
  }

  if (contentCK.match(/\w+/)){
   var thisHTML = unescape(contentCK);
   var temp = document.createElement("div");
   temp.innerHTML = thisHTML;

   // Loop over childNodes and append values to result variable.
   var result = '';
   for (var i=0; i < temp.childNodes.length; i++){
    result += temp.childNodes[i].nodeValue;
   }

   // Cleanup from Matt Thommes
   if (temp.hasChildNodes()){
    while (temp.childNodes.length >= 1){
     temp.removeChild(temp.firstChild);
    } 
   }

   // Set CKEditor instance of choice with the newly unescaped result.
   CKEDITOR.instances[targetCK].setData(result);
  }else{
   CKEDITOR.instances[targetCK].setData('');
  }
  return true;
 }

可以采取多种替代方法来完成此任务,因此我改进了我的问题并将我的代码拆分为答案。我很想阅读这个问题的更多解决方案,因为我对 CKEditor 方法并不完全熟悉,而且 CKEditor 可能内置了一个更简单的解决方案。

于 2009-10-22T00:25:37.430 回答