NullVoxPopuli
NullVoxPopuli

Reputation: 65103

jQuery, how to test of a variable is a text node, containing no markup?

http://jsfiddle.net/DerNalia/zrppg/8/

I have two lines of code that pretty much do the same thing

var doesntbreak = $j("hello");
var breaks = $j(" "); 

​The first one doesn't error, but the second one throws this

  Syntax error, unrecognized expression:  

should'nt they both behave the same?

any insight as to how to solve this? in the actual method I'm using, ele is from the Dom, so it could eb a text node, or any other kind of node.

UPDATE:

the input to the function that I'm using that I noticed this takes selection from the dom. updated example: http://jsfiddle.net/DerNalia/zrppg/11/ <- includes html markup.

So, I guess, my question is, how do I test if something is JUST a text node? and doesn't contain any markup?

Upvotes: 0

Views: 436

Answers (5)

Damodar Bashyal
Damodar Bashyal

Reputation: 941

Not actual answer, but may help someone with similar issue as mine and loosely related to this question. :)

I was getting same issue today, so fixed by removing &nbsp;

Changed:

var breaks = $j("&nbsp;");

to:

var breaks = $j("&nbsp;".replace(/&.*;/g, ""));

Here I am removing &nbsp;, &lt; etc...

Note: value at &nbsp; is dynamic for me, so it can be anything.

Upvotes: 0

NullVoxPopuli
NullVoxPopuli

Reputation: 65103

So, I have to thank Apsillers and Rolando for pointing me in the right direction. Their answers were very close, but gave me the information I needed.

This is what I ended up using:

TEXT_NODE = 3;
objectify = function(n) {
    return $j("<div></div>").html(n).contents();
}

function textOnly(n) {
    var o = objectify(n);
    for (var i = 0; i < o.length; i++) {
        if (objectify(o[i])[0].nodeType != TEXT_NODE) {
            return false
        }
    }
    return true;
}

And here is a

jsFiddle

with some test cases, that neither of the original code submissions passed.

to pass, it needed to handle this kind of input

"hello" // true
"hello<b>there</b>" // false
"<b>there</b>" // false
"&nbsp;" // false

Upvotes: 0

Rolando Corratge Nieves
Rolando Corratge Nieves

Reputation: 1233

try this:

function isTextNode(node){
    div=document.createElement('div');
    div.innerHTML=node;
    return $(div).text()==$(div).html();
}

And "&nbsp;" is'nt a valid selector if you want to find a elements containing some text you must use the :contains selector http://api.jquery.com/contains-selector/

Upvotes: 1

apsillers
apsillers

Reputation: 115920

In general, you cannot create standalone text nodes with the jQuery function. If a string isn't obviously HTML, it gets treated as a selector, and &nbsp; is not recognized by jQuery as a valid selector.

Assuming you want to parse arbitrary strings (which may have HTML tags or not), I suggest something like var result = $('<div></div>').html('&nbsp;').contents();. Place your your HTML or text string in a div to parse it and then immediately extract the parsed result as a jQuery object with the list of elements. You can append the resultant list of elements with $(parentElem).append(result);

Upvotes: 1

Pointy
Pointy

Reputation: 413702

Internet Explorer (older versions at least) don't have built in "querySelector" functions, so the Sizzle engine has to do the work directly. Thus, the slightly different tolerances for bogus input can cause differences in error reporting.

Your selector expression "&nbsp;" is equally invalid in all browsers, however. The library is not obliged to quietly accept anything you pass it, so perhaps you should reconsider your application design.

If you want to check for entities, you could use a regular expression if you're confident that it's just a text node. Or you could get the contents with .text() instead of .html().

Upvotes: 0

Related Questions