# How to get ascendants of a node?

**URL:** <https://discuss.prosemirror.net/t/how-to-get-ascendants-of-a-node/2354>\
**Category:** Uncategorized\
**Created:** [November 18, 2019, 6:47pm UTC](https://discuss.prosemirror.net/t/how-to-get-ascendants-of-a-node/2354 "2019-11-18T18:47:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lcswillems](https://discuss.prosemirror.net/letter_avatar_proxy/v4/letter/l/e5b9ba/32.png) [@lcswillems](https://discuss.prosemirror.net/u/lcswillems)\
**Post date:** [November 18, 2019, 6:47pm UTC](https://discuss.prosemirror.net/t/how-to-get-ascendants-of-a-node/2354/1 "2019-11-18T18:47:23Z")

</div>

I would like to know if a node is inside a paragraph. How could I do?

I saw [this page](https://discuss.prosemirror.net/t/i-miss-node-parent-is-there-a-reason-not-to-expose-it/654) saying there is no `.parent` attribute. But, how could I traverse the ascendants of a node?

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)\
**Post date:** [November 18, 2019, 8:12pm UTC](https://discuss.prosemirror.net/t/how-to-get-ascendants-of-a-node/2354/2 "2019-11-18T20:12:17Z")

</div>

From a `Node` object, you can’t. But if you have a resolved position for the node’s position, you can inspect that (with the `node` method).

---

<div class="post-metadata">

**Author:** ![lcswillems](https://discuss.prosemirror.net/letter_avatar_proxy/v4/letter/l/e5b9ba/32.png) [@lcswillems](https://discuss.prosemirror.net/u/lcswillems)\
**Post date:** [November 18, 2019, 8:43pm UTC](https://discuss.prosemirror.net/t/how-to-get-ascendants-of-a-node/2354/3 "2019-11-18T20:43:13Z")

</div>

Thank you for the answer.

More precisely, I would like to get the common ascendants of the document selection. How could I get them? How could I get a resolved position from the common parent of the selection?

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)\
**Post date:** [November 19, 2019, 7:36am UTC](https://discuss.prosemirror.net/t/how-to-get-ascendants-of-a-node/2354/4 "2019-11-19T07:36:04Z")

</div>

The [`sharedDepth`](http://prosemirror.net/docs/ref/#model.ResolvedPos.sharedDepth) method can give you the depth of shared ancestors (via something like `sel.$from.sharedDepth(sel.to)`), which you can then iterate over.

---

<div class="post-metadata">

**Author:** ![lcswillems](https://discuss.prosemirror.net/letter_avatar_proxy/v4/letter/l/e5b9ba/32.png) [@lcswillems](https://discuss.prosemirror.net/u/lcswillems)\
**Post date:** [November 19, 2019, 8:46am UTC](https://discuss.prosemirror.net/t/how-to-get-ascendants-of-a-node/2354/5 "2019-11-19T08:46:42Z")

</div>

Okay, thank you!

**Suggestion**

It would be also nice to have a `.$before(depth)` method that returns the resolved position of `.before(depth)` (the same for `.$after`, `.$begin`, `.$end`). Hence, everything could be done with resolved position.

**Motivation of this suggestion**

Currently, I find it very difficult to use Prosemirror. The difficulty is because PM has 3 main concepts (nodes, positions and resolved positions) and no easy way to do everything in one of them. PM functions sometimes use pos, sometimes node, sometimes resolved pos, which forces either (a) to convert one in the other, or (b) to keep a synchronized version of each.

(a) is hard:

- If I want to go from pos to node, I need to resolve the pos which requires to have the document node!
- If want to go from node to pos, I can’t! So, I have to always keep a resolved position. But resolved position methods always return position. So, I need to also keep the document node, i.e. I need to keep two objects along side, i.e. I need to do (b), which is painful.

**Edit:**

I have just seen that a resolved position has a link to the doc!

So, $pos.doc.resolve($pos.before(1)) would give a resolve position! But, this is not convenient to write… Having something like `$pos.$before(1)` that returns a resolved position would make it much easier, and now, everything could be done with resolved positions.
