#include "config.h"
#include "MediaList.h"
#include "CSSImportRule.h"
#include "CSSParser.h"
#include "CSSStyleSheet.h"
#include "DOMWindow.h"
#include "Document.h"
#include "ExceptionCode.h"
#include "MediaFeatureNames.h"
#include "MediaQuery.h"
#include "MediaQueryExp.h"
#include "ScriptableDocumentParser.h"
#include <wtf/text/StringBuilder.h>
namespace WebCore {
MediaQuerySet::MediaQuerySet()
: m_fallbackToDescriptor(false)
, m_lastLine(0)
{
}
MediaQuerySet::MediaQuerySet(const String& mediaString, bool fallbackToDescriptor)
: m_fallbackToDescriptor(fallbackToDescriptor)
, m_lastLine(0)
{
bool success = parse(mediaString);
if (!success)
parse("invalid");
}
MediaQuerySet::MediaQuerySet(const MediaQuerySet& o)
: RefCounted<MediaQuerySet>()
, m_fallbackToDescriptor(o.m_fallbackToDescriptor)
, m_lastLine(o.m_lastLine)
, m_queries(o.m_queries.size())
{
for (unsigned i = 0; i < m_queries.size(); ++i)
m_queries[i] = o.m_queries[i]->copy();
}
MediaQuerySet::~MediaQuerySet()
{
}
static String parseMediaDescriptor(const String& string)
{
unsigned length = string.length();
unsigned i = 0;
for (; i < length; ++i) {
unsigned short c = string[i];
if (! ((c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| (c >= '1' && c <= '9')
|| (c == '-')))
break;
}
return string.left(i);
}
bool MediaQuerySet::parse(const String& mediaString)
{
CSSParser parser(CSSStrictMode);
Vector<std::unique_ptr<MediaQuery>> result;
Vector<String> list;
mediaString.split(',', list);
for (unsigned i = 0; i < list.size(); ++i) {
String medium = list[i].stripWhiteSpace();
if (medium.isEmpty()) {
if (!m_fallbackToDescriptor)
return false;
continue;
}
std::unique_ptr<MediaQuery> mediaQuery = parser.parseMediaQuery(medium);
if (!mediaQuery) {
if (!m_fallbackToDescriptor)
return false;
String mediaDescriptor = parseMediaDescriptor(medium);
if (mediaDescriptor.isNull())
continue;
mediaQuery = std::make_unique<MediaQuery>(MediaQuery::None, mediaDescriptor, nullptr);
}
result.append(WTF::move(mediaQuery));
}
if (!m_fallbackToDescriptor && list.isEmpty()) {
String strippedMediaString = mediaString.stripWhiteSpace();
if (!strippedMediaString.isEmpty())
return false;
}
m_queries = WTF::move(result);
return true;
}
bool MediaQuerySet::add(const String& queryString)
{
CSSParser parser(CSSStrictMode);
std::unique_ptr<MediaQuery> parsedQuery = parser.parseMediaQuery(queryString);
if (!parsedQuery && m_fallbackToDescriptor) {
String medium = parseMediaDescriptor(queryString);
if (!medium.isNull())
parsedQuery = std::make_unique<MediaQuery>(MediaQuery::None, medium, nullptr);
}
if (!parsedQuery)
return false;
m_queries.append(WTF::move(parsedQuery));
return true;
}
bool MediaQuerySet::remove(const String& queryStringToRemove)
{
CSSParser parser(CSSStrictMode);
std::unique_ptr<MediaQuery> parsedQuery = parser.parseMediaQuery(queryStringToRemove);
if (!parsedQuery && m_fallbackToDescriptor) {
String medium = parseMediaDescriptor(queryStringToRemove);
if (!medium.isNull())
parsedQuery = std::make_unique<MediaQuery>(MediaQuery::None, medium, nullptr);
}
if (!parsedQuery)
return false;
for (size_t i = 0; i < m_queries.size(); ++i) {
MediaQuery* query = m_queries[i].get();
if (*query == *parsedQuery) {
m_queries.remove(i);
return true;
}
}
return false;
}
void MediaQuerySet::addMediaQuery(std::unique_ptr<MediaQuery> mediaQuery)
{
m_queries.append(WTF::move(mediaQuery));
}
String MediaQuerySet::mediaText() const
{
StringBuilder text;
bool first = true;
for (size_t i = 0; i < m_queries.size(); ++i) {
if (!first)
text.appendLiteral(", ");
else
first = false;
text.append(m_queries[i]->cssText());
}
return text.toString();
}
MediaList::MediaList(MediaQuerySet* mediaQueries, CSSStyleSheet* parentSheet)
: m_mediaQueries(mediaQueries)
, m_parentStyleSheet(parentSheet)
, m_parentRule(0)
{
}
MediaList::MediaList(MediaQuerySet* mediaQueries, CSSRule* parentRule)
: m_mediaQueries(mediaQueries)
, m_parentStyleSheet(0)
, m_parentRule(parentRule)
{
}
MediaList::~MediaList()
{
}
void MediaList::setMediaText(const String& value, ExceptionCode& ec)
{
CSSStyleSheet::RuleMutationScope mutationScope(m_parentRule);
bool success = m_mediaQueries->parse(value);
if (!success) {
ec = SYNTAX_ERR;
return;
}
if (m_parentStyleSheet)
m_parentStyleSheet->didMutate();
}
String MediaList::item(unsigned index) const
{
auto& queries = m_mediaQueries->queryVector();
if (index < queries.size())
return queries[index]->cssText();
return String();
}
void MediaList::deleteMedium(const String& medium, ExceptionCode& ec)
{
CSSStyleSheet::RuleMutationScope mutationScope(m_parentRule);
bool success = m_mediaQueries->remove(medium);
if (!success) {
ec = NOT_FOUND_ERR;
return;
}
if (m_parentStyleSheet)
m_parentStyleSheet->didMutate();
}
void MediaList::appendMedium(const String& medium, ExceptionCode& ec)
{
CSSStyleSheet::RuleMutationScope mutationScope(m_parentRule);
bool success = m_mediaQueries->add(medium);
if (!success) {
ec = INVALID_CHARACTER_ERR;
return;
}
if (m_parentStyleSheet)
m_parentStyleSheet->didMutate();
}
void MediaList::reattach(MediaQuerySet* mediaQueries)
{
ASSERT(mediaQueries);
m_mediaQueries = mediaQueries;
}
#if ENABLE(RESOLUTION_MEDIA_QUERY)
static void addResolutionWarningMessageToConsole(Document* document, const String& serializedExpression, const CSSPrimitiveValue* value)
{
ASSERT(document);
ASSERT(value);
DEPRECATED_DEFINE_STATIC_LOCAL(String, mediaQueryMessage, (ASCIILiteral("Consider using 'dppx' units instead of '%replacementUnits%', as in CSS '%replacementUnits%' means dots-per-CSS-%lengthUnit%, not dots-per-physical-%lengthUnit%, so does not correspond to the actual '%replacementUnits%' of a screen. In media query expression: ")));
DEPRECATED_DEFINE_STATIC_LOCAL(String, mediaValueDPI, (ASCIILiteral("dpi")));
DEPRECATED_DEFINE_STATIC_LOCAL(String, mediaValueDPCM, (ASCIILiteral("dpcm")));
DEPRECATED_DEFINE_STATIC_LOCAL(String, lengthUnitInch, (ASCIILiteral("inch")));
DEPRECATED_DEFINE_STATIC_LOCAL(String, lengthUnitCentimeter, (ASCIILiteral("centimeter")));
String message;
if (value->isDotsPerInch())
message = String(mediaQueryMessage).replace("%replacementUnits%", mediaValueDPI).replace("%lengthUnit%", lengthUnitInch);
else if (value->isDotsPerCentimeter())
message = String(mediaQueryMessage).replace("%replacementUnits%", mediaValueDPCM).replace("%lengthUnit%", lengthUnitCentimeter);
else
ASSERT_NOT_REACHED();
message.append(serializedExpression);
document->addConsoleMessage(MessageSource::CSS, MessageLevel::Debug, message);
}
void reportMediaQueryWarningIfNeeded(Document* document, const MediaQuerySet* mediaQuerySet)
{
if (!mediaQuerySet || !document)
return;
auto& mediaQueries = mediaQuerySet->queryVector();
const size_t queryCount = mediaQueries.size();
if (!queryCount)
return;
for (size_t i = 0; i < queryCount; ++i) {
const MediaQuery* query = mediaQueries[i].get();
String mediaType = query->mediaType();
if (!query->ignored() && !equalIgnoringCase(mediaType, "print")) {
auto& expressions = query->expressions();
for (size_t j = 0; j < expressions.size(); ++j) {
const MediaQueryExp* exp = expressions.at(j).get();
if (exp->mediaFeature() == MediaFeatureNames::resolutionMediaFeature || exp->mediaFeature() == MediaFeatureNames::max_resolutionMediaFeature || exp->mediaFeature() == MediaFeatureNames::min_resolutionMediaFeature) {
CSSValue* cssValue = exp->value();
if (cssValue && cssValue->isPrimitiveValue()) {
CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(cssValue);
if (primitiveValue->isDotsPerInch() || primitiveValue->isDotsPerCentimeter())
addResolutionWarningMessageToConsole(document, mediaQuerySet->mediaText(), primitiveValue);
}
}
}
}
}
}
#endif
}