tag? How to put some HTML inside a tag without escaping from it? Or am I using th...">

Put some HTML inside the <pre class = "prettyprint-override"> tag?

How to put some HTML inside a tag without escaping from it? Or am I using the wrong tag?

PS I can not avoid HTML, it is created by the server.

+3
source share
2 answers

If you have no control over the released HTML, you can still encode it on the client side.

Here's how you could avoid all the markup inside tags <pre>using the jQuery library:

$(function() {
    var pre = $('pre');
    pre.html(htmlEncode(pre.html()));
});

function htmlEncode(value){ 
  return $('<div/>').text(value).html(); 
} 

Edit : on request, the same code without using jQuery:

function encodePreElements() {
    var pre = document.getElementsByTagName('pre');
    for(var i = 0; i < pre.length; i++) {
        var encoded = htmlEncode(pre[i].innerHTML);
        pre[i].innerHTML = encoded;
    }
};

function htmlEncode(value) {
   var div = document.createElement('div');
   var text = document.createTextNode(value);
   div.appendChild(text);
   return div.innerHTML;
}

And run encodePreElementsafter loading the DOM:

<body onLoad='encodePreElements()'>
    <pre>Foo <b>bar</b></pre>
</body>
+9
source

It:

    <pre>
      <&zwj;div>Hello<&zwj;/div>
    </pre>

Will print this:

<div>Hello</div>

Zero Width Joiner = &zwj;

+8
source

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


All Articles