Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rangy how to get all the spans created by applyToRange #441

Open
dportabella opened this issue Mar 8, 2018 · 0 comments
Open

rangy how to get all the spans created by applyToRange #441

dportabella opened this issue Mar 8, 2018 · 0 comments

Comments

@dportabella
Copy link

dportabella commented Mar 8, 2018

On Google Chrome:

I use rangy applier.applyToRange(range) to create span tags on a range.
Then I need to get the list of all the span tags created by rangy.
However, range.getNodes([1]) returns empty for a selected text, when this text is a one unique word surrounded by tag.

var range = rangy.createRangyRange();
range.setStart(firstNode, offsetFirstNode);
range.setEnd(lastNode, offsetSecondNode);
var rangyOptions = { useExistingElements: false };
var applier = rangy.createClassApplier('myclass', rangyOptions);
applier.applyToRange(range);

function getRangyCreatedSpans(range, theClass): HTMLElement[] {
  return range.getNodes([1], function(el) {
    return el.tagName == "SPAN" && el.className == theClass;
  });
}

tags = getRangyCreatedSpans(range, 'myclass');

How can I modify the getRangyCreatedSpans function to get all the span tags created by applier.applyToRange(range);?

One solution would be to use rangy.createClassApplier('myclass_new', rangyOptions) making sure that myclass_new is not used before in the document,
and then use document.getElementsByClassName('myclass_new') to get that list. (I could then modify the classname of all those tags to the one I really needed).
But this is a hacky solution. How to get this using rangy?

Another solution would be to use onElementCreate to pass a function to build a list of the created span tags. But this is a hacky solution as well.

var rangyOptions = {
  useExistingElements: false,
  onElementCreate: myFunctionThatBuildsAListOfSpansCreated
};
applier.applyToRange(range);

How to get all the spans created by applyToRange without using hacky solutions?


I provide this code in order to illustrate the problem (this code is using applyToSelection; but my final solution needs applyToRange).

Test: double click on "oneword" to select the text, and then press the button. it shows that getRangyCreatedSpans 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() {
  var rangyOptions = { useExistingElements: false };
  var applier = rangy.createClassApplier('myclass', rangyOptions);
  applier.applyToSelection();

  var range = rangy.getSelection().getRangeAt(0);
  var spans = range.getNodes([1], function(el) {
    return el.tagName == "SPAN" && el.className == 'myclass';
  });

  console.log(spans);
  console.log(spans.length);
  alert(spans.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>

Note: The previous code has a different behaviour on Google Chrome and Safari. Ideally I would need a solution that works on all browsers.

Note: issue on stackoverflow: https://stackoverflow.com/questions/49156179/rangy-how-to-get-all-the-spans-created-by-applytorange

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant