I am attempting to make an extremely simple prettyprint function with jQuery, but I do not know how to find elements, attributes, and values (shown in the jsfiddle).
I am trying to accomplish the following:
- Wrap elements with
<span class="element" />
- Wrap attributes with
<span class="attribute" />
- Wrap values with
<span class="value" />
- Replace
<
with<
- Replace
>
with>
Here is the current jQuery that I have:
$(document).ready(function() {
$('pre.prettyprint').each(function() {
$(this).css('whitespace','pre-line');
/* Why isnt ^this^ working? */
var code = $(this).html();
// How should I define the following
var element = $(code).find(/* ELEMENTS */);
var attribute = $(code).find(/* ATTRIBUTES */);
var value = $(code).find(/* Values */);
$(element).wrap('<span class="element" />');
$(attribute).wrap('<span class="attribute" />');
$(value).wrap('<span class="value" />');
$(code).find('<').replaceWith('<');
$(code).find('>').replaceWith('>');
});
});
Which is attempting to format this:
<pre class="prettyprint">
<a href="http://website.com">Visit Website</a>
<a href="#top">Back to Top</a>
</pre>
into this:
<pre class="prettyprint">
<span class="element">a <span class="attribute">href</span>=<span class="value">”http://website.com”</span></span>Visit Website<span class="element">/a</span>
<br/>
<span class="element">a <span class="attribute">href</span>=<span class="value">”#top”</span></span>Back to Top<span class="element">/a</span>
</pre>
Thank you ahead of time!
You can see the jsfiddle here: http://jsfiddle.net/JamesKyle/L4b8b/
whitespace
bewhite-space
? – Parasitology