Bug Tracker

Opened 12 years ago

Closed 12 years ago

Last modified 10 years ago

#1888 closed bug (fixed)

jQuery(string) create not valid DOM when string is html with newlines

Reported by: lucifer4u Owned by:
Priority: major Milestone: 1.2.2
Component: ajax Version: 1.2.1
Keywords: Cc:
Blocked by: Blocking:

Description

It is common task to get html from server and to work with it in its own context. I have test.html file with this content:

<html>
  <head>
    <title>
      Test page
    </title>
  </head>
  <body>
    <div>
      Some information
    </div>
  </body>
</html>

So after trying

$.get("test.html",function(data){alert($(data).find("div");}) 

i found an error. Here is more info about:

Test examples:

			var s1 = '<html><head><title>info</title></head><body><div>info</div></body></html>';
			alert($(s1).find('div')); // [object Object]
			var s2 = '<html>\n<head><title>info</title></head><body><div>info</div></body></html>';
			alert($(s2).find('div')); // [error]

Solution:

			function jquery_from_html(data) {
				return $("<moo/>").append(data);
			}

Example of usage:

			$.get(
				"test.html",
				{},
				function(data){
					d = $("div#system", jquery_from_html(s));
					alert(d.text());		
				},
				"html"
			)

Found in jquery source in load function:

						// Create a dummy div to hold the results
						jQuery("<div/>")

Attachments (1)

jquery_test.html (977 bytes) - added by davidserduke 12 years ago.
test case

Download all attachments as: .zip

Change History (3)

comment:1 Changed 12 years ago by joern

Component: coreajax

comment:2 Changed 12 years ago by davidserduke

Resolution: fixed
Status: newclosed

Fixed in [4062]. It was fixed when #1039 and #1733 were fixed.

Changed 12 years ago by davidserduke

Attachment: jquery_test.html added

test case

Note: See TracTickets for help on using tickets.