Implement <option> 'defaultSelected' and 'selected' attributes

Continued from #7743
This commit is contained in:
Corey Farwell 2015-09-25 09:39:35 -04:00
parent 37ce248f31
commit dec0031112
6 changed files with 111 additions and 124 deletions

View file

@ -17,11 +17,18 @@ use dom::eventtarget::{EventTarget, EventTargetTypeId};
use dom::htmlelement::{HTMLElement, HTMLElementTypeId};
use dom::node::{Node, NodeTypeId};
use dom::virtualmethods::VirtualMethods;
use std::cell::Cell;
use util::str::{DOMString, split_html_space_chars};
#[dom_struct]
pub struct HTMLOptionElement {
htmlelement: HTMLElement
htmlelement: HTMLElement,
/// https://html.spec.whatwg.org/multipage/#attr-option-selected
selectedness: Cell<bool>,
/// https://html.spec.whatwg.org/multipage/#concept-option-dirtiness
dirtiness: Cell<bool>,
}
impl HTMLOptionElementDerived for EventTarget {
@ -38,7 +45,9 @@ impl HTMLOptionElement {
document: &Document) -> HTMLOptionElement {
HTMLOptionElement {
htmlelement:
HTMLElement::new_inherited(HTMLElementTypeId::HTMLOptionElement, localName, prefix, document)
HTMLElement::new_inherited(HTMLElementTypeId::HTMLOptionElement, localName, prefix, document),
selectedness: Cell::new(false),
dirtiness: Cell::new(false),
}
}
@ -122,6 +131,23 @@ impl HTMLOptionElementMethods for HTMLOptionElement {
// https://html.spec.whatwg.org/multipage/#attr-option-label
make_setter!(SetLabel, "label");
// https://html.spec.whatwg.org/multipage/#dom-option-defaultselected
make_bool_getter!(DefaultSelected, "selected");
// https://html.spec.whatwg.org/multipage/#dom-option-defaultselected
make_bool_setter!(SetDefaultSelected, "selected");
// https://html.spec.whatwg.org/multipage/#dom-option-selected
fn Selected(&self) -> bool {
self.selectedness.get()
}
// https://html.spec.whatwg.org/multipage/#dom-option-selected
fn SetSelected(&self, selected: bool) {
self.dirtiness.set(true);
self.selectedness.set(selected);
// FIXME: as per the spec, implement 'ask for a reset'
}
}
impl VirtualMethods for HTMLOptionElement {
@ -147,6 +173,22 @@ impl VirtualMethods for HTMLOptionElement {
}
}
},
&atom!(selected) => {
match mutation {
AttributeMutation::Set(_) => {
// https://html.spec.whatwg.org/multipage/#concept-option-selectedness
if !self.dirtiness.get() {
self.selectedness.set(true);
}
},
AttributeMutation::Removed => {
// https://html.spec.whatwg.org/multipage/#concept-option-selectedness
if !self.dirtiness.get() {
self.selectedness.set(false);
}
},
}
},
_ => {},
}
}

View file

@ -11,8 +11,8 @@ interface HTMLOptionElement : HTMLElement {
attribute boolean disabled;
//readonly attribute HTMLFormElement? form;
attribute DOMString label;
// attribute boolean defaultSelected;
// attribute boolean selected;
attribute boolean defaultSelected;
attribute boolean selected;
attribute DOMString value;
attribute DOMString text;

View file

@ -17351,6 +17351,10 @@
"path": "html/semantics/forms/the-option-element/option-label.html",
"url": "/html/semantics/forms/the-option-element/option-label.html"
},
{
"path": "html/semantics/forms/the-option-element/option-selected.html",
"url": "/html/semantics/forms/the-option-element/option-selected.html"
},
{
"path": "html/semantics/forms/the-option-element/option-text-backslash.html",
"url": "/html/semantics/forms/the-option-element/option-text-backslash.html"

View file

@ -5520,24 +5520,12 @@
[HTMLOptionElement interface: attribute form]
expected: FAIL
[HTMLOptionElement interface: attribute defaultSelected]
expected: FAIL
[HTMLOptionElement interface: attribute selected]
expected: FAIL
[HTMLOptionElement interface: attribute index]
expected: FAIL
[HTMLOptionElement interface: document.createElement("option") must inherit property "form" with the proper type (1)]
expected: FAIL
[HTMLOptionElement interface: document.createElement("option") must inherit property "defaultSelected" with the proper type (3)]
expected: FAIL
[HTMLOptionElement interface: document.createElement("option") must inherit property "selected" with the proper type (4)]
expected: FAIL
[HTMLOptionElement interface: document.createElement("option") must inherit property "index" with the proper type (7)]
expected: FAIL

View file

@ -10542,114 +10542,6 @@
[option.tabIndex: IDL set to -2147483648 followed by getAttribute()]
expected: FAIL
[option.defaultSelected (<option selected>): typeof IDL attribute]
expected: FAIL
[option.defaultSelected (<option selected>): IDL get with DOM attribute unset]
expected: FAIL
[option.defaultSelected (<option selected>): setAttribute() to "" followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): setAttribute() to " foo " followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): setAttribute() to undefined followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): setAttribute() to null followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): setAttribute() to 7 followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): setAttribute() to 1.5 followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): setAttribute() to true followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): setAttribute() to false followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): setAttribute() to object "[object Object\]" followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): setAttribute() to NaN followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): setAttribute() to Infinity followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): setAttribute() to -Infinity followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): setAttribute() to "\\0" followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): setAttribute() to object "test-toString" followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): setAttribute() to object "test-valueOf" followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): setAttribute() to "selected" followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): IDL set to "" followed by hasAttribute()]
expected: FAIL
[option.defaultSelected (<option selected>): IDL set to "" followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): IDL set to " foo " followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): IDL set to undefined followed by hasAttribute()]
expected: FAIL
[option.defaultSelected (<option selected>): IDL set to undefined followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): IDL set to null followed by hasAttribute()]
expected: FAIL
[option.defaultSelected (<option selected>): IDL set to null followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): IDL set to 7 followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): IDL set to 1.5 followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): IDL set to false followed by hasAttribute()]
expected: FAIL
[option.defaultSelected (<option selected>): IDL set to object "[object Object\]" followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): IDL set to NaN followed by hasAttribute()]
expected: FAIL
[option.defaultSelected (<option selected>): IDL set to NaN followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): IDL set to Infinity followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): IDL set to -Infinity followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): IDL set to "\\0" followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): IDL set to object "test-toString" followed by IDL get]
expected: FAIL
[option.defaultSelected (<option selected>): IDL set to object "test-valueOf" followed by IDL get]
expected: FAIL
[option.itemScope: typeof IDL attribute]
expected: FAIL

View file

@ -0,0 +1,61 @@
<!doctype html>
<meta charset=utf-8>
<title>HTMLOptionElement.selected</title>
<link rel=author title="Corey Farwell" href="mailto:coreyf@rwell.org">
<link rel=help href="https://html.spec.whatwg.org/multipage/forms.html#dom-option-selected">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<script>
test(function () {
var elem = document.createElement("option");
assert_equals(elem.selected, false);
elem.setAttribute("selected", "");
assert_equals(elem.selected, true);
elem.removeAttribute("selected");
assert_equals(elem.selected, false);
elem.defaultSelected = true
assert_equals(elem.selected, true);
elem.defaultSelected = false;
assert_equals(elem.selected, false);
}, "not dirty");
test(function () {
testDirty(true);
}, "dirty, selected");
test(function () {
testDirty(false);
}, "dirty, not selected");
function testDirty(isSelected) {
var elem = document.createElement("option");
elem.selected = isSelected; // After this assignment, dirtiness=true
assertDirty(elem, isSelected);
elem.selected = !isSelected; // Change the value, still dirty
assertDirty(elem, !isSelected);
};
function assertDirty(elem, expect) {
assert_equals(elem.selected, expect);
elem.setAttribute("selected", "");
assert_equals(elem.selected, expect);
elem.removeAttribute("selected");
assert_equals(elem.selected, expect);
elem.defaultSelected = true;
assert_equals(elem.selected, expect);
elem.defaultSelected = false;
assert_equals(elem.selected, expect);
}
</script>