Skip to content

Commit df909ba

Browse files
committed
feat(cursor): Add getParentContainer() helper function.
1 parent 8e477fa commit df909ba

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

src/cursor.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,35 @@ export default class Cursor {
3737
this.isCursor = true
3838
}
3939

40+
// Get all tags that affect the current selection. Optionally pass a
41+
// method to filter the returned elements.
42+
//
43+
// @param {Function filter(node)} [Optional] Method to filter the returned
44+
// DOM Nodes.
45+
// @return {Array of DOM Nodes}
46+
getTags (filterFunc) {
47+
return content.getTags(this.host, this.range, filterFunc)
48+
}
49+
50+
// Get the names of all tags that affect the current selection. Optionally
51+
// pass a method to filter the returned elements.
52+
//
53+
// @param {Function filter(node)} [Optional] Method to filter the DOM
54+
// Nodes whose names are returned.
55+
// @return {Array<String> of tag names}
56+
getTagNames (filterFunc) {
57+
return content.getTagNames(this.getTags(filterFunc))
58+
}
59+
60+
// Get all tags of the specified type that affect the current selection.
61+
//
62+
// @method getTagsByName
63+
// @param {String} tagName. E.g. 'a' to get all links.
64+
// @return {Array of DOM Nodes}
65+
getTagsByName (tagName) {
66+
return content.getTagsByName(this.host, this.range, tagName)
67+
}
68+
4069
isAtEnd () {
4170
return parser.isEndOfHost(
4271
this.host,
@@ -341,6 +370,24 @@ export default class Cursor {
341370
return content.adoptElement(node, this.win.document)
342371
}
343372

373+
// Check whether the cursor's closest parent element node matches
374+
// the given tagName (and optional className), and return that
375+
// parent if it does or null if not.
376+
getParentContainer (tagName, className) {
377+
const tags = this.getTags()
378+
379+
let node = tags.shift()
380+
if (node.nodeType !== Node.TEXT_NODE) return null
381+
382+
if (tags.length === 0) return null
383+
384+
node = tags.shift()
385+
if (node.nodeName !== tagName) return null
386+
if (className && !node.classList.contains(className)) return null
387+
388+
return node
389+
}
390+
344391
// Currently we call triggerChange manually after format changes.
345392
// This is to prevent excessive triggering of the change event during
346393
// merge or split operations or other manipulations by scripts.

src/selection.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -249,35 +249,6 @@ export default class Selection extends Cursor {
249249
this.setSelection()
250250
}
251251

252-
// Get all tags that affect the current selection. Optionally pass a
253-
// method to filter the returned elements.
254-
//
255-
// @param {Function filter(node)} [Optional] Method to filter the returned
256-
// DOM Nodes.
257-
// @return {Array of DOM Nodes}
258-
getTags (filterFunc) {
259-
return content.getTags(this.host, this.range, filterFunc)
260-
}
261-
262-
// Get the names of all tags that affect the current selection. Optionally
263-
// pass a method to filter the returned elements.
264-
//
265-
// @param {Function filter(node)} [Optional] Method to filter the DOM
266-
// Nodes whose names are returned.
267-
// @return {Array<String> of tag names}
268-
getTagNames (filterFunc) {
269-
return content.getTagNames(this.getTags(filterFunc))
270-
}
271-
272-
// Get all tags of the specified type that affect the current selection.
273-
//
274-
// @method getTagsByName
275-
// @param {String} tagName. E.g. 'a' to get all links.
276-
// @return {Array of DOM Nodes}
277-
getTagsByName (tagName) {
278-
return content.getTagsByName(this.host, this.range, tagName)
279-
}
280-
281252
// Check if the selection is the same as the elements contents.
282253
//
283254
// @method isExactSelection

0 commit comments

Comments
 (0)