DocumentMarker.cpp [plain text]
#include "config.h"
#include "DocumentMarker.h"
namespace WebCore {
DocumentMarkerDetails::~DocumentMarkerDetails()
{
}
class DocumentMarkerDescription : public DocumentMarkerDetails {
public:
static Ref<DocumentMarkerDescription> create(const String&);
const String& description() const { return m_description; }
virtual bool isDescription() const override { return true; }
private:
DocumentMarkerDescription(const String& description)
: m_description(description)
{
}
String m_description;
};
Ref<DocumentMarkerDescription> DocumentMarkerDescription::create(const String& description)
{
return adoptRef(*new DocumentMarkerDescription(description));
}
inline DocumentMarkerDescription* toDocumentMarkerDescription(DocumentMarkerDetails* details)
{
if (details && details->isDescription())
return static_cast<DocumentMarkerDescription*>(details);
return 0;
}
class DocumentMarkerTextMatch : public DocumentMarkerDetails {
public:
static PassRefPtr<DocumentMarkerTextMatch> instanceFor(bool);
bool activeMatch() const { return m_match; }
virtual bool isTextMatch() const override { return true; }
private:
explicit DocumentMarkerTextMatch(bool match)
: m_match(match)
{
}
bool m_match;
};
PassRefPtr<DocumentMarkerTextMatch> DocumentMarkerTextMatch::instanceFor(bool match)
{
static DocumentMarkerTextMatch* trueInstance = adoptRef(new DocumentMarkerTextMatch(true)).leakRef();
static DocumentMarkerTextMatch* falseInstance = adoptRef(new DocumentMarkerTextMatch(false)).leakRef();
return match ? trueInstance : falseInstance;
}
inline DocumentMarkerTextMatch* toDocumentMarkerTextMatch(DocumentMarkerDetails* details)
{
if (details && details->isTextMatch())
return static_cast<DocumentMarkerTextMatch*>(details);
return 0;
}
DocumentMarker::DocumentMarker()
: m_type(Spelling), m_startOffset(0), m_endOffset(0)
{
}
DocumentMarker::DocumentMarker(MarkerType type, unsigned startOffset, unsigned endOffset)
: m_type(type), m_startOffset(startOffset), m_endOffset(endOffset)
{
}
DocumentMarker::DocumentMarker(MarkerType type, unsigned startOffset, unsigned endOffset, const String& description)
: m_type(type)
, m_startOffset(startOffset)
, m_endOffset(endOffset)
, m_details(DocumentMarkerDescription::create(description))
{
ASSERT(m_details);
}
DocumentMarker::DocumentMarker(unsigned startOffset, unsigned endOffset, bool activeMatch)
: m_type(DocumentMarker::TextMatch)
, m_startOffset(startOffset)
, m_endOffset(endOffset)
, m_details(DocumentMarkerTextMatch::instanceFor(activeMatch))
{
ASSERT(m_details);
}
DocumentMarker::DocumentMarker(MarkerType type, unsigned startOffset, unsigned endOffset, PassRefPtr<DocumentMarkerDetails> details)
: m_type(type)
, m_startOffset(startOffset)
, m_endOffset(endOffset)
, m_details(details)
{
ASSERT(m_details);
}
void DocumentMarker::shiftOffsets(int delta)
{
m_startOffset += delta;
m_endOffset += delta;
}
void DocumentMarker::setActiveMatch(bool active)
{
m_details = DocumentMarkerTextMatch::instanceFor(active);
}
const String& DocumentMarker::description() const
{
if (DocumentMarkerDescription* details = toDocumentMarkerDescription(m_details.get()))
return details->description();
return emptyString();
}
bool DocumentMarker::activeMatch() const
{
if (DocumentMarkerTextMatch* details = toDocumentMarkerTextMatch(m_details.get()))
return details->activeMatch();
return false;
}
}