chromium/third_party/blink/renderer/build/scripts/core/css/templates/cssom_types.cc.tmpl

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

{% from 'templates/macros.tmpl' import source_files_for_generated_file %}
{{source_files_for_generated_file(template_file, input_files)}}

#include "third_party/blink/renderer/core/css/cssom/cssom_types.h"

#include "third_party/blink/renderer/core/css/css_property_name.h"
#include "third_party/blink/renderer/core/css/cssom/css_keyword_value.h"
#include "third_party/blink/renderer/core/css/cssom/css_numeric_value.h"
#include "third_party/blink/renderer/core/css/cssom/css_style_value.h"
#include "third_party/blink/renderer/core/css/cssom/css_unsupported_style_value.h"
#include "third_party/blink/renderer/core/css/cssom/cssom_keywords.h"
#include "third_party/blink/renderer/core/css/properties/css_property.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"

namespace blink {

bool CSSOMTypes::IsCSSStyleValueLength(const CSSStyleValue& value) {
  if (!value.IsNumericValue())
    return false;
  return static_cast<const CSSNumericValue&>(value).Type().
    MatchesBaseType(CSSNumericValueType::BaseType::kLength);
}

bool CSSOMTypes::IsCSSStyleValueNumber(const CSSStyleValue& value) {
  if (!value.IsNumericValue())
    return false;
  return static_cast<const CSSNumericValue&>(value).Type().
    MatchesNumber();
}

bool CSSOMTypes::IsCSSStyleValueTime(const CSSStyleValue& value) {
  if (!value.IsNumericValue())
    return false;
  return static_cast<const CSSNumericValue&>(value).Type().
    MatchesBaseType(CSSNumericValueType::BaseType::kTime);
}

bool CSSOMTypes::IsCSSStyleValueAngle(const CSSStyleValue& value) {
  if (!value.IsNumericValue())
    return false;
  return static_cast<const CSSNumericValue&>(value).Type().
    MatchesBaseType(CSSNumericValueType::BaseType::kAngle);
}

bool CSSOMTypes::IsCSSStyleValuePercentage(const CSSStyleValue& value) {
  if (!value.IsNumericValue())
    return false;
  return static_cast<const CSSNumericValue&>(value).Type().
    MatchesPercentage();
}

bool CSSOMTypes::IsCSSStyleValueResolution(const CSSStyleValue& value) {
  if (!value.IsNumericValue())
    return false;
  return static_cast<const CSSNumericValue&>(value).Type().
    MatchesBaseType(CSSNumericValueType::BaseType::kResolution);
}

bool CSSOMTypes::IsCSSStyleValueFlex(const CSSStyleValue& value) {
  if (!value.IsNumericValue())
    return false;
  return static_cast<const CSSNumericValue&>(value).Type().
    MatchesBaseType(CSSNumericValueType::BaseType::kFlex);
}

bool CSSOMTypes::IsCSSStyleValueImage(const CSSStyleValue& value) {
  return value.GetType() == CSSStyleValue::kURLImageType;
}

bool CSSOMTypes::IsCSSStyleValueTransform(const CSSStyleValue& value) {
  return value.GetType() == CSSStyleValue::kTransformType;
}

bool CSSOMTypes::IsCSSStyleValuePosition(const CSSStyleValue& value) {
  return value.GetType() == CSSStyleValue::kPositionType;
}

bool CSSOMTypes::IsPropertySupported(CSSPropertyID id) {
  switch (id) {
    case CSSPropertyID::kVariable:
  {% for property in properties if property.typedom_types %}
    case CSSPropertyID::{{property.enum_key}}:
  {% endfor %}
      return true;
    default:
      return false;
  }
}

bool CSSOMTypes::PropertyCanTake(CSSPropertyID id,
                                 const AtomicString& custom_property_name,
                                 const CSSStyleValue& value) {
  DCHECK_EQ(id == CSSPropertyID::kVariable, !custom_property_name.IsNull());

  if (auto* css_keyword_value = DynamicTo<CSSKeywordValue>(value)) {
    if (id == CSSPropertyID::kWritingMode) {
      CSSValueID value_id = css_keyword_value->KeywordValueID();
      if (value_id == CSSValueID::kSidewaysRl ||
          value_id == CSSValueID::kSidewaysLr) {
        return RuntimeEnabledFeatures::SidewaysWritingModesEnabled();
      }
    }
    return CSSOMKeywords::ValidKeywordForProperty(id, *css_keyword_value);
  }
  if (auto* unsupported_style_value =
          DynamicTo<CSSUnsupportedStyleValue>(value)) {
    auto name = (id == CSSPropertyID::kVariable)
                    ? CSSPropertyName(custom_property_name)
                    : CSSPropertyName(id);
    return unsupported_style_value->IsValidFor(name);
  }
  if (value.GetType() == CSSStyleValue::kUnparsedType) {
    return true;
  }

  switch (id) {
    case CSSPropertyID::kVariable:
      return value.GetType() == CSSStyleValue::kUnparsedType;
    {% for property in properties if property.typedom_types %}
    {% if property.typedom_types != ['Keyword'] %}
    case CSSPropertyID::{{property.enum_key}}:
      return (
          {% for type in property.typedom_types if type != 'Keyword' %}
          {{ "|| " if not loop.first }}CSSOMTypes::IsCSSStyleValue{{type}}(value)
          {% endfor %}
      );
    {% endif %}
    {% endfor %}
    default:
      return false;
  }
}

}  // namespace blink