Skip to content

Commit 88dd1b4

Browse files
committed
- Prepend slash when path starts with an wildchar
- Fixed typos
1 parent 74ab5d2 commit 88dd1b4

3 files changed

Lines changed: 25 additions & 12 deletions

File tree

src/Parser/Directives/AllowParser.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,8 @@ public function __construct($directive)
5555
*/
5656
public function add($line)
5757
{
58-
$line = rtrim($line, '*');
59-
if (($pos = mb_strpos($line, '$')) !== false) {
60-
$line = mb_substr($line, 0, $pos + 1);
61-
}
62-
if (in_array(substr($line, 0, 1), [
63-
'/',
64-
'*',
65-
'?',
66-
]) &&
58+
$line = $this->normalize($line);
59+
if (substr($line, 0, 1) == '/' &&
6760
!in_array($line, $this->path)
6861
) {
6962
$this->path[] = $line;
@@ -72,6 +65,27 @@ public function add($line)
7265
return false;
7366
}
7467

68+
/**
69+
* Normalize rules
70+
*
71+
* @param $line
72+
* @return string
73+
*/
74+
private function normalize($line)
75+
{
76+
// Prepend slash if starting with an wildcard
77+
if (substr($line, 0, 1) == '*') {
78+
$line = '/' . $line;
79+
}
80+
// Remove unnecessary characters after an end anchor
81+
if (($pos = mb_strpos($line, '$')) !== false) {
82+
$line = mb_substr($line, 0, $pos + 1);
83+
}
84+
// Remove unnecessary wildcards
85+
$line = rtrim($line, '*');
86+
return $line;
87+
}
88+
7589
/**
7690
* Render
7791
*

tests/DisallowAllTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function generateDataForTest()
7070
<<<RENDERED
7171
User-agent: *
7272
Disallow: /
73-
Disallow: *test
73+
Disallow: /*test
7474
Disallow: /admin/
7575
RENDERED
7676
]

tests/EndAnchorTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ public function generateDataForTest()
8787
Disallow: /deny/$$
8888
8989
User-Agent: DenyUs
90-
Disallow: /*deny/$
91-
Disallow: deny/$
90+
Disallow: *deny/$
9291
9392
User-Agent: ImageBot
9493
Disallow: *.jpg$

0 commit comments

Comments
 (0)