Skip to content

Commit d78ed95

Browse files
committed
Add Windows-1252 encoding round trips in EscaperEncodingTest
1 parent b239780 commit d78ed95

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

tests/EscaperEncodingTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace InitPHP\Escaper\Tests;
66

77
use InitPHP\Escaper\Escaper;
8+
use InitPHP\Escaper\Exception\EncodingConversionException;
89
use InitPHP\Escaper\Exception\EncodingNotSupportedException;
910
use InitPHP\Escaper\Exception\EscaperException;
1011
use InitPHP\Escaper\Exception\InvalidUtf8Exception;
@@ -73,4 +74,39 @@ public function testInvalidUtf8InAttributeContextThrows(): void
7374
// 0xC3 0x28 is a broken 2-byte sequence.
7475
(new Escaper())->escHtmlAttr("\xC3\x28");
7576
}
77+
78+
public function testWindows1252RoundTripThroughAttributeContext(): void
79+
{
80+
$escaper = new Escaper('windows-1252');
81+
82+
// 0xE9 == "é" in windows-1252. It is outside the attribute whitelist
83+
// so the matcher must produce a numeric entity. The output reaches
84+
// the caller after a UTF-8 → windows-1252 conversion back.
85+
self::assertSame('é', $escaper->escHtmlAttr("\xE9"));
86+
}
87+
88+
public function testWindows1252RoundTripThroughJsContext(): void
89+
{
90+
$escaper = new Escaper('windows-1252');
91+
92+
// 0xE9 == "é". In the JS context the matcher emits é.
93+
self::assertSame('\\u00E9', $escaper->escJs("\xE9"));
94+
}
95+
96+
public function testWindows1252RoundTripThroughCssContext(): void
97+
{
98+
$escaper = new Escaper('windows-1252');
99+
100+
// 0xE9 == "é". In the CSS context the matcher emits "\E9 ".
101+
self::assertSame('\\E9 ', $escaper->escCss("\xE9"));
102+
}
103+
104+
public function testForwardConversionFailureRaisesException(): void
105+
{
106+
$this->expectException(EncodingConversionException::class);
107+
$this->expectExceptionMessage('Failed to convert string from "windows-1252" to "UTF-8".');
108+
109+
// 0x81 is an undefined byte in windows-1252 — iconv returns false.
110+
(new Escaper('windows-1252'))->escHtmlAttr("\x81");
111+
}
76112
}

0 commit comments

Comments
 (0)