Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

<!doctype html>
<title>Calc rounds to integer</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel=author title="Tab Atkins-Bittner" href="https://www.xanthir.com/contact/">
<!--
Verifying that, per V&U, a calc() that results in a non-integer value
gets rounded to the nearest integer
when used in a place that requires <integer> specifically.
This tests both straight-up decimal-point values,
and scinot, which is defined to parse as <number-token>.
-->
<body>
<script>
runTests("animation-timing-function", "steps(xxx)");
runTests("column-span");
runTests("counter-increment", "foo xxx");
runTests("counter-reset", "foo xxx");
runTests("counter-set", "foo xxx");
runTests("font-feature-settings", '"fooo" xxx');
runTests("grid-row");
runTests("grid-template-rows", "repeat(xxx, 10px)");
runTests("hyphenate-limit-chars");
runTests("hyphenate-limit-lines");
runTests("initial-letter", "1.1 xxx");
runTests("max-lines");
runTests("order");
runTests("orphans");
runTests("text-combine-upright", "digits xxx");
runTests("transition-timing-function", "steps(xxx)");
runTests("widows");
runTests("z-index");
function runTests(prop, valPattern="xxx") {
const el = document.body;
// Don't spuriously fail bc the prop or val isn't supported.
if(!verifySupport(el, prop, valPattern)) return;
const validValues = [
"10",
"calc(10)",
"calc(10.1)",
"calc(1e1)",
"calc(1.1e1)",
];
const invalidValues = [
"1e1",
"1.1e1",
"10.1",
];
for(let testVal of validValues) {
testInt(el, prop, testVal, valPattern);
}
for(let testVal of invalidValues) {
testIntInvalid(el, prop, testVal, valPattern);
}
}
function verifySupport(el, prop, valPattern) {
let testVal = "10";
if(valPattern !== undefined) {
testVal = valPattern.replace("xxx", testVal);
}
el.removeAttribute("style");
const nullVal = getComputedStyle(el)[prop];
el.style.setProperty(prop, testVal);
return getComputedStyle(el)[prop] != nullVal;
}
function testInt(el, prop, testVal, valPattern) {
// to avoid needing to specify serialization,
// just test whether it parses at all
if(valPattern !== undefined) {
testVal = valPattern.replace("xxx", testVal);
}
test(()=>{
el.removeAttribute("style");
const nullVal = getComputedStyle(el)[prop];
el.style.setProperty(prop, testVal);
assert_not_equals(getComputedStyle(el)[prop], nullVal);
}, `${prop} should accept "${testVal}"`)
}
function testIntInvalid(el, prop, testVal, valPattern) {
// similarly, just verify it doesn't parse at all
if(valPattern !== undefined) {
testVal = valPattern.replace("xxx", testVal);
}
test(()=>{
el.removeAttribute("style");
const nullVal = getComputedStyle(el)[prop];
el.style.setProperty(prop, testVal);
assert_equals(getComputedStyle(el)[prop], nullVal);
}, `${prop} should not accept "${testVal}"`)
}
</script>