timdown/rangy

rangy.getSelection().getRangeAt(0).getNodes([1]) returns empty

dportabella opened this issue · 1 comments

rangy.getSelection().getRangeAt(0).getNodes([1]) returns empty for a selected text, when this text is a one unique word surrounded by tag.

Test: double click on "oneword" to select the text, and then press the button. it shows that getNodes returns an empty array (this looks like a bug of rangy). note: you can see that rangy created the span tag and it is not yellow. select the "two words" or a part of it, and click test, and you will see that getNodes returns a non-empty array (as expected). remember to reload the page when testing, as rangy is adding spans.

rangy.init();

function test() {
  rangyOptions = { useExistingElements: false };
  applier = rangy.createClassApplier("myclass", rangyOptions);
  applier.applyToSelection();

  var nodes = rangy.getSelection().getRangeAt(0).getNodes([1]);
  console.log(nodes);
  console.log(nodes.length);
  alert(nodes.length);
}
.myclass {
    background-color: rgba(255, 255, 0, 1);
}
<div>two words</div>
<div>oneword</div>
<button onclick="test()">test</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rangy/1.3.0/rangy-core.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rangy/1.3.0/rangy-classapplier.js"></script>

Issue from:
https://stackoverflow.com/questions/49156179/rangy-getselection-getrangeat0-getnodes1-returns-empty

I've opened a new issue better explained:
#441