diff --git a/components/script/dom/domstringmap.rs b/components/script/dom/domstringmap.rs new file mode 100644 index 00000000000..ba0031e1098 --- /dev/null +++ b/components/script/dom/domstringmap.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 http://mozilla.org/MPL/2.0/. */ + +use dom::bindings::cell::DOMRefCell; +use dom::bindings::codegen::Bindings::DOMStringMapBinding; +use dom::bindings::codegen::Bindings::DOMStringMapBinding::DOMStringMapMethods; +use dom::bindings::global::GlobalRef; +use dom::bindings::js::{JSRef, Temporary}; +use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object}; +use servo_util::str::DOMString; + +use std::collections::hashmap::HashMap; + +#[dom_struct] +pub struct DOMStringMap { + map: DOMRefCell>, + reflector_: Reflector, +} + +impl DOMStringMap { + fn new_inherited() -> DOMStringMap { + DOMStringMap { + map: DOMRefCell::new(HashMap::new()), + reflector_: Reflector::new(), + } + } + + pub fn new(global: &GlobalRef) -> Temporary { + reflect_dom_object(box DOMStringMap::new_inherited(), + global, DOMStringMapBinding::Wrap) + } +} + +impl<'a> DOMStringMapMethods for JSRef<'a, DOMStringMap> { + fn NamedCreator(self, name: DOMString, value: DOMString) { + self.map.borrow_mut().insert(name, value); + } + + fn NamedSetter(self, name: DOMString, value: DOMString) { + self.map.borrow_mut().insert(name, value); + } + + fn NamedGetter(self, name: DOMString, found: &mut bool) -> DOMString { + match self.map.borrow().find(&name) { + Some(value) => { + *found = true; + value.clone() + }, + None => { + *found = false; + String::new() + } + } + } +} + +impl Reflectable for DOMStringMap { + fn reflector<'a>(&'a self) -> &'a Reflector { + &self.reflector_ + } +} diff --git a/components/script/dom/webidls/DOMStringMap.webidl b/components/script/dom/webidls/DOMStringMap.webidl new file mode 100644 index 00000000000..7ae0a1b9278 --- /dev/null +++ b/components/script/dom/webidls/DOMStringMap.webidl @@ -0,0 +1,12 @@ +/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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 http://mozilla.org/MPL/2.0/. */ + +// https://html.spec.whatwg.org/#the-domstringmap-interface +[OverrideBuiltins] +interface DOMStringMap { + getter DOMString (DOMString name); + setter creator void (DOMString name, DOMString value); + //deleter void (DOMString name); +}; diff --git a/components/script/lib.rs b/components/script/lib.rs index e93ddd9dc29..64fd584c757 100644 --- a/components/script/lib.rs +++ b/components/script/lib.rs @@ -89,6 +89,7 @@ pub mod dom { pub mod characterdata; pub mod domrect; pub mod domrectlist; + pub mod domstringmap; pub mod comment; pub mod console; mod create; diff --git a/tests/content/test_interfaces.html b/tests/content/test_interfaces.html index 825356e7d39..5d544ebeaca 100644 --- a/tests/content/test_interfaces.html +++ b/tests/content/test_interfaces.html @@ -67,6 +67,7 @@ var interfaceNamesInGlobalScope = [ "DOMImplementation", "DOMParser", "DOMTokenList", + "DOMStringMap", "Element", "Event", "EventTarget", diff --git a/tests/wpt/metadata/html/dom/interfaces.html.ini b/tests/wpt/metadata/html/dom/interfaces.html.ini index a2367497351..ab044ab945f 100644 --- a/tests/wpt/metadata/html/dom/interfaces.html.ini +++ b/tests/wpt/metadata/html/dom/interfaces.html.ini @@ -1692,18 +1692,9 @@ [PropertyNodeList interface: operation getValues()] expected: FAIL - [DOMStringMap interface: existence and properties of interface object] - expected: FAIL - [DOMStringMap interface object length] expected: FAIL - [DOMStringMap interface: existence and properties of interface prototype object] - expected: FAIL - - [DOMStringMap interface: existence and properties of interface prototype object\'s "constructor" property] - expected: FAIL - [DOMStringMap must be primary interface of document.head.dataset] expected: FAIL