Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Parsing text with markup into nodes

    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:

    Code:
    <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!

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    (new DOMParser()).parseFromString(theStr, "text/xml");

    For Mozilla at least.

  • #3
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks! I tried this, and unfortunately the function kept complaining that the XML syntax was incorrect.

    Here's what it ended up looking like:
    Code:
    <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:
    Code:
    <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.

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •