...

View Full Version : Parsing text with markup into nodes



zetagauss
07-31-2004, 02:30 AM
Hey guys,

I'm trying to figure out how I can take user input from a textarea and insert it into a table as HTML. An example:


<script type="text/javascript">
<!--
function echo_input() {
var input = document.getElementById("user_input").value;
var cell = document.getElementById("display_area");
cell.childNodes[0].nodeValue = input;
}
//-->
</script>

<form>
<textarea id="user_input" name="user_input" onkeyup="echo_input();"></textarea>
</form>

<table>
<tr>
<td id="display_area">test</td>
</tr>
</table>

The childNodes[0] that the script keeps editing is the text node that starts out as "test". The problem is that I want the text that I put in the table to be parsed if any markup is in it (so that a <br /> will become a break, and not literally printed out as "<br />"), something that the DOM automatically escapes. It seems a very cumbersome means of doing this is to write a javascript parser, which would go through the text and create different types of nodes as it stumbles upon markup.

There as got to be a better way of doing this.

Does anyone know if there is some parsing function, or a way to pass a string to be evaluated and the correct nodes and heirarchy created?

Thanks!

jkd
07-31-2004, 05:17 AM
(new DOMParser()).parseFromString(theStr, "text/xml");

For Mozilla at least. :)

zetagauss
07-31-2004, 04:13 PM
Thanks! I tried this, and unfortunately the function kept complaining that the XML syntax was incorrect.

Here's what it ended up looking like:

<script type="text/javascript">
<!--
function echo_input() {
var input = document.getElementById("user_input").value;
var cell = document.getElementById("display_area");
input_nodes = (new DOMParser()).parseFromString(input, "text/xml");
for (var i = 0; i < input_nodes.childNodes.length; i++) {
cell.appendChild(input_nodes.childNodes[i]);
}
}
//-->
</script>

<form>
<textarea id="user_input" name="user_input" onkeyup="echo_input();"></textarea>
</form>

<table>
<tr>
<td id="display_area">test</td>
</tr>
</table>

I figured out that the problem was that the input wasn't being wrapped by tags. So then I tried this:

<script type="text/javascript">
<!--
function echo_input() {
var input = document.getElementById("user_input").value;
input = "<span>" + input + "</span>";
var cell = document.getElementById("display_area");
while (cell.hasChildNodes()) {
cell.removeChild(cell.lastChild);
}
input_nodes = (new DOMParser()).parseFromString(input, "text/xml");
for (var i = 0; i < input_nodes.childNodes.length; i++) {
cell.appendChild(input_nodes.childNodes[i]);
}
}
//-->
</script>

<form>
<textarea id="user_input" name="user_input" onkeyup="echo_input();"></textarea>
</form>

<table>
<tr>
<td id="display_area">test</td>
</tr>
</table>

Now that the function had stopped screaming, I found out that it's not being parsed as HTML (<center> doesn't center, and inline styles don't work). Do you have any suggestions about how to get the parser to recognize the input as HTML? I tried using (new DOMParser()).parseFromString(input, "text/html"), but that isn't defined.

There's got to be a way to do this other than with innerHTML.

jkd
08-01-2004, 01:43 AM
Just a guess, but instead of surrounding the input in "<span>"+bla+"</span>", maybe try:

'<span xmlns="http://www.w3.org/1999/xhtml">' + bla + '</span>'

Also, you are using nodes from a different document... you should be using importNode() to import to the page document first.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum