[jQuery] Find textNodes and wrapping it with a tag

[jQuery] Find textNodes and wrapping it with a tag


For my List tree plugin, it needs to handle situations where the
structure contains LI tags with text and not text wrapped with a HTML
tag (normally a A tag)
For example:
<ul>
<li> title
<ul>
<li>...<li>
<li>...<li>
</ul>
</li>
</ul>
My CSS and tree code handles it nicely when the text wrapped with a
tag like:
<li> <a>title</a>
But when its not wrapped, I tried to use CSS to adjust it but can't
find a CSS selector that deal with this without screwing up the tree.
So I figure I could do it with code
$('li').filter(function(k) {
if (this.firstChild.nodeType == 3) {
var s = "";
s += "<a>";
s += this.firstChild.nodeValue
s += "</a>";
this.firstChild.nodeValue = s;
return true;
}
return false;
});
Of course, its not correct since the nodeType is still 3.
This didn't work right neither, since it wraps the entire inner
content of <li></li>
var $t = $('li').filter(function(k) {
return (this.firstChild.nodeType == 3);
});
$t.wrap("<a></a>").
So short of removeNode()/createNode, if there a jQuery way to do this?
TIA
---
HLS