This source file includes following definitions.
- DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED
- createOffMainThread
- set
- add
- remove
- addMediaQuery
- mediaText
- trace
- m_parentRule
- m_parentRule
- setMediaText
- item
- deleteMedium
- appendMedium
- reattach
- trace
- addResolutionWarningMessageToConsole
- isResolutionMediaFeature
- reportMediaQueryWarningIfNeeded
#include "config.h"
#include "core/css/MediaList.h"
#include "MediaFeatureNames.h"
#include "bindings/v8/ExceptionState.h"
#include "core/css/parser/BisonCSSParser.h"
#include "core/css/CSSStyleSheet.h"
#include "core/css/MediaQuery.h"
#include "core/css/MediaQueryExp.h"
#include "core/css/parser/MediaQueryParser.h"
#include "core/dom/Document.h"
#include "core/frame/DOMWindow.h"
#include "wtf/text/StringBuilder.h"
namespace WebCore {
MediaQuerySet::MediaQuerySet()
{
}
MediaQuerySet::MediaQuerySet(const MediaQuerySet& o)
: m_queries(o.m_queries.size())
{
for (unsigned i = 0; i < m_queries.size(); ++i)
m_queries[i] = o.m_queries[i]->copy();
}
DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(MediaQuerySet)
PassRefPtrWillBeRawPtr<MediaQuerySet> MediaQuerySet::create(const String& mediaString)
{
if (mediaString.isEmpty())
return MediaQuerySet::create();
if (RuntimeEnabledFeatures::mediaQueryParserEnabled())
return MediaQueryParser::parse(mediaString);
BisonCSSParser parser(strictCSSParserContext());
return parser.parseMediaQueryList(mediaString);
}
PassRefPtrWillBeRawPtr<MediaQuerySet> MediaQuerySet::createOffMainThread(const String& mediaString)
{
if (mediaString.isEmpty())
return MediaQuerySet::create();
return MediaQueryParser::parse(mediaString);
}
bool MediaQuerySet::set(const String& mediaString)
{
RefPtrWillBeRawPtr<MediaQuerySet> result = create(mediaString);
m_queries.swap(result->m_queries);
return true;
}
bool MediaQuerySet::add(const String& queryString)
{
RefPtrWillBeRawPtr<MediaQuerySet> result = create(queryString);
if (result->m_queries.size() != 1)
return true;
OwnPtrWillBeRawPtr<MediaQuery> newQuery = result->m_queries[0].release();
ASSERT(newQuery);
for (size_t i = 0; i < m_queries.size(); ++i) {
MediaQuery* query = m_queries[i].get();
if (*query == *newQuery)
return true;
}
m_queries.append(newQuery.release());
return true;
}
bool MediaQuerySet::remove(const String& queryStringToRemove)
{
RefPtrWillBeRawPtr<MediaQuerySet> result = create(queryStringToRemove);
if (result->m_queries.size() != 1)
return true;
OwnPtrWillBeRawPtr<MediaQuery> newQuery = result->m_queries[0].release();
ASSERT(newQuery);
bool found = false;
for (size_t i = 0; i < m_queries.size(); ++i) {
MediaQuery* query = m_queries[i].get();
if (*query == *newQuery) {
m_queries.remove(i);
--i;
found = true;
}
}
return found;
}
void MediaQuerySet::addMediaQuery(PassOwnPtrWillBeRawPtr<MediaQuery> mediaQuery)
{
m_queries.append(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();
}
void MediaQuerySet::trace(Visitor* visitor)
{
#if ENABLE(OILPAN)
visitor->trace(m_queries);
#endif
}
MediaList::MediaList(MediaQuerySet* mediaQueries, CSSStyleSheet* parentSheet)
: m_mediaQueries(mediaQueries)
, m_parentStyleSheet(parentSheet)
, m_parentRule(nullptr)
{
}
MediaList::MediaList(MediaQuerySet* mediaQueries, CSSRule* parentRule)
: m_mediaQueries(mediaQueries)
, m_parentStyleSheet(nullptr)
, m_parentRule(parentRule)
{
}
MediaList::~MediaList()
{
}
void MediaList::setMediaText(const String& value)
{
CSSStyleSheet::RuleMutationScope mutationScope(m_parentRule);
m_mediaQueries->set(value);
if (m_parentStyleSheet)
m_parentStyleSheet->didMutate();
}
String MediaList::item(unsigned index) const
{
const WillBeHeapVector<OwnPtrWillBeMember<MediaQuery> >& queries = m_mediaQueries->queryVector();
if (index < queries.size())
return queries[index]->cssText();
return String();
}
void MediaList::deleteMedium(const String& medium, ExceptionState& exceptionState)
{
CSSStyleSheet::RuleMutationScope mutationScope(m_parentRule);
bool success = m_mediaQueries->remove(medium);
if (!success) {
exceptionState.throwDOMException(NotFoundError, "Failed to delete '" + medium + "'.");
return;
}
if (m_parentStyleSheet)
m_parentStyleSheet->didMutate();
}
void MediaList::appendMedium(const String& medium, ExceptionState& exceptionState)
{
CSSStyleSheet::RuleMutationScope mutationScope(m_parentRule);
bool success = m_mediaQueries->add(medium);
if (!success) {
exceptionState.throwDOMException(InvalidCharacterError, "The value provided ('" + medium + "') is not a valid medium.");
return;
}
if (m_parentStyleSheet)
m_parentStyleSheet->didMutate();
}
void MediaList::reattach(MediaQuerySet* mediaQueries)
{
ASSERT(mediaQueries);
m_mediaQueries = mediaQueries;
}
void MediaList::trace(Visitor* visitor)
{
visitor->trace(m_mediaQueries);
visitor->trace(m_parentStyleSheet);
visitor->trace(m_parentRule);
}
static void addResolutionWarningMessageToConsole(Document* document, const String& serializedExpression, const CSSPrimitiveValue* value)
{
ASSERT(document);
ASSERT(value);
DEFINE_STATIC_LOCAL(String, mediaQueryMessage, ("Consider using 'dppx' units, 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: "));
DEFINE_STATIC_LOCAL(String, mediaValueDPI, ("dpi"));
DEFINE_STATIC_LOCAL(String, mediaValueDPCM, ("dpcm"));
DEFINE_STATIC_LOCAL(String, lengthUnitInch, ("inch"));
DEFINE_STATIC_LOCAL(String, lengthUnitCentimeter, ("centimeter"));
StringBuilder message;
if (value->isDotsPerInch())
message.append(String(mediaQueryMessage).replace("%replacementUnits%", mediaValueDPI).replace("%lengthUnit%", lengthUnitInch));
else if (value->isDotsPerCentimeter())
message.append(String(mediaQueryMessage).replace("%replacementUnits%", mediaValueDPCM).replace("%lengthUnit%", lengthUnitCentimeter));
else
ASSERT_NOT_REACHED();
message.append(serializedExpression);
document->addConsoleMessage(CSSMessageSource, DebugMessageLevel, message.toString());
}
static inline bool isResolutionMediaFeature(const String& mediaFeature)
{
return mediaFeature == MediaFeatureNames::resolutionMediaFeature
|| mediaFeature == MediaFeatureNames::maxResolutionMediaFeature
|| mediaFeature == MediaFeatureNames::minResolutionMediaFeature;
}
void reportMediaQueryWarningIfNeeded(Document* document, const MediaQuerySet* mediaQuerySet)
{
if (!mediaQuerySet || !document)
return;
const WillBeHeapVector<OwnPtrWillBeMember<MediaQuery> >& mediaQueries = mediaQuerySet->queryVector();
const size_t queryCount = mediaQueries.size();
if (!queryCount)
return;
CSSPrimitiveValue* suspiciousValue = 0;
bool dotsPerPixelUsed = false;
for (size_t i = 0; i < queryCount; ++i) {
const MediaQuery* query = mediaQueries[i].get();
if (equalIgnoringCase(query->mediaType(), "print"))
continue;
const ExpressionHeapVector& expressions = query->expressions();
for (size_t j = 0; j < expressions.size(); ++j) {
const MediaQueryExp* expression = expressions.at(j).get();
if (isResolutionMediaFeature(expression->mediaFeature())) {
CSSValue* cssValue = expression->value();
if (cssValue && cssValue->isPrimitiveValue()) {
CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(cssValue);
if (primitiveValue->isDotsPerPixel())
dotsPerPixelUsed = true;
else if (primitiveValue->isDotsPerInch() || primitiveValue->isDotsPerCentimeter())
suspiciousValue = primitiveValue;
}
}
}
}
if (suspiciousValue && !dotsPerPixelUsed)
addResolutionWarningMessageToConsole(document, mediaQuerySet->mediaText(), suspiciousValue);
}
}