File: StyleLengthResolution.cpp

package info (click to toggle)
webkit2gtk 2.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 429,764 kB
  • sloc: cpp: 3,697,587; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (426 lines) | stat: -rw-r--r-- 17,781 bytes parent folder | download | duplicates (7)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 * Copyright (C) 2025 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 "StyleLengthResolution.h"

#include "BoxSides.h"
#include "CSSPrimitiveNumericUnits.h"
#include "CSSToLengthConversionData.h"
#include "ContainerQueryEvaluator.h"
#include "Document.h"
#include "Element.h"
#include "FontCascade.h"
#include "FontCascadeDescription.h"
#include "FontMetrics.h"
#include "NodeRenderStyle.h"
#include "RenderBox.h"
#include "RenderBoxInlines.h"
#include "RenderStyle.h"
#include "RenderStyleInlines.h"
#include "RenderView.h"

namespace WebCore {
namespace Style {

static double lengthOfViewportPhysicalAxisForLogicalAxis(LogicalBoxAxis logicalAxis, const FloatSize& size, const RenderStyle* style)
{
    if (!style)
        return 0;

    switch (mapAxisLogicalToPhysical(style->writingMode(), logicalAxis)) {
    case BoxAxis::Horizontal:
        return size.width();
    case BoxAxis::Vertical:
        return size.height();
    }

    RELEASE_ASSERT_NOT_REACHED();
}

static double lengthOfViewportPhysicalAxisForLogicalAxis(LogicalBoxAxis logicalAxis, const FloatSize& size, const RenderView& renderView)
{
    RefPtr rootElement = renderView.document().documentElement();
    if (!rootElement)
        return 0;

    return lengthOfViewportPhysicalAxisForLogicalAxis(logicalAxis, size, rootElement->renderStyle());
}

double computeUnzoomedNonCalcLengthDouble(double value, CSS::LengthUnit lengthUnit, CSSPropertyID propertyToCompute, const FontCascade* fontCascadeForUnit, const RenderView* renderView)
{
    using enum CSS::LengthUnit;

    switch (lengthUnit) {
    case Px:
        return value;
    case Cm:
        return CSS::pixelsPerCm * value;
    case Mm:
        return CSS::pixelsPerMm * value;
    case Q:
        return CSS::pixelsPerQ * value;
    case In:
        return CSS::pixelsPerInch * value;
    case Pt:
        return CSS::pixelsPerPt * value;
    case Pc:
        return CSS::pixelsPerPc * value;

    // MARK: "font dependent" and "root font dependent" resolution

    case Em:
    case QuirkyEm:
    case Rem: {
        ASSERT(fontCascadeForUnit);
        auto& fontDescription = fontCascadeForUnit->fontDescription();
        return ((propertyToCompute == CSSPropertyFontSize) ? fontDescription.specifiedSize() : fontDescription.computedSize()) * value;
    }
    case Ex:
    case Rex: {
        ASSERT(fontCascadeForUnit);
        auto& fontMetrics = fontCascadeForUnit->metricsOfPrimaryFont();
        if (fontMetrics.xHeight())
            return fontMetrics.xHeight().value() * value;
        auto& fontDescription = fontCascadeForUnit->fontDescription();
        return ((propertyToCompute == CSSPropertyFontSize) ? fontDescription.specifiedSize() : fontDescription.computedSize()) / 2.0 * value;
    }
    case Cap:
    case Rcap: {
        ASSERT(fontCascadeForUnit);
        auto& fontMetrics = fontCascadeForUnit->metricsOfPrimaryFont();
        if (fontMetrics.capHeight())
            return fontMetrics.capHeight().value() * value;
        return fontMetrics.intAscent() * value;
    }
    case Ch:
    case Rch:
        ASSERT(fontCascadeForUnit);
        return fontCascadeForUnit->zeroWidth() * value;
    case Ic:
    case Ric:
        ASSERT(fontCascadeForUnit);
        return fontCascadeForUnit->metricsOfPrimaryFont().ideogramWidth().value_or(0) * value;

    // MARK: "viewport percentage" resolution

    case Vh:
        return renderView ? renderView->sizeForCSSDefaultViewportUnits().height() / 100.0 * value : 0;
    case Vw:
        return renderView ? renderView->sizeForCSSDefaultViewportUnits().width() / 100.0 * value : 0;
    case Vmax:
        return renderView ? renderView->sizeForCSSDefaultViewportUnits().maxDimension() / 100.0 * value : value;
    case Vmin:
        return renderView ? renderView->sizeForCSSDefaultViewportUnits().minDimension() / 100.0 * value : value;
    case Vb:
        return renderView ? lengthOfViewportPhysicalAxisForLogicalAxis(LogicalBoxAxis::Block, renderView->sizeForCSSDefaultViewportUnits(), *renderView) / 100.0 * value : 0;
    case Vi:
        return renderView ? lengthOfViewportPhysicalAxisForLogicalAxis(LogicalBoxAxis::Inline, renderView->sizeForCSSDefaultViewportUnits(), *renderView) / 100.0 * value : 0;
    case Svh:
        return renderView ? renderView->sizeForCSSSmallViewportUnits().height() / 100.0 * value : 0;
    case Svw:
        return renderView ? renderView->sizeForCSSSmallViewportUnits().width() / 100.0 * value : 0;
    case Svmax:
        return renderView ? renderView->sizeForCSSSmallViewportUnits().maxDimension() / 100.0 * value : value;
    case Svmin:
        return renderView ? renderView->sizeForCSSSmallViewportUnits().minDimension() / 100.0 * value : value;
    case Svb:
        return renderView ? lengthOfViewportPhysicalAxisForLogicalAxis(LogicalBoxAxis::Block, renderView->sizeForCSSSmallViewportUnits(), *renderView) / 100.0 * value : 0;
    case Svi:
        return renderView ? lengthOfViewportPhysicalAxisForLogicalAxis(LogicalBoxAxis::Inline, renderView->sizeForCSSSmallViewportUnits(), *renderView) / 100.0 * value : 0;
    case Lvh:
        return renderView ? renderView->sizeForCSSLargeViewportUnits().height() / 100.0 * value : 0;
    case Lvw:
        return renderView ? renderView->sizeForCSSLargeViewportUnits().width() / 100.0 * value : 0;
    case Lvmax:
        return renderView ? renderView->sizeForCSSLargeViewportUnits().maxDimension() / 100.0 * value : value;
    case Lvmin:
        return renderView ? renderView->sizeForCSSLargeViewportUnits().minDimension() / 100.0 * value : value;
    case Lvb:
        return renderView ? lengthOfViewportPhysicalAxisForLogicalAxis(LogicalBoxAxis::Block, renderView->sizeForCSSLargeViewportUnits(), *renderView) / 100.0 * value : 0;
    case Lvi:
        return renderView ? lengthOfViewportPhysicalAxisForLogicalAxis(LogicalBoxAxis::Inline, renderView->sizeForCSSLargeViewportUnits(), *renderView) / 100.0 * value : 0;
    case Dvh:
        return renderView ? renderView->sizeForCSSDynamicViewportUnits().height() / 100.0 * value : 0;
    case Dvw:
        return renderView ? renderView->sizeForCSSDynamicViewportUnits().width() / 100.0 * value : 0;
    case Dvmax:
        return renderView ? renderView->sizeForCSSDynamicViewportUnits().maxDimension() / 100.0 * value : value;
    case Dvmin:
        return renderView ? renderView->sizeForCSSDynamicViewportUnits().minDimension() / 100.0 * value : value;
    case Dvb:
        return renderView ? lengthOfViewportPhysicalAxisForLogicalAxis(LogicalBoxAxis::Block, renderView->sizeForCSSDynamicViewportUnits(), *renderView) / 100.0 * value : 0;
    case Dvi:
        return renderView ? lengthOfViewportPhysicalAxisForLogicalAxis(LogicalBoxAxis::Inline, renderView->sizeForCSSDynamicViewportUnits(), *renderView) / 100.0 * value : 0;

    case Lh:
    case Rlh:
    case Cqw:
    case Cqh:
    case Cqi:
    case Cqb:
    case Cqmin:
    case Cqmax:
        ASSERT_NOT_REACHED();
        return -1.0;
    }

    RELEASE_ASSERT_NOT_REACHED();
}

double computeNonCalcLengthDouble(double value, CSS::LengthUnit lengthUnit, const CSSToLengthConversionData& conversionData)
{
    using enum CSS::LengthUnit;

    auto resolveContainerUnit = [&](CQ::Axis physicalAxis) -> std::optional<double> {
        ASSERT(physicalAxis == CQ::Axis::Width || physicalAxis == CQ::Axis::Height);

        conversionData.setUsesContainerUnits();

        RefPtr element = conversionData.elementForContainerUnitResolution();
        if (!element)
            return { };

        auto mode = conversionData.style()->pseudoElementType() == PseudoId::None
            ? Style::ContainerQueryEvaluator::SelectionMode::Element
            : Style::ContainerQueryEvaluator::SelectionMode::PseudoElement;

        // "The query container for each axis is the nearest ancestor container that accepts container size queries on that axis."
        while ((element = Style::ContainerQueryEvaluator::selectContainer(physicalAxis, nullString(), *element, mode))) {
            auto* containerRenderer = dynamicDowncast<RenderBox>(element->renderer());
            if (containerRenderer && containerRenderer->hasEligibleContainmentForSizeQuery()) {
                auto widthOrHeight = physicalAxis == CQ::Axis::Width ? containerRenderer->contentBoxWidth() : containerRenderer->contentBoxHeight();
                return widthOrHeight * value / 100;
            }
            // For pseudo-elements the element itself can be the container. Avoid looping forever.
            mode = Style::ContainerQueryEvaluator::SelectionMode::Element;
        }
        return { };
    };

    switch (lengthUnit) {
    case Px:
        break;
    case Cm:
        value = CSS::pixelsPerCm * value;
        break;
    case Mm:
        value = CSS::pixelsPerMm * value;
        break;
    case Q:
        value = CSS::pixelsPerQ * value;
        break;
    case In:
        value = CSS::pixelsPerInch * value;
        break;
    case Pt:
        value = CSS::pixelsPerPt * value;
        break;
    case Pc:
        value = CSS::pixelsPerPc * value;
        break;

    // MARK: "font dependent" resolution

    case Em:
    case QuirkyEm:
    case Ex:
    case Cap:
    case Ch:
    case Ic:
        // FIXME: We have a bug right now where the zoom will be applied twice to EX units.
        // We really need to compute EX using fontMetrics for the original specifiedSize and not use
        // our actual constructed rendering font.
        value = computeUnzoomedNonCalcLengthDouble(value, lengthUnit, conversionData.propertyToCompute(), &conversionData.fontCascadeForFontUnits());
        break;

    case Lh:
        if (conversionData.computingLineHeight() || conversionData.computingFontSize()) {
            // Try to get the parent's computed line-height, or fall back to the initial line-height of this element's font spacing.
            value *= conversionData.parentStyle() ? conversionData.parentStyle()->computedLineHeight() : conversionData.fontCascadeForFontUnits().metricsOfPrimaryFont().intLineSpacing();
        } else
            value *= conversionData.computedLineHeightForFontUnits();
        break;

    // MARK: "root font dependent" resolution

    case Rcap:
    case Rch:
    case Rem:
    case Rex:
    case Ric:
        value = computeUnzoomedNonCalcLengthDouble(value, lengthUnit, conversionData.propertyToCompute(), conversionData.rootStyle() ? &conversionData.rootStyle()->fontCascade() : &conversionData.fontCascadeForFontUnits());
        break;

    case Rlh:
        if (conversionData.rootStyle()) {
            if (conversionData.computingLineHeight() || conversionData.computingFontSize())
                value *= conversionData.rootStyle()->computeLineHeight(conversionData.rootStyle()->specifiedLineHeight());
            else
                value *= conversionData.rootStyle()->computedLineHeight();
        }
        break;

    // MARK: "viewport-percentage" resolution

    case Vh:
        return value * conversionData.defaultViewportFactor().height();

    case Vw:
        return value * conversionData.defaultViewportFactor().width();

    case Vmax:
        return value * conversionData.defaultViewportFactor().maxDimension();

    case Vmin:
        return value * conversionData.defaultViewportFactor().minDimension();

    case Vb:
        return value * lengthOfViewportPhysicalAxisForLogicalAxis(LogicalBoxAxis::Block, conversionData.defaultViewportFactor(), conversionData.style());

    case Vi:
        return value * lengthOfViewportPhysicalAxisForLogicalAxis(LogicalBoxAxis::Inline, conversionData.defaultViewportFactor(), conversionData.style());

    case Svh:
        return value * conversionData.smallViewportFactor().height();

    case Svw:
        return value * conversionData.smallViewportFactor().width();

    case Svmax:
        return value * conversionData.smallViewportFactor().maxDimension();

    case Svmin:
        return value * conversionData.smallViewportFactor().minDimension();

    case Svb:
        return value * lengthOfViewportPhysicalAxisForLogicalAxis(LogicalBoxAxis::Block, conversionData.smallViewportFactor(), conversionData.style());

    case Svi:
        return value * lengthOfViewportPhysicalAxisForLogicalAxis(LogicalBoxAxis::Inline, conversionData.smallViewportFactor(), conversionData.style());

    case Lvh:
        return value * conversionData.largeViewportFactor().height();

    case Lvw:
        return value * conversionData.largeViewportFactor().width();

    case Lvmax:
        return value * conversionData.largeViewportFactor().maxDimension();

    case Lvmin:
        return value * conversionData.largeViewportFactor().minDimension();

    case Lvb:
        return value * lengthOfViewportPhysicalAxisForLogicalAxis(LogicalBoxAxis::Block, conversionData.largeViewportFactor(), conversionData.style());

    case Lvi:
        return value * lengthOfViewportPhysicalAxisForLogicalAxis(LogicalBoxAxis::Inline, conversionData.largeViewportFactor(), conversionData.style());

    case Dvh:
        return value * conversionData.dynamicViewportFactor().height();

    case Dvw:
        return value * conversionData.dynamicViewportFactor().width();

    case Dvmax:
        return value * conversionData.dynamicViewportFactor().maxDimension();

    case Dvmin:
        return value * conversionData.dynamicViewportFactor().minDimension();

    case Dvb:
        return value * lengthOfViewportPhysicalAxisForLogicalAxis(LogicalBoxAxis::Block, conversionData.dynamicViewportFactor(), conversionData.style());

    case Dvi:
        return value * lengthOfViewportPhysicalAxisForLogicalAxis(LogicalBoxAxis::Inline, conversionData.dynamicViewportFactor(), conversionData.style());

    // MARK: "container-percentage" resolution

    case Cqw: {
        if (auto resolvedValue = resolveContainerUnit(CQ::Axis::Width))
            return *resolvedValue;
        return computeNonCalcLengthDouble(value, Svw, conversionData);
    }

    case Cqh: {
        if (auto resolvedValue = resolveContainerUnit(CQ::Axis::Height))
            return *resolvedValue;
        return computeNonCalcLengthDouble(value, Svh, conversionData);
    }

    case Cqi: {
        if (auto resolvedValue = resolveContainerUnit(conversionData.style()->writingMode().isHorizontal() ? CQ::Axis::Width : CQ::Axis::Height))
            return *resolvedValue;
        return computeNonCalcLengthDouble(value, Svi, conversionData);
    }

    case Cqb: {
        if (auto resolvedValue = resolveContainerUnit(conversionData.style()->writingMode().isHorizontal() ? CQ::Axis::Height : CQ::Axis::Width))
            return *resolvedValue;
        return computeNonCalcLengthDouble(value, Svb, conversionData);
    }

    case Cqmax:
        if (value < 0)
            return std::min(computeNonCalcLengthDouble(value, Cqb, conversionData), computeNonCalcLengthDouble(value, Cqi, conversionData));
        return std::max(computeNonCalcLengthDouble(value, Cqb, conversionData), computeNonCalcLengthDouble(value, Cqi, conversionData));

    case Cqmin:
        if (value < 0)
            return std::max(computeNonCalcLengthDouble(value, Cqb, conversionData), computeNonCalcLengthDouble(value, Cqi, conversionData));
        return std::min(computeNonCalcLengthDouble(value, Cqb, conversionData), computeNonCalcLengthDouble(value, Cqi, conversionData));
    }

    // We do not apply the zoom factor when we are computing the value of the font-size property. The zooming
    // for font sizes is much more complicated, since we have to worry about enforcing the minimum font size preference
    // as well as enforcing the implicit "smart minimum."
    if (conversionData.computingFontSize() || isFontOrRootFontRelativeLength(lengthUnit))
        return value;

    return value * conversionData.zoom();
}

bool equalForLengthResolution(const RenderStyle& styleA, const RenderStyle& styleB)
{
    // These properties affect results of `computeNonCalcLengthDouble` above.

    if (styleA.fontDescription().computedSize() != styleB.fontDescription().computedSize())
        return false;
    if (styleA.fontDescription().specifiedSize() != styleB.fontDescription().specifiedSize())
        return false;

    if (styleA.metricsOfPrimaryFont().xHeight() != styleB.metricsOfPrimaryFont().xHeight())
        return false;
    if (styleA.metricsOfPrimaryFont().zeroWidth() != styleB.metricsOfPrimaryFont().zeroWidth())
        return false;

    if (styleA.zoom() != styleB.zoom())
        return false;

    return true;
}

} // namespace Style
} // namespace WebCore