Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how you get node text without it's children? #4

Open
dragonattack opened this issue Aug 23, 2015 · 2 comments
Open

how you get node text without it's children? #4

dragonattack opened this issue Aug 23, 2015 · 2 comments

Comments

@dragonattack
Copy link

Hi, first of all, thanks for the nice lib.

for example, we have code like this:

<li id="listItem">
    This is some text
    <span id="firstSpan">First span text</span>
    <span id="secondSpan">Second span text</span>
</li>

We need to get This is some text only. the text() method will give us This is some textFirst span textSecond span text

There's solution for jQuery: http://stackoverflow.com/questions/3442394/jquery-using-text-to-retrieve-only-text-not-nested-in-child-tags

Is it possible with hQuery?
Thanks.

@dragonattack
Copy link
Author

as workaround I did it that way:

$title = $row->find('#listItem')->text();
foreach ($row->find('#listItem')->children() as $v) $title = str_replace($v->text(), '', $title);
$title = trim($title);

But of course, it's not cool :) And could really fail if we have short phrases in children, that are present in our top phrase, i.e.:

<li id="listItem">
    Unconditional string
    <span id="firstSpan">condition</span>
    <span id="secondSpan">ring</span>
</li>

@duzun
Copy link
Owner

duzun commented Aug 23, 2015

Hi @dragonattack
Thanks for this hit.
I would suggest

$("#foo").contents().not($("#foo").children()).text();

to avoid cloning.

I did not implement .contents yet, but surely it is a necessary method.

I'm going to implement it whenever I have some free time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants