Skip to main content

Bug Tracker

Side navigation

#1024 closed feature (fixed)

Opened March 06, 2007 01:05AM UTC

Closed August 31, 2007 03:38AM UTC

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

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.

Attachments (0)
Change History (4)

Changed April 27, 2007 10:22PM UTC by john comment:1

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)" ) );
};

Changed June 09, 2007 06:03PM UTC by jakecigar comment:2

Replying to [comment:1 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)" ) );
> };
> 

Changed June 15, 2007 12:44AM UTC by jakecigar comment:3

I am updating my plugin at

http://jqueryjs.googlecode.com/svn/trunk/plugins/textNodes/

it also included John's childNodes

Changed August 31, 2007 03:38AM UTC by john comment:4

description: 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.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.
milestone: 1.1.31.2
resolution: → fixed
status: newclosed
version: 1.1.21.1.4

Fixed in SVN rev [3019].