mirror of
https://github.com/servo/servo.git
synced 2025-07-23 15:23:42 +01:00
Merge pull request #3387 from prasoon2211/base64_methods_for_worker
Implement atob and btoa methods for worker
This commit is contained in:
commit
61642d64b5
3 changed files with 88 additions and 70 deletions
|
@ -23,7 +23,7 @@ partial interface WorkerGlobalScope { // not obsolete
|
||||||
readonly attribute WorkerNavigator navigator;
|
readonly attribute WorkerNavigator navigator;
|
||||||
};
|
};
|
||||||
//WorkerGlobalScope implements WindowTimers;
|
//WorkerGlobalScope implements WindowTimers;
|
||||||
//WorkerGlobalScope implements WindowBase64;
|
WorkerGlobalScope implements WindowBase64;
|
||||||
|
|
||||||
// Proprietary
|
// Proprietary
|
||||||
partial interface WorkerGlobalScope {
|
partial interface WorkerGlobalScope {
|
||||||
|
|
|
@ -126,6 +126,81 @@ pub struct TimerData {
|
||||||
pub funval: Traceable<JSVal>,
|
pub funval: Traceable<JSVal>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// http://www.whatwg.org/html/#atob
|
||||||
|
pub fn base64_btoa(btoa: DOMString) -> Fallible<DOMString> {
|
||||||
|
let input = btoa.as_slice();
|
||||||
|
// "The btoa() method must throw an InvalidCharacterError exception if
|
||||||
|
// the method's first argument contains any character whose code point
|
||||||
|
// is greater than U+00FF."
|
||||||
|
if input.chars().any(|c: char| c > '\u00FF') {
|
||||||
|
Err(InvalidCharacter)
|
||||||
|
} else {
|
||||||
|
// "Otherwise, the user agent must convert that argument to a
|
||||||
|
// sequence of octets whose nth octet is the eight-bit
|
||||||
|
// representation of the code point of the nth character of
|
||||||
|
// the argument,"
|
||||||
|
let octets = input.chars().map(|c: char| c as u8).collect::<Vec<u8>>();
|
||||||
|
|
||||||
|
// "and then must apply the base64 algorithm to that sequence of
|
||||||
|
// octets, and return the result. [RFC4648]"
|
||||||
|
Ok(octets.as_slice().to_base64(STANDARD))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// http://www.whatwg.org/html/#atob
|
||||||
|
pub fn base64_atob(atob: DOMString) -> Fallible<DOMString> {
|
||||||
|
// "Let input be the string being parsed."
|
||||||
|
let mut input = atob.as_slice();
|
||||||
|
|
||||||
|
// "Remove all space characters from input."
|
||||||
|
// serialize::base64::from_base64 ignores \r and \n,
|
||||||
|
// but it treats the other space characters as
|
||||||
|
// invalid input.
|
||||||
|
fn is_html_space(c: char) -> bool {
|
||||||
|
HTML_SPACE_CHARACTERS.iter().any(|&m| m == c)
|
||||||
|
}
|
||||||
|
let without_spaces = input.chars()
|
||||||
|
.filter(|&c| ! is_html_space(c))
|
||||||
|
.collect::<String>();
|
||||||
|
input = without_spaces.as_slice();
|
||||||
|
|
||||||
|
// "If the length of input divides by 4 leaving no remainder, then:
|
||||||
|
// if input ends with one or two U+003D EQUALS SIGN (=) characters,
|
||||||
|
// remove them from input."
|
||||||
|
if input.len() % 4 == 0 {
|
||||||
|
if input.ends_with("==") {
|
||||||
|
input = input.slice_to(input.len() - 2)
|
||||||
|
} else if input.ends_with("=") {
|
||||||
|
input = input.slice_to(input.len() - 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// "If the length of input divides by 4 leaving a remainder of 1,
|
||||||
|
// throw an InvalidCharacterError exception and abort these steps."
|
||||||
|
if input.len() % 4 == 1 {
|
||||||
|
return Err(InvalidCharacter)
|
||||||
|
}
|
||||||
|
|
||||||
|
// "If input contains a character that is not in the following list of
|
||||||
|
// characters and character ranges, throw an InvalidCharacterError
|
||||||
|
// exception and abort these steps:
|
||||||
|
//
|
||||||
|
// U+002B PLUS SIGN (+)
|
||||||
|
// U+002F SOLIDUS (/)
|
||||||
|
// Alphanumeric ASCII characters"
|
||||||
|
if input.chars()
|
||||||
|
.find(|&c| !(c == '+' || c == '/' || c.is_alphanumeric()))
|
||||||
|
.is_some() {
|
||||||
|
return Err(InvalidCharacter)
|
||||||
|
}
|
||||||
|
|
||||||
|
match input.from_base64() {
|
||||||
|
Ok(data) => Ok(data.iter().map(|&b| b as char).collect::<String>()),
|
||||||
|
Err(..) => Err(InvalidCharacter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
impl<'a> WindowMethods for JSRef<'a, Window> {
|
impl<'a> WindowMethods for JSRef<'a, Window> {
|
||||||
fn Alert(&self, s: DOMString) {
|
fn Alert(&self, s: DOMString) {
|
||||||
// Right now, just print to the console
|
// Right now, just print to the console
|
||||||
|
@ -273,78 +348,12 @@ impl<'a> WindowMethods for JSRef<'a, Window> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// http://www.whatwg.org/html/#atob
|
|
||||||
fn Btoa(&self, btoa: DOMString) -> Fallible<DOMString> {
|
fn Btoa(&self, btoa: DOMString) -> Fallible<DOMString> {
|
||||||
let input = btoa.as_slice();
|
base64_btoa(btoa)
|
||||||
// "The btoa() method must throw an InvalidCharacterError exception if
|
|
||||||
// the method's first argument contains any character whose code point
|
|
||||||
// is greater than U+00FF."
|
|
||||||
if input.chars().any(|c: char| c > '\u00FF') {
|
|
||||||
Err(InvalidCharacter)
|
|
||||||
} else {
|
|
||||||
// "Otherwise, the user agent must convert that argument to a
|
|
||||||
// sequence of octets whose nth octet is the eight-bit
|
|
||||||
// representation of the code point of the nth character of
|
|
||||||
// the argument,"
|
|
||||||
let octets = input.chars().map(|c: char| c as u8).collect::<Vec<u8>>();
|
|
||||||
|
|
||||||
// "and then must apply the base64 algorithm to that sequence of
|
|
||||||
// octets, and return the result. [RFC4648]"
|
|
||||||
Ok(octets.as_slice().to_base64(STANDARD))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// http://www.whatwg.org/html/#atob
|
|
||||||
fn Atob(&self, atob: DOMString) -> Fallible<DOMString> {
|
fn Atob(&self, atob: DOMString) -> Fallible<DOMString> {
|
||||||
// "Let input be the string being parsed."
|
base64_atob(atob)
|
||||||
let mut input = atob.as_slice();
|
|
||||||
|
|
||||||
// "Remove all space characters from input."
|
|
||||||
// serialize::base64::from_base64 ignores \r and \n,
|
|
||||||
// but it treats the other space characters as
|
|
||||||
// invalid input.
|
|
||||||
fn is_html_space(c: char) -> bool {
|
|
||||||
HTML_SPACE_CHARACTERS.iter().any(|&m| m == c)
|
|
||||||
}
|
|
||||||
let without_spaces = input.chars()
|
|
||||||
.filter(|&c| ! is_html_space(c))
|
|
||||||
.collect::<String>();
|
|
||||||
input = without_spaces.as_slice();
|
|
||||||
|
|
||||||
// "If the length of input divides by 4 leaving no remainder, then:
|
|
||||||
// if input ends with one or two U+003D EQUALS SIGN (=) characters,
|
|
||||||
// remove them from input."
|
|
||||||
if input.len() % 4 == 0 {
|
|
||||||
if input.ends_with("==") {
|
|
||||||
input = input.slice_to(input.len() - 2)
|
|
||||||
} else if input.ends_with("=") {
|
|
||||||
input = input.slice_to(input.len() - 1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// "If the length of input divides by 4 leaving a remainder of 1,
|
|
||||||
// throw an InvalidCharacterError exception and abort these steps."
|
|
||||||
if input.len() % 4 == 1 {
|
|
||||||
return Err(InvalidCharacter)
|
|
||||||
}
|
|
||||||
|
|
||||||
// "If input contains a character that is not in the following list of
|
|
||||||
// characters and character ranges, throw an InvalidCharacterError
|
|
||||||
// exception and abort these steps:
|
|
||||||
//
|
|
||||||
// U+002B PLUS SIGN (+)
|
|
||||||
// U+002F SOLIDUS (/)
|
|
||||||
// Alphanumeric ASCII characters"
|
|
||||||
if input.chars()
|
|
||||||
.find(|&c| !(c == '+' || c == '/' || c.is_alphanumeric()))
|
|
||||||
.is_some() {
|
|
||||||
return Err(InvalidCharacter)
|
|
||||||
}
|
|
||||||
|
|
||||||
match input.from_base64() {
|
|
||||||
Ok(data) => Ok(data.iter().map(|&b| b as char).collect::<String>()),
|
|
||||||
Err(..) => Err(InvalidCharacter)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use dom::bindings::codegen::Bindings::WorkerGlobalScopeBinding::WorkerGlobalScopeMethods;
|
use dom::bindings::codegen::Bindings::WorkerGlobalScopeBinding::WorkerGlobalScopeMethods;
|
||||||
use dom::bindings::error::{ErrorResult, Syntax, Network, FailureUnknown};
|
use dom::bindings::error::{ErrorResult, Fallible, Syntax, Network, FailureUnknown};
|
||||||
use dom::bindings::trace::Untraceable;
|
use dom::bindings::trace::Untraceable;
|
||||||
use dom::bindings::global;
|
use dom::bindings::global;
|
||||||
use dom::bindings::js::{JS, JSRef, Temporary, OptionalSettable};
|
use dom::bindings::js::{JS, JSRef, Temporary, OptionalSettable};
|
||||||
|
@ -12,6 +12,7 @@ use dom::console::Console;
|
||||||
use dom::eventtarget::{EventTarget, WorkerGlobalScopeTypeId};
|
use dom::eventtarget::{EventTarget, WorkerGlobalScopeTypeId};
|
||||||
use dom::workerlocation::WorkerLocation;
|
use dom::workerlocation::WorkerLocation;
|
||||||
use dom::workernavigator::WorkerNavigator;
|
use dom::workernavigator::WorkerNavigator;
|
||||||
|
use dom::window::{base64_atob, base64_btoa};
|
||||||
use script_task::ScriptChan;
|
use script_task::ScriptChan;
|
||||||
|
|
||||||
use servo_net::resource_task::{ResourceTask, load_whole_resource};
|
use servo_net::resource_task::{ResourceTask, load_whole_resource};
|
||||||
|
@ -137,6 +138,14 @@ impl<'a> WorkerGlobalScopeMethods for JSRef<'a, WorkerGlobalScope> {
|
||||||
}
|
}
|
||||||
Temporary::new(self.console.get().get_ref().clone())
|
Temporary::new(self.console.get().get_ref().clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn Btoa(&self, btoa: DOMString) -> Fallible<DOMString> {
|
||||||
|
base64_btoa(btoa)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn Atob(&self, atob: DOMString) -> Fallible<DOMString> {
|
||||||
|
base64_atob(atob)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Reflectable for WorkerGlobalScope {
|
impl Reflectable for WorkerGlobalScope {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue