@@ -66,7 +66,7 @@ public function testAdd()
66
66
67
67
$ crawler = new Crawler ();
68
68
$ crawler ->add ('<html><body>Foo</body></html> ' );
69
- $ this ->assertEquals ('Foo ' , $ crawler ->filterXPath ('//body ' )->text (), '->add() adds nodes from a string ' );
69
+ $ this ->assertEquals ('Foo ' , $ crawler ->filterXPath ('//body ' )->value (), '->add() adds nodes from a string ' );
70
70
}
71
71
72
72
/**
@@ -114,7 +114,7 @@ public function testAddHtmlContentCharset()
114
114
$ crawler = new Crawler ();
115
115
$ crawler ->addHtmlContent ('<html><div class="foo">Tiếng Việt</html> ' , 'UTF-8 ' );
116
116
117
- $ this ->assertEquals ('Tiếng Việt ' , $ crawler ->filterXPath ('//div ' )->text ());
117
+ $ this ->assertEquals ('Tiếng Việt ' , $ crawler ->filterXPath ('//div ' )->value ());
118
118
}
119
119
120
120
public function testAddHtmlContentInvalidBaseTag ()
@@ -131,7 +131,7 @@ public function testAddHtmlContentUnsupportedCharset()
131
131
$ crawler = new Crawler ();
132
132
$ crawler ->addHtmlContent (file_get_contents (__DIR__ .'/Fixtures/windows-1250.html ' ), 'Windows-1250 ' );
133
133
134
- $ this ->assertEquals ('Žťčýů ' , $ crawler ->filterXPath ('//p ' )->text ());
134
+ $ this ->assertEquals ('Žťčýů ' , $ crawler ->filterXPath ('//p ' )->value ());
135
135
}
136
136
137
137
/**
@@ -143,7 +143,7 @@ public function testAddHtmlContentCharsetGbk()
143
143
//gbk encode of <html><p>中文</p></html>
144
144
$ crawler ->addHtmlContent (base64_decode ('PGh0bWw+PHA+1tDOxDwvcD48L2h0bWw+ ' ), 'gbk ' );
145
145
146
- $ this ->assertEquals ('中文 ' , $ crawler ->filterXPath ('//p ' )->text ());
146
+ $ this ->assertEquals ('中文 ' , $ crawler ->filterXPath ('//p ' )->value ());
147
147
}
148
148
149
149
public function testAddHtmlContentWithErrors ()
@@ -184,7 +184,7 @@ public function testAddXmlContentCharset()
184
184
$ crawler = new Crawler ();
185
185
$ crawler ->addXmlContent ('<html><div class="foo">Tiếng Việt</div></html> ' , 'UTF-8 ' );
186
186
187
- $ this ->assertEquals ('Tiếng Việt ' , $ crawler ->filterXPath ('//div ' )->text ());
187
+ $ this ->assertEquals ('Tiếng Việt ' , $ crawler ->filterXPath ('//div ' )->value ());
188
188
}
189
189
190
190
public function testAddXmlContentWithErrors ()
@@ -238,7 +238,7 @@ public function testAddContent()
238
238
239
239
$ crawler = new Crawler ();
240
240
$ crawler ->addContent ('<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><span>中文</span></html> ' );
241
- $ this ->assertEquals ('中文 ' , $ crawler ->filterXPath ('//span ' )->text (), '->addContent() guess wrong charset ' );
241
+ $ this ->assertEquals ('中文 ' , $ crawler ->filterXPath ('//span ' )->value (), '->addContent() guess wrong charset ' );
242
242
}
243
243
244
244
/**
@@ -248,7 +248,7 @@ public function testAddContentNonUtf8()
248
248
{
249
249
$ crawler = new Crawler ();
250
250
$ crawler ->addContent (iconv ('UTF-8 ' , 'SJIS ' , '<html><head><meta charset="Shift_JIS"></head><body>日本語</body></html> ' ));
251
- $ this ->assertEquals ('日本語 ' , $ crawler ->filterXPath ('//body ' )->text (), '->addContent() can recognize "Shift_JIS" in html5 meta charset tag ' );
251
+ $ this ->assertEquals ('日本語 ' , $ crawler ->filterXPath ('//body ' )->value (), '->addContent() can recognize "Shift_JIS" in html5 meta charset tag ' );
252
252
}
253
253
254
254
public function testAddDocument ()
@@ -304,14 +304,14 @@ public function testEq()
304
304
$ this ->assertNotSame ($ crawler , $ crawler ->eq (0 ), '->eq() returns a new instance of a crawler ' );
305
305
$ this ->assertInstanceOf ('Symfony \\Component \\DomCrawler \\Crawler ' , $ crawler , '->eq() returns a new instance of a crawler ' );
306
306
307
- $ this ->assertEquals ('Two ' , $ crawler ->eq (1 )->text (), '->eq() returns the nth node of the list ' );
307
+ $ this ->assertEquals ('Two ' , $ crawler ->eq (1 )->value (), '->eq() returns the nth node of the list ' );
308
308
$ this ->assertCount (0 , $ crawler ->eq (100 ), '->eq() returns an empty crawler if the nth node does not exist ' );
309
309
}
310
310
311
311
public function testEach ()
312
312
{
313
313
$ data = $ this ->createTestCrawler ()->filterXPath ('//ul[1]/li ' )->each (function ($ node , $ i ) {
314
- return $ i .'- ' .$ node ->text ();
314
+ return $ i .'- ' .$ node ->value ();
315
315
});
316
316
317
317
$ this ->assertEquals (array ('0-One ' , '1-Two ' , '2-Three ' ), $ data , '->each() executes an anonymous function on each node of the list ' );
@@ -382,16 +382,35 @@ public function testNodeName()
382
382
}
383
383
}
384
384
385
+ /**
386
+ * @group legacy
387
+ * @expectedException \InvalidArgumentException
388
+ */
385
389
public function testText ()
386
390
{
387
- $ this ->assertEquals (' One ' , $ this ->createTestCrawler ()->filterXPath ('//li ' )->text (), '->text() returns the node value of the first element of the node list ' );
391
+ $ this ->assertEquals (" one two \n three " , $ this ->createTestCrawler ()->filterXPath ('//div[@id="text-whitespaces"] ' )->text (), '->text() returns the node value of the first element of the node list ' );
388
392
389
- try {
390
- $ this ->createTestCrawler ()->filterXPath ('//ol ' )->text ();
391
- $ this ->fail ('->text() throws an \InvalidArgumentException if the node list is empty ' );
392
- } catch (\InvalidArgumentException $ e ) {
393
- $ this ->assertTrue (true , '->text() throws an \InvalidArgumentException if the node list is empty ' );
394
- }
393
+ $ this ->createTestCrawler ()->filterXPath ('//ol ' )->text ();
394
+ }
395
+
396
+ /**
397
+ * @expectedException \InvalidArgumentException
398
+ */
399
+ public function testTextWithNormalizedWhitespaces ()
400
+ {
401
+ $ 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 ' );
402
+
403
+ $ this ->createTestCrawler ()->filterXPath ('//ol ' )->text (true );
404
+ }
405
+
406
+ /**
407
+ * @expectedException \InvalidArgumentException
408
+ */
409
+ public function testValue ()
410
+ {
411
+ $ this ->assertEquals ("one two \nthree " , $ this ->createTestCrawler ()->filterXPath ('//div[@id="text-whitespaces"] ' )->value (), '->value() returns the node value of the first element of the node list ' );
412
+
413
+ $ this ->createTestCrawler ()->filterXPath ('//ol ' )->value ();
395
414
}
396
415
397
416
public function testHtml ()
@@ -463,7 +482,7 @@ public function testFilterXPathWithDefaultNamespace()
463
482
{
464
483
$ crawler = $ this ->createTestXmlCrawler ()->filterXPath ('//default:entry/default:id ' );
465
484
$ this ->assertCount (1 , $ crawler , '->filterXPath() automatically registers a namespace ' );
466
- $ this ->assertSame ('tag:youtube.com,2008:video:kgZRZmEc9j4 ' , $ crawler ->text ());
485
+ $ this ->assertSame ('tag:youtube.com,2008:video:kgZRZmEc9j4 ' , $ crawler ->value ());
467
486
}
468
487
469
488
public function testFilterXPathWithCustomDefaultNamespace ()
@@ -473,7 +492,7 @@ public function testFilterXPathWithCustomDefaultNamespace()
473
492
$ crawler = $ crawler ->filterXPath ('//x:entry/x:id ' );
474
493
475
494
$ this ->assertCount (1 , $ crawler , '->filterXPath() lets to override the default namespace prefix ' );
476
- $ this ->assertSame ('tag:youtube.com,2008:video:kgZRZmEc9j4 ' , $ crawler ->text ());
495
+ $ this ->assertSame ('tag:youtube.com,2008:video:kgZRZmEc9j4 ' , $ crawler ->value ());
477
496
}
478
497
479
498
public function testFilterXPathWithNamespace ()
@@ -486,7 +505,7 @@ public function testFilterXPathWithMultipleNamespaces()
486
505
{
487
506
$ crawler = $ this ->createTestXmlCrawler ()->filterXPath ('//media:group/yt:aspectRatio ' );
488
507
$ this ->assertCount (1 , $ crawler , '->filterXPath() automatically registers multiple namespaces ' );
489
- $ this ->assertSame ('widescreen ' , $ crawler ->text ());
508
+ $ this ->assertSame ('widescreen ' , $ crawler ->value ());
490
509
}
491
510
492
511
public function testFilterXPathWithManuallyRegisteredNamespace ()
@@ -496,7 +515,7 @@ public function testFilterXPathWithManuallyRegisteredNamespace()
496
515
497
516
$ crawler = $ crawler ->filterXPath ('//m:group/yt:aspectRatio ' );
498
517
$ this ->assertCount (1 , $ crawler , '->filterXPath() uses manually registered namespace ' );
499
- $ this ->assertSame ('widescreen ' , $ crawler ->text ());
518
+ $ this ->assertSame ('widescreen ' , $ crawler ->value ());
500
519
}
501
520
502
521
public function testFilterXPathWithAnUrl ()
@@ -505,7 +524,7 @@ public function testFilterXPathWithAnUrl()
505
524
506
525
$ crawler = $ crawler ->filterXPath ('//media:category[@scheme="http://gdata.youtube.com/schemas/2007/categories.cat"] ' );
507
526
$ this ->assertCount (1 , $ crawler );
508
- $ this ->assertSame ('Music ' , $ crawler ->text ());
527
+ $ this ->assertSame ('Music ' , $ crawler ->value ());
509
528
}
510
529
511
530
public function testFilterXPathWithFakeRoot ()
@@ -622,7 +641,7 @@ public function testFilterWithDefaultNamespace()
622
641
{
623
642
$ crawler = $ this ->createTestXmlCrawler ()->filter ('default|entry default|id ' );
624
643
$ this ->assertCount (1 , $ crawler , '->filter() automatically registers namespaces ' );
625
- $ this ->assertSame ('tag:youtube.com,2008:video:kgZRZmEc9j4 ' , $ crawler ->text ());
644
+ $ this ->assertSame ('tag:youtube.com,2008:video:kgZRZmEc9j4 ' , $ crawler ->value ());
626
645
}
627
646
628
647
public function testFilterWithNamespace ()
@@ -635,7 +654,7 @@ public function testFilterWithMultipleNamespaces()
635
654
{
636
655
$ crawler = $ this ->createTestXmlCrawler ()->filter ('media|group yt|aspectRatio ' );
637
656
$ this ->assertCount (1 , $ crawler , '->filter() automatically registers namespaces ' );
638
- $ this ->assertSame ('widescreen ' , $ crawler ->text ());
657
+ $ this ->assertSame ('widescreen ' , $ crawler ->value ());
639
658
}
640
659
641
660
public function testFilterWithDefaultNamespaceOnly ()
@@ -901,7 +920,7 @@ public function testLast()
901
920
$ this ->assertNotSame ($ crawler , $ crawler ->last (), '->last() returns a new instance of a crawler ' );
902
921
$ this ->assertInstanceOf ('Symfony \\Component \\DomCrawler \\Crawler ' , $ crawler , '->last() returns a new instance of a crawler ' );
903
922
904
- $ this ->assertEquals ('Three ' , $ crawler ->last ()->text ());
923
+ $ this ->assertEquals ('Three ' , $ crawler ->last ()->value ());
905
924
}
906
925
907
926
public function testFirst ()
@@ -910,7 +929,7 @@ public function testFirst()
910
929
$ this ->assertNotSame ($ crawler , $ crawler ->first (), '->first() returns a new instance of a crawler ' );
911
930
$ this ->assertInstanceOf ('Symfony \\Component \\DomCrawler \\Crawler ' , $ crawler , '->first() returns a new instance of a crawler ' );
912
931
913
- $ this ->assertEquals ('One ' , $ crawler ->first ()->text ());
932
+ $ this ->assertEquals ('One ' , $ crawler ->first ()->value ());
914
933
}
915
934
916
935
public function testSiblings ()
@@ -921,13 +940,13 @@ public function testSiblings()
921
940
922
941
$ nodes = $ crawler ->siblings ();
923
942
$ this ->assertEquals (2 , $ nodes ->count ());
924
- $ this ->assertEquals ('One ' , $ nodes ->eq (0 )->text ());
925
- $ this ->assertEquals ('Three ' , $ nodes ->eq (1 )->text ());
943
+ $ this ->assertEquals ('One ' , $ nodes ->eq (0 )->value ());
944
+ $ this ->assertEquals ('Three ' , $ nodes ->eq (1 )->value ());
926
945
927
946
$ nodes = $ this ->createTestCrawler ()->filterXPath ('//li ' )->eq (0 )->siblings ();
928
947
$ this ->assertEquals (2 , $ nodes ->count ());
929
- $ this ->assertEquals ('Two ' , $ nodes ->eq (0 )->text ());
930
- $ this ->assertEquals ('Three ' , $ nodes ->eq (1 )->text ());
948
+ $ this ->assertEquals ('Two ' , $ nodes ->eq (0 )->value ());
949
+ $ this ->assertEquals ('Three ' , $ nodes ->eq (1 )->value ());
931
950
932
951
try {
933
952
$ this ->createTestCrawler ()->filterXPath ('//ol ' )->siblings ();
@@ -945,7 +964,7 @@ public function testNextAll()
945
964
946
965
$ nodes = $ crawler ->nextAll ();
947
966
$ this ->assertEquals (1 , $ nodes ->count ());
948
- $ this ->assertEquals ('Three ' , $ nodes ->eq (0 )->text ());
967
+ $ this ->assertEquals ('Three ' , $ nodes ->eq (0 )->value ());
949
968
950
969
try {
951
970
$ this ->createTestCrawler ()->filterXPath ('//ol ' )->nextAll ();
@@ -963,7 +982,7 @@ public function testPreviousAll()
963
982
964
983
$ nodes = $ crawler ->previousAll ();
965
984
$ this ->assertEquals (2 , $ nodes ->count ());
966
- $ this ->assertEquals ('Two ' , $ nodes ->eq (0 )->text ());
985
+ $ this ->assertEquals ('Two ' , $ nodes ->eq (0 )->value ());
967
986
968
987
try {
969
988
$ this ->createTestCrawler ()->filterXPath ('//ol ' )->previousAll ();
@@ -981,9 +1000,9 @@ public function testChildren()
981
1000
982
1001
$ nodes = $ crawler ->children ();
983
1002
$ this ->assertEquals (3 , $ nodes ->count ());
984
- $ this ->assertEquals ('One ' , $ nodes ->eq (0 )->text ());
985
- $ this ->assertEquals ('Two ' , $ nodes ->eq (1 )->text ());
986
- $ this ->assertEquals ('Three ' , $ nodes ->eq (2 )->text ());
1003
+ $ this ->assertEquals ('One ' , $ nodes ->eq (0 )->value ());
1004
+ $ this ->assertEquals ('Two ' , $ nodes ->eq (1 )->value ());
1005
+ $ this ->assertEquals ('Three ' , $ nodes ->eq (2 )->value ());
987
1006
988
1007
try {
989
1008
$ this ->createTestCrawler ()->filterXPath ('//ol ' )->children ();
@@ -1161,6 +1180,8 @@ public function createTestCrawler($uri = null)
1161
1180
<div id="child2" xmlns:foo="http://example.com"></div>
1162
1181
</div>
1163
1182
<div id="sibling"><img /></div>
1183
+ <div id="text-whitespaces">one two
1184
+ three </div>
1164
1185
</body>
1165
1186
</html>
1166
1187
' );
0 commit comments