1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/*
* Copyright (C) 2016-2023 Apple Inc. All rights reserved.
* Copyright (C) 2024 Samuel Weinig <sam@webkit.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "CSSPropertyParserConsumer+Motion.h"
#include "CSSOffsetRotateValue.h"
#include "CSSParserContext.h"
#include "CSSParserTokenRange.h"
#include "CSSPrimitiveValue.h"
#include "CSSPropertyParserConsumer+Angle.h"
#include "CSSPropertyParserConsumer+AngleDefinitions.h"
#include "CSSPropertyParserConsumer+Ident.h"
#include "CSSPropertyParserConsumer+MetaConsumer.h"
#include "CSSPropertyParserConsumer+Position.h"
#include "CSSPropertyParserConsumer+Primitives.h"
#include "CSSPropertyParserConsumer+Shapes.h"
#include "CSSPropertyParserConsumer+URL.h"
#include "CSSPropertyParsing.h"
#include "CSSRayValue.h"
#include "CSSValueKeywords.h"
#include "CSSValueList.h"
#include <wtf/SortedArrayMap.h>
namespace WebCore {
namespace CSSPropertyParserHelpers {
static RefPtr<CSSValue> consumeRayFunction(CSSParserTokenRange& range, const CSSParserContext& context)
{
// ray( <angle> && <ray-size>? && contain? && [at <position>]? )
// <ray-size> = closest-side | closest-corner | farthest-side | farthest-corner | sides
// https://drafts.fxtf.org/motion-1/#ray-function
static constexpr std::pair<CSSValueID, CSS::RaySize> sizeMappings[] {
{ CSSValueClosestSide, CSS::RaySize { CSS::Keyword::ClosestSide { } } },
{ CSSValueClosestCorner, CSS::RaySize { CSS::Keyword::ClosestCorner { } } },
{ CSSValueFarthestSide, CSS::RaySize { CSS::Keyword::FarthestSide { } } },
{ CSSValueFarthestCorner, CSS::RaySize { CSS::Keyword::FarthestCorner { } } },
{ CSSValueSides, CSS::RaySize { CSS::Keyword::Sides { } } },
};
static constexpr SortedArrayMap sizeMap { sizeMappings };
if (range.peek().type() != FunctionToken || range.peek().functionId() != CSSValueRay)
return { };
auto args = consumeFunction(range);
std::optional<CSS::Angle<>> angle;
std::optional<CSS::RaySize> size;
std::optional<CSS::Keyword::Contain> contain;
std::optional<CSS::Position> position;
auto consumeAngle = [&] -> bool {
if (angle)
return false;
angle = MetaConsumer<CSS::Angle<>>::consume(args, context, { }, { .parserMode = context.mode });
return angle.has_value();
};
auto consumeSize = [&] -> bool {
if (size)
return false;
size = consumeIdentUsingMapping(args, sizeMap);
return size.has_value();
};
auto consumeContain = [&] -> bool {
if (contain || !consumeIdentRaw<CSSValueContain>(args).has_value())
return false;
contain = CSS::Keyword::Contain { };
return contain.has_value();
};
auto consumeAtPosition = [&] -> bool {
if (position || !consumeIdentRaw<CSSValueAt>(args).has_value())
return false;
position = consumePositionUnresolved(args, context);
return position.has_value();
};
while (!args.atEnd()) {
if (consumeAngle() || consumeSize() || consumeContain() || consumeAtPosition())
continue;
return { };
}
// The <angle> argument is the only one that is required.
if (!angle)
return { };
return CSSRayValue::create(
CSS::RayFunction {
.parameters = CSS::Ray {
WTFMove(*angle),
size.value_or(CSS::RaySize { CSS::Keyword::ClosestSide { } }),
WTFMove(contain),
WTFMove(position)
}
}
);
}
RefPtr<CSSValue> consumeOffsetPath(CSSParserTokenRange& range, const CSSParserContext& context)
{
// <'offset-path'> = none | <offset-path> || <coord-box>
//
// NOTE: The sub-production, <offset-path> (without the quotation marks) is distinct and defined as:
// <offset-path> = <ray()> | <url> | <basic-shape>
//
// So, this expands out to a grammar of:
//
// <'offset-path'> = none | [ <ray()> | <url> | <basic-shape> || <coord-box> ]
//
// which is almost the same as <'clip-path'> above, with the following differences:
//
// 1. <'clip-path'> does not support `ray()`.
// 2. <'clip-path'> does not allow a `box` to be provided with `<url>`.
// 3. <'clip-path'> specifies `<geometry-box>` rather than `<coord-box>`.
//
// https://drafts.fxtf.org/motion-1/#propdef-offset-path
if (range.peek().id() == CSSValueNone)
return consumeIdent(range);
// FIXME: It should be possible to consume both a <url> and <coord-box>.
if (auto url = consumeURL(range))
return url;
RefPtr<CSSValue> shapeOrRay;
RefPtr<CSSValue> box;
auto consumeRay = [&]() -> bool {
if (shapeOrRay)
return false;
shapeOrRay = consumeRayFunction(range, context);
return !!shapeOrRay;
};
auto consumeShape = [&]() -> bool {
if (shapeOrRay)
return false;
shapeOrRay = consumeBasicShape(range, context, PathParsingOption::RejectPathFillRule);
return !!shapeOrRay;
};
auto consumeBox = [&]() -> bool {
if (box)
return false;
// FIXME: The Motion Path spec calls for this to be a <coord-box>, not a <geometry-box>, the difference being that the former does not contain "margin-box" as a valid term.
// However, the spec also has a few examples using "margin-box", so there seems to be some abiguity to be resolved. See: https://github.com/w3c/fxtf-drafts/issues/481.
box = CSSPropertyParsing::consumeGeometryBox(range);
return !!box;
};
while (!range.atEnd()) {
if (consumeRay() || consumeShape() || consumeBox())
continue;
break;
}
bool hasShapeOrRay = !!shapeOrRay;
CSSValueListBuilder list;
if (shapeOrRay)
list.append(shapeOrRay.releaseNonNull());
// Default value is border-box.
if (box && (box->valueID() != CSSValueBorderBox || !hasShapeOrRay))
list.append(box.releaseNonNull());
if (list.isEmpty())
return nullptr;
return CSSValueList::createSpaceSeparated(WTFMove(list));
}
RefPtr<CSSValue> consumeOffsetRotate(CSSParserTokenRange& range, const CSSParserContext& context)
{
auto rangeCopy = range;
// Attempt to parse the first token as the modifier (auto / reverse keyword). If
// successful, parse the second token as the angle. If not, try to parse the other
// way around.
auto modifier = consumeIdent<CSSValueAuto, CSSValueReverse>(rangeCopy);
auto angle = consumeAngle(rangeCopy, context);
if (!modifier)
modifier = consumeIdent<CSSValueAuto, CSSValueReverse>(rangeCopy);
if (!angle && !modifier)
return nullptr;
range = rangeCopy;
return CSSOffsetRotateValue::create(WTFMove(modifier), WTFMove(angle));
}
} // namespace CSSPropertyParserHelpers
} // namespace WebCore
|