-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DomCrawler] Added Crawler::innerText() method #42338
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
5.4 | ||
--- | ||
|
||
* Add `Crawler::innerText` method. | ||
|
||
5.3 | ||
--- | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -354,6 +354,18 @@ public function testText() | |
$this->assertSame('my value', $this->createTestCrawler(null)->filterXPath('//ol')->text('my value')); | ||
} | ||
|
||
/** | ||
* Tests that innerText() returns only text that is the direct descendent of the current node, in contrast to | ||
* text() that returns the text of all child nodes. | ||
*/ | ||
Comment on lines
+357
to
+360
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment is not needed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A test scenario is not needed, in a technical sense, but it is needed in a maintainability sense. It conveys what the original intent of the test is, which helps greatly in determining how to fix it when it should break in future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From my POV we can keep it, but There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps, but I think writing test scenarios is a good habit to be into. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we use this anywhere else in the DomCrawler's test suite? If not, I am in favour of removing the comment and, if necessary, change the method name to something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Strongly disagree. Prose is much easier to read as space-separated words instead of stuffing entire sentences into camel-cased method names. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in this case |
||
public function testInnerText() | ||
{ | ||
self::assertCount(1, $crawler = $this->createTestCrawler()->filterXPath('//*[@id="complex-element"]')); | ||
|
||
self::assertSame('Parent text Child text', $crawler->text()); | ||
self::assertSame('Parent text', $crawler->innerText()); | ||
} | ||
|
||
public function testHtml() | ||
{ | ||
$this->assertEquals('<img alt="Bar">', $this->createTestCrawler()->filterXPath('//a[5]')->html()); | ||
|
@@ -1283,6 +1295,10 @@ public function createTestCrawler($uri = null) | |
<div id="child2" xmlns:foo="http://example.com"></div> | ||
</div> | ||
<div id="sibling"><img /></div> | ||
<div id="complex-element"> | ||
Parent text | ||
<span>Child text</span> | ||
</div> | ||
</body> | ||
</html> | ||
'); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can also be expressed as
child::node()/text()
. I don't know which is "better".