InnerText polyfill for Firefox

In WebKit innerText, it seems the text of the element is returned, which the user sees is exactly what I need.

Are there policies for Firefox?

For instance:

<div id = '1'> <br> f <div style = 'display: none'> no </div> oo bar </div>

<script>
function test () {return document.getElementById ('1'). innerText}
</script>

Function test will return "\ n foo bar".

The goal is to make an editable text area in which links are available, and where tags are highlighted and where anchor and selection are created on the fly during input.

My approach:

In each keyboard:

  • keep cursor position
  • cut the text with innerText
  • parse links and tags of text returned innerText

!

+3
2

toString() Selection Firefox, innerText. innerText , , .

function getInnerText(el) {
    var text = "";
    if (typeof window.getSelection != "undefined") {
        var sel = window.getSelection();
        sel.removeAllRanges();
        var range = document.createRange();
        range.selectNodeContents(el);
        sel.addRange(range);
        text = sel.toString();
        sel.removeAllRanges();
    }
    return text;
}
+1

Source: https://habr.com/ru/post/1768996/


All Articles