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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/core/css/cssom_utils.h"
#include "third_party/blink/renderer/core/css/css_custom_ident_value.h"
#include "third_party/blink/renderer/core/css/css_grid_template_areas_value.h"
#include "third_party/blink/renderer/core/css/css_identifier_value.h"
#include "third_party/blink/renderer/core/css/css_string_value.h"
#include "third_party/blink/renderer/core/css/css_value_list.h"
namespace blink {
// static
bool CSSOMUtils::IncludeDependentGridLineEndValue(const CSSValue* line_start,
const CSSValue* line_end) {
const bool line_end_is_initial_value =
IsA<CSSIdentifierValue>(line_end) &&
To<CSSIdentifierValue>(line_end)->GetValueID() == CSSValueID::kAuto;
// "When grid-column-start is omitted, if grid-row-start is a <custom-ident>,
// all four longhands are set to that value. Otherwise, it is set to auto.
// When grid-row-end is omitted, if grid-row-start is a <custom-ident>,
// grid-row-end is set to that <custom-ident>; otherwise, it is set to auto.
// When grid-column-end is omitted, if grid-column-start is a <custom-ident>,
// grid-column-end is set to that <custom-ident>; otherwise, it is set to
// auto."
//
// https://www.w3.org/TR/css-grid-2/#placement-shorthands
//
// In order to produce a shortest-possible-serialization, we need essentially
// the converse of that statement, as parsing handles the
// literal interpretation. In particular, `CSSValueList` values (integer
// literals) are always included, duplicate `custom-ident` values get
// dropped, as well as initial values if they match the equivalent
// `line_start` value.
return IsA<CSSValueList>(line_end) ||
((*line_end != *line_start) &&
(IsA<CSSCustomIdentValue>(line_start) || !line_end_is_initial_value));
}
// static
bool CSSOMUtils::IsAutoValue(const CSSValue* value) {
return IsA<CSSIdentifierValue>(value) &&
To<CSSIdentifierValue>(value)->GetValueID() == CSSValueID::kAuto;
}
// static
bool CSSOMUtils::IsNoneValue(const CSSValue* value) {
return IsA<CSSIdentifierValue>(value) &&
To<CSSIdentifierValue>(value)->GetValueID() == CSSValueID::kNone;
}
// static
bool CSSOMUtils::IsEmptyValueList(const CSSValue* value) {
const CSSValueList* value_list = DynamicTo<CSSValueList>(value);
return value_list && value_list->length() == 0;
}
// static
bool CSSOMUtils::HasGridRepeatValue(const CSSValueList* value_list) {
if (value_list) {
for (const auto& value : *value_list) {
if (value->IsGridRepeatValue()) {
return true;
}
}
}
return false;
}
// static
bool CSSOMUtils::IsMasonryColumnDirectionValue(
const CSSValue* masonry_direction_values) {
const auto* masonry_direction_value =
DynamicTo<CSSIdentifierValue>(masonry_direction_values);
return masonry_direction_value &&
(masonry_direction_value->GetValueID() == CSSValueID::kColumn ||
masonry_direction_value->GetValueID() == CSSValueID::kColumnReverse);
}
// static
String CSSOMUtils::NamedGridAreaTextForPosition(
const NamedGridAreaMap& grid_area_map,
wtf_size_t row,
wtf_size_t column) {
for (const auto& item : grid_area_map) {
const GridArea& area = item.value;
if (row >= area.rows.StartLine() && row < area.rows.EndLine() &&
column >= area.columns.StartLine() && column < area.columns.EndLine()) {
return item.key;
}
}
return ".";
}
// static
String CSSOMUtils::SerializeGridAreaText(
const cssvalue::CSSGridTemplateAreasValue* template_areas,
wtf_size_t fixed_index,
bool is_row) {
const NamedGridAreaMap& grid_area_map = template_areas->GridAreaMap();
const wtf_size_t count =
is_row ? template_areas->ColumnCount() : template_areas->RowCount();
StringBuilder result;
for (wtf_size_t i = 0; i < count; ++i) {
if (is_row) {
result.Append(
NamedGridAreaTextForPosition(grid_area_map, fixed_index, i));
} else {
result.Append(
NamedGridAreaTextForPosition(grid_area_map, i, fixed_index));
}
if (i != count - 1) {
result.Append(' ');
}
}
return result.ReleaseString();
}
// static
CSSValueList* CSSOMUtils::ComputedValueForGridTemplateShorthand(
const CSSValue* template_row_values,
const CSSValue* template_column_values,
const CSSValue* template_area_values) {
const bool has_initial_template_rows = IsNoneValue(template_row_values);
const bool has_initial_template_columns = IsNoneValue(template_column_values);
const bool has_initial_template_areas =
!template_area_values || IsNoneValue(template_area_values);
CSSValueList* list = CSSValueList::CreateSlashSeparated();
// 1- 'none' case.
if (has_initial_template_areas && has_initial_template_rows &&
has_initial_template_columns) {
list->Append(*template_row_values);
return list;
}
// It is invalid to specify `grid-template-areas` without
// `grid-template-rows`.
if (!has_initial_template_areas && has_initial_template_rows) {
return list;
}
// 2- <grid-template-rows> / <grid-template-columns>
if (!IsA<CSSValueList>(template_row_values) || has_initial_template_areas) {
list->Append(*template_row_values);
list->Append(*template_column_values);
return list;
}
// 3- [ <line-names>? <string> <track-size>? <line-names>? ]+
// [ / <track-list> ]?
//
// "Note that the repeat() function isn’t allowed in these track listings, as
// the tracks are intended to visually line up one-to-one with the
// rows/columns in the “ASCII art”."
//
// https://www.w3.org/TR/css-grid-2/#explicit-grid-shorthand
const CSSValueList* template_row_value_list =
DynamicTo<CSSValueList>(template_row_values);
DCHECK(template_row_value_list);
if (HasGridRepeatValue(template_row_value_list) ||
HasGridRepeatValue(DynamicTo<CSSValueList>(template_column_values))) {
return list;
}
// In this serialization, there must be a value for grid-areas.
const cssvalue::CSSGridTemplateAreasValue* template_areas =
DynamicTo<cssvalue::CSSGridTemplateAreasValue>(template_area_values);
DCHECK(template_areas);
// Handle [ <line-names>? <string> <track-size>? <line-names>? ]+
CSSValueList* template_row_list = CSSValueList::CreateSpaceSeparated();
wtf_size_t row = 0;
for (const auto& row_value : *template_row_value_list) {
if (row_value->IsGridLineNamesValue()) {
template_row_list->Append(*row_value);
continue;
}
String grid_area_text =
SerializeGridAreaText(template_areas, row, /*is_row=*/true);
DCHECK(!grid_area_text.empty());
template_row_list->Append(
*MakeGarbageCollected<CSSStringValue>(grid_area_text));
++row;
// Omit `auto` values.
if (!IsAutoValue(row_value.Get())) {
template_row_list->Append(*row_value);
}
}
// If the actual number of rows serialized via `grid-template-rows` doesn't
// match the rows defined via grid-areas, the shorthand cannot be serialized
// and we must return the empty string.
if (row != template_areas->RowCount()) {
return list;
}
list->Append(*template_row_list);
// Handle [ / <track-list> ]?
if (!has_initial_template_columns) {
list->Append(*template_column_values);
}
return list;
}
// static
CSSValueList* CSSOMUtils::ComputedValueForMasonryShorthand(
const CSSValue* masonry_template_tracks_values,
const CSSValue* template_area_values,
const CSSValue* masonry_direction_values,
const CSSValue* masonry_fill_values) {
const bool has_initial_masonry_template_tracks =
IsNoneValue(masonry_template_tracks_values);
const bool has_initial_template_areas = IsNoneValue(template_area_values);
CSSValueList* list = CSSValueList::CreateSpaceSeparated();
if (has_initial_template_areas && has_initial_masonry_template_tracks) {
list->Append(*template_area_values);
}
if (!has_initial_template_areas) {
// If we have template columns, we can serialize the template areas as is.
// Otherwise, for template rows, we need to serialize multiple string tokens
// into a single space-separated string.
if (IsMasonryColumnDirectionValue(masonry_direction_values)) {
list->Append(*template_area_values);
} else {
const cssvalue::CSSGridTemplateAreasValue* template_areas =
DynamicTo<cssvalue::CSSGridTemplateAreasValue>(template_area_values);
DCHECK(template_areas);
String template_area_text = SerializeGridAreaText(
template_areas, /*fixed_index=*/0, /*is_row=*/false);
list->Append(*MakeGarbageCollected<CSSStringValue>(template_area_text));
}
}
if (!has_initial_masonry_template_tracks) {
list->Append(*masonry_template_tracks_values);
}
list->Append(*masonry_direction_values);
list->Append(*masonry_fill_values);
return list;
}
} // namespace blink
|