Skip to main content

Bug Tracker

Side navigation

#2425 closed bug (invalid)

Opened February 27, 2008 03:33PM UTC

Closed January 25, 2011 12:45AM UTC

Last modified March 14, 2012 05:26AM UTC

.text() and CDATA

Reported by: john Owned by: john
Priority: low Milestone:
Component: core Version: 1.4.3
Keywords: Cc:
Blocked by: Blocking:
Description

Currently accessing CDATA contents can be pretty annoying across platforms (IE is different from the rest. .text() should be made to handle these differences.

Attachments (0)
Change History (11)

Changed February 28, 2008 09:14AM UTC by Markus.Staab comment:1

+1 there should be a crossbrowser solution

Changed September 27, 2008 02:51AM UTC by raccettura comment:2

I've just re-encountered this. For the curious my workaround is instead of using .text() to use this plugin I created (simply replace .text() with .getCDATA():

jQuery.fn.getCDATA = function() {
	if($.browser.msie)
		return this[0].childNodes[0].nodeValue;

	// Other browsers do this
	return this[0].childNodes[1].nodeValue;
};

It ain't pretty, but in my case did the job.

Changed September 28, 2008 09:25PM UTC by dan_nl comment:3

Are you specifying the dataType in your ajax call? If specified as xml .text() should get you the node text within the CDATA; same goes for the .get method.

$.ajax({

type: "GET",

url: "your.xml",

dataType: "xml"

success: your_function

});

Changed September 28, 2008 10:42PM UTC by raccettura comment:4

I'm not specifying a type. The response from the server is application/xml, so jQuery should be treating it as such.

Per the docs:

The type of data that you're expecting back from the server. If none is specified, jQuery will intelligently pass either responseXML or responseText to your success callback, based on the MIME type of the response

Based on this, jQuery should correctly handle this.

Now I'm wondering if the bug is in jQuery's detection methods or in it's ability to parse CDATA.

Changed November 01, 2010 08:53PM UTC by rwaldron comment:5

keywords: → needsreview
milestone: 1.2.4
owner: → john
status: newpending

Thanks for taking the time to contribute to the jQuery project! Please provide a jsFiddle that outlines practical use cases for your enhancement request.

Changed November 10, 2010 10:51PM UTC by rwaldron comment:6

keywords: needsreview
priority: majorlow

Changed November 19, 2010 12:02PM UTC by andrew.murphy@btinternet.com comment:7

Doesn't work for me.

When fetching XML, our server doesn't send the right mime-type,

  • if I specify datatype, needs to be browser specific (FF, IE) : 'xml' or 'text/xml' get it the wrong way round => barfs
  • neither way parses CDATA correctly...
  • whichever datatype is specified, (or if I don't pass the datatype field) data is treated as HTML
  • - which works fine... until a CDATA field (which is bad HTML)

Bug

  • respect the datatype field, and make it cross browser (i.e. ignore server mime-type if datatype is set)

Workaround

  • use datatype plain text
  • then convert using "normal" JS

if ( window.DOMParser )

{var parser = new DOMParser(); xml = parser.parseFromString( data, "text/xml" );}

else

{ xml = new ActiveXObject("Microsoft.XMLDOM"); xml.async = "false"; xml.loadXML( data ); }

Which leads to a:

Feature Request

  • add: xml_to_text() and text_to_xml()

Changed December 04, 2010 08:05AM UTC by trac-o-bot comment:8

resolution: → invalid
status: pendingclosed

Because we get so many tickets, we often need to return them to the initial reporter for more information. If that person does not reply within 14 days, the ticket will automatically be closed, and that has happened in this case. If you still are interested in pursuing this issue, feel free to add a comment with the requested information and we will be happy to reopen the ticket if it is still valid. Thanks!

Changed January 18, 2011 03:39PM UTC by andrew.murphy@btinternet.com comment:9

Bug mentioned in comment #8 happened in version 1.4.3.

Still valid.

Changed January 18, 2011 05:40PM UTC by danheberden comment:10

resolution: invalid
status: closedreopened
version: 1.2.31.4.3

I'd like to see a test case of this with the latest build of jquery

Changed January 25, 2011 12:45AM UTC by snover comment:11

resolution: → invalid
status: reopenedclosed

Please reopen/comment on this ticket only if you have a valid test case for the latest version of jQuery.