1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 19:05:08 +00:00

LibWeb: Implement CanvasGradient.addColorStop() according to spec

The object is still not usable for anything, but at least now it behaves
correctly with regards to throwing exceptions.
This commit is contained in:
Andreas Kling 2022-02-05 15:16:35 +01:00
parent 7e1bf4d300
commit e6f279dada
2 changed files with 33 additions and 3 deletions

View file

@ -4,6 +4,8 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/QuickSort.h>
#include <LibWeb/DOM/ExceptionOr.h>
#include <LibWeb/HTML/CanvasGradient.h>
namespace Web::HTML {
@ -45,9 +47,29 @@ CanvasGradient::~CanvasGradient()
{
}
void CanvasGradient::add_color_stop(double offset, String const& color)
// https://html.spec.whatwg.org/multipage/canvas.html#dom-canvasgradient-addcolorstop
DOM::ExceptionOr<void> CanvasGradient::add_color_stop(double offset, String const& color)
{
dbgln("CanvasGradient#addColorStop({}, '{}')", offset, color);
// 1. If the offset is less than 0 or greater than 1, then throw an "IndexSizeError" DOMException.
if (offset < 0 || offset > 1)
return DOM::IndexSizeError::create("CanvasGradient color stop offset out of bounds");
// 2. Let parsed color be the result of parsing color.
auto parsed_color = Color::from_string(color);
// 3. If parsed color is failure, throw a "SyntaxError" DOMException.
if (!parsed_color.has_value())
return DOM::SyntaxError::create("Could not parse color for CanvasGradient");
// 4. Place a new stop on the gradient, at offset offset relative to the whole gradient, and with the color parsed color.
m_color_stops.append(ColorStop { offset, parsed_color.value() });
// FIXME: If multiple stops are added at the same offset on a gradient, then they must be placed in the order added,
// with the first one closest to the start of the gradient, and each subsequent one infinitesimally further along
// towards the end point (in effect causing all but the first and last stop added at each point to be ignored).
quick_sort(m_color_stops, [](auto& a, auto& b) { return a.offset < b.offset; });
return {};
}
}