Skip to content

Commit

Permalink
Add tests for form newline normalization in filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreu Botella authored and annevk committed Dec 4, 2020
1 parent 0f98fe9 commit a8fad46
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions html/semantics/forms/form-submission-0/newline-normalization.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@
return form;
}

function createFormWithFile(testCase, name, filename) {
const form = document.createElement("form");
const input = document.createElement("input");
input.type = "file";
input.name = name;
const dataTransfer = new DataTransfer();
dataTransfer.items.add(new File([], filename, { type: "text/plain" }));
input.files = dataTransfer.files;
form.appendChild(input);
document.body.appendChild(form);
testCase.add_cleanup(() => {
document.body.removeChild(form);
});
return form;
}

test((testCase) => {
const formData = new FormData(createForm(testCase, "a", "b\nc"));
assert_equals(formData.get("a"), "b\r\nc");
Expand Down Expand Up @@ -61,6 +77,34 @@
const formData = new FormData(createForm(testCase, "a\n\rb", "c"));
assert_equals([...formData][0][0], "a\r\n\r\nb");
}, document.title + ": \\n\\r in the name becomes \\r\\n\\r\\n");

test((testCase) => {
const formData = new FormData(
createFormWithFile(testCase, "a", "b\nc")
);
assert_equals(formData.get("a").name, "b\nc");
}, document.title + ": \\n in the filename stays unchanged");

test((testCase) => {
const formData = new FormData(
createFormWithFile(testCase, "a", "b\rc")
);
assert_equals(formData.get("a").name, "b\rc");
}, document.title + ": \\r in the filename stays unchanged");

test((testCase) => {
const formData = new FormData(
createFormWithFile(testCase, "a", "b\r\nc")
);
assert_equals(formData.get("a").name, "b\r\nc");
}, document.title + ": \\r\\n in the filename stays unchanged");

test((testCase) => {
const formData = new FormData(
createFormWithFile(testCase, "a", "b\n\rc")
);
assert_equals(formData.get("a").name, "b\n\rc");
}, document.title + ": \\n\\r in the filename stays unchanged");
</script>
</body>
</html>

0 comments on commit a8fad46

Please sign in to comment.