Skip to content

Library does not read CDATA sections #13

Closed
irnc opened this Issue Oct 15, 2012 · 5 comments

3 participants

@irnc
irnc commented Oct 15, 2012

For example from string <phrase section="default" code="widgethome"><![CDATA[Home]]></phrase> only attributes are read, resulting object does not have '#' property.

@SaltwaterC
Owner

The library usually handled root elements that have child nodes. The CDATA was only implemented for the child nodes. I'll make a patch.

@irnc
irnc commented Oct 18, 2012

Actually when I tested, it does not worked even on child nodes. XML string was as follows (with XML declaration included):

<lang>
<phrase section="default" code="widgethome"><![CDATA[Home]]></phrase>
</lang>
@SaltwaterC
Owner

Doesn't work when the child nodes have attributes. There's an unit test that has a CDATA element from a WordPress feed. Thank you for your feedback. I'll keep you posted.

@gbaumgart

i am not quite sure that works for you, but it does solve the problem for me :

if (children[i].name() == 'text' && children[i].type() == 'text') {
jsobj['#'] = children[i].text().trim();
if (jsobj['#'].match(/^\s*$/)) {
delete(jsobj['#']);
}
for (var j = 0, chtlen = children[i].childNodes().length; j < chtlen; j++) {
if (children[i].child(j).name() == 'text') {
var text = {}, textattrs = children[i].child(j).attrs();
text['#'] = children[i].child(j).text();
if (textattrs.length > 0) {
text['@'] = {};
}

                for (var k = 0, atlen = textattrs.length; k < atlen; i++) {
                    text['@'][textattrs[k].name()] = textattrs[k].value();
                }
                jsobj['text'] = text;
                break; // only allow one "<text></text>" element for now
            }
        }
        continue;
    }else if(children[i].type()=='cdata')
    {

//added :
var val = children[i].toString().trim();
val = val.replace(/^<![CDATA[/, '').replace(/]]>$/, '');
jsobj['#']=val;
}

@SaltwaterC
Owner

Thanks. I'll have a look and write some tests after dealing with a couple of other libraries. I am well aware that the algorithm isn't quite straight forward. It took me a while till I understood what Brian White did.

I totally forgot about this issue as it didn't fit then in my schedule. Sorry about that. Remembered about it yesterday when I started to rewrite some of my test suites and integrate my stuff with Travis CI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.