/* * 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 ContentData_h #define ContentData_h #include "CounterContent.h" #include "StyleImage.h" #include "RenderPtr.h" #include namespace WebCore { class Document; class RenderObject; class RenderStyle; class ContentData { WTF_MAKE_FAST_ALLOCATED; public: enum Type { CounterDataType, ImageDataType, QuoteDataType, TextDataType }; virtual ~ContentData() { } Type type() const { return m_type; } bool isCounter() const { return type() == CounterDataType; } bool isImage() const { return type() == ImageDataType; } bool isQuote() const { return type() == QuoteDataType; } bool isText() const { return type() == TextDataType; } virtual RenderPtr createContentRenderer(Document&, const RenderStyle&) const = 0; std::unique_ptr clone() const; ContentData* next() const { return m_next.get(); } void setNext(std::unique_ptr next) { m_next = WTFMove(next); } void setAltText(const String& alt) { m_altText = alt; } const String& altText() const { return m_altText; } protected: explicit ContentData(Type type) : m_type(type) { } private: virtual std::unique_ptr cloneInternal() const = 0; std::unique_ptr m_next; String m_altText; Type m_type; }; class ImageContentData final : public ContentData { public: explicit ImageContentData(PassRefPtr image) : ContentData(ImageDataType) , m_image(image) { ASSERT(m_image); } const StyleImage& image() const { return *m_image; } void setImage(PassRefPtr image) { ASSERT(image); m_image = image; } virtual RenderPtr createContentRenderer(Document&, const RenderStyle&) const override; private: virtual std::unique_ptr cloneInternal() const override { std::unique_ptr image = std::make_unique(m_image.get()); image->setAltText(altText()); return image; } RefPtr m_image; }; inline bool operator==(const ImageContentData& a, const ImageContentData& b) { return a.image() == b.image(); } inline bool operator!=(const ImageContentData& a, const ImageContentData& b) { return !(a == b); } class TextContentData final : public ContentData { public: explicit TextContentData(const String& text) : ContentData(TextDataType) , m_text(text) { } const String& text() const { return m_text; } void setText(const String& text) { m_text = text; } virtual RenderPtr createContentRenderer(Document&, const RenderStyle&) const override; private: virtual std::unique_ptr cloneInternal() const override { return std::make_unique(text()); } String m_text; }; inline bool operator==(const TextContentData& a, const TextContentData& b) { return a.text() == b.text(); } inline bool operator!=(const TextContentData& a, const TextContentData& b) { return !(a == b); } class CounterContentData final : public ContentData { public: explicit CounterContentData(std::unique_ptr counter) : ContentData(CounterDataType) , m_counter(WTFMove(counter)) { ASSERT(m_counter); } const CounterContent& counter() const { return *m_counter; } void setCounter(std::unique_ptr counter) { ASSERT(counter); m_counter = WTFMove(counter); } virtual RenderPtr createContentRenderer(Document&, const RenderStyle&) const override; private: virtual std::unique_ptr cloneInternal() const override { auto counterData = std::make_unique(counter()); return std::make_unique(WTFMove(counterData)); } std::unique_ptr m_counter; }; inline bool operator==(const CounterContentData& a, const CounterContentData& b) { return a.counter() == b.counter(); } inline bool operator!=(const CounterContentData& a, const CounterContentData& b) { return !(a == b); } class QuoteContentData final : public ContentData { public: explicit QuoteContentData(QuoteType quote) : ContentData(QuoteDataType) , m_quote(quote) { } QuoteType quote() const { return m_quote; } void setQuote(QuoteType quote) { m_quote = quote; } virtual RenderPtr createContentRenderer(Document&, const RenderStyle&) const override; private: virtual std::unique_ptr cloneInternal() const override { return std::make_unique(quote()); } QuoteType m_quote; }; inline bool operator==(const QuoteContentData& a, const QuoteContentData& b) { return a.quote() == b.quote(); } inline bool operator!=(const QuoteContentData& a, const QuoteContentData& b) { return !(a == b); } inline bool operator==(const ContentData& a, const ContentData& b) { if (a.type() != b.type()) return false; switch (a.type()) { case ContentData::CounterDataType: return downcast(a) == downcast(b); case ContentData::ImageDataType: return downcast(a) == downcast(b); case ContentData::QuoteDataType: return downcast(a) == downcast(b); case ContentData::TextDataType: return downcast(a) == downcast(b); } ASSERT_NOT_REACHED(); return false; } inline bool operator!=(const ContentData& a, const ContentData& b) { return !(a == b); } } // namespace WebCore #define SPECIALIZE_TYPE_TRAITS_CONTENT_DATA(ToClassName, ContentDataName) \ SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::ToClassName) \ static bool isType(const WebCore::ContentData& contentData) { return contentData.is##ContentDataName(); } \ SPECIALIZE_TYPE_TRAITS_END() SPECIALIZE_TYPE_TRAITS_CONTENT_DATA(ImageContentData, Image) SPECIALIZE_TYPE_TRAITS_CONTENT_DATA(TextContentData, Text) SPECIALIZE_TYPE_TRAITS_CONTENT_DATA(CounterContentData, Counter) SPECIALIZE_TYPE_TRAITS_CONTENT_DATA(QuoteContentData, Quote) #endif // ContentData_h