diff options
author | webbeef <me@webbeef.org> | 2025-04-15 10:12:48 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-04-15 17:12:48 +0000 |
commit | 98884a508186a5a0fe5e3a214e59e2cadaa65736 (patch) | |
tree | 20a6815045109311b56614b2ab308ec452e720d9 /components/script/mime.rs | |
parent | 32d59cfff42b7e2f2a1c39699cacfb6efa10fdd9 (diff) | |
download | servo-98884a508186a5a0fe5e3a214e59e2cadaa65736.tar.gz servo-98884a508186a5a0fe5e3a214e59e2cadaa65736.zip |
Switch to data_url::mime for document content type (#36522)
The data_url Mime parser has a more conformant behavior in most cases,
including dealing with charsets.
Testing: wpt expectations with new passes are updated.
Signed-off-by: webbeef <me@webbeef.org>
Diffstat (limited to 'components/script/mime.rs')
-rw-r--r-- | components/script/mime.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/components/script/mime.rs b/components/script/mime.rs new file mode 100644 index 00000000000..8d45e84252a --- /dev/null +++ b/components/script/mime.rs @@ -0,0 +1,62 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +use data_url::mime::Mime; +use headers::ContentType; + +pub(crate) static APPLICATION: &str = "application"; +pub(crate) static CHARSET: &str = "charset"; +pub(crate) static HTML: &str = "html"; +pub(crate) static TEXT: &str = "text"; +pub(crate) static XML: &str = "xml"; + +/// Convenience methods to make the data_url Mime type more ergonomic. +pub(crate) trait MimeExt { + /// Creates a new Mime from type and subtype, without any parameter. + fn new(type_: &str, subtype: &str) -> Self; + + /// Checks that this Mime matches a given type and subtype, ignoring + /// parameters. + fn matches(&self, type_: &str, subtype: &str) -> bool; + + /// Checks that the subtype has a given suffix. + /// Eg. image/svg+xml has the the xml suffix. + fn has_suffix(&self, suffix: &str) -> bool; + + /// TODO: replace by a derive on data_url. + fn clone(&self) -> Self; + + /// Build a Mime from the value of a Content-Type header. + fn from_ct(ct: ContentType) -> Self; +} + +impl MimeExt for Mime { + fn new(type_: &str, subtype: &str) -> Self { + Mime { + type_: type_.into(), + subtype: subtype.into(), + parameters: vec![], + } + } + + fn matches(&self, type_: &str, subtype: &str) -> bool { + self.type_ == type_ && self.subtype == subtype + } + + fn has_suffix(&self, suffix: &str) -> bool { + self.subtype.ends_with(&format!("+{}", suffix)) + } + + fn clone(&self) -> Self { + Self { + type_: self.type_.clone(), + subtype: self.subtype.clone(), + parameters: self.parameters.clone(), + } + } + + fn from_ct(ct: ContentType) -> Self { + ct.to_string().parse().unwrap() + } +} |