haikuwebkit/LayoutTests/css3/color/color-function-parsing.html

85 lines
3.8 KiB
HTML
Raw Permalink Normal View History

Parse color() function https://bugs.webkit.org/show_bug.cgi?id=164146 <rdar://problems/29007218> Reviewed by Darin Adler. Source/WebCore: Support the new CSS color() function: https://drafts.csswg.org/css-color/#color-function There are separate code paths for the old and new CSS parser. Tests: css3/color/color-function-computed-style.html css3/color/color-function-parsing.html * css/CSSComputedStyleDeclaration.cpp: Use Color directly, not via rgb(). (WebCore::ComputedStyleExtractor::currentColorOrValidColor): (WebCore::ComputedStyleExtractor::valueForShadow): (WebCore::ComputedStyleExtractor::propertyValue): * css/CSSValueKeywords.in: Note that there is a color function, but the keyword is already defined. Also add keywords for the color spaces. * css/SVGCSSValueKeywords.in: sRGB is used outside of SVG now. * css/parser/CSSParser.cpp: Old CSS parser code to handle color(). (WebCore::isPercent): Helper to tell if a ValueWithCalculation is a percentage or not. (WebCore::CSSParser::parseColorInt): Renamed. (WebCore::CSSParser::parseColorDouble): Helper to get a Number/Percentage into a double (WebCore::CSSParser::parseRGBParameters): (WebCore::CSSParser::parseColorFunctionParameters): (WebCore::CSSParser::parseHSLParameters): (WebCore::CSSParser::parseColorFromValue): (WebCore::CSSParser::colorIntFromValue): Deleted. * css/parser/CSSParser.h: * css/parser/CSSPropertyParserHelpers.cpp: New CSS parser code to handle color(). (WebCore::CSSPropertyParserHelpers::parseColorFunctionParameters): (WebCore::CSSPropertyParserHelpers::parseColorFunction): * platform/graphics/Color.h: (WebCore::Color::isValid): An extended color is valid. (WebCore::Color::rgb): Move the code to a standalone inline so that I could add a longer comment. * platform/graphics/ExtendedColor.cpp: (WebCore::ExtendedColor::cssText): Alpha output is only needed if != 1. * platform/graphics/cg/ColorCG.cpp: Handle ExtendedColor -> CGColor. (WebCore::leakCGColor): (WebCore::cachedCGColor): LayoutTests: Test that exercises the new color() function in CSS. It checks all valid and invalid input, with the exception of fallback content. * css3/color/color-function-computed-style-expected.txt: Added. * css3/color/color-function-computed-style.html: Added. * css3/color/color-function-parsing-expected.txt: Added. * css3/color/color-function-parsing.html: Added. Canonical link: https://commits.webkit.org/181898@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@208116 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2016-10-29 22:34:43 +00:00
<style></style>
<script src="../../resources/js-test-pre.js"></script>
<p id="description"></p>
<div id="console"></div>
<script>
description("Test the parsing of the color() function.");
let styleElement = document.querySelector("style");
let stylesheet = styleElement.sheet;
let cssRule, declaration;
function testColorFunction(description, rule, expectedValue)
{
debug("");
debug(`TEST: ${description} --> ${rule}`);
stylesheet.insertRule(`#target { color: ${rule}; }`, 0);
cssRule = stylesheet.cssRules.item(0);
declaration = cssRule.style;
shouldBe("declaration.getPropertyValue('color')", `'${expectedValue}'`);
stylesheet.deleteRule(0);
}
testColorFunction("Basic sRGB white", "color(srgb 1 1 1)", "color(srgb 1 1 1)");
testColorFunction("White with lots of space", "color( srgb 1 1 1 )", "color(srgb 1 1 1)");
testColorFunction("sRGB color", "color(srgb 0.25 0.5 0.75)", "color(srgb 0.25 0.5 0.75)");
testColorFunction("Different case for sRGB", "color(SrGb 0.25 0.5 0.75)", "color(srgb 0.25 0.5 0.75)");
testColorFunction("sRGB color with unnecessary decimals", "color(srgb 1.00000 0.500000 0.20)", "color(srgb 1 0.5 0.2)");
testColorFunction("sRGB white with 0.5 alpha", "color(srgb 1 1 1 / 0.5)", "color(srgb 1 1 1 / 0.5)");
testColorFunction("sRGB white with 0 alpha", "color(srgb 1 1 1 / 0)", "color(srgb 1 1 1 / 0)");
testColorFunction("sRGB white with 50% alpha", "color(srgb 1 1 1 / 50%)", "color(srgb 1 1 1 / 0.5)");
testColorFunction("sRGB white with 0% alpha", "color(srgb 1 1 1 / 0%)", "color(srgb 1 1 1 / 0)");
testColorFunction("One missing component is 0", "color(srgb 1 1)", "color(srgb 1 1 0)");
testColorFunction("Two missing components are 0", "color(srgb 1)", "color(srgb 1 0 0)");
testColorFunction("All components missing", "color(srgb)", "color(srgb 0 0 0)");
testColorFunction("Display P3 color", "color(display-p3 0.6 0.7 0.8)", "color(display-p3 0.6 0.7 0.8)");
testColorFunction("Different case for Display P3", "color(dIspLaY-P3 0.6 0.7 0.8)", "color(display-p3 0.6 0.7 0.8)");
debug("");
debug("");
debug("Fallback tests.")
debug("");
testColorFunction("Unknown color space should fallback", "color(unknown 1 2 3, red)", "color(unknown 1 2 3, red)");
debug("");
debug("");
debug("Clamping tests.")
debug("");
// NOTE: If we start supporting extended-range sRGB without changing the keyword, these
// tests may fail.
testColorFunction("sRGB color with negative component should clamp to 0", "color(srgb -0.25 0.5 0.75)", "color(srgb 0 0.5 0.75)");
testColorFunction("sRGB color with component > 1 should clamp", "color(srgb 0.25 1.5 0.75)", "color(srgb 0.25 1 0.75)");
testColorFunction("Display P3 color with negative component should clamp to 0", "color(display-p3 0.5 -199 0.75)", "color(display-p3 0.5 0 0.75)");
testColorFunction("Display P3 color with component > 1 should clamp", "color(display-p3 184 1.00001 2347329746587)", "color(display-p3 1 1 1)");
testColorFunction("Alpha > 1 should clamp", "color(srgb 0.1 0.2 0.3 / 1.9)", "color(srgb 0.1 0.2 0.3)");
testColorFunction("Negative alpha should clamp", "color(srgb 1 1 1 / -0.2)", "color(srgb 1 1 1 / 0)");
debug("");
debug("");
debug("Invalid property value tests.")
debug("");
testColorFunction("Empty", "color()", "");
testColorFunction("Bad color space", "color(banana 1 1 1)", "");
testColorFunction("Bad Display P3 color space", "color(displayp3 1 1 1)", "");
testColorFunction("No color space", "color(1 1 1)", "");
testColorFunction("Too many parameters", "color(srgb 1 1 1 1)", "");
testColorFunction("Way too many parameters", "color(srgb 1 1 1 1 1)", "");
testColorFunction("Bad parameters", "color(srgb 1 eggs 1)", "");
testColorFunction("Bad alpha", "color(srgb 1 1 1 / bacon)", "");
testColorFunction("Junk after alpha", "color(srgb 1 1 1 / 1 cucumber)", "");
debug("");
</script>
<script src="../../resources/js-test-post.js"></script>