mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01:00
Use encoding-rs instead of rust-encoding for CSS parsing
This commit is contained in:
parent
a3ac21d23d
commit
3c36a36cc9
6 changed files with 37 additions and 26 deletions
|
@ -40,6 +40,7 @@ devtools_traits = {path = "../devtools_traits"}
|
|||
dom_struct = {path = "../dom_struct"}
|
||||
domobject_derive = {path = "../domobject_derive"}
|
||||
encoding = "0.2"
|
||||
encoding_rs = "0.7"
|
||||
euclid = "0.15"
|
||||
fnv = "1.0"
|
||||
gleam = "0.4"
|
||||
|
|
|
@ -35,6 +35,7 @@ extern crate dom_struct;
|
|||
#[macro_use]
|
||||
extern crate domobject_derive;
|
||||
extern crate encoding;
|
||||
extern crate encoding_rs;
|
||||
extern crate euclid;
|
||||
extern crate fnv;
|
||||
extern crate gleam;
|
||||
|
|
|
@ -13,8 +13,7 @@ use dom::eventtarget::EventTarget;
|
|||
use dom::htmlelement::HTMLElement;
|
||||
use dom::htmllinkelement::{RequestGenerationId, HTMLLinkElement};
|
||||
use dom::node::{document_from_node, window_from_node};
|
||||
use encoding::EncodingRef;
|
||||
use encoding::all::UTF_8;
|
||||
use encoding_rs::UTF_8;
|
||||
use hyper::header::ContentType;
|
||||
use hyper::mime::{Mime, TopLevel, SubLevel};
|
||||
use hyper_serde::Serde;
|
||||
|
@ -127,7 +126,7 @@ impl FetchResponseListener for StylesheetContext {
|
|||
let data = if is_css { mem::replace(&mut self.data, vec![]) } else { vec![] };
|
||||
|
||||
// TODO: Get the actual value. http://dev.w3.org/csswg/css-syntax/#environment-encoding
|
||||
let environment_encoding = UTF_8 as EncodingRef;
|
||||
let environment_encoding = UTF_8;
|
||||
let protocol_encoding_label = metadata.charset.as_ref().map(|s| &**s);
|
||||
let final_url = metadata.final_url;
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ gecko = ["nsstring", "num_cpus",
|
|||
"style_traits/gecko", "fallible/known_system_malloc"]
|
||||
use_bindgen = ["bindgen", "regex", "toml"]
|
||||
servo = ["serde", "style_traits/servo", "servo_atoms", "servo_config", "html5ever",
|
||||
"cssparser/serde", "encoding", "malloc_size_of/servo",
|
||||
"cssparser/serde", "encoding_rs", "malloc_size_of/servo",
|
||||
|
||||
# FIXME: Uncomment when https://github.com/servo/servo/pull/16953 has landed:
|
||||
#"arrayvec/use_union"
|
||||
|
@ -36,7 +36,7 @@ bitflags = "1.0"
|
|||
byteorder = "1.0"
|
||||
cfg-if = "0.1.0"
|
||||
cssparser = "0.22.0"
|
||||
encoding = {version = "0.2", optional = true}
|
||||
encoding_rs = {version = "0.7", optional = true}
|
||||
euclid = "0.15"
|
||||
fallible = { path = "../fallible" }
|
||||
fnv = "1.0"
|
||||
|
|
|
@ -4,43 +4,45 @@
|
|||
|
||||
//! Parsing stylesheets from bytes (not `&str`).
|
||||
|
||||
extern crate encoding;
|
||||
extern crate encoding_rs;
|
||||
|
||||
use context::QuirksMode;
|
||||
use cssparser::{stylesheet_encoding, EncodingSupport};
|
||||
use error_reporting::ParseErrorReporter;
|
||||
use media_queries::MediaList;
|
||||
use self::encoding::{EncodingRef, DecoderTrap};
|
||||
use servo_arc::Arc;
|
||||
use shared_lock::SharedRwLock;
|
||||
use std::borrow::Cow;
|
||||
use std::str;
|
||||
use stylesheets::{Stylesheet, StylesheetLoader, Origin, UrlExtraData};
|
||||
|
||||
struct RustEncoding;
|
||||
struct EncodingRs;
|
||||
|
||||
impl EncodingSupport for RustEncoding {
|
||||
type Encoding = EncodingRef;
|
||||
impl EncodingSupport for EncodingRs {
|
||||
type Encoding = &'static encoding_rs::Encoding;
|
||||
|
||||
fn utf8() -> Self::Encoding {
|
||||
encoding::all::UTF_8
|
||||
encoding_rs::UTF_8
|
||||
}
|
||||
|
||||
fn is_utf16_be_or_le(encoding: &Self::Encoding) -> bool {
|
||||
matches!(encoding.name(), "utf-16be" | "utf-16le")
|
||||
*encoding == encoding_rs::UTF_16LE ||
|
||||
*encoding == encoding_rs::UTF_16BE
|
||||
}
|
||||
|
||||
fn from_label(ascii_label: &[u8]) -> Option<Self::Encoding> {
|
||||
str::from_utf8(ascii_label).ok().and_then(encoding::label::encoding_from_whatwg_label)
|
||||
encoding_rs::Encoding::for_label(ascii_label)
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_stylesheet_bytes(css: &[u8], protocol_encoding_label: Option<&str>,
|
||||
environment_encoding: Option<EncodingRef>)
|
||||
-> (String, EncodingRef) {
|
||||
let fallback_encoding = stylesheet_encoding::<RustEncoding>(
|
||||
fn decode_stylesheet_bytes<'a>(css: &'a [u8], protocol_encoding_label: Option<&str>,
|
||||
environment_encoding: Option<&'static encoding_rs::Encoding>)
|
||||
-> Cow<'a, str> {
|
||||
let fallback_encoding = stylesheet_encoding::<EncodingRs>(
|
||||
css, protocol_encoding_label.map(str::as_bytes), environment_encoding);
|
||||
let (result, used_encoding) = encoding::decode(css, DecoderTrap::Replace, fallback_encoding);
|
||||
(result.unwrap(), used_encoding)
|
||||
let (result, _used_encoding, _) = fallback_encoding.decode(&css);
|
||||
// FIXME record used encoding for environment encoding of @import
|
||||
result
|
||||
}
|
||||
|
||||
impl Stylesheet {
|
||||
|
@ -52,7 +54,7 @@ impl Stylesheet {
|
|||
pub fn from_bytes<R>(bytes: &[u8],
|
||||
url_data: UrlExtraData,
|
||||
protocol_encoding_label: Option<&str>,
|
||||
environment_encoding: Option<EncodingRef>,
|
||||
environment_encoding: Option<&'static encoding_rs::Encoding>,
|
||||
origin: Origin,
|
||||
media: MediaList,
|
||||
shared_lock: SharedRwLock,
|
||||
|
@ -62,8 +64,7 @@ impl Stylesheet {
|
|||
-> Stylesheet
|
||||
where R: ParseErrorReporter
|
||||
{
|
||||
let (string, _) = decode_stylesheet_bytes(
|
||||
bytes, protocol_encoding_label, environment_encoding);
|
||||
let string = decode_stylesheet_bytes(bytes, protocol_encoding_label, environment_encoding);
|
||||
Stylesheet::from_str(&string,
|
||||
url_data,
|
||||
origin,
|
||||
|
@ -80,14 +81,13 @@ impl Stylesheet {
|
|||
pub fn update_from_bytes<R>(existing: &Stylesheet,
|
||||
bytes: &[u8],
|
||||
protocol_encoding_label: Option<&str>,
|
||||
environment_encoding: Option<EncodingRef>,
|
||||
environment_encoding: Option<&'static encoding_rs::Encoding>,
|
||||
url_data: UrlExtraData,
|
||||
stylesheet_loader: Option<&StylesheetLoader>,
|
||||
error_reporter: &R)
|
||||
where R: ParseErrorReporter
|
||||
{
|
||||
let (string, _) = decode_stylesheet_bytes(
|
||||
bytes, protocol_encoding_label, environment_encoding);
|
||||
let string = decode_stylesheet_bytes(bytes, protocol_encoding_label, environment_encoding);
|
||||
Self::update_from_str(existing,
|
||||
&string,
|
||||
url_data,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue