Skip to content

Commit

Permalink
Merge pull request #17384 from bernhardoj/fix/17284-copying-only-get-…
Browse files Browse the repository at this point in the history
…the-innermost-style

alllow selection to copy nested markdown text style
  • Loading branch information
jasperhuangg authored Apr 19, 2023
2 parents 370758d + 41f1bf2 commit 5a3cbd8
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/libs/SelectionScraper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ const getHTMLOfSelection = () => {

// If clonedSelection has no text content this data has no meaning to us.
if (clonedSelection.textContent) {
let node = null;
let parent;
let child = clonedSelection;

// If selection starts and ends within same text node we use its parentNode. This is because we can't
// use closest function on a [Text](https://developer.mozilla.org/en-US/docs/Web/API/Text) node.
Expand All @@ -61,19 +62,21 @@ const getHTMLOfSelection = () => {
// </div>
// and finally commonAncestorContainer.parentNode.closest('data-testid') is targeted dom.
if (range.commonAncestorContainer instanceof HTMLElement) {
node = range.commonAncestorContainer.closest(`[${tagAttribute}]`);
parent = range.commonAncestorContainer.closest(`[${tagAttribute}]`);
} else {
node = range.commonAncestorContainer.parentNode.closest(`[${tagAttribute}]`);
parent = range.commonAncestorContainer.parentNode.closest(`[${tagAttribute}]`);
}

// This means "range.commonAncestorContainer" is a text node. We simply get its parent node.
if (!node) {
node = range.commonAncestorContainer.parentNode;
// Keep traversing up to clone all parents with 'data-testid' attribute.
while (parent) {
const cloned = parent.cloneNode();
cloned.appendChild(child);
child = cloned;

parent = parent.parentNode.closest(`[${tagAttribute}]`);
}

node = node.cloneNode();
node.appendChild(clonedSelection);
div.appendChild(node);
div.appendChild(child);
}
}

Expand Down

0 comments on commit 5a3cbd8

Please sign in to comment.