Bug Tracker

Opened 15 years ago

Closed 14 years ago

#1024 closed feature (fixed)

children() and text nodes

Reported by: vmx Owned by:
Priority: minor Milestone: 1.2
Component: core Version: 1.1.4
Keywords: Cc:
Blocked by: Blocking:

Description (last modified by john)

It would be nice if children() would also return text nodes. Of course the default behaviour shouldn't be changed. But what about an additional parameter you can set to "true" if you want text nodes to be returned, too. An additional check for every text node would be needed, as phantom nodes shouldn't be return though.

Change History (4)

comment:1 Changed 15 years ago by john

Milestone: 1.1.3
need: Review
Type: enhancementfeature

One possible solution:

jQuery.fn.childNodes = function() {
     return this.pushStack( jQuery.map( this, "jQuery.makeArray(a.childNodes)" ) );
};

comment:2 in reply to:  1 Changed 15 years ago by jakecigar

Replying to john: Does this really need to be in core? a plugin to deal with those nasty textNodes is more to my liking. this code is still in testing, I will release as a plugin if you like.

jQuery.fn.span = function() {return this.wrap('<span/>').parent()};
jQuery.fn.split = function(re,f) {
	var text=[];
	this.each(function(){
		var tnp = this.parentNode;
		var splits = this.nodeValue.split(re);
		for (var i=0;i<splits.length;i++){
			var t = document.createTextNode(f ? f(splits[i]) : splits[i]);
			tnp.insertBefore(t,this);
			text.push(t);
		};
		tnp.removeChild(this);
	});
	return this.pushStack( text );
};
jQuery.fn.replace  = function(re,f) {
	var text=[], tNodes=false;
	this.each(function(){
		var $this = jQuery(this);
		if (this.nodeType == 3){
			tNodes=true;
			text.push(this.parentNode.insertBefore(document.createTextNode(this.nodeValue.replace(re,f)),this));
			this.parentNode.removeChild(this)
		}else{
			text.push($this.text($this.text().replace(re,f)))
		}
	});
	return this.pushStack(tNodes ? text : this);
};
jQuery.fn.textNodes = function() {
	var text=[];
	this.each(function(){
		var children =this.childNodes;
		for (var i = 0; i < children.length; i++){
			var child = children[i];
			if (child.nodeType == 3) text.push(child);
			}
	})
	return this.pushStack(text);
};

One possible solution:

jQuery.fn.childNodes = function() {
     return this.pushStack( jQuery.map( this, "jQuery.makeArray(a.childNodes)" ) );
};

comment:3 Changed 15 years ago by jakecigar

I am updating my plugin at http://jqueryjs.googlecode.com/svn/trunk/plugins/textNodes/

it also included John's childNodes

comment:4 Changed 14 years ago by john

Description: modified (diff)
Milestone: 1.1.31.2
Resolution: fixed
Status: newclosed
Version: 1.1.21.1.4

Fixed in SVN rev [3019].

Note: See TracTickets for help on using tickets.