ThomasWeinert/FluentDOM

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

Closed this issue · 1 comments

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");
}

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);