Commit 3e318355 authored by Dominik Röttsches's avatar Dominik Röttsches Committed by Commit Bot

Parse ranges for weight in @font-face

Allow ranges for font-weight in @font-face, compare
https://drafts.csswg.org/css-fonts-4/#font-prop-desc

Bug: 749091
Change-Id: I7e3fffe6f71d95ad5bc0703d4e01edbbb108cfd0
Reviewed-on: https://chromium-review.googlesource.com/581149Reviewed-by: default avatarEmil A Eklund <eae@chromium.org>
Reviewed-by: default avatarmeade_UTC10 <meade@chromium.org>
Reviewed-by: default avatarBugs Nash <bugsnash@chromium.org>
Commit-Queue: Dominik Röttsches <drott@chromium.org>
Cr-Commit-Position: refs/heads/master@{#491375}
parent 44915afe
......@@ -19,7 +19,7 @@ test(function() {
assertParsedValue('transition-timing-function', 'steps(calc(1 + 2), start)', 'steps(3, start)');
assertParsedValue('grid-row-start', 'calc(1 + 2) test', '3 test');
assertParsedValue('grid-row-start', 'calc(1 / 2) test', '');
assertParsedValue('font-weight', 'calc(100 + 200)', '');
assertParsedValue('font-weight', 'calc(100 + 200)', '300');
assertParsedValue('flex', 'calc(1 + 2) calc(3 + 4)', '3 7 0%');
assertParsedValue('-webkit-filter', 'saturate(calc(4 / 2))', 'saturate(2)');
assertParsedValue('-webkit-filter', 'invert(calc(4 / 2))', 'invert(1)');
......
......@@ -18,6 +18,8 @@ var styleValidTests = {
'900',
'850',
'850.3',
'calc(100 + 300)',
'calc(0.2 + 205.5)',
],
'stretch': ['51%', '199%', 'calc(10% + 20%)']
};
......@@ -55,7 +57,8 @@ function testParseStyle() {
var faceTests = {
'weight': [
['100', '100'], ['700', '700'], ['900', '900'], ['bold', 'bold'],
['normal', 'normal']
['normal', 'normal'], ['100 400', '100 400'], ['100 101.5', '100 101.5'],
['999.8 999.9', '999.8 999.9']
],
'stretch': [
['100%', '100%'],
......@@ -68,6 +71,10 @@ var faceTests = {
var faceInvalidTests = {
'weight': [
'-100 200',
'100 -200',
'500 400',
'100 1001',
'1001',
'1000.5',
'100 200 300',
......
......@@ -831,7 +831,8 @@ bool CSSPropertyParser::ParseFontFaceDescriptor(CSSPropertyID prop_id) {
parsed_value = ConsumeFontVariantList(range_);
break;
case CSSPropertyFontWeight:
parsed_value = CSSPropertyFontUtils::ConsumeFontWeight(range_);
parsed_value =
CSSPropertyFontUtils::ConsumeFontWeight(range_, kCSSFontFaceRuleMode);
break;
case CSSPropertyFontFeatureSettings:
parsed_value = CSSPropertyFontUtils::ConsumeFontFeatureSettings(range_);
......
......@@ -4,15 +4,16 @@
#include "core/css/properties/CSSPropertyAPIFontWeight.h"
#include "core/css/parser/CSSParserContext.h"
#include "core/css/properties/CSSPropertyFontUtils.h"
namespace blink {
const CSSValue* CSSPropertyAPIFontWeight::parseSingleValue(
CSSParserTokenRange& range,
const CSSParserContext&,
const CSSParserContext& context,
const CSSParserLocalContext&) {
return CSSPropertyFontUtils::ConsumeFontWeight(range);
return CSSPropertyFontUtils::ConsumeFontWeight(range, context.Mode());
}
} // namespace blink
......@@ -114,6 +114,18 @@ CSSIdentifierValue* CSSPropertyFontUtils::ConsumeFontStretchKeywordOnly(
return nullptr;
}
static CSSValue* CombineToRangeListOrNull(const CSSPrimitiveValue* range_start,
const CSSPrimitiveValue* range_end) {
DCHECK(range_start);
DCHECK(range_end);
if (range_end->GetFloatValue() < range_start->GetFloatValue())
return nullptr;
CSSValueList* value_list = CSSValueList::CreateSpaceSeparated();
value_list->Append(*range_start);
value_list->Append(*range_end);
return value_list;
}
CSSValue* CSSPropertyFontUtils::ConsumeFontStretch(CSSParserTokenRange& range) {
CSSIdentifierValue* parsed_keyword = ConsumeFontStretchKeywordOnly(range);
if (parsed_keyword)
......@@ -125,18 +137,44 @@ CSSValue* CSSPropertyFontUtils::ConsumeFontStretch(CSSParserTokenRange& range) {
return percent;
}
CSSValue* CSSPropertyFontUtils::ConsumeFontWeight(CSSParserTokenRange& range) {
CSSValue* CSSPropertyFontUtils::ConsumeFontWeight(
CSSParserTokenRange& range,
const CSSParserMode& parser_mode) {
const CSSParserToken& token = range.Peek();
if (token.Id() >= CSSValueNormal && token.Id() <= CSSValueLighter)
return CSSPropertyParserHelpers::ConsumeIdent(range);
if (token.GetType() != kNumberToken)
// Avoid consuming the first zero of font: 0/0; e.g. in the Acid3 test.
if (token.GetType() == kNumberToken &&
(token.NumericValue() < 1 || token.NumericValue() > 1000))
return nullptr;
float weight = token.NumericValue();
if (weight < 1 || weight > 1000)
CSSPrimitiveValue* start_weight = nullptr;
start_weight =
CSSPropertyParserHelpers::ConsumeNumber(range, kValueRangeNonNegative);
if (!start_weight || start_weight->GetFloatValue() < 1 ||
start_weight->GetFloatValue() > 1000)
return nullptr;
range.ConsumeIncludingWhitespace();
return CSSPrimitiveValue::Create(weight,
CSSPrimitiveValue::UnitType::kNumber);
// In a non-font-face context, more than one number is not allowed. Return
// what we have. If there is trailing garbage, the AtEnd() check in
// CSSPropertyParser::ParseValueStart will catch that.
if (parser_mode != kCSSFontFaceRuleMode)
return start_weight;
CSSPrimitiveValue* end_weight = nullptr;
if (!range.AtEnd()) {
end_weight =
CSSPropertyParserHelpers::ConsumeNumber(range, kValueRangeNonNegative);
if (!end_weight || end_weight->GetFloatValue() < 1 ||
end_weight->GetFloatValue() > 1000)
return nullptr;
}
if (end_weight)
return CombineToRangeListOrNull(start_weight, end_weight);
return start_weight;
}
// TODO(bugsnash): move this to the FontFeatureSettings API when it is no longer
......
......@@ -37,7 +37,8 @@ class CSSPropertyFontUtils {
CSSParserTokenRange&);
static CSSValue* ConsumeFontStretch(CSSParserTokenRange&);
static CSSValue* ConsumeFontStyle(CSSParserTokenRange&);
static CSSValue* ConsumeFontWeight(CSSParserTokenRange&);
static CSSValue* ConsumeFontWeight(CSSParserTokenRange&,
const CSSParserMode&);
static CSSValue* ConsumeFontFeatureSettings(CSSParserTokenRange&);
static CSSFontFeatureValue* ConsumeFontFeatureTag(CSSParserTokenRange&);
......
......@@ -115,7 +115,8 @@ bool ConsumeFont(bool important,
continue;
}
if (!font_weight) {
font_weight = CSSPropertyFontUtils::ConsumeFontWeight(range);
font_weight =
CSSPropertyFontUtils::ConsumeFontWeight(range, context.Mode());
if (font_weight)
continue;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment