Files
ladybird/Tests/LibWeb/Text/input/URL/url.html
Shannon Booth d6af5bf5eb LibURL: Allow inputs containing only whitespace
The check for:

```
    if (start_index >= end_index)
        return {};
```

To prevent an out of bounds when trimming the start and end of the input
of whitespace was preventing valid URLs (only having whitespace in the
input) from being parsed.

Instead, prevent start_index from ever getting above end_index in the
first place, and don't treat empty inputs as an error.

Fixes one WPT test on:

https://wpt.live/url/url-constructor.any.html
2024-08-05 17:21:26 +01:00

59 lines
2.2 KiB
HTML

<script src="../include.js"></script>
<script>
test(() => {
function escapeWhitespace(str) {
return str
.replace(/\t/g, '\\t')
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r');
}
function printURL(url) {
println(`protocol => '${url.protocol}'`);
println(`username => '${url.username}'`);
println(`password => '${url.password}'`);
println(`host => '${url.host}'`);
println(`hostname => '${url.hostname}'`);
println(`port => '${url.port}'`);
println(`pathname => '${url.pathname}'`);
println(`search => '${url.search}'`);
println(`hash => '${url.hash}'`);
}
const urls = [
{ input: 'ftp://serenityos.org:21' },
{ input: 'http://[0:1:0:1:0:1:0:1]' },
{ input: 'http://[1:0:1:0:1:0:1:0]' },
{ input: 'http://[1:1:0:0:1:0:0:0]/' },
{ input: 'unknown://serenityos.org:0' },
{ input: 'http://serenityos.org/cat?dog#meow"woof' },
{ input: '/hello', base: 'file://friends/' },
{ input: '//d:/..', base: 'file:///C:/a/b' },
{ input: 'file://a%C2%ADb/p' },
{ input: 'http://user%20name:pa%40ss%3Aword@www.ladybird.org' },
{ input: 'h\tt\nt\rp://h\to\ns\rt:9\t0\n0\r0/p\ta\nt\rh?q\tu\ne\rry#f\tr\na\rg' },
{ input: ' \t', base: 'http://ladybird.org/foo/bar' },
];
for (url of urls) {
if (url.base === undefined)
println(`new URL('${escapeWhitespace(url.input)}', ${url.base})`);
else
println(`new URL('${escapeWhitespace(url.input)}', '${escapeWhitespace(url.base)}')`);
printURL(new URL(url.input, url.base));
}
println('=========================================');
for (url of urls) {
if (url.base === undefined)
println(`URL.parse('${escapeWhitespace(url.input)}', ${url.base})`);
else
println(`URL.parse('${escapeWhitespace(url.input)}', '${escapeWhitespace(url.base)}')`);
printURL(URL.parse(url.input, url.base));
}
});
</script>