Skip to content

Commit 2ffea36

Browse files
authored
Merge pull request #14 from tomverran/support-cr-only
Support files with only carriage return line separators
2 parents c8444e8 + 7a3f787 commit 2ffea36

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/Robot/RobotsFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct($content)
2525
$withoutComments = preg_replace( '/#.*/', '', strtolower($content));
2626
$lines = [];
2727

28-
foreach(explode("\n", $withoutComments) as $line) {
28+
foreach(preg_split("/[\r\n]+/", $withoutComments) as $line) {
2929
$lineParts = array_filter(array_map('trim', explode(':', $line)));
3030
if (!empty($lineParts)) {
3131
$lines[] = $lineParts;

tests/RobotsFileTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace tomverran\Robot;
3+
4+
5+
class RobotsFileTest extends \PHPUnit_Framework_TestCase
6+
{
7+
8+
public function getTestFile($separator)
9+
{
10+
return new RobotsFile("User-Agent: *${separator}Disallow: /");
11+
}
12+
13+
public function newLines()
14+
{
15+
return [
16+
'only cr' => ["\r"],
17+
'only lf' =>["\n"],
18+
'cr + lf' =>["\r\n"]
19+
];
20+
}
21+
22+
/**
23+
* @test
24+
* @dataProvider newLines
25+
* @param $newline string The line separator to use
26+
*/
27+
public function givenCrOnlyLineBreaks_pickUpTwoLines($newline)
28+
{
29+
$test = $this->getTestFile($newline);
30+
$this->assertEquals(['user-agent', '*'], [$test->key(), $test->current()]);
31+
$test->next();
32+
$this->assertEquals(['disallow', '/'], [$test->key(), $test->current()]);
33+
}
34+
}

0 commit comments

Comments
 (0)