question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

insertNode div, adds extra <p><br></p>

See original GitHub issue

When I try to insert a div via insertNode, for example a bootstrap Card block, while my cursor is at the start or end of a paragraph, then insertNode adds two nodes! It adds my own node and another node which contains an empty paragraph like:

<p><br></p>

steps to reproduce

Place cursor at start or end of a paragraph and then run the following via a plugin.

var template = $('<div class="card"><div class="card-body"><p>example</p></div></div>');
context.invoke('editor.insertNode', template[0]);

attempt to fix the bug

I tried to fix the problem in my plugin, by finding the empty paragraphs and removing them (in this case, theId is the ID of the node I’ve just added, used as a selector below).

// Remove previous empty paragraph
var emptyPar = $('#'+theId).prev();
if(emptyPar[0].innerHTML==='<br>')
	emptyPar.remove();

// Remove next empty paragraph
var emptyPar = $('#'+theId).next();
if(emptyPar[0].innerHTML==='<br>')
	emptyPar.remove();

The above code works as expected, the bootstrap div card is added and any extra empty paragraphs are removed.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
ghostcommented, Jan 5, 2018

I’m sorry but code doesn’t seem to be of any help, it can’t add something at the cursor position.

I did some more tests with insertNode and it seems like it has several problems, it doesn’t seem fully implemented. For example, sometimes it adds the empty paragraph I describe in this bug report, but sometimes it does not, I am not sure why but it feels inconsistent. As a separate but report, I’ve mentioned it allows you to break the html by adding the wrong tags within a paragraph tag.

Personally, I wish insertNode has a second parameter insertNode(somenode, “after”) so that the new node is added after the current paragraph and not inside it.

Do you think its possible to implement that please?

2reactions
seltix5commented, Nov 19, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to insert an element after another element in JavaScript ...
insertBefore is called with null as it's second argument, then it will add the node at the end of the collection. In other...
Read more >
Design Patterns with React Easy State | by Bertalan Miklos
Easy State is a practical Proxy-based state management library for React. Learn how to create applications with it through an app, which pairs...
Read more >
Bimsync Viewer API - Development
addBack() adds the previous set of elements on the stack — in this case $("div.after-addback") — to the current set, selecting both the...
Read more >
Diff - 4e1bce7498..0ff0ce95c4 - chromium/src - Git at Google
getWebContents(), "div"); + } catch (InterruptedException ... A preference that indicates that user accepted Voice Interaction Value Prop.
Read more >
How to Insert an Element After Another Element in JavaScript
Let's see which are they and try examples. To insert an element after another element you should use the insertBefore() method. Here is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found