Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

<!DOCTYPE HTML>
<html>
<!--
-->
<head>
<title>Test for Bug 659596</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=659596">Mozilla Bug 659596</a>
<p id="display"></p>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 659596 */
function checkReflection(option, attribute) {
/**
* Getting.
*/
// When attribute isn't present.
var tests = [ "", "foo" ];
for (var test of tests) {
option.removeAttribute(attribute);
option.textContent = test;
is(option.getAttribute(attribute), null,
"option " + attribute + "'s value should be null");
is(option[attribute], option.textContent,
"option." + attribute + " should reflect the text content when the attribute isn't set");
}
// When attribute is present.
tests = [
[ "", "" ],
[ "", "foo" ],
[ "foo", "bar" ],
[ "foo", "" ],
];
for (var test of tests) {
option.setAttribute(attribute, test[0]);
option.textContent = test[1];
is(option[attribute], option.getAttribute(attribute),
"option." + attribute + " should reflect the content attribute when it is set");
}
/**
* Setting.
*/
// When attribute isn't present.
tests = [
[ "", "new" ],
[ "foo", "new" ],
];
for (var test of tests) {
option.removeAttribute(attribute);
option.textContent = test[0];
option[attribute] = test[1]
is(option.getAttribute(attribute), test[1],
"when setting, the content attribute should change");
is(option.textContent, test[0],
"when setting, the text content should not change");
}
// When attribute is present.
tests = [
[ "", "", "new" ],
[ "", "foo", "new" ],
[ "foo", "bar", "new" ],
[ "foo", "", "new" ],
];
for (var test of tests) {
option.setAttribute(attribute, test[0]);
option.textContent = test[1];
option[attribute] = test[2];
is(option.getAttribute(attribute), test[2],
"when setting, the content attribute should change");
is(option.textContent, test[1],
"when setting, the text content should not change");
}
}
var option = document.createElement("option");
checkReflection(option, "value");
checkReflection(option, "label");
{
const richOption = document.createElement("option");
const image = document.createElement("img");
image.alt = "ALT";
image.append("hidden image text");
richOption.append("before", image, "after");
is(SpecialPowers.wrap(richOption).renderedLabel, "beforeALTafter",
"The displayed option label includes alternative text without extra spaces");
const select = document.createElement("select");
select.append(richOption);
document.body.append(select);
is(SpecialPowers.wrap(select).openOrClosedShadowRoot.querySelector("label").textContent,
"beforeALTafter", "The select fallback button includes image alternative text");
image.alt = "NEW";
is(SpecialPowers.wrap(select).openOrClosedShadowRoot.querySelector("label").textContent,
"beforeNEWafter", "Changing image alternative text updates the select button");
image.removeAttribute("alt");
is(SpecialPowers.wrap(select).openOrClosedShadowRoot.querySelector("label").textContent,
"beforeafter", "Removing image alternative text updates the select button");
image.alt = "ALT";
select.remove();
richOption.label = "";
is(richOption.label, "", "An empty option label attribute is reflected");
is(SpecialPowers.wrap(richOption).renderedLabel, "beforeALTafter",
"An empty option label attribute falls back to the displayed text");
richOption.label = "explicit";
is(SpecialPowers.wrap(richOption).renderedLabel, "explicit",
"A nonempty option label takes precedence over its contents");
}
{
const group = document.createElement("optgroup");
const legend = document.createElement("legend");
const image = document.createElement("img");
image.alt = "b";
image.append("hidden image text");
const script = document.createElement("script");
script.textContent = "hidden script text";
const svgScript = document.createElementNS("http://www.w3.org/2000/svg", "script");
svgScript.textContent = "hidden SVG script text";
legend.append(" \ta", image, script, svgScript, "c\n ");
group.append(" \n", document.createComment("before legend"), legend);
group.label = "attribute";
is(group.label, "attribute", "The optgroup label IDL attribute still reflects");
is(SpecialPowers.wrap(group).renderedLabel, "abc",
"A first element child legend supplies the HTML-aware group label");
image.alt = " \tb\n ";
is(SpecialPowers.wrap(group).renderedLabel, "a b c",
"Whitespace in image alternative text is stripped and collapsed");
image.removeAttribute("alt");
is(SpecialPowers.wrap(group).renderedLabel, "ac",
"An image without alternative text still has its descendants skipped");
image.alt = "";
is(SpecialPowers.wrap(group).renderedLabel, "ac",
"An empty alternative text adds no spaces");
group.prepend(document.createElement("span"));
is(SpecialPowers.wrap(group).renderedLabel, "attribute",
"A later legend does not override the label attribute");
group.label = "";
is(SpecialPowers.wrap(group).renderedLabel, "",
"An empty label stays empty when the first element child is not a legend");
group.removeAttribute("label");
is(SpecialPowers.wrap(group).renderedLabel, "",
"A missing label yields an empty label when the first child is not a legend");
}
</script>
</pre>
</body>
</html>