Bug Tracker

Ticket #1024 (closed feature: fixed)

Opened 8 years ago

Last modified 7 years ago

children() and text nodes

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

Description (last modified by john) (diff)

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

comment:1 follow-up: ↓ 2 Changed 8 years ago by john

  • need set to Review
  • Type changed from enhancement to feature
  • Milestone set to 1.1.3

One possible solution:

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

comment:2 in reply to: ↑ 1 Changed 8 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 8 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 7 years ago by john

  • Status changed from new to closed
  • Version changed from 1.1.2 to 1.1.4
  • Resolution set to fixed
  • Description modified (diff)
  • Milestone changed from 1.1.3 to 1.2

Fixed in SVN rev [3019].

Note: See TracTickets for help on using tickets.