mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Remove sizes attr.
R=ojan@chromium.org Review URL: https://codereview.chromium.org/732813002
This commit is contained in:
parent
73365743da
commit
9ea3c3648b
@ -299,8 +299,6 @@ sky_core_files = [
|
||||
"css/parser/MediaQueryToken.h",
|
||||
"css/parser/MediaQueryTokenizer.cpp",
|
||||
"css/parser/MediaQueryTokenizer.h",
|
||||
"css/parser/SizesAttributeParser.cpp",
|
||||
"css/parser/SizesAttributeParser.h",
|
||||
"css/parser/SizesCalcParser.cpp",
|
||||
"css/parser/SizesCalcParser.h",
|
||||
"css/PointerProperties.h",
|
||||
|
||||
@ -1,154 +0,0 @@
|
||||
// Copyright 2014 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "config.h"
|
||||
#include "core/css/parser/SizesAttributeParser.h"
|
||||
|
||||
#include "core/MediaTypeNames.h"
|
||||
#include "core/css/MediaQueryEvaluator.h"
|
||||
#include "core/css/parser/MediaQueryTokenizer.h"
|
||||
#include "core/css/parser/SizesCalcParser.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
SizesAttributeParser::SizesAttributeParser(PassRefPtr<MediaValues> mediaValues, const String& attribute)
|
||||
: m_mediaValues(mediaValues)
|
||||
, m_length(0)
|
||||
, m_lengthWasSet(false)
|
||||
, m_viewportDependant(false)
|
||||
{
|
||||
MediaQueryTokenizer::tokenize(attribute, m_tokens);
|
||||
m_isValid = parse(m_tokens);
|
||||
}
|
||||
|
||||
unsigned SizesAttributeParser::length()
|
||||
{
|
||||
if (m_isValid)
|
||||
return effectiveSize();
|
||||
return effectiveSizeDefaultValue();
|
||||
}
|
||||
|
||||
bool SizesAttributeParser::calculateLengthInPixels(MediaQueryTokenIterator startToken, MediaQueryTokenIterator endToken, unsigned& result)
|
||||
{
|
||||
if (startToken == endToken)
|
||||
return false;
|
||||
MediaQueryTokenType type = startToken->type();
|
||||
if (type == DimensionToken) {
|
||||
int length;
|
||||
if (!CSSPrimitiveValue::isLength(startToken->unitType()))
|
||||
return false;
|
||||
m_viewportDependant = CSSPrimitiveValue::isViewportPercentageLength(startToken->unitType());
|
||||
if ((m_mediaValues->computeLength(startToken->numericValue(), startToken->unitType(), length)) && (length > 0)) {
|
||||
result = (unsigned)length;
|
||||
return true;
|
||||
}
|
||||
} else if (type == FunctionToken) {
|
||||
SizesCalcParser calcParser(startToken, endToken, m_mediaValues);
|
||||
if (!calcParser.isValid())
|
||||
return false;
|
||||
m_viewportDependant = calcParser.viewportDependant();
|
||||
result = calcParser.result();
|
||||
return true;
|
||||
} else if (type == NumberToken && !startToken->numericValue()) {
|
||||
result = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static void reverseSkipIrrelevantTokens(MediaQueryTokenIterator& token, MediaQueryTokenIterator startToken)
|
||||
{
|
||||
MediaQueryTokenIterator endToken = token;
|
||||
while (token != startToken && (token->type() == WhitespaceToken || token->type() == CommentToken || token->type() == EOFToken))
|
||||
--token;
|
||||
if (token != endToken)
|
||||
++token;
|
||||
}
|
||||
|
||||
static void reverseSkipUntilComponentStart(MediaQueryTokenIterator& token, MediaQueryTokenIterator startToken)
|
||||
{
|
||||
if (token == startToken)
|
||||
return;
|
||||
--token;
|
||||
if (token->blockType() != MediaQueryToken::BlockEnd)
|
||||
return;
|
||||
unsigned blockLevel = 0;
|
||||
while (token != startToken) {
|
||||
if (token->blockType() == MediaQueryToken::BlockEnd) {
|
||||
++blockLevel;
|
||||
} else if (token->blockType() == MediaQueryToken::BlockStart) {
|
||||
--blockLevel;
|
||||
if (!blockLevel)
|
||||
break;
|
||||
}
|
||||
|
||||
--token;
|
||||
}
|
||||
}
|
||||
|
||||
bool SizesAttributeParser::mediaConditionMatches(PassRefPtr<MediaQuerySet> mediaCondition)
|
||||
{
|
||||
// A Media Condition cannot have a media type other then screen.
|
||||
MediaQueryEvaluator mediaQueryEvaluator(*m_mediaValues);
|
||||
return mediaQueryEvaluator.eval(mediaCondition.get());
|
||||
}
|
||||
|
||||
bool SizesAttributeParser::parseMediaConditionAndLength(MediaQueryTokenIterator startToken, MediaQueryTokenIterator endToken)
|
||||
{
|
||||
MediaQueryTokenIterator lengthTokenStart;
|
||||
MediaQueryTokenIterator lengthTokenEnd;
|
||||
|
||||
reverseSkipIrrelevantTokens(endToken, startToken);
|
||||
lengthTokenEnd = endToken;
|
||||
reverseSkipUntilComponentStart(endToken, startToken);
|
||||
lengthTokenStart = endToken;
|
||||
unsigned length;
|
||||
if (!calculateLengthInPixels(lengthTokenStart, lengthTokenEnd, length))
|
||||
return false;
|
||||
RefPtr<MediaQuerySet> mediaCondition = MediaQueryParser::parseMediaCondition(startToken, endToken);
|
||||
if (mediaCondition && mediaConditionMatches(mediaCondition)) {
|
||||
m_length = length;
|
||||
m_lengthWasSet = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SizesAttributeParser::parse(Vector<MediaQueryToken>& tokens)
|
||||
{
|
||||
if (tokens.isEmpty())
|
||||
return false;
|
||||
MediaQueryTokenIterator startToken = tokens.begin();
|
||||
MediaQueryTokenIterator endToken;
|
||||
// Split on a comma token, and send the result tokens to be parsed as (media-condition, length) pairs
|
||||
for (MediaQueryTokenIterator token = tokens.begin(); token != tokens.end(); ++token) {
|
||||
if (token->type() == CommaToken) {
|
||||
endToken = token;
|
||||
if (parseMediaConditionAndLength(startToken, endToken))
|
||||
return true;
|
||||
startToken = token;
|
||||
++startToken;
|
||||
}
|
||||
}
|
||||
endToken = tokens.end();
|
||||
return parseMediaConditionAndLength(startToken, --endToken);
|
||||
}
|
||||
|
||||
unsigned SizesAttributeParser::effectiveSize()
|
||||
{
|
||||
if (m_lengthWasSet)
|
||||
return m_length;
|
||||
return effectiveSizeDefaultValue();
|
||||
}
|
||||
|
||||
unsigned SizesAttributeParser::effectiveSizeDefaultValue()
|
||||
{
|
||||
// Returning the equivalent of "100vw"
|
||||
m_viewportDependant = true;
|
||||
return m_mediaValues->viewportWidth();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@ -1,43 +0,0 @@
|
||||
// Copyright 2014 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SizesAttributeParser_h
|
||||
#define SizesAttributeParser_h
|
||||
|
||||
#include "core/css/MediaValues.h"
|
||||
#include "core/css/parser/MediaQueryParser.h"
|
||||
#include "platform/heap/Handle.h"
|
||||
#include "wtf/text/WTFString.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
class SizesAttributeParser {
|
||||
STACK_ALLOCATED();
|
||||
public:
|
||||
SizesAttributeParser(PassRefPtr<MediaValues>, const String&);
|
||||
|
||||
bool viewportDependant() const { return m_viewportDependant; }
|
||||
unsigned length();
|
||||
|
||||
private:
|
||||
bool parse(Vector<MediaQueryToken>& tokens);
|
||||
bool parseMediaConditionAndLength(MediaQueryTokenIterator startToken, MediaQueryTokenIterator endToken);
|
||||
unsigned effectiveSize();
|
||||
bool calculateLengthInPixels(MediaQueryTokenIterator startToken, MediaQueryTokenIterator endToken, unsigned& result);
|
||||
bool mediaConditionMatches(PassRefPtr<MediaQuerySet> mediaCondition);
|
||||
unsigned effectiveSizeDefaultValue();
|
||||
|
||||
RefPtr<MediaQuerySet> m_mediaCondition;
|
||||
RefPtr<MediaValues> m_mediaValues;
|
||||
unsigned m_length;
|
||||
bool m_lengthWasSet;
|
||||
bool m_viewportDependant;
|
||||
Vector<MediaQueryToken> m_tokens;
|
||||
bool m_isValid;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif
|
||||
|
||||
@ -1,91 +0,0 @@
|
||||
// Copyright 2014 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "config.h"
|
||||
#include "core/css/parser/SizesAttributeParser.h"
|
||||
|
||||
#include "core/MediaTypeNames.h"
|
||||
#include "core/css/MediaValuesCached.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace blink {
|
||||
|
||||
typedef struct {
|
||||
const char* input;
|
||||
const unsigned effectiveSize;
|
||||
const bool viewportDependant;
|
||||
} TestCase;
|
||||
|
||||
TEST(SizesAttributeParserTest, Basic)
|
||||
{
|
||||
TestCase testCases[] = {
|
||||
{"screen", 500, true},
|
||||
{"(min-width:500px)", 500, true},
|
||||
{"(min-width:500px) 200px", 200, false},
|
||||
{"(min-width:500px) 50vw", 250, true},
|
||||
{"(min-width:500px) 200px, 400px", 200, false},
|
||||
{"400px, (min-width:500px) 200px", 400, false},
|
||||
{"40vw, (min-width:500px) 201px", 200, true},
|
||||
{"(min-width:500px) 201px, 40vw", 201, false},
|
||||
{"(min-width:5000px) 40vw, 201px", 201, false},
|
||||
{"(min-width:500px) calc(201px), calc(40vw)", 201, false},
|
||||
{"(min-width:5000px) calc(40vw), calc(201px)", 201, false},
|
||||
{"(min-width:5000px) 200px, 400px", 400, false},
|
||||
{"(blalbadfsdf) 200px, 400px", 400, false},
|
||||
{"0", 0, false},
|
||||
{"-0", 0, false},
|
||||
{"1", 500, true},
|
||||
{"300px, 400px", 300, false},
|
||||
{"(min-width:5000px) 200px, (min-width:500px) 400px", 400, false},
|
||||
{"", 500, true},
|
||||
{" ", 500, true},
|
||||
{" /**/ ", 500, true},
|
||||
{" /**/ 300px", 300, false},
|
||||
{"300px /**/ ", 300, false},
|
||||
{" /**/ (min-width:500px) /**/ 300px", 300, false},
|
||||
{"-100px, 200px", 200, false},
|
||||
{"-50vw, 20vw", 100, true},
|
||||
{"50asdf, 200px", 200, false},
|
||||
{"asdf, 200px", 200, false},
|
||||
{"(max-width: 3000px) 200w, 400w", 500, true},
|
||||
{",, , /**/ ,200px", 200, false},
|
||||
{"50vw", 250, true},
|
||||
{"5em", 80, false},
|
||||
{"5rem", 80, false},
|
||||
{"calc(40vw*2)", 400, true},
|
||||
{"(min-width:5000px) calc(5000px/10), (min-width:500px) calc(1200px/3)", 400, false},
|
||||
{"(min-width:500px) calc(1200/3)", 500, true},
|
||||
{"(min-width:500px) calc(1200px/(0px*14))", 500, true},
|
||||
{"(max-width: 3000px) 200px, 400px", 200, false},
|
||||
{"(max-width: 3000px) 20em, 40em", 320, false},
|
||||
{"(max-width: 3000px) 0, 40em", 0, false},
|
||||
{"(max-width: 3000px) 50vw, 40em", 250, true},
|
||||
{"(max-width: 3000px) 50px, 40vw", 50, false},
|
||||
{0, 0, false} // Do not remove the terminator line.
|
||||
};
|
||||
|
||||
MediaValuesCached::MediaValuesCachedData data;
|
||||
data.viewportWidth = 500;
|
||||
data.viewportHeight = 500;
|
||||
data.deviceWidth = 500;
|
||||
data.deviceHeight = 500;
|
||||
data.devicePixelRatio = 2.0;
|
||||
data.colorBitsPerComponent = 24;
|
||||
data.monochromeBitsPerComponent = 0;
|
||||
data.primaryPointerType = PointerTypeFine;
|
||||
data.defaultFontSize = 16;
|
||||
data.threeDEnabled = true;
|
||||
data.mediaType = MediaTypeNames::screen;
|
||||
data.strictMode = true;
|
||||
RefPtr<MediaValues> mediaValues = MediaValuesCached::create(data);
|
||||
|
||||
for (unsigned i = 0; testCases[i].input; ++i) {
|
||||
SizesAttributeParser parser(mediaValues, testCases[i].input);
|
||||
ASSERT_EQ(testCases[i].effectiveSize, parser.length());
|
||||
ASSERT_EQ(testCases[i].viewportDependant, parser.viewportDependant());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@ -29,7 +29,6 @@
|
||||
#include "core/css/MediaQueryListListener.h"
|
||||
#include "core/css/MediaQueryMatcher.h"
|
||||
#include "core/css/MediaValuesDynamic.h"
|
||||
#include "core/css/parser/SizesAttributeParser.h"
|
||||
#include "core/dom/Attribute.h"
|
||||
#include "core/dom/NodeTraversal.h"
|
||||
#include "core/fetch/ImageResource.h"
|
||||
@ -384,14 +383,6 @@ FloatSize HTMLImageElement::defaultDestinationSize() const
|
||||
void HTMLImageElement::selectSourceURL(ImageLoader::UpdateFromElementBehavior behavior)
|
||||
{
|
||||
unsigned effectiveSize = 0;
|
||||
if (RuntimeEnabledFeatures::pictureSizesEnabled()) {
|
||||
String sizes = getAttribute(HTMLNames::sizesAttr);
|
||||
if (!sizes.isNull())
|
||||
UseCounter::count(document(), UseCounter::Sizes);
|
||||
SizesAttributeParser parser = SizesAttributeParser(MediaValuesDynamic::create(document()), sizes);
|
||||
effectiveSize = parser.length();
|
||||
m_effectiveSizeViewportDependant = parser.viewportDependant();
|
||||
}
|
||||
ImageCandidate candidate = bestFitSourceForImageAttributes(
|
||||
document().devicePixelRatio(), effectiveSize,
|
||||
getAttribute(HTMLNames::srcAttr), getAttribute(HTMLNames::srcsetAttr));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user