Skip to content

Commit 90432f2

Browse files
committed
feat: Add getParentContainer() helper function.
1 parent 35e4e2a commit 90432f2

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/cursor.js

+15
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,21 @@ export default class Cursor {
289289
return content.adoptElement(node, this.win.document)
290290
}
291291

292+
// Check whether the cursor's closest parent element node matches
293+
// the given tagName (and optional className), and return that
294+
// parent if it does or null if not.
295+
getParentContainer (tagName, className) {
296+
const container = this.range.startContainer
297+
298+
if (container.nodeType !== Node.TEXT_NODE) return null
299+
300+
const parent = container.parentElement
301+
if (parent.nodeName !== tagName) return null
302+
if (className && !parent.classList.contains(className)) return null
303+
304+
return parent
305+
}
306+
292307
// Currently we call triggerChange manually after format changes.
293308
// This is to prevent excessive triggering of the change event during
294309
// merge or split operations or other manipulations by scripts.

0 commit comments

Comments
 (0)