# Transaction.replaceSelection() \> only replaces the text from first Node in the Slice

**URL:** <https://discuss.prosemirror.net/t/transaction-replaceselection-only-replaces-the-text-from-first-node-in-the-slice/3390>\
**Category:** Uncategorized\
**Created:** [January 6, 2021, 7:56pm UTC](https://discuss.prosemirror.net/t/transaction-replaceselection-only-replaces-the-text-from-first-node-in-the-slice/3390 "2021-01-06T19:56:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![raphaelsantos](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/raphaelsantos/32/1349_2.png) [@raphaelsantos](https://discuss.prosemirror.net/u/raphaelsantos)\
**Post date:** [January 6, 2021, 7:56pm UTC](https://discuss.prosemirror.net/t/transaction-replaceselection-only-replaces-the-text-from-first-node-in-the-slice/3390/1 "2021-01-06T19:56:29Z")

</div>

I implemented a custom **handlePaste** method using **EditorProps** interface where I use **Transaction.replaceSelection()**, pretty similar way to the original pasting implementation, I just update a couple nodes in the slice before doing the replacement.

However I noticed that the first Node in my Slice has its attributes overriden by those of the Node that is the selection (they share the same NodeType). Every other node in the slice keeps their attributes as expected.

I was diving a bit into the code with breakpoints and noticed that it happens within **Fitter.findFittable()**, most specifically in this highlighted line:

> <https://github.com/ProseMirror/prosemirror-transform/blob/c3bbb16560f2b0f1df3a79a3237ffa051bf747a7/src/replace.js#L138>

It makes so the fragment being added (for the first line) is the TextNode and not the Node itself. So when pasting a number of nodes, the first one loses all its data, only the TextNode is preserved.

Is this expected behavior when using **replaceSelection**? If so, how can I work around it so the entire pasted content remains unchanged, wherever it gets pasted?

---

<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:** [January 6, 2021, 8:43pm UTC](https://discuss.prosemirror.net/t/transaction-replaceselection-only-replaces-the-text-from-first-node-in-the-slice/3390/2 "2021-01-06T20:43:35Z")

</div>

> Is this expected behavior when using replaceSelection?

Yes, if the selection starts _inside_ a given node, and the slice you provide it starts with an open node, the replace fitter will try to fit the content of that open node into the parent of the start position. Your problem might be with the `openStart` value of your slice. `replaceSelection` goes through `replaceRange`, which will do some magic to expand the replaced range when appropriate (for example when the selection starts at the start of a node and ends at the end of a node, and the replaced content could replace that node), but if the replacing nodes are open that won’t kick in.

---

<div class="post-metadata">

**Author:** ![raphaelsantos](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/raphaelsantos/32/1349_2.png) [@raphaelsantos](https://discuss.prosemirror.net/u/raphaelsantos)\
**Post date:** [January 6, 2021, 10:23pm UTC](https://discuss.prosemirror.net/t/transaction-replaceselection-only-replaces-the-text-from-first-node-in-the-slice/3390/3 "2021-01-06T22:23:56Z")

</div>

I tried forcing `openStart` to **0** when pasting into an empty line (`node.content.size === 0`) and it seems to work as I expect. Haven’t run into side effects so far.

Thanks.
