#include "config.h"
#include "MediaQuery.h"
#include "MediaQueryExp.h"
#include <wtf/text/StringBuilder.h>
namespace WebCore {
String MediaQuery::serialize() const
{
StringBuilder result;
if (!m_ignored) {
switch (m_restrictor) {
case MediaQuery::Only:
result.appendLiteral("only ");
break;
case MediaQuery::Not:
result.appendLiteral("not ");
break;
case MediaQuery::None:
break;
}
if (m_expressions->isEmpty()) {
result.append(m_mediaType);
return result.toString();
}
if (m_mediaType != "all" || m_restrictor != None) {
result.append(m_mediaType);
result.appendLiteral(" and ");
}
result.append(m_expressions->at(0)->serialize());
for (size_t i = 1; i < m_expressions->size(); ++i) {
result.appendLiteral(" and ");
result.append(m_expressions->at(i)->serialize());
}
} else {
result.appendLiteral("not all");
}
return result.toString();
}
MediaQuery::MediaQuery(Restrictor r, const String& mediaType, std::unique_ptr<ExpressionVector> exprs)
: m_restrictor(r)
, m_mediaType(mediaType.lower())
, m_expressions(WTF::move(exprs))
, m_ignored(false)
{
if (!m_expressions) {
m_expressions = std::make_unique<ExpressionVector>();
return;
}
std::sort(m_expressions->begin(), m_expressions->end(), [](const std::unique_ptr<MediaQueryExp>& a, const std::unique_ptr<MediaQueryExp>& b) {
return codePointCompare(a->serialize(), b->serialize()) < 0;
});
String key;
for (int i = m_expressions->size() - 1; i >= 0; --i) {
if (!m_ignored)
m_ignored = !m_expressions->at(i)->isValid();
if (m_expressions->at(i)->serialize() == key)
m_expressions->remove(i);
else
key = m_expressions->at(i)->serialize();
}
}
MediaQuery::MediaQuery(const MediaQuery& o)
: m_restrictor(o.m_restrictor)
, m_mediaType(o.m_mediaType)
, m_expressions(std::make_unique<ExpressionVector>(o.m_expressions->size()))
, m_ignored(o.m_ignored)
, m_serializationCache(o.m_serializationCache)
{
for (unsigned i = 0; i < m_expressions->size(); ++i)
(*m_expressions)[i] = std::make_unique<MediaQueryExp>(*o.m_expressions->at(i));
}
MediaQuery::~MediaQuery()
{
}
bool MediaQuery::operator==(const MediaQuery& other) const
{
return cssText() == other.cssText();
}
String MediaQuery::cssText() const
{
if (m_serializationCache.isNull())
const_cast<MediaQuery*>(this)->m_serializationCache = serialize();
return m_serializationCache;
}
}