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
|
/*
* Copyright (C) 2000 Lars Knoll (knoll@kde.org)
* (C) 2000 Antti Koivisto (koivisto@kde.org)
* (C) 2000 Dirk Mueller (mueller@kde.org)
* Copyright (C) 2003, 2005, 2006, 2007, 2008, 2010 Apple Inc. All rights
* reserved.
* Copyright (C) 2006 Graham Dennis (graham.dennis@gmail.com)
*
* 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.
*
*/
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_STYLE_CONTENT_DATA_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_STYLE_CONTENT_DATA_H_
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/css/css_value.h"
#include "third_party/blink/renderer/core/style/computed_style_constants.h"
#include "third_party/blink/renderer/core/style/style_image.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/member.h"
#include "third_party/blink/renderer/platform/wtf/casting.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
#include "third_party/blink/renderer/platform/wtf/text/strcat.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
namespace blink {
class LayoutObject;
class TreeScope;
class StyleEngine;
class CountersAttachmentContext;
class ContentData : public GarbageCollected<ContentData> {
public:
virtual ~ContentData() = default;
virtual bool IsCounter() const { return false; }
virtual bool IsAltCounter() const { return false; }
virtual bool IsImage() const { return false; }
virtual bool IsQuote() const { return false; }
virtual bool IsText() const { return false; }
virtual bool IsAltText() const { return false; }
virtual bool IsNone() const { return false; }
virtual bool IsAlt() const { return IsAltText() || IsAltCounter(); }
CORE_EXPORT static String ConcatenateAltText(
const ContentData& first_alt_data);
// Create a layout object for this piece of content. `owner` is the layout
// object that has the content property, e.g. a pseudo-element, or an @page
// margin box.
virtual LayoutObject* CreateLayoutObject(LayoutObject& owner) const = 0;
virtual ContentData* Clone() const;
ContentData* Next() const { return next_.Get(); }
void SetNext(ContentData* next) {
DCHECK(!IsNone());
DCHECK(!next || !next->IsNone());
next_ = next;
}
virtual bool Equals(const ContentData&) const = 0;
virtual void Trace(Visitor*) const;
// For debugging/logging only.
virtual String DebugString() const { return "<unknown>"; }
private:
virtual ContentData* CloneInternal() const = 0;
Member<ContentData> next_;
friend std::ostream& operator<<(std::ostream& stream,
const ContentData& content_data);
};
inline std::ostream& operator<<(std::ostream& stream,
const ContentData& content_data) {
const ContentData* ptr = &content_data;
stream << "ContentData{";
while (ptr) {
stream << content_data.DebugString();
stream << ",";
ptr = ptr->next_.Get();
}
return stream << "}";
}
class ImageContentData final : public ContentData {
public:
explicit ImageContentData(StyleImage* image) : image_(image) {
DCHECK(image_);
}
const StyleImage* GetImage() const { return image_.Get(); }
StyleImage* GetImage() { return image_.Get(); }
void SetImage(StyleImage* image) {
DCHECK(image);
image_ = image;
}
bool IsImage() const override { return true; }
LayoutObject* CreateLayoutObject(LayoutObject& owner) const override;
bool Equals(const ContentData& data) const override {
if (!data.IsImage()) {
return false;
}
return *static_cast<const ImageContentData&>(data).GetImage() ==
*GetImage();
}
void Trace(Visitor*) const override;
String DebugString() const override {
StringBuilder str;
str.Append("<image: ");
if (image_->IsImageResource()) {
str.Append("[is_resource]");
}
if (image_->IsPendingImage()) {
str.Append("[pending]");
}
if (image_->IsGeneratedImage()) {
str.Append("[generated]");
}
if (image_->IsContentful()) {
str.Append("[contentful]");
}
if (image_->IsImageResourceSet()) {
str.Append("[resourceset]");
}
if (image_->IsPaintImage()) {
str.Append("[paint]");
}
if (image_->IsCrossfadeImage()) {
str.Append("[crossfade]");
}
str.Append(image_->CssValue()->CssText());
str.Append(">");
return str.ReleaseString();
}
private:
ContentData* CloneInternal() const override {
StyleImage* image = const_cast<StyleImage*>(GetImage());
return MakeGarbageCollected<ImageContentData>(image);
}
Member<StyleImage> image_;
};
template <>
struct DowncastTraits<ImageContentData> {
static bool AllowFrom(const ContentData& content) {
return content.IsImage();
}
};
class TextContentData final : public ContentData {
public:
explicit TextContentData(const String& text) : text_(text) {}
const String& GetText() const { return text_; }
void SetText(const String& text) { text_ = text; }
bool IsText() const override { return true; }
LayoutObject* CreateLayoutObject(LayoutObject& owner) const override;
bool Equals(const ContentData& data) const override {
if (!data.IsText()) {
return false;
}
return static_cast<const TextContentData&>(data).GetText() == GetText();
}
String DebugString() const override { return text_; }
private:
ContentData* CloneInternal() const override {
return MakeGarbageCollected<TextContentData>(GetText());
}
String text_;
};
template <>
struct DowncastTraits<TextContentData> {
static bool AllowFrom(const ContentData& content) { return content.IsText(); }
};
class AltTextContentData final : public ContentData {
public:
explicit AltTextContentData(const String& text) : text_(text) {}
String GetText() const { return text_; }
void SetText(const String& text) { text_ = text; }
bool IsAltText() const override { return true; }
LayoutObject* CreateLayoutObject(LayoutObject& owner) const override;
bool Equals(const ContentData& data) const override {
if (!data.IsAltText()) {
return false;
}
return static_cast<const AltTextContentData&>(data).GetText() == GetText();
}
String DebugString() const override { return StrCat({"<alt: ", text_, ">"}); }
private:
ContentData* CloneInternal() const override {
return MakeGarbageCollected<AltTextContentData>(GetText());
}
String text_;
};
template <>
struct DowncastTraits<AltTextContentData> {
static bool AllowFrom(const ContentData& content) {
return content.IsAltText();
}
};
struct CounterData {
DISALLOW_NEW();
public:
CounterData(const AtomicString& identifier,
const AtomicString& style,
const AtomicString& separator,
const TreeScope* tree_scope)
: identifier(identifier),
list_style(style),
separator(separator),
tree_scope(tree_scope) {}
void Trace(Visitor* v) const { v->Trace(tree_scope); }
AtomicString identifier;
AtomicString list_style;
AtomicString separator;
Member<const TreeScope> tree_scope;
};
class CounterContentData : public ContentData {
friend class ContentData;
public:
CounterContentData(const AtomicString& identifier,
const AtomicString& style,
const AtomicString& separator,
const TreeScope* tree_scope)
: counter_data_(identifier, style, separator, tree_scope) {}
explicit CounterContentData(CounterData counter_data)
: counter_data_(std::move(counter_data)) {}
bool IsCounter() const override { return true; }
LayoutObject* CreateLayoutObject(LayoutObject& owner) const override;
const AtomicString& Identifier() const { return counter_data_.identifier; }
const AtomicString& ListStyle() const { return counter_data_.list_style; }
const AtomicString& Separator() const { return counter_data_.separator; }
const TreeScope* GetTreeScope() const { return counter_data_.tree_scope; }
void Trace(Visitor*) const override;
String DebugString() const override { return "<counter>"; }
private:
ContentData* CloneInternal() const override {
return MakeGarbageCollected<CounterContentData>(counter_data_);
}
protected:
bool Equals(const ContentData& data) const override {
if (!data.IsCounter()) {
return false;
}
const CounterContentData& other =
static_cast<const CounterContentData&>(data);
return Identifier() == other.Identifier() &&
ListStyle() == other.ListStyle() &&
Separator() == other.Separator() &&
GetTreeScope() == other.GetTreeScope();
}
CounterData counter_data_;
};
template <>
struct DowncastTraits<CounterContentData> {
static bool AllowFrom(const ContentData& content) {
return content.IsCounter();
}
};
class AltCounterContentData : public CounterContentData {
using CounterContentData::CounterContentData;
public:
bool IsAltCounter() const override { return true; }
LayoutObject* CreateLayoutObject(LayoutObject& owner) const override;
const String& GetText() const { return counter_value_text_; }
void UpdateText(CountersAttachmentContext& context,
const StyleEngine& style_engine,
const LayoutObject& content_generating_object);
String DebugString() const override { return "<alt-counter>"; }
private:
void SetText(const String& text) { counter_value_text_ = text; }
ContentData* CloneInternal() const override {
auto* data = MakeGarbageCollected<AltCounterContentData>(counter_data_);
data->SetText(GetText());
return data;
}
bool Equals(const ContentData& data) const override {
if (!data.IsAltCounter()) {
return false;
}
const AltCounterContentData& other =
static_cast<const AltCounterContentData&>(data);
return CounterContentData::Equals(other) && GetText() == other.GetText();
}
// Text value of counter() or counters() to be used in ax object.
String counter_value_text_;
};
template <>
struct DowncastTraits<AltCounterContentData> {
static bool AllowFrom(const ContentData& content) {
return content.IsAltCounter();
}
};
class QuoteContentData final : public ContentData {
public:
explicit QuoteContentData(QuoteType quote) : quote_(quote) {}
QuoteType Quote() const { return quote_; }
void SetQuote(QuoteType quote) { quote_ = quote; }
bool IsQuote() const override { return true; }
LayoutObject* CreateLayoutObject(LayoutObject& owner) const override;
bool Equals(const ContentData& data) const override {
if (!data.IsQuote()) {
return false;
}
return static_cast<const QuoteContentData&>(data).Quote() == Quote();
}
String DebugString() const override { return "<quote>"; }
private:
ContentData* CloneInternal() const override {
return MakeGarbageCollected<QuoteContentData>(Quote());
}
QuoteType quote_;
};
template <>
struct DowncastTraits<QuoteContentData> {
static bool AllowFrom(const ContentData& content) {
return content.IsQuote();
}
};
class NoneContentData final : public ContentData {
public:
explicit NoneContentData() {}
bool IsNone() const override { return true; }
LayoutObject* CreateLayoutObject(LayoutObject& owner) const override;
bool Equals(const ContentData& data) const override { return data.IsNone(); }
String DebugString() const override { return "<none>"; }
private:
ContentData* CloneInternal() const override {
return MakeGarbageCollected<NoneContentData>();
}
};
template <>
struct DowncastTraits<NoneContentData> {
static bool AllowFrom(const ContentData& content) { return content.IsNone(); }
};
inline bool operator==(const ContentData& a, const ContentData& b) {
const ContentData* ptr_a = &a;
const ContentData* ptr_b = &b;
while (ptr_a && ptr_b && ptr_a->Equals(*ptr_b)) {
ptr_a = ptr_a->Next();
ptr_b = ptr_b->Next();
}
return !ptr_a && !ptr_b;
}
// In order for an image to be rendered from the content property on an actual
// element, there can be at most one piece of image content data, followed by
// some optional alternative text.
inline bool ShouldUseContentDataForElement(const ContentData* content_data) {
if (!content_data) {
return false;
}
if (!content_data->IsImage()) {
return false;
}
if (content_data->Next() && !content_data->Next()->IsAlt()) {
return false;
}
return true;
}
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_STYLE_CONTENT_DATA_H_
|