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

Why does Query->get() return an array instead of an element? #70

Closed
shtse8 opened this issue May 3, 2017 · 1 comment
Closed

Why does Query->get() return an array instead of an element? #70

shtse8 opened this issue May 3, 2017 · 1 comment
Assignees
Labels
Milestone

Comments

@shtse8
Copy link

shtse8 commented May 3, 2017

Reference: https://github.com/FluentDOM/FluentDOM/wiki/Query-Get

For my understanding, get() should return only 1 element. Why does it return an array containing an element instead of the element?

JQuery Version

Code:

var href = $('<div><a href="/1">1</a><a href="/2">2</a><a href="/3">3</a></div>').find("a").get(0).getAttribute("href");

Result:

/1

FluentDom Version

Expected Code:

$href = FluentDom::QueryCss('<div><a href="/1">1</a><a href="/2">2</a><a href="/3">3</a></div>', "html-fragment")->find("a")->get(0)->getAttribute("href");

Actual Code:

$document = FluentDom::QueryCss('<div><a href="/1">1</a><a href="/2">2</a><a href="/3">3</a></div>', "html-fragment");
$href = '';
if ( $linkDom =  $document->find("a")->get(0)[0] ) {
    $href = $linkDom->getAttribute("href");
}
@ThomasWeinert
Copy link
Owner

It's an bug. Not sure how that happened. I don't think I can just fix it in a minor version to old and it might break things.

However FluentDOM::Query allows ArrayAccess, so this does work:

$fd = FluentDom::Query('<div><a href="/1">1</a><a href="/2">2</a><a href="/3">3</a></div>', "html-fragment");
$href = $fd->find("//a")[0]['href'];
var_dump($href);

@ThomasWeinert ThomasWeinert self-assigned this Jun 28, 2017
@ThomasWeinert ThomasWeinert added this to the 7.0 milestone Jun 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants