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 APPLE INC. ``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 APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, 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 THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "DOMCSSRegisterCustomProperty.h"
28
29#include "CSSCustomPropertyValue.h"
30#include "CSSPropertyNames.h"
31#include "CSSPropertyParser.h"
32#include "CSSRegisteredCustomProperty.h"
33#include "CSSTokenizer.h"
34#include "DOMCSSNamespace.h"
35#include "Document.h"
36#include "StyleBuilderConverter.h"
37#include <wtf/text/WTFString.h>
38
39namespace WebCore {
40
41ExceptionOr<void> DOMCSSRegisterCustomProperty::registerProperty(Document& document, const DOMCSSCustomPropertyDescriptor& descriptor)
42{
43 if (!isCustomPropertyName(descriptor.name))
44 return Exception { SyntaxError, "The name of this property is not a custom property name." };
45
46 RefPtr<CSSCustomPropertyValue> initialValue;
47 if (!descriptor.initialValue.isEmpty()) {
48 CSSTokenizer tokenizer(descriptor.initialValue);
49 StyleResolver styleResolver(document);
50
51 // We need to initialize this so that we can successfully parse computationally dependent values (like em units).
52 // We don't actually need the values to be accurate, since they will be rejected later anyway
53 styleResolver.applyPropertyToStyle(CSSPropertyInvalid, nullptr, styleResolver.defaultStyleForElement());
54 styleResolver.updateFont();
55
56 HashSet<CSSPropertyID> dependencies;
57 CSSPropertyParser::collectParsedCustomPropertyValueDependencies(descriptor.syntax, false, dependencies, tokenizer.tokenRange(), strictCSSParserContext());
58
59 if (!dependencies.isEmpty())
60 return Exception { SyntaxError, "The given initial value must be computationally independent." };
61
62 initialValue = CSSPropertyParser::parseTypedCustomPropertyValue(descriptor.name, descriptor.syntax, tokenizer.tokenRange(), styleResolver, strictCSSParserContext());
63
64 if (!initialValue || !initialValue->isResolved())
65 return Exception { SyntaxError, "The given initial value does not parse for the given syntax." };
66
67 initialValue->collectDirectComputationalDependencies(dependencies);
68 initialValue->collectDirectRootComputationalDependencies(dependencies);
69
70 if (!dependencies.isEmpty())
71 return Exception { SyntaxError, "The given initial value must be computationally independent." };
72 }
73
74 CSSRegisteredCustomProperty property { descriptor.name, descriptor.syntax, descriptor.inherits, WTFMove(initialValue) };
75 if (!document.registerCSSProperty(WTFMove(property)))
76 return Exception { InvalidModificationError, "This property has already been registered." };
77
78 document.styleScope().didChangeStyleSheetEnvironment();
79
80 return { };
81}
82
83DOMCSSRegisterCustomProperty* DOMCSSRegisterCustomProperty::from(DOMCSSNamespace& css)
84{
85 auto* supplement = static_cast<DOMCSSRegisterCustomProperty*>(Supplement<DOMCSSNamespace>::from(&css, supplementName()));
86 if (!supplement) {
87 auto newSupplement = std::make_unique<DOMCSSRegisterCustomProperty>(css);
88 supplement = newSupplement.get();
89 provideTo(&css, supplementName(), WTFMove(newSupplement));
90 }
91 return supplement;
92}
93
94const char* DOMCSSRegisterCustomProperty::supplementName()
95{
96 return "DOMCSSRegisterCustomProperty";
97}
98
99}
100