summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/css/css_computed_style_declaration.cc
blob: 4df6327a8eea7e725ed90e70b4b7658d4f366336 (plain)
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/*
 * Copyright (C) 2004 Zack Rusin <zack@kde.org>
 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc.
 * All rights reserved.
 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
 * Copyright (C) 2007 Nicholas Shanks <webkit@nickshanks.com>
 * Copyright (C) 2011 Sencha, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "third_party/blink/renderer/core/css/css_computed_style_declaration.h"

#include "base/stl_util.h"
#include "third_party/blink/renderer/core/css/computed_style_css_value_mapping.h"
#include "third_party/blink/renderer/core/css/css_identifier_value.h"
#include "third_party/blink/renderer/core/css/css_primitive_value.h"
#include "third_party/blink/renderer/core/css/css_primitive_value_mappings.h"
#include "third_party/blink/renderer/core/css/css_property_id_templates.h"
#include "third_party/blink/renderer/core/css/css_property_names.h"
#include "third_party/blink/renderer/core/css/css_selector.h"
#include "third_party/blink/renderer/core/css/css_variable_data.h"
#include "third_party/blink/renderer/core/css/parser/css_parser.h"
#include "third_party/blink/renderer/core/css/properties/css_property_ref.h"
#include "third_party/blink/renderer/core/css/style_engine.h"
#include "third_party/blink/renderer/core/css/zoom_adjusted_pixel_value.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/pseudo_element.h"
#include "third_party/blink/renderer/core/frame/web_feature.h"
#include "third_party/blink/renderer/core/html/html_frame_owner_element.h"
#include "third_party/blink/renderer/core/layout/layout_object.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/heap/heap.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"

namespace blink {

namespace {

// List of all properties we know how to compute, omitting shorthands.
// NOTE: Do not use this list, use computableProperties() instead
// to respect runtime enabling of CSS properties.
const CSSPropertyID kComputedPropertyArray[] = {
    CSSPropertyID::kAnimationDelay, CSSPropertyID::kAnimationDirection,
    CSSPropertyID::kAnimationDuration, CSSPropertyID::kAnimationFillMode,
    CSSPropertyID::kAnimationIterationCount, CSSPropertyID::kAnimationName,
    CSSPropertyID::kAnimationPlayState, CSSPropertyID::kAnimationTimingFunction,
    CSSPropertyID::kBackgroundAttachment, CSSPropertyID::kBackgroundBlendMode,
    CSSPropertyID::kBackgroundClip, CSSPropertyID::kBackgroundColor,
    CSSPropertyID::kBackgroundImage, CSSPropertyID::kBackgroundOrigin,
    // more-specific background-position-x/y are non-standard
    CSSPropertyID::kBackgroundPosition, CSSPropertyID::kBackgroundRepeat,
    CSSPropertyID::kBackgroundSize, CSSPropertyID::kBorderBottomColor,
    CSSPropertyID::kBorderBottomLeftRadius,
    CSSPropertyID::kBorderBottomRightRadius, CSSPropertyID::kBorderBottomStyle,
    CSSPropertyID::kBorderBottomWidth, CSSPropertyID::kBorderCollapse,
    CSSPropertyID::kBorderImageOutset, CSSPropertyID::kBorderImageRepeat,
    CSSPropertyID::kBorderImageSlice, CSSPropertyID::kBorderImageSource,
    CSSPropertyID::kBorderImageWidth, CSSPropertyID::kBorderLeftColor,
    CSSPropertyID::kBorderLeftStyle, CSSPropertyID::kBorderLeftWidth,
    CSSPropertyID::kBorderRightColor, CSSPropertyID::kBorderRightStyle,
    CSSPropertyID::kBorderRightWidth, CSSPropertyID::kBorderTopColor,
    CSSPropertyID::kBorderTopLeftRadius, CSSPropertyID::kBorderTopRightRadius,
    CSSPropertyID::kBorderTopStyle, CSSPropertyID::kBorderTopWidth,
    CSSPropertyID::kBottom, CSSPropertyID::kBoxShadow,
    CSSPropertyID::kBoxSizing, CSSPropertyID::kBreakAfter,
    CSSPropertyID::kBreakBefore, CSSPropertyID::kBreakInside,
    CSSPropertyID::kCaptionSide, CSSPropertyID::kClear, CSSPropertyID::kClip,
    CSSPropertyID::kColor, CSSPropertyID::kContent, CSSPropertyID::kCursor,
    CSSPropertyID::kDirection, CSSPropertyID::kDisplay,
    CSSPropertyID::kEmptyCells, CSSPropertyID::kFloat,
    CSSPropertyID::kFontFamily, CSSPropertyID::kFontKerning,
    CSSPropertyID::kFontOpticalSizing, CSSPropertyID::kFontSize,
    CSSPropertyID::kFontSizeAdjust, CSSPropertyID::kFontStretch,
    CSSPropertyID::kFontStyle, CSSPropertyID::kFontVariant,
    CSSPropertyID::kFontVariantLigatures, CSSPropertyID::kFontVariantCaps,
    CSSPropertyID::kFontVariantNumeric, CSSPropertyID::kFontVariantEastAsian,
    CSSPropertyID::kFontWeight, CSSPropertyID::kHeight,
    CSSPropertyID::kImageOrientation, CSSPropertyID::kImageRendering,
    CSSPropertyID::kIsolation, CSSPropertyID::kJustifyItems,
    CSSPropertyID::kJustifySelf, CSSPropertyID::kLeft,
    CSSPropertyID::kLetterSpacing, CSSPropertyID::kLineHeight,
    CSSPropertyID::kLineHeightStep, CSSPropertyID::kListStyleImage,
    CSSPropertyID::kListStylePosition, CSSPropertyID::kListStyleType,
    CSSPropertyID::kMarginBottom, CSSPropertyID::kMarginLeft,
    CSSPropertyID::kMarginRight, CSSPropertyID::kMarginTop,
    CSSPropertyID::kMaxHeight, CSSPropertyID::kMaxWidth,
    CSSPropertyID::kMinHeight, CSSPropertyID::kMinWidth,
    CSSPropertyID::kMixBlendMode, CSSPropertyID::kObjectFit,
    CSSPropertyID::kObjectPosition, CSSPropertyID::kOffsetAnchor,
    CSSPropertyID::kOffsetDistance, CSSPropertyID::kOffsetPath,
    CSSPropertyID::kOffsetPosition, CSSPropertyID::kOffsetRotate,
    CSSPropertyID::kOpacity, CSSPropertyID::kOrphans,
    CSSPropertyID::kOutlineColor, CSSPropertyID::kOutlineOffset,
    CSSPropertyID::kOutlineStyle, CSSPropertyID::kOutlineWidth,
    CSSPropertyID::kOverflowAnchor, CSSPropertyID::kOverflowWrap,
    CSSPropertyID::kOverflowX, CSSPropertyID::kOverflowY,
    CSSPropertyID::kPaddingBottom, CSSPropertyID::kPaddingLeft,
    CSSPropertyID::kPaddingRight, CSSPropertyID::kPaddingTop,
    CSSPropertyID::kPointerEvents, CSSPropertyID::kPosition,
    CSSPropertyID::kResize, CSSPropertyID::kRight,
    CSSPropertyID::kScrollBehavior, CSSPropertyID::kScrollCustomization,
    CSSPropertyID::kSpeak, CSSPropertyID::kTableLayout, CSSPropertyID::kTabSize,
    CSSPropertyID::kTextAlign, CSSPropertyID::kTextAlignLast,
    CSSPropertyID::kTextDecoration, CSSPropertyID::kTextDecorationLine,
    CSSPropertyID::kTextDecorationStyle, CSSPropertyID::kTextDecorationColor,
    CSSPropertyID::kTextDecorationSkipInk, CSSPropertyID::kTextJustify,
    CSSPropertyID::kTextUnderlinePosition, CSSPropertyID::kTextIndent,
    CSSPropertyID::kTextRendering, CSSPropertyID::kTextShadow,
    CSSPropertyID::kTextSizeAdjust, CSSPropertyID::kTextOverflow,
    CSSPropertyID::kTextTransform, CSSPropertyID::kTop,
    CSSPropertyID::kTouchAction, CSSPropertyID::kTransitionDelay,
    CSSPropertyID::kTransitionDuration, CSSPropertyID::kTransitionProperty,
    CSSPropertyID::kTransitionTimingFunction, CSSPropertyID::kUnicodeBidi,
    CSSPropertyID::kVerticalAlign, CSSPropertyID::kVisibility,
    CSSPropertyID::kWhiteSpace, CSSPropertyID::kWidows, CSSPropertyID::kWidth,
    CSSPropertyID::kWillChange, CSSPropertyID::kWordBreak,
    CSSPropertyID::kWordSpacing, CSSPropertyID::kZIndex, CSSPropertyID::kZoom,

    CSSPropertyID::kWebkitAppearance, CSSPropertyID::kBackfaceVisibility,
    CSSPropertyID::kWebkitBorderHorizontalSpacing,
    CSSPropertyID::kWebkitBorderImage,
    CSSPropertyID::kWebkitBorderVerticalSpacing, CSSPropertyID::kWebkitBoxAlign,
    CSSPropertyID::kWebkitBoxDecorationBreak,
    CSSPropertyID::kWebkitBoxDirection, CSSPropertyID::kWebkitBoxFlex,
    CSSPropertyID::kWebkitBoxOrdinalGroup, CSSPropertyID::kWebkitBoxOrient,
    CSSPropertyID::kWebkitBoxPack, CSSPropertyID::kWebkitBoxReflect,
    CSSPropertyID::kColumnCount, CSSPropertyID::kColumnGap,
    CSSPropertyID::kColumnRuleColor, CSSPropertyID::kColumnRuleStyle,
    CSSPropertyID::kColumnRuleWidth, CSSPropertyID::kColumnSpan,
    CSSPropertyID::kColumnWidth, CSSPropertyID::kBackdropFilter,
    CSSPropertyID::kAlignContent, CSSPropertyID::kAlignItems,
    CSSPropertyID::kAlignSelf, CSSPropertyID::kFlexBasis,
    CSSPropertyID::kFlexGrow, CSSPropertyID::kFlexShrink,
    CSSPropertyID::kFlexDirection, CSSPropertyID::kFlexWrap,
    CSSPropertyID::kJustifyContent, CSSPropertyID::kWebkitFontSmoothing,
    CSSPropertyID::kGridAutoColumns, CSSPropertyID::kGridAutoFlow,
    CSSPropertyID::kGridAutoRows, CSSPropertyID::kGridColumnEnd,
    CSSPropertyID::kGridColumnStart, CSSPropertyID::kGridTemplateAreas,
    CSSPropertyID::kGridTemplateColumns, CSSPropertyID::kGridTemplateRows,
    CSSPropertyID::kGridRowEnd, CSSPropertyID::kGridRowStart,
    CSSPropertyID::kRowGap, CSSPropertyID::kWebkitHighlight,
    CSSPropertyID::kHyphens, CSSPropertyID::kWebkitHyphenateCharacter,
    CSSPropertyID::kWebkitLineBreak, CSSPropertyID::kWebkitLineClamp,
    CSSPropertyID::kWebkitLocale, CSSPropertyID::kWebkitMaskBoxImage,
    CSSPropertyID::kWebkitMaskBoxImageOutset,
    CSSPropertyID::kWebkitMaskBoxImageRepeat,
    CSSPropertyID::kWebkitMaskBoxImageSlice,
    CSSPropertyID::kWebkitMaskBoxImageSource,
    CSSPropertyID::kWebkitMaskBoxImageWidth, CSSPropertyID::kWebkitMaskClip,
    CSSPropertyID::kWebkitMaskComposite, CSSPropertyID::kWebkitMaskImage,
    CSSPropertyID::kWebkitMaskOrigin, CSSPropertyID::kWebkitMaskPosition,
    CSSPropertyID::kWebkitMaskRepeat, CSSPropertyID::kWebkitMaskSize,
    CSSPropertyID::kOrder, CSSPropertyID::kPerspective,
    CSSPropertyID::kPerspectiveOrigin, CSSPropertyID::kWebkitPrintColorAdjust,
    CSSPropertyID::kWebkitRtlOrdering, CSSPropertyID::kShapeOutside,
    CSSPropertyID::kShapeImageThreshold, CSSPropertyID::kShapeMargin,
    CSSPropertyID::kWebkitTapHighlightColor, CSSPropertyID::kWebkitTextCombine,
    CSSPropertyID::kWebkitTextDecorationsInEffect,
    CSSPropertyID::kWebkitTextEmphasisColor,
    CSSPropertyID::kWebkitTextEmphasisPosition,
    CSSPropertyID::kWebkitTextEmphasisStyle,
    CSSPropertyID::kWebkitTextFillColor, CSSPropertyID::kWebkitTextOrientation,
    CSSPropertyID::kWebkitTextSecurity, CSSPropertyID::kWebkitTextStrokeColor,
    CSSPropertyID::kWebkitTextStrokeWidth, CSSPropertyID::kTransform,
    CSSPropertyID::kTransformOrigin, CSSPropertyID::kTransformStyle,
    CSSPropertyID::kWebkitUserDrag, CSSPropertyID::kWebkitUserModify,
    CSSPropertyID::kUserSelect, CSSPropertyID::kWebkitWritingMode,
    CSSPropertyID::kWebkitAppRegion, CSSPropertyID::kBufferedRendering,
    CSSPropertyID::kClipPath, CSSPropertyID::kClipRule, CSSPropertyID::kMask,
    CSSPropertyID::kFilter, CSSPropertyID::kFloodColor,
    CSSPropertyID::kFloodOpacity, CSSPropertyID::kLightingColor,
    CSSPropertyID::kStopColor, CSSPropertyID::kStopOpacity,
    CSSPropertyID::kColorInterpolation,
    CSSPropertyID::kColorInterpolationFilters, CSSPropertyID::kColorRendering,
    CSSPropertyID::kFill, CSSPropertyID::kFillOpacity, CSSPropertyID::kFillRule,
    CSSPropertyID::kMarkerEnd, CSSPropertyID::kMarkerMid,
    CSSPropertyID::kMarkerStart, CSSPropertyID::kMaskType,
    CSSPropertyID::kMaskSourceType, CSSPropertyID::kShapeRendering,
    CSSPropertyID::kStroke, CSSPropertyID::kStrokeDasharray,
    CSSPropertyID::kStrokeDashoffset, CSSPropertyID::kStrokeLinecap,
    CSSPropertyID::kStrokeLinejoin, CSSPropertyID::kStrokeMiterlimit,
    CSSPropertyID::kStrokeOpacity, CSSPropertyID::kStrokeWidth,
    CSSPropertyID::kAlignmentBaseline, CSSPropertyID::kBaselineShift,
    CSSPropertyID::kDominantBaseline, CSSPropertyID::kTextAnchor,
    CSSPropertyID::kWritingMode, CSSPropertyID::kVectorEffect,
    CSSPropertyID::kPaintOrder, CSSPropertyID::kD, CSSPropertyID::kCx,
    CSSPropertyID::kCy, CSSPropertyID::kX, CSSPropertyID::kY, CSSPropertyID::kR,
    CSSPropertyID::kRx, CSSPropertyID::kRy, CSSPropertyID::kTranslate,
    CSSPropertyID::kRotate, CSSPropertyID::kScale, CSSPropertyID::kCaretColor,
    CSSPropertyID::kLineBreak, CSSPropertyID::kMathStyle};

CSSValueID CssIdentifierForFontSizeKeyword(int keyword_size) {
  DCHECK_NE(keyword_size, 0);
  DCHECK_LE(keyword_size, 8);
  return static_cast<CSSValueID>(static_cast<int>(CSSValueID::kXxSmall) +
                                 keyword_size - 1);
}

void LogUnimplementedPropertyID(const CSSProperty& property) {
  DEFINE_STATIC_LOCAL(HashSet<CSSPropertyID>, property_id_set, ());
  if (property.PropertyID() == CSSPropertyID::kVariable)
    return;
  if (!property_id_set.insert(property.PropertyID()).is_new_entry)
    return;

  DLOG(ERROR) << "Blink does not yet implement getComputedStyle for '"
              << property.GetPropertyName() << "'.";
}

}  // namespace

const Vector<const CSSProperty*>&
CSSComputedStyleDeclaration::ComputableProperties(
    const ExecutionContext* execution_context) {
  DEFINE_STATIC_LOCAL(Vector<const CSSProperty*>, properties, ());
  if (properties.IsEmpty()) {
    CSSProperty::FilterWebExposedCSSPropertiesIntoVector(
        execution_context, kComputedPropertyArray,
        base::size(kComputedPropertyArray), properties);
  }
  return properties;
}

CSSComputedStyleDeclaration::CSSComputedStyleDeclaration(
    Node* n,
    bool allow_visited_style,
    const String& pseudo_element_name)
    : CSSStyleDeclaration(n ? n->GetExecutionContext() : nullptr),
      node_(n),
      pseudo_element_specifier_(
          CSSSelector::ParsePseudoId(pseudo_element_name)),
      allow_visited_style_(allow_visited_style) {}

CSSComputedStyleDeclaration::~CSSComputedStyleDeclaration() = default;

String CSSComputedStyleDeclaration::cssText() const {
  StringBuilder result;
  static const Vector<const CSSProperty*>& properties =
      ComputableProperties(GetExecutionContext());

  for (unsigned i = 0; i < properties.size(); i++) {
    if (i)
      result.Append(' ');
    result.Append(properties[i]->GetPropertyName());
    result.Append(": ");
    result.Append(GetPropertyValue(properties[i]->PropertyID()));
    result.Append(';');
  }

  return result.ToString();
}

void CSSComputedStyleDeclaration::setCSSText(const ExecutionContext*,
                                             const String&,
                                             ExceptionState& exception_state) {
  exception_state.ThrowDOMException(
      DOMExceptionCode::kNoModificationAllowedError,
      "These styles are computed, and therefore read-only.");
}

const CSSValue*
CSSComputedStyleDeclaration::GetFontSizeCSSValuePreferringKeyword() const {
  if (!node_)
    return nullptr;

  node_->GetDocument().UpdateStyleAndLayout(DocumentUpdateReason::kEditing);

  const ComputedStyle* style =
      node_->EnsureComputedStyle(pseudo_element_specifier_);
  if (!style)
    return nullptr;

  if (int keyword_size = style->GetFontDescription().KeywordSize()) {
    return CSSIdentifierValue::Create(
        CssIdentifierForFontSizeKeyword(keyword_size));
  }

  return ZoomAdjustedPixelValue(style->GetFontDescription().ComputedPixelSize(),
                                *style);
}

bool CSSComputedStyleDeclaration::IsMonospaceFont() const {
  if (!node_)
    return false;

  const ComputedStyle* style =
      node_->EnsureComputedStyle(pseudo_element_specifier_);
  if (!style)
    return false;

  return style->GetFontDescription().IsMonospace();
}
const ComputedStyle* CSSComputedStyleDeclaration::ComputeComputedStyle() const {
  Node* styled_node = this->StyledNode();
  DCHECK(styled_node);
  const ComputedStyle* style = styled_node->EnsureComputedStyle(
      styled_node->IsPseudoElement() ? kPseudoIdNone
                                     : pseudo_element_specifier_);
  if (style && style->IsEnsuredOutsideFlatTree()) {
    UseCounter::Count(node_->GetDocument(),
                      WebFeature::kGetComputedStyleOutsideFlatTree);
  }
  return style;
}

Node* CSSComputedStyleDeclaration::StyledNode() const {
  if (!node_)
    return nullptr;

  if (auto* node_element = DynamicTo<Element>(node_.Get())) {
    if (PseudoElement* element =
            node_element->GetPseudoElement(pseudo_element_specifier_))
      return element;
  }
  return node_.Get();
}

LayoutObject* CSSComputedStyleDeclaration::StyledLayoutObject() const {
  auto* node = StyledNode();
  if (!node)
    return nullptr;

  if (pseudo_element_specifier_ != kPseudoIdNone && node == node_.Get())
    return nullptr;

  return node->GetLayoutObject();
}

const CSSValue* CSSComputedStyleDeclaration::GetPropertyCSSValue(
    CSSPropertyID property_id) const {
  if (property_id == CSSPropertyID::kVariable) {
    // TODO(https://crbug.com/980160): Disallow calling this function with
    // kVariable.
    return nullptr;
  }
  return GetPropertyCSSValue(CSSPropertyName(property_id));
}

const CSSValue* CSSComputedStyleDeclaration::GetPropertyCSSValue(
    AtomicString custom_property_name) const {
  return GetPropertyCSSValue(CSSPropertyName(custom_property_name));
}

HeapHashMap<AtomicString, Member<const CSSValue>>
CSSComputedStyleDeclaration::GetVariables() const {
  const ComputedStyle* style = ComputeComputedStyle();
  if (!style)
    return {};
  DCHECK(StyledNode());
  return ComputedStyleCSSValueMapping::GetVariables(
      *style, StyledNode()->GetDocument().GetPropertyRegistry());
}

const CSSValue* CSSComputedStyleDeclaration::GetPropertyCSSValue(
    const CSSPropertyName& property_name) const {
  Node* styled_node = StyledNode();
  if (!styled_node)
    return nullptr;

  Document& document = styled_node->GetDocument();

  if (HTMLFrameOwnerElement* owner = document.LocalOwner()) {
    // We are inside an iframe. If any of our ancestor iframes needs a style
    // and/or layout update, we need to make that up-to-date to resolve viewport
    // media queries and generate boxes as we might be moving to/from
    // display:none in some element in the chain of ancestors.
    //
    // TODO(futhark@chromium.org): There is an open question what the computed
    // style should be in a display:none iframe. If the property we are querying
    // is not layout dependent, we will not update the iframe layout box here.
    bool is_layout_dependent_property =
        !property_name.IsCustomProperty() &&
        CSSProperty::Get(property_name.Id()).IsLayoutDependentProperty();
    if (is_layout_dependent_property ||
        document.GetStyleEngine().HasViewportDependentMediaQueries()) {
      owner->GetDocument().UpdateStyleAndLayout(
          DocumentUpdateReason::kJavaScript);
      // The style recalc could have caused the styled node to be discarded or
      // replaced if it was a PseudoElement so we need to update it.
      styled_node = StyledNode();
    }
  }

  document.UpdateStyleAndLayoutTreeForNode(styled_node);

  CSSPropertyRef ref(property_name, document);
  if (!ref.IsValid())
    return nullptr;
  const CSSProperty& property_class = ref.GetProperty();

  // The style recalc could have caused the styled node to be discarded or
  // replaced if it was a PseudoElement so we need to update it.
  styled_node = StyledNode();
  LayoutObject* layout_object = StyledLayoutObject();
  const ComputedStyle* style = ComputeComputedStyle();

  if (property_class.IsLayoutDependent(style, layout_object)) {
    document.UpdateStyleAndLayoutForNode(styled_node,
                                         DocumentUpdateReason::kJavaScript);
    styled_node = StyledNode();
    style = ComputeComputedStyle();
    layout_object = StyledLayoutObject();
  }

  if (!style)
    return nullptr;

  const CSSValue* value = property_class.CSSValueFromComputedStyle(
      *style, layout_object, allow_visited_style_);
  if (value)
    return value;

  LogUnimplementedPropertyID(property_class);
  return nullptr;
}

String CSSComputedStyleDeclaration::GetPropertyValue(
    CSSPropertyID property_id) const {
  // allow_visited_style_ is true only for access from DevTools.
  if (!allow_visited_style_ &&
      property_id == CSSPropertyID::kWebkitAppearance) {
    UseCounter::Count(
        node_->GetDocument(),
        WebFeature::kGetComputedStyleForWebkitAppearanceExcludeDevTools);
  }
  const CSSValue* value = GetPropertyCSSValue(property_id);
  if (value)
    return value->CssText();
  return "";
}

unsigned CSSComputedStyleDeclaration::length() const {
  if (!node_ || !node_->InActiveDocument())
    return 0;
  return ComputableProperties(GetExecutionContext()).size();
}

String CSSComputedStyleDeclaration::item(unsigned i) const {
  if (i >= length())
    return "";

  return ComputableProperties(GetExecutionContext())[i]
      ->GetPropertyNameString();
}

bool CSSComputedStyleDeclaration::CssPropertyMatches(
    CSSPropertyID property_id,
    const CSSValue& property_value) const {
  if (property_id == CSSPropertyID::kFontSize &&
      (property_value.IsPrimitiveValue() ||
       property_value.IsIdentifierValue()) &&
      node_) {
    // This is only used by editing code.
    node_->GetDocument().UpdateStyleAndLayout(DocumentUpdateReason::kEditing);
    const ComputedStyle* style =
        node_->EnsureComputedStyle(pseudo_element_specifier_);
    if (style && style->GetFontDescription().KeywordSize()) {
      CSSValueID size_value = CssIdentifierForFontSizeKeyword(
          style->GetFontDescription().KeywordSize());
      auto* identifier_value = DynamicTo<CSSIdentifierValue>(property_value);
      if (identifier_value && identifier_value->GetValueID() == size_value)
        return true;
    }
  }
  const CSSValue* value = GetPropertyCSSValue(property_id);
  return DataEquivalent(value, &property_value);
}

MutableCSSPropertyValueSet* CSSComputedStyleDeclaration::CopyProperties()
    const {
  return CopyPropertiesInSet(ComputableProperties(GetExecutionContext()));
}

MutableCSSPropertyValueSet* CSSComputedStyleDeclaration::CopyPropertiesInSet(
    const Vector<const CSSProperty*>& properties) const {
  HeapVector<CSSPropertyValue, 256> list;
  list.ReserveInitialCapacity(properties.size());
  for (unsigned i = 0; i < properties.size(); ++i) {
    const CSSProperty& property = *properties[i];
    const CSSValue* value = GetPropertyCSSValue(property.GetCSSPropertyName());
    if (value)
      list.push_back(CSSPropertyValue(property, *value, false));
  }
  return MakeGarbageCollected<MutableCSSPropertyValueSet>(list.data(),
                                                          list.size());
}

CSSRule* CSSComputedStyleDeclaration::parentRule() const {
  return nullptr;
}

String CSSComputedStyleDeclaration::getPropertyValue(
    const String& property_name) {
  CSSPropertyID property_id =
      cssPropertyID(GetExecutionContext(), property_name);
  if (!isValidCSSPropertyID(property_id))
    return String();
  if (property_id == CSSPropertyID::kVariable) {
    const CSSValue* value = GetPropertyCSSValue(AtomicString(property_name));
    if (value)
      return value->CssText();
    return String();
  }
#if DCHECK_IS_ON
  DCHECK(CSSProperty::Get(property_id).IsEnabled());
#endif
  return GetPropertyValue(property_id);
}

String CSSComputedStyleDeclaration::getPropertyPriority(const String&) {
  // All computed styles have a priority of not "important".
  return "";
}

String CSSComputedStyleDeclaration::GetPropertyShorthand(const String&) {
  return "";
}

bool CSSComputedStyleDeclaration::IsPropertyImplicit(const String&) {
  return false;
}

void CSSComputedStyleDeclaration::setProperty(const ExecutionContext*,
                                              const String& name,
                                              const String&,
                                              const String&,
                                              ExceptionState& exception_state) {
  exception_state.ThrowDOMException(
      DOMExceptionCode::kNoModificationAllowedError,
      "These styles are computed, and therefore the '" + name +
          "' property is read-only.");
}

String CSSComputedStyleDeclaration::removeProperty(
    const String& name,
    ExceptionState& exception_state) {
  exception_state.ThrowDOMException(
      DOMExceptionCode::kNoModificationAllowedError,
      "These styles are computed, and therefore the '" + name +
          "' property is read-only.");
  return String();
}

const CSSValue* CSSComputedStyleDeclaration::GetPropertyCSSValueInternal(
    CSSPropertyID property_id) {
  if (property_id == CSSPropertyID::kWebkitAppearance && node_) {
    UseCounter::Count(node_->GetDocument(),
                      WebFeature::kGetComputedStyleWebkitAppearance);
  }
  return GetPropertyCSSValue(property_id);
}

const CSSValue* CSSComputedStyleDeclaration::GetPropertyCSSValueInternal(
    AtomicString custom_property_name) {
  DCHECK_EQ(CSSPropertyID::kVariable,
            cssPropertyID(GetExecutionContext(), custom_property_name));
  return GetPropertyCSSValue(custom_property_name);
}

String CSSComputedStyleDeclaration::GetPropertyValueInternal(
    CSSPropertyID property_id) {
  return GetPropertyValue(property_id);
}

void CSSComputedStyleDeclaration::SetPropertyInternal(
    CSSPropertyID id,
    const String&,
    const String&,
    bool,
    SecureContextMode,
    ExceptionState& exception_state) {
  exception_state.ThrowDOMException(
      DOMExceptionCode::kNoModificationAllowedError,
      "These styles are computed, and therefore the '" +
          CSSUnresolvedProperty::Get(id).GetPropertyNameString() +
          "' property is read-only.");
}

void CSSComputedStyleDeclaration::Trace(Visitor* visitor) {
  visitor->Trace(node_);
  CSSStyleDeclaration::Trace(visitor);
}

}  // namespace blink