Skip to content

Commit 869a609

Browse files
committed
wip
1 parent bf1479d commit 869a609

File tree

6 files changed

+23
-35
lines changed

6 files changed

+23
-35
lines changed

docs/1-essentials/02-views.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -232,18 +232,6 @@ The example above will only render the child `div` elements:
232232
<div>Post C</div>
233233
```
234234

235-
### Invalid single quotes
236-
237-
While attributes with single quotes are allowed by the HTML spec, they are not by Tempest View. You must always use double quotes for attribute values, for all attributes.
238-
239-
```
240-
<{:hl-keyword:div:} {:hl-property::isset:}="$title">{{ $title }}</{:hl-keyword:div:}>
241-
<{:hl-keyword:div:} {:hl-property:class:}="foo bar"></{:hl-keyword:div:}>
242-
243-
<{:hl-keyword:div:} {:hl-property::isset:}={:hl-error:'title':}>{{ $title }}</{:hl-keyword:div:}>
244-
<{:hl-keyword:div:} {:hl-property:class:}={:hl-error:'foo bar':}></{:hl-keyword:div:}>
245-
```
246-
247235
## View components
248236

249237
Components allow for splitting the user interface into independent and reusable pieces.

packages/view/src/Exceptions/AttributesWithSingleQuotesWereNotAllowed.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/view/src/Parser/TempestViewLexer.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Tempest\View\Parser;
44

5-
use Tempest\View\Exceptions\AttributesWithSingleQuotesWereNotAllowed;
6-
75
final class TempestViewLexer
86
{
97
private const string WHITESPACE = PHP_EOL . "\n\t\f ";
@@ -133,12 +131,12 @@ private function lexTag(): array
133131
);
134132

135133
if ($hasValue) {
136-
if ($this->seek() === '\'') {
137-
throw new AttributesWithSingleQuotesWereNotAllowed();
138-
}
134+
$quote = $this->seek() === '\''
135+
? '\''
136+
: '"';
139137

140-
$attributeValue = $this->consumeIncluding('"');
141-
$attributeValue .= $this->consumeIncluding('"');
138+
$attributeValue = $this->consumeIncluding($quote);
139+
$attributeValue .= $this->consumeIncluding($quote);
142140

143141
$tokens[] = new Token(
144142
content: $attributeValue,

packages/view/src/Parser/Token.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ private function attributeName(string $name): string
142142

143143
private function attributeValue(string $value): string
144144
{
145-
return str($value)->afterFirst('"')->beforeLast('"')->toString();
145+
$value = str($value);
146+
147+
$quote = $value->startsWith('"') ? '"' : "'";
148+
149+
return str($value)->afterFirst($quote)->beforeLast($quote)->toString();
146150
}
147151
}

packages/view/tests/TempestViewLexerTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use PHPUnit\Framework\Attributes\TestWith;
66
use PHPUnit\Framework\TestCase;
7-
use Tempest\View\Exceptions\AttributesWithSingleQuotesWereNotAllowed;
87
use Tempest\View\Parser\TempestViewLexer;
98
use Tempest\View\Parser\Token;
109
use Tempest\View\Parser\TokenCollection;
@@ -353,9 +352,18 @@ public function test_single_quote_attributes(): void
353352
<div class='hello'></div>
354353
HTML;
355354

356-
$this->expectException(AttributesWithSingleQuotesWereNotAllowed::class);
355+
$tokens = new TempestViewLexer($html)->lex();
357356

358-
new TempestViewLexer($html)->lex();
357+
$this->assertTokens(
358+
expected: [
359+
new Token('<div', TokenType::OPEN_TAG_START),
360+
new Token(' class=', TokenType::ATTRIBUTE_NAME),
361+
new Token('\'hello\'', TokenType::ATTRIBUTE_VALUE),
362+
new Token('>', TokenType::OPEN_TAG_END),
363+
new Token('</div>', TokenType::CLOSING_TAG),
364+
],
365+
actual: $tokens,
366+
);
359367
}
360368

361369
private function assertTokens(array $expected, TokenCollection $actual): void

tests/Integration/View/TempestViewRendererTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Tests\Tempest\Integration\View;
66

77
use Tempest\Support\Html\HtmlString;
8-
use Tempest\View\Exceptions\AttributesWithSingleQuotesWereNotAllowed;
98
use Tempest\View\Exceptions\ElementWasInvalid;
109
use Tempest\View\ViewCache;
1110
use Tests\Tempest\Fixtures\Controllers\RelativeViewController;
@@ -851,10 +850,10 @@ public function test_parse_rss_feed(): void
851850

852851
public function test_attributes_with_single_quotes(): void
853852
{
854-
$this->expectException(AttributesWithSingleQuotesWereNotAllowed::class);
855-
856853
$html = $this->render(<<<'HTML'
857854
<div class='hello'></div>
858855
HTML);
856+
857+
$this->assertSnippetsMatch('<div class="hello"></div>', $html);
859858
}
860859
}

0 commit comments

Comments
 (0)