Bug Tracker

Ticket #1733 (closed bug: fixed)

Opened 14 months ago

Last modified 7 weeks ago

addClass gives error on textnodes

Reported by: achun Owned by:
Priority: major Milestone: 1.2.2
Component: core Version: 1.2.1
Keywords: addClass Cc:
Needs: Commit

Description

$("<b>addClass</b>\n<b>addClass</b>").addClass('tmp')

error! in className:add fun

		add: function( elem, c ){
			jQuery.each( (c || "").split(/\s+/), function(i, cur){
				if ( !jQuery.className.has( elem.className, cur ) )
					elem.className += ( elem.className ? " " : "" ) + cur;
			});
		},

fix?

		add: function( elem, c ){
			jQuery.each( (c || "").split(/\s+/), function(i, cur){
				if ( !jQuery.className.has( elem.className||"", cur ) )
					elem.className += ( elem.className ? " " : "" ) + cur;
			});
		},

Attachments

1733.diff (2.5 KB) - added by brandon 14 months ago.
Patch + Tests for text nodes in jQuery.className methods

Change History

Changed 14 months ago by brandon

Patch + Tests for text nodes in jQuery.className methods

  Changed 14 months ago by brandon

  • need changed from Review to Commit

I've attached patch + tests to make jQuery.className.remove/add/has not fail when it gets a non element node.

However, I'm wondering if the better solution (or in addition to) should be that text nodes do not make it into the jQuery collection via jQuery.clean.

follow-up: ↓ 3   Changed 13 months ago by davidserduke

I don't think we can keep text nodes out of the jQuery collection. I looked in to that recently and realized there are several ways to get text nodes in. In fact, the function .contents() specifically includes text nodes.

http://docs.jquery.com/Traversing/contents

With that said, do you think it makes sense to implement the patch? It looks like a good solution to me.

in reply to: ↑ 2   Changed 13 months ago by brandon

Replying to davidserduke:

Yeah we definitely can't keep text nodes out now ... however we need to be mindful of this in our manipulation methods such as css, attr and the class methods. We should probably go ahead and be proactive and test/patch the other manipulation methods.

  Changed 12 months ago by davidserduke

  • summary changed from addClass? to addClass fails on textnodes

  Changed 12 months ago by davidserduke

  • summary changed from addClass fails on textnodes to addClass gives error on textnodes

  Changed 12 months ago by davidserduke

See #1039 for a similar ticket.

  Changed 12 months ago by davidserduke

  • status changed from new to closed
  • resolution set to fixed

Fixed in [4062] taking Brandon's suggestion that text nodes should not break jQuery even when they are in the jQuery object. addClass was fixed along with the other functions in the API.

Note: See TracTickets for help on using tickets.