Question

Is there a (already implemented) solution that decodes already encoded syntax from googles code-prettify?

<?php
$foo = "bar";

gets to

<pre class="prettyprint prettyprinted"><span class="pun">&lt;?</span><span class="pln">php
$foo </span><span class="pun">=</span><span class="pln"> </span><span class="str">"bar"</span><span class="pun">;</span></pre>

Now I want decode it again to

<?php
$foo = "bar";
Était-ce utile?

La solution

The textContent DOM property will let you access the plain text content of a DOM node, so just get the prettified <pre> element.

document.getElementsByClassName('prettyprinted')[0].textContent

Since textContent just concatenates text nodes, it works for prettyprinted <pre> and <xmp> elements and <code> elements that have CSS style whitespace:pre.

It should work for elements with linenums where prettify inserts an <li> element per line because the text still contains the original line-breaks, but I would test that separately if you rely upon that.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top