xhtmldoctypexml-entities

xhtml adding entities


I have an Website which is delivered with as xhtml (with header an doctype). I get data from the server which sometimes contains some   entities what leads to an error when I try to set a string containing this via innerHTML. How I have already found out I can replace all of this items on serverside, but what I would like to know is:

Can I also add this entities to the doctype declaration instead of replacing it on server?

EDIT:

in other words, why does this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
[
    <!ENTITY nbsp  "&#160;">
]>

just not work?


Solution

  • Update 2:

    From http://www.w3.org/TR/2008/WD-html5-20080610/dom.html#innerhtml1 (2nd part on setting), the algorithm for XHTML does not seem to mention whether it is aware of the DOCTYPE information, though it is aware of the namespaces at least. (Note I switched to the simpler yet standards-compliant XHTML5 DOCTYPE.)

    I think you might either provide the following test-case and report as a bug, or perhaps bring it up on the WhatWG mailing list, as I think the behavior should at least be spec'd(unless it is, and I am just not seeing it):

    <!DOCTYPE html [
        <!ENTITY nbsp "&#160;">
    ]>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <div id="a">old&nbsp;text</div>
    <script><![CDATA[
    document.getElementById('a').innerHTML = 'new&nbsp;text';
    ]]></script>
    </html>
    

    In the meantime, if you don't want to replace on the server, you could use such a hack as below with DOMParser():

    <!DOCTYPE html [
        <!ENTITY nbsp "&#160;">
    ]>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <div id="a">old&nbsp;text</div>
    <script><![CDATA[
    
    document.getElementById('a').innerHTML = new DOMParser().parseFromString(
        '<!DOCTYPE html [<!ENTITY nbsp "&#160;">]>'+
        '<html xmlns="http://www.w3.org/1999/xhtml">'+
          'new&nbsp;text'+
        '</html>',
        'application/xhtml+xml'
      ).documentElement.innerHTML;
    
    ]]></script>
    </html>