From 89b8499df8bd9a9c4a78d93fb644fb764c5fabbb Mon Sep 17 00:00:00 2001 From: askalski Date: Sat, 9 Jan 2016 21:40:45 +0100 Subject: [PATCH] Implement encoding determination for external scripts. --- components/script/dom/htmlscriptelement.rs | 21 +++-- tests/wpt/metadata/MANIFEST.json | 8 ++ .../external-script-utf8.js | 5 ++ .../external-script-windows1250.js | 5 ++ .../the-script-element/script-charset-01.html | 89 +++++++++++++++++++ .../the-script-element/script-charset-02.html | 40 +++++++++ .../serve-with-content-type.py | 15 ++++ 7 files changed, 175 insertions(+), 8 deletions(-) create mode 100644 tests/wpt/web-platform-tests/html/semantics/scripting-1/the-script-element/external-script-utf8.js create mode 100644 tests/wpt/web-platform-tests/html/semantics/scripting-1/the-script-element/external-script-windows1250.js create mode 100644 tests/wpt/web-platform-tests/html/semantics/scripting-1/the-script-element/script-charset-01.html create mode 100644 tests/wpt/web-platform-tests/html/semantics/scripting-1/the-script-element/script-charset-02.html create mode 100644 tests/wpt/web-platform-tests/html/semantics/scripting-1/the-script-element/serve-with-content-type.py diff --git a/components/script/dom/htmlscriptelement.rs b/components/script/dom/htmlscriptelement.rs index bbd1884dc4f..eaafcadd7cc 100644 --- a/components/script/dom/htmlscriptelement.rs +++ b/components/script/dom/htmlscriptelement.rs @@ -25,7 +25,6 @@ use dom::node::{ChildrenMutation, CloneChildrenFlag, Node}; use dom::node::{document_from_node, window_from_node}; use dom::virtualmethods::VirtualMethods; use dom::window::ScriptHelpers; -use encoding::all::UTF_8; use encoding::label::encoding_from_whatwg_label; use encoding::types::{DecoderTrap, Encoding, EncodingRef}; use html5ever::tree_builder::NextParserState; @@ -71,7 +70,7 @@ pub struct HTMLScriptElement { #[ignore_heap_size_of = "Defined in rust-encoding"] /// https://html.spec.whatwg.org/multipage/#concept-script-encoding - block_character_encoding: DOMRefCell, + block_character_encoding: DOMRefCell>, } impl HTMLScriptElement { @@ -86,7 +85,7 @@ impl HTMLScriptElement { ready_to_be_parser_executed: Cell::new(false), parser_document: JS::from_ref(document), load: DOMRefCell::new(None), - block_character_encoding: DOMRefCell::new(UTF_8 as EncodingRef), + block_character_encoding: DOMRefCell::new(None), } } @@ -248,7 +247,7 @@ impl HTMLScriptElement { // Step 13. if let Some(ref charset) = element.get_attribute(&ns!(), &atom!("charset")) { if let Some(encodingRef) = encoding_from_whatwg_label(&charset.Value()) { - *self.block_character_encoding.borrow_mut() = encodingRef; + *self.block_character_encoding.borrow_mut() = Some(encodingRef); } } @@ -391,10 +390,16 @@ impl HTMLScriptElement { // Step 2.b.1.a. ScriptOrigin::External(Ok((metadata, bytes))) => { - // TODO(#9185): implement encoding determination. - (DOMString::from(UTF_8.decode(&*bytes, DecoderTrap::Replace).unwrap()), - true, - metadata.final_url) + debug!("loading external script, url = {}", metadata.final_url); + + let encoding = metadata.charset + .and_then(|encoding| encoding_from_whatwg_label(&encoding)) + .or_else(|| *self.block_character_encoding.borrow()) + .unwrap_or_else(|| self.parser_document.encoding()); + + (DOMString::from(encoding.decode(&*bytes, DecoderTrap::Replace).unwrap()), + true, + metadata.final_url) }, // Step 2.b.1.c. diff --git a/tests/wpt/metadata/MANIFEST.json b/tests/wpt/metadata/MANIFEST.json index 6932cb31acb..feefdc2077e 100644 --- a/tests/wpt/metadata/MANIFEST.json +++ b/tests/wpt/metadata/MANIFEST.json @@ -19241,6 +19241,14 @@ "path": "html/semantics/scripting-1/the-script-element/script-before-after-events.html", "url": "/html/semantics/scripting-1/the-script-element/script-before-after-events.html" }, + { + "path": "html/semantics/scripting-1/the-script-element/script-charset-01.html", + "url": "/html/semantics/scripting-1/the-script-element/script-charset-01.html" + }, + { + "path": "html/semantics/scripting-1/the-script-element/script-charset-02.html", + "url": "/html/semantics/scripting-1/the-script-element/script-charset-02.html" + }, { "path": "html/semantics/scripting-1/the-script-element/script-for-event-xhtml.xhtml", "url": "/html/semantics/scripting-1/the-script-element/script-for-event-xhtml.xhtml" diff --git a/tests/wpt/web-platform-tests/html/semantics/scripting-1/the-script-element/external-script-utf8.js b/tests/wpt/web-platform-tests/html/semantics/scripting-1/the-script-element/external-script-utf8.js new file mode 100644 index 00000000000..eb442c97bc9 --- /dev/null +++ b/tests/wpt/web-platform-tests/html/semantics/scripting-1/the-script-element/external-script-utf8.js @@ -0,0 +1,5 @@ +(function() { + window.getSomeString = function() { + return "śćążź"; //<- these are five Polish letters, similar to scazz. It can be read correctly only with windows 1250 encoding. + }; +})(); diff --git a/tests/wpt/web-platform-tests/html/semantics/scripting-1/the-script-element/external-script-windows1250.js b/tests/wpt/web-platform-tests/html/semantics/scripting-1/the-script-element/external-script-windows1250.js new file mode 100644 index 00000000000..50de6932ba2 --- /dev/null +++ b/tests/wpt/web-platform-tests/html/semantics/scripting-1/the-script-element/external-script-windows1250.js @@ -0,0 +1,5 @@ +(function() { + window.getSomeString = function() { + return "œæ¹¿Ÿ"; //<- these are five Polish letters, similar to scazz. It can be read correctly only with windows 1250 encoding. + }; +})(); diff --git a/tests/wpt/web-platform-tests/html/semantics/scripting-1/the-script-element/script-charset-01.html b/tests/wpt/web-platform-tests/html/semantics/scripting-1/the-script-element/script-charset-01.html new file mode 100644 index 00000000000..c5ac0d0a62a --- /dev/null +++ b/tests/wpt/web-platform-tests/html/semantics/scripting-1/the-script-element/script-charset-01.html @@ -0,0 +1,89 @@ + + + + Script @type: unknown parameters + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + Script @type: unknown parameters + + + + +
+ + + + + + + + + + diff --git a/tests/wpt/web-platform-tests/html/semantics/scripting-1/the-script-element/serve-with-content-type.py b/tests/wpt/web-platform-tests/html/semantics/scripting-1/the-script-element/serve-with-content-type.py new file mode 100644 index 00000000000..7cfe6f4cec3 --- /dev/null +++ b/tests/wpt/web-platform-tests/html/semantics/scripting-1/the-script-element/serve-with-content-type.py @@ -0,0 +1,15 @@ +import os + +def main(request, response): + directory = os.path.dirname(__file__) + + try: + file_name = request.GET.first("fn") + content_type = request.GET.first("ct") + with open(os.path.join(directory, file_name), "rb") as fh: + content = fh.read() + + response.headers.set("Content-Type", content_type) + response.content = content + except: + response.set_error(400, "Not enough parameters or file not found")