@@ -73,7 +73,7 @@ public function testAdd()
73
73
74
74
$ crawler = $ this ->createCrawler ();
75
75
$ crawler ->add ($ this ->getDoctype ().'<html><body>Foo</body></html> ' );
76
- $ this ->assertEquals ('Foo ' , $ crawler ->filterXPath ('//body ' )->text (), '->add() adds nodes from a string ' );
76
+ $ this ->assertEquals ('Foo ' , $ crawler ->filterXPath ('//body ' )->textContent (), '->add() adds nodes from a string ' );
77
77
}
78
78
79
79
/**
@@ -120,7 +120,7 @@ public function testAddHtmlContentCharset()
120
120
$ crawler = $ this ->createCrawler ();
121
121
$ crawler ->addHtmlContent ($ this ->getDoctype ().'<html><div class="foo">Tiếng Việt</html> ' , 'UTF-8 ' );
122
122
123
- $ this ->assertEquals ('Tiếng Việt ' , $ crawler ->filterXPath ('//div ' )->text ());
123
+ $ this ->assertEquals ('Tiếng Việt ' , $ crawler ->filterXPath ('//div ' )->textContent ());
124
124
}
125
125
126
126
public function testAddHtmlContentInvalidBaseTag ()
@@ -140,7 +140,7 @@ public function testAddHtmlContentCharsetGbk()
140
140
//gbk encode of <html><p>中文</p></html>
141
141
$ crawler ->addHtmlContent ($ this ->getDoctype ().base64_decode ('PGh0bWw+PHA+1tDOxDwvcD48L2h0bWw+ ' ), 'gbk ' );
142
142
143
- $ this ->assertEquals ('中文 ' , $ crawler ->filterXPath ('//p ' )->text ());
143
+ $ this ->assertEquals ('中文 ' , $ crawler ->filterXPath ('//p ' )->textContent ());
144
144
}
145
145
146
146
public function testAddXmlContent ()
@@ -156,7 +156,7 @@ public function testAddXmlContentCharset()
156
156
$ crawler = $ this ->createCrawler ();
157
157
$ crawler ->addXmlContent ($ this ->getDoctype ().'<html><div class="foo">Tiếng Việt</div></html> ' , 'UTF-8 ' );
158
158
159
- $ this ->assertEquals ('Tiếng Việt ' , $ crawler ->filterXPath ('//div ' )->text ());
159
+ $ this ->assertEquals ('Tiếng Việt ' , $ crawler ->filterXPath ('//div ' )->textContent ());
160
160
}
161
161
162
162
public function testAddContent ()
@@ -187,7 +187,7 @@ public function testAddContent()
187
187
188
188
$ crawler = $ this ->createCrawler ();
189
189
$ crawler ->addContent ($ this ->getDoctype ().'<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><span>中文</span></html> ' );
190
- $ this ->assertEquals ('中文 ' , $ crawler ->filterXPath ('//span ' )->text (), '->addContent() guess wrong charset ' );
190
+ $ this ->assertEquals ('中文 ' , $ crawler ->filterXPath ('//span ' )->textContent (), '->addContent() guess wrong charset ' );
191
191
}
192
192
193
193
/**
@@ -197,7 +197,7 @@ public function testAddContentNonUtf8()
197
197
{
198
198
$ crawler = $ this ->createCrawler ();
199
199
$ crawler ->addContent (iconv ('UTF-8 ' , 'SJIS ' , $ this ->getDoctype ().'<html><head><meta charset="Shift_JIS"></head><body>日本語</body></html> ' ));
200
- $ this ->assertEquals ('日本語 ' , $ crawler ->filterXPath ('//body ' )->text (), '->addContent() can recognize "Shift_JIS" in html5 meta charset tag ' );
200
+ $ this ->assertEquals ('日本語 ' , $ crawler ->filterXPath ('//body ' )->textContent (), '->addContent() can recognize "Shift_JIS" in html5 meta charset tag ' );
201
201
}
202
202
203
203
public function testAddDocument ()
@@ -253,14 +253,14 @@ public function testEq()
253
253
$ this ->assertNotSame ($ crawler , $ crawler ->eq (0 ), '->eq() returns a new instance of a crawler ' );
254
254
$ this ->assertInstanceOf ('Symfony \\Component \\DomCrawler \\Crawler ' , $ crawler , '->eq() returns a new instance of a crawler ' );
255
255
256
- $ this ->assertEquals ('Two ' , $ crawler ->eq (1 )->text (), '->eq() returns the nth node of the list ' );
256
+ $ this ->assertEquals ('Two ' , $ crawler ->eq (1 )->textContent (), '->eq() returns the nth node of the list ' );
257
257
$ this ->assertCount (0 , $ crawler ->eq (100 ), '->eq() returns an empty crawler if the nth node does not exist ' );
258
258
}
259
259
260
260
public function testEach ()
261
261
{
262
262
$ data = $ this ->createTestCrawler ()->filterXPath ('//ul[1]/li ' )->each (function ($ node , $ i ) {
263
- return $ i .'- ' .$ node ->text ();
263
+ return $ i .'- ' .$ node ->textContent ();
264
264
});
265
265
266
266
$ this ->assertEquals (['0-One ' , '1-Two ' , '2-Three ' ], $ data , '->each() executes an anonymous function on each node of the list ' );
@@ -333,16 +333,33 @@ public function testNodeName()
333
333
334
334
public function testText ()
335
335
{
336
- $ this ->assertEquals ('One ' , $ this ->createTestCrawler ()->filterXPath ('//li ' )->text (), '->text() returns the node value of the first element of the node list ' );
336
+ $ this ->assertEquals ("one two \nthree " , $ this ->createTestCrawler ()->filterXPath ('//div[@id="text-whitespaces"] ' )->text (), '->text() returns the node value of the first element of the node list ' );
337
+ }
337
338
338
- try {
339
- $ this ->createTestCrawler ()->filterXPath ('//ol ' )->text ();
340
- $ this ->fail ('->text() throws an \InvalidArgumentException if the node list is empty ' );
341
- } catch (\InvalidArgumentException $ e ) {
342
- $ this ->assertTrue (true , '->text() throws an \InvalidArgumentException if the node list is empty ' );
343
- }
339
+ public function testTextWithNormalizedWhitespaces ()
340
+ {
341
+ $ this ->assertEquals ('one two three ' , $ this ->createTestCrawler ()->filterXPath ('//div[@id="text-whitespaces"] ' )->text (true ), '->text() returns the node value of the first element of the node list ' );
342
+ }
344
343
345
- $ this ->assertSame ('my value ' , $ this ->createTestCrawler (null )->filterXPath ('//ol ' )->text ('my value ' ));
344
+ /**
345
+ * @expectedException \InvalidArgumentException
346
+ */
347
+ public function testTextThrowsExceptionWhenNodeListIsEmpty ()
348
+ {
349
+ $ this ->createTestCrawler ()->filterXPath ('//ol ' )->text (true );
350
+ }
351
+
352
+ public function testValue ()
353
+ {
354
+ $ this ->assertEquals ("one two \nthree " , $ this ->createTestCrawler ()->filterXPath ('//div[@id="text-whitespaces"] ' )->textContent (), '->value() returns the node value of the first element of the node list ' );
355
+ }
356
+
357
+ /**
358
+ * @expectedException \InvalidArgumentException
359
+ */
360
+ public function testValueThrowsExceptionWhenNodeListIsEmpty ()
361
+ {
362
+ $ this ->createTestCrawler ()->filterXPath ('//ol ' )->textContent ();
346
363
}
347
364
348
365
public function testHtml ()
@@ -419,7 +436,7 @@ public function testFilterXPathWithDefaultNamespace()
419
436
{
420
437
$ crawler = $ this ->createTestXmlCrawler ()->filterXPath ('//default:entry/default:id ' );
421
438
$ this ->assertCount (1 , $ crawler , '->filterXPath() automatically registers a namespace ' );
422
- $ this ->assertSame ('tag:youtube.com,2008:video:kgZRZmEc9j4 ' , $ crawler ->text ());
439
+ $ this ->assertSame ('tag:youtube.com,2008:video:kgZRZmEc9j4 ' , $ crawler ->textContent ());
423
440
}
424
441
425
442
public function testFilterXPathWithCustomDefaultNamespace ()
@@ -429,7 +446,7 @@ public function testFilterXPathWithCustomDefaultNamespace()
429
446
$ crawler = $ crawler ->filterXPath ('//x:entry/x:id ' );
430
447
431
448
$ this ->assertCount (1 , $ crawler , '->filterXPath() lets to override the default namespace prefix ' );
432
- $ this ->assertSame ('tag:youtube.com,2008:video:kgZRZmEc9j4 ' , $ crawler ->text ());
449
+ $ this ->assertSame ('tag:youtube.com,2008:video:kgZRZmEc9j4 ' , $ crawler ->textContent ());
433
450
}
434
451
435
452
public function testFilterXPathWithNamespace ()
@@ -442,7 +459,7 @@ public function testFilterXPathWithMultipleNamespaces()
442
459
{
443
460
$ crawler = $ this ->createTestXmlCrawler ()->filterXPath ('//media:group/yt:aspectRatio ' );
444
461
$ this ->assertCount (1 , $ crawler , '->filterXPath() automatically registers multiple namespaces ' );
445
- $ this ->assertSame ('widescreen ' , $ crawler ->text ());
462
+ $ this ->assertSame ('widescreen ' , $ crawler ->textContent ());
446
463
}
447
464
448
465
public function testFilterXPathWithManuallyRegisteredNamespace ()
@@ -452,7 +469,7 @@ public function testFilterXPathWithManuallyRegisteredNamespace()
452
469
453
470
$ crawler = $ crawler ->filterXPath ('//m:group/yt:aspectRatio ' );
454
471
$ this ->assertCount (1 , $ crawler , '->filterXPath() uses manually registered namespace ' );
455
- $ this ->assertSame ('widescreen ' , $ crawler ->text ());
472
+ $ this ->assertSame ('widescreen ' , $ crawler ->textContent ());
456
473
}
457
474
458
475
public function testFilterXPathWithAnUrl ()
@@ -461,7 +478,7 @@ public function testFilterXPathWithAnUrl()
461
478
462
479
$ crawler = $ crawler ->filterXPath ('//media:category[@scheme="http://gdata.youtube.com/schemas/2007/categories.cat"] ' );
463
480
$ this ->assertCount (1 , $ crawler );
464
- $ this ->assertSame ('Music ' , $ crawler ->text ());
481
+ $ this ->assertSame ('Music ' , $ crawler ->textContent ());
465
482
}
466
483
467
484
public function testFilterXPathWithFakeRoot ()
@@ -578,7 +595,7 @@ public function testFilterWithDefaultNamespace()
578
595
{
579
596
$ crawler = $ this ->createTestXmlCrawler ()->filter ('default|entry default|id ' );
580
597
$ this ->assertCount (1 , $ crawler , '->filter() automatically registers namespaces ' );
581
- $ this ->assertSame ('tag:youtube.com,2008:video:kgZRZmEc9j4 ' , $ crawler ->text ());
598
+ $ this ->assertSame ('tag:youtube.com,2008:video:kgZRZmEc9j4 ' , $ crawler ->textContent ());
582
599
}
583
600
584
601
public function testFilterWithNamespace ()
@@ -591,7 +608,7 @@ public function testFilterWithMultipleNamespaces()
591
608
{
592
609
$ crawler = $ this ->createTestXmlCrawler ()->filter ('media|group yt|aspectRatio ' );
593
610
$ this ->assertCount (1 , $ crawler , '->filter() automatically registers namespaces ' );
594
- $ this ->assertSame ('widescreen ' , $ crawler ->text ());
611
+ $ this ->assertSame ('widescreen ' , $ crawler ->textContent ());
595
612
}
596
613
597
614
public function testFilterWithDefaultNamespaceOnly ()
@@ -854,7 +871,7 @@ public function testLast()
854
871
$ this ->assertNotSame ($ crawler , $ crawler ->last (), '->last() returns a new instance of a crawler ' );
855
872
$ this ->assertInstanceOf ('Symfony \\Component \\DomCrawler \\Crawler ' , $ crawler , '->last() returns a new instance of a crawler ' );
856
873
857
- $ this ->assertEquals ('Three ' , $ crawler ->last ()->text ());
874
+ $ this ->assertEquals ('Three ' , $ crawler ->last ()->textContent ());
858
875
}
859
876
860
877
public function testFirst ()
@@ -863,7 +880,7 @@ public function testFirst()
863
880
$ this ->assertNotSame ($ crawler , $ crawler ->first (), '->first() returns a new instance of a crawler ' );
864
881
$ this ->assertInstanceOf ('Symfony \\Component \\DomCrawler \\Crawler ' , $ crawler , '->first() returns a new instance of a crawler ' );
865
882
866
- $ this ->assertEquals ('One ' , $ crawler ->first ()->text ());
883
+ $ this ->assertEquals ('One ' , $ crawler ->first ()->textContent ());
867
884
}
868
885
869
886
public function testSiblings ()
@@ -874,13 +891,13 @@ public function testSiblings()
874
891
10000
875
892
$ nodes = $ crawler ->siblings ();
876
893
$ this ->assertEquals (2 , $ nodes ->count ());
877
- $ this ->assertEquals ('One ' , $ nodes ->eq (0 )->text ());
878
- $ this ->assertEquals ('Three ' , $ nodes ->eq (1 )->text ());
894
+ $ this ->assertEquals ('One ' , $ nodes ->eq (0 )->textContent ());
895
+ $ this ->assertEquals ('Three ' , $ nodes ->eq (1 )->textContent ());
879
896
880
897
$ nodes = $ this ->createTestCrawler ()->filterXPath ('//li ' )->eq (0 )->siblings ();
881
898
$ this ->assertEquals (2 , $ nodes ->count ());
882
- $ this ->assertEquals ('Two ' , $ nodes ->eq (0 )->text ());
883
- $ this ->assertEquals ('Three ' , $ nodes ->eq (1 )->text ());
899
+ $ this ->assertEquals ('Two ' , $ nodes ->eq (0 )->textContent ());
900
+ $ this ->assertEquals ('Three ' , $ nodes ->eq (1 )->textContent ());
884
901
885
902
try {
886
903
$ this ->createTestCrawler ()->filterXPath ('//ol ' )->siblings ();
@@ -898,7 +915,7 @@ public function testNextAll()
898
915
899
916
$ nodes = $ crawler ->nextAll ();
900
917
$ this ->assertEquals (1 , $ nodes ->count ());
901
- $ this ->assertEquals ('Three ' , $ nodes ->eq (0 )->text ());
918
+ $ this ->assertEquals ('Three ' , $ nodes ->eq (0 )->textContent ());
902
919
903
920
try {
904
921
$ this ->createTestCrawler ()->filterXPath ('//ol ' )->nextAll ();
@@ -916,7 +933,7 @@ public function testPreviousAll()
916
933
917
934
$ nodes = $ crawler ->previousAll ();
918
935
$ this ->assertEquals (2 , $ nodes ->count ());
919
- $ this ->assertEquals ('Two ' , $ nodes ->eq (0 )->text ());
936
+ $ this ->assertEquals ('Two ' , $ nodes ->eq (0 )->textContent ());
920
937
921
938
try {
922
939
$ this ->createTestCrawler ()->filterXPath ('//ol ' )->previousAll ();
@@ -934,9 +951,9 @@ public function testChildren()
934
951
935
952
$ nodes = $ crawler ->children ();
936
953
$ this ->assertEquals (3 , $ nodes ->count ());
937
- $ this ->assertEquals ('One ' , $ nodes ->eq (0 )->text ());
938
- $ this ->assertEquals ('Two ' , $ nodes ->eq (1 )->text ());
939
- $ this ->assertEquals ('Three ' , $ nodes ->eq (2 )->text ());
954
+ $ this ->assertEquals ('One ' , $ nodes ->eq (0 )->textContent ());
955
+ $ this ->assertEquals ('Two ' , $ nodes ->eq (1 )->textContent ());
956
+ $ this ->assertEquals ('Three ' , $ nodes ->eq (2 )->textContent ());
940
957
941
958
try {
942
959
$ this ->createTestCrawler ()->filterXPath ('//ol ' )->children ();
@@ -1209,6 +1226,8 @@ public function createTestCrawler($uri = null)
1209
1226
<div id="child2" xmlns:foo="http://example.com"></div>
1210
1227
</div>
1211
1228
<div id="sibling"><img /></div>
1229
+ <div id="text-whitespaces">one two
1230
+ three </div>
1212
1231
</body>
1213
1232
</html>
1214
1233
' );
0 commit comments