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

/*
 * (C) 1999-2003 Lars Knoll ([email protected])
 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
 * Copyright (C) 2013 Intel Corporation. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

{% 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/style_property_shorthand.h"

#include <iterator>

#include "third_party/blink/renderer/core/css/properties/longhands.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"

{% macro define_shorthand(property, expansion) -%}
  static const CSSProperty* longhands[] = {
    {% for longhand in expansion.enabled_longhands %}
    &Get{{longhand.property_id}}(),
    {% endfor %}
  };

  static const StylePropertyShorthand shorthand(
      CSSPropertyID::{{property.enum_key}}, longhands);
{%- endmacro %}

namespace blink {
{% for property in properties %}
  {% set function_prefix = property.name.to_lower_camel_case() %}
  {% for expansion in property.expansions[1:] %}

static const StylePropertyShorthand* {{function_prefix}}Shorthand{{expansion.index}}() {
    {% for flag in expansion.flags %}
  if ({{flag.enabled and '!' or ''}}RuntimeEnabledFeatures::{{flag.name}}Enabled())
    return nullptr;
    {% endfor %}

  {{define_shorthand(property, expansion)}}
  return &shorthand;
}
  {% endfor %}

const StylePropertyShorthand& {{function_prefix}}Shorthand() {
  {% if property.expansions|length > 1 %}
    {% for expansion in property.expansions[1:] %}
  if (const auto* s = {{function_prefix}}Shorthand{{expansion.index}}())
    return *s;
    {% endfor %}

  {% endif %}
  {% if property.expansions[0].flags %}
    {% for flag in property.expansions[0].flags %}
  DCHECK({{not flag.enabled and '!' or ''}}RuntimeEnabledFeatures::{{flag.name}}Enabled());
    {% endfor %}

  {% endif %}
  {# Note: only expansions[0] can be empty #}
  {% if property.expansions[0].is_empty %}
  static StylePropertyShorthand empty_shorthand;
  return empty_shorthand;
  {% else %}
  {{define_shorthand(property, property.expansions[0])}}
  return shorthand;
  {% endif %}
}
{% endfor %}

// Returns an empty list if the property is not a shorthand
const StylePropertyShorthand& shorthandForProperty(CSSPropertyID propertyID) {
  // FIXME: We shouldn't switch between shorthand/not shorthand based on a runtime flag
  static StylePropertyShorthand empty_shorthand;

  switch (propertyID) {
    {% for property in properties %}
      {% set function_prefix = property.name.to_lower_camel_case() %}
    case CSSPropertyID::{{property.enum_key}}:
      return {{function_prefix}}Shorthand();
    {% endfor %}
    default: {
      return empty_shorthand;
    }
  }
}

void getMatchingShorthandsForLonghand(
    CSSPropertyID propertyID, Vector<StylePropertyShorthand, 4>* result) {
  DCHECK(!result->size());
  switch (propertyID) {
  {% for longhand_enum_key, shorthands in longhands_dictionary.items() %}
    case CSSPropertyID::{{longhand_enum_key}}: {
      {% for shorthand in shorthands %}
        {% if not shorthand.known_exposed %}
      if (CSSProperty::Get(CSSPropertyID::{{shorthand.enum_key}}).IsWebExposed())
        result->UncheckedAppend({{shorthand.name.to_lower_camel_case()}}Shorthand());
        {% else %}
      DCHECK(CSSProperty::Get(CSSPropertyID::{{shorthand.enum_key}}).IsWebExposed());
      result->UncheckedAppend({{shorthand.name.to_lower_camel_case()}}Shorthand());
        {% endif %}
      {% endfor %}
      break;
    }
    {% endfor %}
    default:
      break;
  }
}

} // namespace blink