Using .each
you can tap into the list of elements and simply add a space before each span
:
this.other.selectAll('div').data(this.words).call(function(){
this.enter().append('div').each(function() {
if (this.previousSibling && previousSibling.nodeValue == ' ') {
return; // Already have a space before this span, don't add another.
}
this.insertAdjacentHTML('beforebegin', ' ')
});
...
Another option is to use the ::after
pseudo-element and a non-breaking space character:
div {
display: inline-block;
}
div::after {
content: '\00a0'; // Non-breaking space.
}