Skip to content

Commit f626aaf

Browse files
committed
More strict parsing rules for the setters.
1 parent 891e5f1 commit f626aaf

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/parser.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,18 @@ impl<'i> Input<'i> {
201201
Input::with_log(input, None)
202202
}
203203

204-
pub fn with_log(input: &'i str, vfn: Option<&dyn Fn(SyntaxViolation)>) -> Self {
204+
pub fn no_trim(input: &'i str) -> Self {
205+
Input {
206+
chars: input.chars(),
207+
}
208+
}
209+
210+
pub fn with_log(original_input: &'i str, vfn: Option<&dyn Fn(SyntaxViolation)>) -> Self {
211+
let input = original_input.trim_matches(c0_control_or_space);
205212
if let Some(vfn) = vfn {
213+
if input.len() != original_input.len() {
214+
vfn(SyntaxViolation::C0SpaceIgnored)
215+
}
206216
if input.chars().any(|c| matches!(c, '\t' | '\n' | '\r')) {
207217
vfn(SyntaxViolation::TabOrNewlineIgnored)
208218
}
@@ -1463,6 +1473,12 @@ fn c0_control_or_space(ch: char) -> bool {
14631473
ch <= ' ' // U+0000 to U+0020
14641474
}
14651475

1476+
/// https://infra.spec.whatwg.org/#ascii-tab-or-newline
1477+
#[inline]
1478+
fn ascii_tab_or_new_line(ch: char) -> bool {
1479+
matches!(ch, '\t' | '\r' | '\n')
1480+
}
1481+
14661482
/// https://url.spec.whatwg.org/#ascii-alpha
14671483
#[inline]
14681484
pub fn ascii_alpha(ch: char) -> bool {

src/quirks.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
//! you probably want to use `Url` method instead.
1313
1414
use parser::{default_port, Context, Input, Parser, SchemeType};
15+
use std::cell::RefCell;
16+
use SyntaxViolation;
1517
use {idna, Host, ParseError, Position, Url};
1618

1719
/// https://url.spec.whatwg.org/#dom-url-domaintoascii
@@ -103,15 +105,18 @@ pub fn set_host(url: &mut Url, new_host: &str) -> Result<(), ()> {
103105
if url.cannot_be_a_base() {
104106
return Err(());
105107
}
108+
// Host parsing rules are strict,
109+
// We don't want to trim the input
110+
let input = Input::no_trim(new_host);
106111
let host;
107112
let opt_port;
108113
{
109114
let scheme = url.scheme();
110115
let scheme_type = SchemeType::from(scheme);
111116
let result = if scheme_type == SchemeType::File {
112-
Parser::get_file_host(Input::new(new_host))
117+
Parser::get_file_host(input)
113118
} else {
114-
Parser::parse_host(Input::new(new_host), scheme_type)
119+
Parser::parse_host(input, scheme_type)
115120
};
116121
match result {
117122
Ok((h, remaining)) => {
@@ -160,11 +165,14 @@ pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
160165
if url.cannot_be_a_base() {
161166
return Err(());
162167
}
168+
// Host parsing rules are strict,
169+
// We don't want to trim the input
170+
let input = Input::no_trim(new_hostname);
163171
let scheme_type = SchemeType::from(url.scheme());
164172
let result = if scheme_type == SchemeType::File {
165-
Parser::get_file_host(Input::new(new_hostname))
173+
Parser::get_file_host(input)
166174
} else {
167-
Parser::parse_host(Input::new(new_hostname), scheme_type)
175+
Parser::parse_host(input, scheme_type)
168176
};
169177
if let Ok((host, _remaining)) = result {
170178
if let Host::Domain(h) = &host {

0 commit comments

Comments
 (0)