|
5 | 5 | namespace InitPHP\Escaper\Tests; |
6 | 6 |
|
7 | 7 | use InitPHP\Escaper\Escaper; |
| 8 | +use InitPHP\Escaper\Exception\EncodingConversionException; |
8 | 9 | use InitPHP\Escaper\Exception\EncodingNotSupportedException; |
9 | 10 | use InitPHP\Escaper\Exception\EscaperException; |
10 | 11 | use InitPHP\Escaper\Exception\InvalidUtf8Exception; |
@@ -73,4 +74,39 @@ public function testInvalidUtf8InAttributeContextThrows(): void |
73 | 74 | // 0xC3 0x28 is a broken 2-byte sequence. |
74 | 75 | (new Escaper())->escHtmlAttr("\xC3\x28"); |
75 | 76 | } |
| 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 | + } |
76 | 112 | } |
0 commit comments