Skip to content

Commit

Permalink
Prepare to treat separated ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Dec 16, 2022
1 parent fb2d7b9 commit bed7b46
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
12 changes: 6 additions & 6 deletions webextensions/content_scripts/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ async function findURIRanges(options = {}) {
for (let i = 0, maxi = selection.rangeCount; i < maxi; i++) {
const selectionRange = selection.getRangeAt(i);
const selectionText = rangeToText(selectionRange);
const preceding = getPrecedingRange(selectionRange);
const following = getFollowingRange(selectionRange);
const precedings = getPrecedingRanges(selectionRange);
const followings = getFollowingRanges(selectionRange);
const rangeData = getRangeData(selectionRange);
rangeData.text = selectionText;
rangeData.expandedText = `${preceding.text}${selectionText}${following.text}`;
rangeData.expandedText = `${precedings.map(part => part.text).join('')}${selectionText}${followings.map(part => part.text).join('')}`;
selectionRanges.push(rangeData);
}
const ranges = await browser.runtime.sendMessage({
Expand All @@ -233,9 +233,9 @@ function getSelectionEventData(event) {
}
else {
const selectionRange = selection.getRangeAt(0);
const preceding = getPrecedingRange(selectionRange);
const following = getFollowingRange(selectionRange);
text = `${preceding.text}${rangeToText(selectionRange)}${following.text}`;
const precedings = getPrecedingRanges(selectionRange);
const followings = getFollowingRanges(selectionRange);
text = `${precedings.map(part => part.text).join('')}${rangeToText(selectionRange)}${followings.map(part => part.text).join('')}`;
cursor = getRangeData(selectionRange);
}

Expand Down
47 changes: 36 additions & 11 deletions webextensions/content_scripts/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

const STATE_CONTINUE_PHYSICALLY = 1 << 0;
const STATE_CONTINUE_VISUALLY = 1 << 1;
const STATE_CONTINUE = STATE_CONTINUE_PHYSICALLY | STATE_CONTINUE_VISUALLY;
const STATE_SEPARATED = 1 << 2;

function rangeToText(range) {
Expand Down Expand Up @@ -74,7 +73,8 @@ function nodeToText(node) {
};
}

function getPrecedingRange(sourceRange) {
function getPrecedingRanges(sourceRange) {
const ranges = [];
const range = document.createRange();
range.setStart(sourceRange.startContainer, sourceRange.startOffset);
range.setEnd(sourceRange.startContainer, sourceRange.startOffset);
Expand All @@ -97,14 +97,27 @@ function getPrecedingRange(sourceRange) {
range.setStartBefore(walker.currentNode);
}
const { text: partialText, state } = nodeToText(walker.currentNode);
if (state == STATE_SEPARATED)
break;
text = `${partialText}${text}`;
switch (state) {
case STATE_CONTINUE_PHYSICALLY:
text = `${partialText}${text}`;
continue;

case STATE_CONTINUE_VISUALLY:
ranges.unshift({ range: range.cloneRange(), text });
text = partialText;
range.collapse(true);
continue;

case STATE_SEPARATED:
break;
}
}
return { range, text };
ranges.unshift({ range, text });
return ranges;
}

function getFollowingRange(sourceRange) {
function getFollowingRanges(sourceRange) {
const ranges = [];
const range = document.createRange();
range.setStart(sourceRange.endContainer, sourceRange.endOffset);
range.setEnd(sourceRange.endContainer, sourceRange.endOffset);
Expand All @@ -127,11 +140,23 @@ function getFollowingRange(sourceRange) {
range.setEndAfter(walker.currentNode);
}
const { text: partialText, state } = nodeToText(walker.currentNode);
if (state == STATE_SEPARATED)
break;
text += partialText;
switch (state) {
case STATE_CONTINUE_PHYSICALLY:
text += partialText;
continue;

case STATE_CONTINUE_VISUALLY:
ranges.push({ range: range.cloneRange(), text });
text = partialText;
range.collapse(false);
continue;

case STATE_SEPARATED:
break;
}
}
return { range, text };
ranges.push({ range, text });
return ranges;
}

function createVisibleTextNodeWalker() {
Expand Down

0 comments on commit bed7b46

Please sign in to comment.