expanding character entity references in javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Higson

    expanding character entity references in javascript

    Does anyone know a technique in javascript to transform from (for example)
    ♥ to the char '♥'?

    I'm doing this because I have to interpret some data I got over XHTMLHTTP
    that isn't XML, but might contain some XML char entities.

    Thanks,
    Jim
  • Jim Higson

    #2
    Re: expanding character entity references in javascript

    Jim Higson wrote:
    [color=blue]
    > Does anyone know a technique in javascript to transform from (for example)
    > ♥ to the char '♥'?
    >
    > I'm doing this because I have to interpret some data I got over XHTMLHTTP
    > that isn't XML, but might contain some XML char entities.[/color]

    Btw, I'm using XHTML so setting innerHTML on a temp element and then reading
    the contents isn't really an option. Under moz this gives an error because
    innerHTML is read only.

    Jim

    Comment

    • Douglas Crockford

      #3
      Re: expanding character entity references in javascript

      > Does anyone know a technique in javascript to transform from (for example)[color=blue]
      > ♥ to the char '♥'?
      >
      > I'm doing this because I have to interpret some data I got over XHTMLHTTP
      > that isn't XML, but might contain some XML char entities.[/color]

      String.prototyp e.deentityify = function (o) {
      var i, j, s = this, o = String.prototyp e.deentityify.d ata, v;
      for (;;) {
      i = s.lastIndexOf(' &');
      if (i < 0) {
      break;
      }
      j = s.indexOf(';', i);
      if (i + 1 >= j) {
      break;
      }
      v = o[s.substring(i + 1, j)];
      if (!v) {
      break;
      }
      s = s.substring(0, i) + v + s.substring(j + 1);
      }
      return s;
      }

      String.prototyp e.deentityify.d ata = {
      apos: "'",
      lt: '<',
      gt: '>'};

      var s = "&lt;cool&gt;". deentityify();

      JSLint, The JavaScript Code Quality and Coverage Tool. This file allows JSLint to be run from a web browser. It can accept a source program and analyze it without sending it over the network.

      Comment

      • Jim Higson

        #4
        Re: expanding character entity references in javascript

        Douglas Crockford wrote:
        [color=blue][color=green]
        >> Does anyone know a technique in javascript to transform from (for
        >> example) &hearts; to the char '♥'?
        >>
        >> I'm doing this because I have to interpret some data I got over XHTMLHTTP
        >> that isn't XML, but might contain some XML char entities.[/color]
        >
        > String.prototyp e.deentityify = function (o) {
        > var i, j, s = this, o = String.prototyp e.deentityify.d ata, v;
        > for (;;) {
        > i = s.lastIndexOf(' &');
        > if (i < 0) {
        > break;
        > }
        > j = s.indexOf(';', i);
        > if (i + 1 >= j) {
        > break;
        > }
        > v = o[s.substring(i + 1, j)];
        > if (!v) {
        > break;
        > }
        > s = s.substring(0, i) + v + s.substring(j + 1);
        > }
        > return s;
        > }
        >
        > String.prototyp e.deentityify.d ata = {
        > apos: "'",
        > lt: '<',
        > gt: '>'};
        >
        > var s = "&lt;cool&gt;". deentityify();
        >
        > http://www.JSLint.com[/color]


        Thanks for the response, but I needed something that can handle any char
        reference. So I built pu the hash with some js code generated from a quick
        Perl script taking values from the W3C's XHTML DTD. Add a bit of regex and
        I had it done straight away before I saw your response. Once I'd resigned
        to coding this in javascript (instead of tricking the browser into decoding
        the references) it was actually pretty easy.

        I'll post the output in case anyone wants to do this (it's about 3k, or 1k
        with content-type gzip)


        var CHAR_REF_REGEX = /&(\w{2,7}|#\d{3 ,4});/g;
        function expand_char_ref erences( str )
        {
        var rtn = "";
        var hit;
        var last_ref_end = 0;
        while( (hit = CHAR_REF_REGEX. exec( str )) != null )
        {
        var charcode = new Number( hit[1] );

        if( isNaN( charcode ) )
        charcode = CHAR_ENTITIES[ hit[1].toLowerCase() ];

        rtn += str.substring( last_ref_end , hit.index ) +
        String.fromChar Code( charcode );
        last_ref_end = hit.index + hit[0].length;
        }
        // if no matches, this will just be all of str:
        rtn += str.substring( last_ref_end );
        return rtn;
        }
        var
        CHAR_ENTITIES={ 'nbsp':160,'iex cl':161,'cent': 162,'pound':163 ,'curren':164,' yen':165,'brvba r':166,'sect':1 67,'uml':168,'c opy':169,'ordf' :170,'laquo':17 1,'not':172,'sh y':173,'reg':17 4,'macr':175,'d eg':176,'plusmn ':177,'sup2':17 8,'sup3':179,'a cute':180,'micr o':181,'para':1 82,'middot':183 ,'cedil':184,'s up1':185,'ordm' :186,'raquo':18 7,'frac14':188, 'frac12':189,'f rac34':190,'iqu est':191,'agrav e':192,'aacute' :193,'acirc':19 4,'atilde':195, 'auml':196,'ari ng':197,'aelig' :198,'ccedil':1 99,'egrave':200 ,'eacute':201,' ecirc':202,'eum l':203,'igrave' :204,'iacute':2 05,'icirc':206, 'iuml':207,'eth ':208,'ntilde': 209,'ograve':21 0,'oacute':211, 'ocirc':212,'ot ilde':213,'ouml ':214,'times':2 15,'oslash':216 ,'ugrave':217,' uacute':218,'uc irc':219,'uuml' :220,'yacute':2 21,'thorn':222, 'szlig':223,'ag rave':224,'aacu te':225,'acirc' :226,'atilde':2 27,'auml':228,' aring':229,'ael ig':230,'ccedil ':231,'egrave': 232,'eacute':23 3,'ecirc':234,' euml':235,'igra ve':236,'iacute ':237,'icirc':2 38,'iuml':239,' eth':240,'ntild e':241,'ograve' :242,'oacute':2 43,'ocirc':244, 'otilde':245,'o uml':246,'divid e':247,'oslash' :248,'ugrave':2 49,'uacute':250 ,'ucirc':251,'u uml':252,'yacut e':253,'thorn': 254,'yuml':255, 'lt':38,'gt':62 ,'amp':38,'apos ':39,'quot':34, 'oelig':338,'oe lig':339,'scaro n':352,'scaron' :353,'yuml':376 ,'circ':710,'ti lde':732,'ensp' :8194,'emsp':81 95,'thinsp':820 1,'zwnj':8204,' zwj':8205,'lrm' :8206,'rlm':820 7,'ndash':8211, 'mdash':8212,'l squo':8216,'rsq uo':8217,'sbquo ':8218,'ldquo': 8220,'rdquo':82 21,'bdquo':8222 ,'dagger':8224, 'dagger':8225,' permil':8240,'l saquo':8249,'rs aquo':8250,'eur o':8364,'fnof': 402,'alpha':913 ,'beta':914,'ga mma':915,'delta ':916,'epsilon' :917,'zeta':918 ,'eta':919,'the ta':920,'iota': 921,'kappa':922 ,'lambda':923,' mu':924,'nu':92 5,'xi':926,'omi cron':927,'pi': 928,'rho':929,' sigma':931,'tau ':932,'upsilon' :933,'phi':934, 'chi':935,'psi' :936,'omega':93 7,'alpha':945,' beta':946,'gamm a':947,'delta': 948,'epsilon':9 49,'zeta':950,' eta':951,'theta ':952,'iota':95 3,'kappa':954,' lambda':955,'mu ':956,'nu':957, 'xi':958,'omicr on':959,'pi':96 0,'rho':961,'si gmaf':962,'sigm a':963,'tau':96 4,'upsilon':965 ,'phi':966,'chi ':967,'psi':968 ,'omega':969,'t hetasym':977,'u psih':978,'piv' :982,'bull':822 6,'hellip':8230 ,'prime':8242,' prime':8243,'ol ine':8254,'fras l':8260,'weierp ':8472,'image': 8465,'real':847 6,'trade':8482, 'alefsym':8501, 'larr':8592,'ua rr':8593,'rarr' :8594,'darr':85 95,'harr':8596, 'crarr':8629,'l arr':8656,'uarr ':8657,'rarr':8 658,'darr':8659 ,'harr':8660,'f orall':8704,'pa rt':8706,'exist ':8707,'empty': 8709,'nabla':87 11,'isin':8712, 'notin':8713,'n i':8715,'prod': 8719,'sum':8721 ,'minus':8722,' lowast':8727,'r adic':8730,'pro p':8733,'infin' :8734,'ang':873 6,'and':8743,'o r':8744,'cap':8 745,'cup':8746, 'int':8747,'the re4':8756,'sim' :8764,'cong':87 73,'asymp':8776 ,'ne':8800,'equ iv':8801,'le':8 804,'ge':8805,' sub':8834,'sup' :8835,'nsub':88 36,'sube':8838, 'supe':8839,'op lus':8853,'otim es':8855,'perp' :8869,'sdot':89 01,'lceil':8968 ,'rceil':8969,' lfloor':8970,'r floor':8971,'la ng':9001,'rang' :9002,'loz':967 4,'spades':9824 ,'clubs':9827,' hearts':9829,'d iams':9830}



        Comment

        Working...