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
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Peculiarity with entities

    For the sake of this post, I'll just show the basics of this "problem". Here's a simple function...
    Code:
    function makeButton( val )
    {
    	var b = document.createElement( "input" );
    	b.type = "button";
    	b.value = val;
    	document.body.appendChild( b );
    }
    Just makes a button with the specified value, right? Well, if I want the value to contain an entity, such as » i just pass it that when I call the function
    Code:
    makeButton( "»" );
    I know what you're thinking, "What's the problem?", because everyting above works as expected. Here's the weird part: If the entity is hard-coded into the function, it doesn't work. So,
    Code:
    function makeButton()
    {
    	var b = document.createElement( "input" );
    	b.type = "button";
    	b.value = "»";
    	document.body.appendChild( b );
    }
    Dosn't actually insert "»" as the value, but the literal string "»"

    Is there some type of translation of the entity that occurs here? Between the function call and execution? I even thought that because the call is in the BODY and the function is in the HEAD that maybe the entity is valuated differently, but after moving the function to the BODY I find this isn't the case.

    I'm not really looking for a workaround or anything, just curious as to the nature of this behavior.

    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #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
    You are directly giving button the string, as opposed to the string being passed through the HTML or XML parser (expanding the entity), and then assigned.

    In theory, you could use document.createEntityReference('#187'), but not even Mozilla implements that method.

    Since you know it is #187 however, you can just parse out the &# and the ; from it, and escape it appropriately with JS:

    var num = parseInt(ent.replace(/\D/g, ''), 16);

    var esc = '\\x' + ((num < 16) ? '0' : '') + num.toString(16);

    var char = eval('"' + esc + '"');


  • #3
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Interesting, so at what point is the string passed to the function parsed, expanding the entity? Between call and receipt?
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #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
    Entities are expanded only once. That is when the XML parser runs through the page. It will not expand or even attempt to parse anything inside <![CDATA ]]> sections, however if your entity is anywhere else, it should be expanded. This includes attribute values, etc.

    If you dynamically create a string containing an entity, then assign that string to some visual property, it will not be parsed through the XML parser.


  •  

    Posting Permissions

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