在等待答案的同时,我开始编写自己的解析器。这有点粗糙,因为没有跨浏览器支持,而且我没有对文本进行任何修改 - 这意味着 HTML 中的任何换行符和其他空格都将被保留。
还有很多我还没有清理的冗余,比如遍历我已经知道隐藏的节点的子节点。
无论如何,代码:
function ParsedRange(range){
this.text = "";
this.nodeIndices = [];
this.highlight = function(startIndex, endIndex){
var selection = window.getSelection();
var startNode = this.nodeIndices[startIndex].node;
var endNode = this.nodeIndices[endIndex].node;
var startOffset = startIndex - this.nodeIndices[startIndex].startIndex;
var endOffset = endIndex - this.nodeIndices[endIndex].startIndex + 1;
// Scroll into view
startNode.parentNode.scrollIntoViewIfNeeded();
// Highlight
range.setStart(startNode, startOffset);
range.setEnd(endNode, endOffset);
selection.removeAllRanges();
selection.addRange(range);
};
// Parsing starts here
var startIndex;
var rootNode = range.commonAncestorContainer;
var startNode = range.startContainer;
var endNode = range.endContainer;
var treeWalker = document.createTreeWalker(rootNode, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT, null, false); // Only walk text and element nodes
var currentNode = treeWalker.currentNode;
// Move to start node
while (currentNode && currentNode != startNode) currentNode = treeWalker.nextNode();
// Extract text
var nodeText;
while (currentNode && currentNode != endNode){ // Handle end node separately
// Continue to next node if current node is hidden
if (isHidden(currentNode)){
currentNode = treeWalker.nextNode();
continue;
}
// Extract text if text node
if (currentNode.nodeType == 3){
if (currentNode == startNode) nodeText = currentNode.nodeValue.substring(range.startOffset); // Extract from start of selection if first node
else nodeText = currentNode.nodeValue; // Else extra entire node
this.text += nodeText;
if (currentNode == startNode) startIndex = range.startOffset * -1;
else startIndex = this.nodeIndices.length;
for (var i=0; i<nodeText.length; i++){
this.nodeIndices.push({
startIndex: startIndex,
node: currentNode
});
}
}
// Continue to next node
currentNode = treeWalker.nextNode();
}
// Extract text from end node if it's a text node
if (currentNode == endNode && currentNode.nodeType == 3 && !isHidden(currentNode)){
if (endNode == startNode) nodeText = currentNode.nodeValue.substring(range.startOffset, range.endOffset); // Extract only selected part if end and start nodes are the same
else nodeText = currentNode.nodeValue.substring(0, range.endOffset); // Else extract up to where the selection ends in the end node
this.text += nodeText;
if (currentNode == startNode) startIndex = range.startOffset*-1;
else startIndex = this.nodeIndices.length;
for (var i=0; i<nodeText.length; i++){
this.nodeIndices.push({
startIndex: startIndex,
node: currentNode
});
}
}
return this;
}
ParsedRange.removeHighlight = function(){
window.getSelection().removeAllRanges();
};
function isHidden(element){
// Get parent node if element is a text node
if (element.nodeType == 3) element = element.parentNode;
// Only check visibility of the element itself
if (window.getComputedStyle(element, null).getPropertyValue("visibility") == "hidden") return true;
// Check display and dimensions for element and its parents
while (element){
if (element.nodeType == 9) return false; // Document
if (element.tagName == "NOSCRIPT") return true;
if (window.getComputedStyle(element, null).getPropertyValue("display") == "none") return true;
if (element.offsetWidth == 0 || element.offsetHeight == 0){ // If element does not have overflow:visible it is hidden
if (window.getComputedStyle(element, null).getPropertyValue("overflow") != "visible"){
return true;
}
}
element = element.parentNode;
}
return false;
}
isHidden()
由于它集成到我的项目中的方式,我将它作为一个类(除了辅助函数)。
除此之外,该类通过传递一个有效范围来工作,然后它将提取范围内的文本并保存对所有节点的引用。这些引用在函数中使用,该highlight()
函数使用浏览器选择根据开始和结束字符索引突出显示。
关于nodeIndices
财产的额外说明(认为这可能没有意义)。nodeIndices
是一个包含以下形式的对象的数组:
{
startIndex: // Int
node: // Reference to text node
}
对于我提取到结果文本中的每个字符,我将其中一个对象推送到 上nodeIndices
,该node
属性只是对文本节点的引用,文本来自该节点。startIndex
定义节点在整个文本中从哪个字符开始。
使用这个数组,我可以将字符索引ParsedParagraph.text
转换为 HTML 节点以及该节点内相应字符的索引。
使用示例:
// Get start/end nodes and offsets for range
var startNode = // Code to get start node here, can be a text node or an element node
var startOffset = // Offset into the start node
var endNode = // Code to get end node here, can be a text node or an element node
var endOffset = // Offset into the end node
// Create the range
var range = document.createRange();
range.setStart(startNode, startOffset);
range.setEnd(endNode, endOffset);
// Parse the range using the ParsedRange class
var parsedRange = new ParsedRange(range);
parsedRange.text; // Contains visible text with whitespaces preserved.
parsedRange.highlight(startIndex, endIndex); // Will highlight the corresponding text inside parsedRange.text using browser selection