You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Using Range API to make a selection, the 'Save selection' menu results in a wider range than expected.
To Reproduce
I‘ve made an example to illustrate this problem:
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>p[data-single-file-selected-content] { color: coral; font-weight: bold; }</style><script>constSINGLE_FILE_PREFIX="single-file-";constSELECTED_CONTENT_ATTRIBUTE_NAME="data-"+SINGLE_FILE_PREFIX+"selected-content";</script><script>// Identical to https://github.com/gildas-lormeau/SingleFile/blob/62a4c9f535a528a8ba026bc8a3b7d8d11c067c43/src/ui/content/content-ui.js#L198-L243functionmarkSelectedContent(){constselection=getSelection();letselectionFound;for(letindexRange=0;indexRange<selection.rangeCount;indexRange++){letrange=selection.getRangeAt(indexRange);if(range&&range.commonAncestorContainer){consttreeWalker=document.createTreeWalker(range.commonAncestorContainer);letrangeSelectionFound=false;letfinished=false;while(!finished){if(rangeSelectionFound||treeWalker.currentNode==range.startContainer||treeWalker.currentNode==range.endContainer){rangeSelectionFound=true;if(range.startContainer!=range.endContainer||range.startOffset!=range.endOffset){selectionFound=true;markSelectedNode(treeWalker.currentNode);}}if(selectionFound&&treeWalker.currentNode==range.startContainer){markSelectedParents(treeWalker.currentNode);}if(treeWalker.currentNode==range.endContainer){finished=true;}else{treeWalker.nextNode();}}if(selectionFound&&treeWalker.currentNode==range.endContainer&&treeWalker.currentNode.querySelectorAll){treeWalker.currentNode.querySelectorAll("*").forEach(descendantElement=>markSelectedNode(descendantElement));}}}returnselectionFound;}functionmarkSelectedNode(node){constelement=node.nodeType==Node.ELEMENT_NODE ? node : node.parentElement;element.setAttribute(SELECTED_CONTENT_ATTRIBUTE_NAME,"");}functionmarkSelectedParents(node){if(node.parentElement){markSelectedNode(node);markSelectedParents(node.parentElement);}}</script><scripttype="module">functionselectAndMark(withRange){constrange=newRange();range.setStart(container1,0);range.setEnd(container1,3);withRange(range);console.log(range);constselection=getSelection();selection.removeAllRanges();selection.addRange(range);markSelectedContent();}button1.onclick=()=>{selectAndMark(range=>{range.setStart(container1,1);range.setEnd(container1,4);});};button2.onclick=()=>{selectAndMark(range=>{range.setStartAfter(container2.firstChild);range.setEndAfter(container2.childNodes[3]);});};button3.onclick=()=>{selectAndMark(range=>{range.setStart(container3.childNodes[1],0);range.setEnd(container3.childNodes[3],1);});};</script></head><body><divid="container1"><p>1</p><p>2</p><p>3</p><p>4</p><p>5</p></div><hr><divid="container2"><p>1</p><p>2</p><p>3</p><p>4</p><p>5</p></div><hr><divid="container3"><p>1</p><p>2</p><p>3</p><p>4</p><p>5</p></div><hr><buttonid="button1">Mark contents in #1</button><buttonid="button2">Mark contents in #2</button><buttonid="button3">Mark contents in #3</button></body></html>
As you can see, clicking button1 and button2 results in a wider range than expected, while clicking button3 works as expected.
Expected behavior
Currently, markSelectedContent incorrectly handle situations when range.startContainer === range.endContainer, which commonly occurs with the output of Range#selectNode. This problem should be fixed to facilitate automation.
Describe the bug
Using Range API to make a selection, the 'Save selection' menu results in a wider range than expected.
To Reproduce
I‘ve made an example to illustrate this problem:
As you can see, clicking
button1
andbutton2
results in a wider range than expected, while clickingbutton3
works as expected.Expected behavior
Currently,
markSelectedContent
incorrectly handle situations whenrange.startContainer === range.endContainer
, which commonly occurs with the output ofRange#selectNode
. This problem should be fixed to facilitate automation.One feasible fix might be like:
Environment
The text was updated successfully, but these errors were encountered: