1/*
2 * Copyright (C) 2018 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
17 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
18 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
22 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
23 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "CSSPaintImageValue.h"
29
30#if ENABLE(CSS_PAINTING_API)
31
32#include "CSSVariableData.h"
33#include "CustomPaintImage.h"
34#include "PaintWorkletGlobalScope.h"
35#include "RenderElement.h"
36#include <wtf/text/StringBuilder.h>
37
38namespace WebCore {
39
40String CSSPaintImageValue::customCSSText() const
41{
42 StringBuilder result;
43 result.appendLiteral("paint(");
44 result.append(m_name);
45 // FIXME: print args.
46 result.append(')');
47 return result.toString();
48}
49
50RefPtr<Image> CSSPaintImageValue::image(RenderElement& renderElement, const FloatSize& size)
51{
52 if (size.isEmpty())
53 return nullptr;
54 auto* selectedGlobalScope = renderElement.document().paintWorkletGlobalScopeForName(m_name);
55 if (!selectedGlobalScope)
56 return nullptr;
57 auto locker = holdLock(selectedGlobalScope->paintDefinitionLock());
58 auto* registration = selectedGlobalScope->paintDefinitionMap().get(m_name);
59
60 if (!registration)
61 return nullptr;
62
63 // FIXME: Check if argument list matches syntax.
64 Vector<String> arguments;
65 CSSParserTokenRange localRange(m_arguments->tokenRange());
66
67 while (!localRange.atEnd()) {
68 StringBuilder builder;
69 while (!localRange.atEnd() && localRange.peek() != CommaToken) {
70 if (localRange.peek() == CommentToken)
71 localRange.consume();
72 else if (localRange.peek().getBlockType() == CSSParserToken::BlockStart) {
73 localRange.peek().serialize(builder);
74 builder.append(localRange.consumeBlock().serialize());
75 builder.append(')');
76 } else
77 localRange.consume().serialize(builder);
78 }
79 if (!localRange.atEnd())
80 localRange.consume(); // comma token
81 arguments.append(builder.toString());
82 }
83
84 return CustomPaintImage::create(*registration, size, renderElement, arguments);
85}
86
87} // namespace WebCore
88
89#endif
90