aboutsummaryrefslogtreecommitdiffstats
path: root/components/script
diff options
context:
space:
mode:
authorbors-servo <lbergstrom+bors@mozilla.com>2018-03-26 17:30:36 -0400
committerGitHub <noreply@github.com>2018-03-26 17:30:36 -0400
commit1981efcc3585d244f6293716fbcf833afa58e629 (patch)
tree2a6d32df31a85660c927cd981e7e5b9787f45321 /components/script
parent4b8416fafbbfec7f3adbc46874ea779ed90ad3d1 (diff)
parent36838d2d46ca1e8782ecabb2ad5b854ba693dac2 (diff)
downloadservo-1981efcc3585d244f6293716fbcf833afa58e629.tar.gz
servo-1981efcc3585d244f6293716fbcf833afa58e629.zip
Auto merge of #20431 - talklittle:issues-13234-5600-squashed, r=jdm
TextDecoder: streaming decode, ignoreBOM <!-- Please describe your changes on the following line: --> Implement streaming decode and ignoreBOM flag for TextDecoder. https://encoding.spec.whatwg.org/#dom-textdecoder-decode https://encoding.spec.whatwg.org/#dom-textdecoder-ignorebom --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] These changes fix #13234 (github issue number if applicable). - [x] These changes fix #5600 (github issue number if applicable). <!-- Either: --> - [ ] There are tests for these changes OR - [x] These changes do not require tests because the wpt tests are used for testing: * /encoding/textdecoder-fatal-streaming.html * /encoding/textdecoder-streaming.html * /encoding/textdecoder-ignorebom.html <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/20431) <!-- Reviewable:end -->
Diffstat (limited to 'components/script')
-rw-r--r--components/script/dom/bindings/trace.rs5
-rw-r--r--components/script/dom/textdecoder.rs97
-rw-r--r--components/script/dom/webidls/TextDecoder.webidl6
3 files changed, 81 insertions, 27 deletions
diff --git a/components/script/dom/bindings/trace.rs b/components/script/dom/bindings/trace.rs
index be38b14a540..0fd94953e3b 100644
--- a/components/script/dom/bindings/trace.rs
+++ b/components/script/dom/bindings/trace.rs
@@ -47,7 +47,7 @@ use dom::bindings::root::{Dom, DomRoot};
use dom::bindings::str::{DOMString, USVString};
use dom::bindings::utils::WindowProxyHandler;
use dom::document::PendingRestyle;
-use encoding_rs::Encoding;
+use encoding_rs::{Decoder, Encoding};
use euclid::{Transform2D, Transform3D, Point2D, Vector2D, Rect, TypedSize2D, TypedScale};
use euclid::Length as EuclidLength;
use html5ever::{Prefix, LocalName, Namespace, QualName};
@@ -127,6 +127,9 @@ unsafe_no_jsmanaged_fields!(CSSError);
unsafe_no_jsmanaged_fields!(&'static Encoding);
+unsafe_no_jsmanaged_fields!(RefCell<Decoder>);
+unsafe_no_jsmanaged_fields!(RefCell<Vec<u8>>);
+
unsafe_no_jsmanaged_fields!(Reflector);
unsafe_no_jsmanaged_fields!(Duration);
diff --git a/components/script/dom/textdecoder.rs b/components/script/dom/textdecoder.rs
index 3c061322f07..b9e0bdd9d55 100644
--- a/components/script/dom/textdecoder.rs
+++ b/components/script/dom/textdecoder.rs
@@ -11,22 +11,34 @@ use dom::bindings::root::DomRoot;
use dom::bindings::str::{DOMString, USVString};
use dom::globalscope::GlobalScope;
use dom_struct::dom_struct;
-use encoding_rs::Encoding;
+use encoding_rs::{Decoder, DecoderResult, Encoding};
use std::borrow::ToOwned;
+use std::cell::{Cell, RefCell};
#[dom_struct]
pub struct TextDecoder {
reflector_: Reflector,
encoding: &'static Encoding,
fatal: bool,
+ ignoreBOM: bool,
+ #[ignore_malloc_size_of = "defined in encoding_rs"]
+ decoder: RefCell<Decoder>,
+ in_stream: RefCell<Vec<u8>>,
+ do_not_flush: Cell<bool>,
}
impl TextDecoder {
- fn new_inherited(encoding: &'static Encoding, fatal: bool) -> TextDecoder {
+ fn new_inherited(encoding: &'static Encoding, fatal: bool, ignoreBOM: bool) -> TextDecoder {
TextDecoder {
reflector_: Reflector::new(),
encoding: encoding,
fatal: fatal,
+ ignoreBOM: ignoreBOM,
+ decoder: RefCell::new(
+ if ignoreBOM { encoding.new_decoder() } else { encoding.new_decoder_without_bom_handling() }
+ ),
+ in_stream: RefCell::new(Vec::new()),
+ do_not_flush: Cell::new(false),
}
}
@@ -34,8 +46,9 @@ impl TextDecoder {
Err(Error::Range("The given encoding is not supported.".to_owned()))
}
- pub fn new(global: &GlobalScope, encoding: &'static Encoding, fatal: bool) -> DomRoot<TextDecoder> {
- reflect_dom_object(Box::new(TextDecoder::new_inherited(encoding, fatal)),
+ pub fn new(global: &GlobalScope, encoding: &'static Encoding, fatal: bool, ignoreBOM: bool)
+ -> DomRoot<TextDecoder> {
+ reflect_dom_object(Box::new(TextDecoder::new_inherited(encoding, fatal, ignoreBOM)),
global,
TextDecoderBinding::Wrap)
}
@@ -49,7 +62,7 @@ impl TextDecoder {
None => return TextDecoder::make_range_error(),
Some(enc) => enc
};
- Ok(TextDecoder::new(global, encoding, options.fatal))
+ Ok(TextDecoder::new(global, encoding, options.fatal, options.ignoreBOM))
}
}
@@ -65,30 +78,68 @@ impl TextDecoderMethods for TextDecoder {
self.fatal
}
+ // https://encoding.spec.whatwg.org/#dom-textdecoder-ignorebom
+ fn IgnoreBOM(&self) -> bool {
+ self.ignoreBOM
+ }
+
// https://encoding.spec.whatwg.org/#dom-textdecoder-decode
fn Decode(
&self,
input: Option<ArrayBufferViewOrArrayBuffer>,
- _options: &TextDecodeOptions
+ options: &TextDecodeOptions
) -> Fallible<USVString> {
- match input {
- Some(arr) => {
- let vec: Vec<u8> = match arr {
- ArrayBufferViewOrArrayBuffer::ArrayBufferView(ref a) => a.to_vec(),
- ArrayBufferViewOrArrayBuffer::ArrayBuffer(ref a) => a.to_vec()
- };
- let s = if self.fatal {
- match self.encoding.decode_without_bom_handling_and_without_replacement(&vec) {
- Some(s) => s,
- None => return Err(Error::Type("Decoding failed".to_owned())),
- }
- } else {
- let (s, _has_errors) = self.encoding.decode_without_bom_handling(&vec);
- s
- };
- Ok(USVString(s.into_owned()))
+ // Step 1.
+ if !self.do_not_flush.get() {
+ if self.ignoreBOM {
+ self.decoder.replace(self.encoding.new_decoder_without_bom_handling());
+ } else {
+ self.decoder.replace(self.encoding.new_decoder());
}
- None => Ok(USVString("".to_owned()))
+ self.in_stream.replace(Vec::new());
}
+
+ // Step 2.
+ self.do_not_flush.set(options.stream);
+
+ // Step 3.
+ match input {
+ Some(ArrayBufferViewOrArrayBuffer::ArrayBufferView(ref a)) => {
+ self.in_stream.borrow_mut().extend_from_slice(&a.to_vec());
+ },
+ Some(ArrayBufferViewOrArrayBuffer::ArrayBuffer(ref a)) => {
+ self.in_stream.borrow_mut().extend_from_slice(&a.to_vec());
+ },
+ None => {},
+ };
+
+ let mut decoder = self.decoder.borrow_mut();
+ let (remaining, s) = {
+ let mut in_stream = self.in_stream.borrow_mut();
+
+ let (remaining, s) = if self.fatal {
+ // Step 4.
+ let mut out_stream = String::with_capacity(
+ decoder.max_utf8_buffer_length_without_replacement(in_stream.len()).unwrap()
+ );
+ // Step 5: Implemented by encoding_rs::Decoder.
+ match decoder.decode_to_string_without_replacement(&in_stream, &mut out_stream, !options.stream) {
+ (DecoderResult::InputEmpty, read) => {
+ (in_stream.split_off(read), out_stream)
+ },
+ // Step 5.3.3.
+ _ => return Err(Error::Type("Decoding failed".to_owned())),
+ }
+ } else {
+ // Step 4.
+ let mut out_stream = String::with_capacity(decoder.max_utf8_buffer_length(in_stream.len()).unwrap());
+ // Step 5: Implemented by encoding_rs::Decoder.
+ let (_result, read, _replaced) = decoder.decode_to_string(&in_stream, &mut out_stream, !options.stream);
+ (in_stream.split_off(read), out_stream)
+ };
+ (remaining, s)
+ };
+ self.in_stream.replace(remaining);
+ Ok(USVString(s))
}
}
diff --git a/components/script/dom/webidls/TextDecoder.webidl b/components/script/dom/webidls/TextDecoder.webidl
index b511fec3d79..4e9c3683dd7 100644
--- a/components/script/dom/webidls/TextDecoder.webidl
+++ b/components/script/dom/webidls/TextDecoder.webidl
@@ -5,18 +5,18 @@
// https://encoding.spec.whatwg.org/#interface-textdecoder
dictionary TextDecoderOptions {
boolean fatal = false;
- // boolean ignoreBOM = false;
+ boolean ignoreBOM = false;
};
dictionary TextDecodeOptions {
- // boolean stream = false;
+ boolean stream = false;
};
[Constructor(optional DOMString label = "utf-8", optional TextDecoderOptions options), Exposed=(Window,Worker)]
interface TextDecoder {
readonly attribute DOMString encoding;
readonly attribute boolean fatal;
- // readonly attribute boolean ignoreBOM;
+ readonly attribute boolean ignoreBOM;
[Throws]
USVString decode(optional BufferSource input, optional TextDecodeOptions options);
};