Skip to content

Commit d814d6c

Browse files
author
Geert Eltink
committed
Merge pull request #56 from xtreamwayz/hotfix/force-utf8-encoding
Enforce UTF-8 encoding to prevent issues with libxml2
2 parents fd15c55 + 4f456b0 commit d814d6c

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ All notable changes to this project will be documented in this file, in reverse
1818

1919
### Fixed
2020

21-
- Nothing.
21+
- [#56](https://github.com/xtreamwayz/html-form-validator/pull/56) fixes unicode characters support. UTF-8 is now
22+
enforced internally to make this happen. To prevent unwanted output only the first form is returned in
23+
`FormFactory->asString()`.
2224

2325
## 0.7.0 - 2016-04-16
2426

src/FormFactory.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ public function __construct(string $htmlForm, Factory $factory = null, array $de
6363

6464
// Ignore invalid tag errors during loading (e.g. datalist)
6565
libxml_use_internal_errors(true);
66-
// Don't add missing doctype, html and body
67-
$this->document->loadHTML($htmlForm, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
66+
// Enforce UTF-8 encoding and don't add missing doctype, html and body
67+
$this->document->loadHTML(
68+
'<?xml version="1.0" encoding="UTF-8"?>' . $htmlForm,
69+
LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD
70+
);
6871
libxml_use_internal_errors(false);
6972

7073
// Inject default values (from models etc)
@@ -101,7 +104,8 @@ public function asString(ValidationResultInterface $result = null) : string
101104

102105
$this->document->formatOutput = true;
103106

104-
return $this->document->saveHTML();
107+
// Return the first form only to prevent returning the XML declaration
108+
return $this->document->saveHTML($this->document->getElementsByTagName('form')->item(0));
105109
}
106110

107111
public function validateRequest(ServerRequestInterface $request) : ValidationResultInterface

test/Fixtures/utf8-encoding.test

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
<test description>
3+
--HTML-FORM--
4+
<form action="/" method="post">
5+
<input type="text" name="valid_value" required value="Îñţérñåţîöñåļîžåţîöñ" />
6+
<input type="text" name="valid_default" required />
7+
<input type="text" name="valid_reuse" required data-reuse-submitted-value="true" />
8+
</form>
9+
--DEFAULT-VALUES--
10+
{
11+
"valid_default": "Îñţérñåţîöñåļîžåţîöñ"
12+
}
13+
--SUBMITTED-VALUES--
14+
{
15+
"valid_value": "Îñţérñåţîöñåļîžåţîöñ",
16+
"valid_default": "Îñţérñåţîöñåļîžåţîöñ",
17+
"valid_reuse": "Îñţérñåţîöñåļîžåţîöñ"
18+
}
19+
--EXPECTED-VALUES--
20+
{
21+
"valid_value": "Îñţérñåţîöñåļîžåţîöñ",
22+
"valid_default": "Îñţérñåţîöñåļîžåţîöñ",
23+
"valid_reuse": "Îñţérñåţîöñåļîžåţîöñ"
24+
}
25+
--EXPECTED-FORM--
26+
<form action="/" method="post">
27+
<input type="text" name="valid_value" required value="Îñţérñåţîöñåļîžåţîöñ" />
28+
<input type="text" name="valid_default" required value="Îñţérñåţîöñåļîžåţîöñ" />
29+
<input type="text" name="valid_reuse" required value="Îñţérñåţîöñåļîžåţîöñ" />
30+
</form>

0 commit comments

Comments
 (0)