You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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);
}
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.How can I modify the
getRangyCreatedSpans
function to get all the span tags created byapplier.applyToRange(range);
?One solution would be to use
rangy.createClassApplier('myclass_new', rangyOptions)
making sure thatmyclass_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.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.
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
The text was updated successfully, but these errors were encountered: