Skip to content

Commit

Permalink
Port: Catch errors due to "Restricted" selection. ccd0#2905
Browse files Browse the repository at this point in the history
from 2a47dfd
  • Loading branch information
TuxedoTako committed Mar 19, 2023
1 parent f547779 commit 59af4ba
Showing 1 changed file with 38 additions and 36 deletions.
74 changes: 38 additions & 36 deletions src/Posting/QR.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,44 +383,46 @@ var QR = {
postRange.selectNode(root);
let text = post.board.ID === g.BOARD.ID ? `>>${post}\n` : `>>>/${post.board}/${post}\n`;
for (let i = 0, end = sel.rangeCount, asc = 0 <= end; asc ? i < end : i > end; asc ? i++ : i--) {
var insideCode, node;
range = sel.getRangeAt(i);
// Trim range to be fully inside post
if (range.compareBoundaryPoints(Range.START_TO_START, postRange) < 0) {
range.setStartBefore(root);
}
if (range.compareBoundaryPoints(Range.END_TO_END, postRange) > 0) {
range.setEndAfter(root);
}
try {
var insideCode, node;
range = sel.getRangeAt(i);
// Trim range to be fully inside post
if (range.compareBoundaryPoints(Range.START_TO_START, postRange) < 0) {
range.setStartBefore(root);
}
if (range.compareBoundaryPoints(Range.END_TO_END, postRange) > 0) {
range.setEndAfter(root);
}

if (!range.toString().trim()) { continue; }
if (!range.toString().trim()) { continue; }

var frag = range.cloneContents();
var ancestor = range.commonAncestorContainer;
// Quoting the insides of a spoiler/code tag.
if ($.x('ancestor-or-self::*[self::s or contains(@class,"removed-spoiler")]', ancestor)) {
$.prepend(frag, $.tn('[spoiler]'));
$.add(frag, $.tn('[/spoiler]'));
}
if (insideCode = $.x('ancestor-or-self::pre[contains(@class,"prettyprint")]', ancestor)) {
$.prepend(frag, $.tn('[code]'));
$.add(frag, $.tn('[/code]'));
}
for (node of $$((insideCode ? 'br' : '.prettyprint br'), frag)) {
$.replace(node, $.tn('\n'));
}
for (node of $$('br', frag)) {
if (node !== frag.lastChild) { $.replace(node, $.tn('\n>')); }
}
g.SITE.insertTags?.(frag);
for (node of $$('.linkify[data-original]', frag)) {
$.replace(node, $.tn(node.dataset.original));
}
for (node of $$('.embedder', frag)) {
if (node.previousSibling?.nodeValue === ' ') { $.rm(node.previousSibling); }
$.rm(node);
}
text += `>${frag.textContent.trim()}\n`;
var frag = range.cloneContents();
var ancestor = range.commonAncestorContainer;
// Quoting the insides of a spoiler/code tag.
if ($.x('ancestor-or-self::*[self::s or contains(@class,"removed-spoiler")]', ancestor)) {
$.prepend(frag, $.tn('[spoiler]'));
$.add(frag, $.tn('[/spoiler]'));
}
if (insideCode = $.x('ancestor-or-self::pre[contains(@class,"prettyprint")]', ancestor)) {
$.prepend(frag, $.tn('[code]'));
$.add(frag, $.tn('[/code]'));
}
for (node of $$((insideCode ? 'br' : '.prettyprint br'), frag)) {
$.replace(node, $.tn('\n'));
}
for (node of $$('br', frag)) {
if (node !== frag.lastChild) { $.replace(node, $.tn('\n>')); }
}
g.SITE.insertTags?.(frag);
for (node of $$('.linkify[data-original]', frag)) {
$.replace(node, $.tn(node.dataset.original));
}
for (node of $$('.embedder', frag)) {
if (node.previousSibling?.nodeValue === ' ') { $.rm(node.previousSibling); }
$.rm(node);
}
text += `>${frag.textContent.trim()}\n`;
} catch (error) { }
}

QR.openPost();
Expand Down

0 comments on commit 59af4ba

Please sign in to comment.