Skip to content

Commit b2268b6

Browse files
committed
fix(surround): fix both nested code (#44) and multiple code spans (#46)
The previous fix for #44 used parentElement.querySelector() to detect existing <code> elements in the block, but this was too broad: it blocked wrapping any new selection as long as any <code> existed anywhere in the block, breaking the ability to apply inline code to multiple non-contiguous selections (#46). The correct scope for the check is the selection itself, not the whole block. Using range.cloneContents().querySelector() only inspects what the user has actually selected: - If the selection contains an existing <code> → skip wrap (prevents #44) - If the selection is clean, even if other <code> exist elsewhere in the block → wrap as normal (fixes #46) Closes #46
1 parent 908296f commit b2268b6

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

src/index.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,20 @@ export default class InlineCode implements InlineTool {
8888

8989
let termWrapper = this.api.selection.findParentTag(this.tag, InlineCode.CSS) as HTMLElement;
9090

91-
/**
92-
* If the current selection is already wrapped in a code tag, unwrap it.
93-
* Otherwise, wrap the selection — regardless of whether other <code> elements
94-
* exist elsewhere in the same block (fixes #46).
95-
*/
9691
if (termWrapper) {
9792
this.unwrap(termWrapper);
9893
} else {
99-
this.wrap(range);
94+
/**
95+
* Only wrap if the selection itself does not already contain a <code> element.
96+
* Using cloneContents() scopes the check to the actual selection, not the
97+
* whole block — this prevents nested <code> tags (#44) while still allowing
98+
* multiple non-contiguous code spans in the same block (#46).
99+
*/
100+
const selectionContainsCode = range.cloneContents().querySelector(this.tag) !== null;
101+
102+
if (!selectionContainsCode) {
103+
this.wrap(range);
104+
}
100105
}
101106
}
102107

0 commit comments

Comments
 (0)