Skip to content

Commit

Permalink
Cherry-pick 0a0261e. rdar://problem/108897052
Browse files Browse the repository at this point in the history
    Fix list of supported font-formats at parse time (256313)
    https://bugs.webkit.org/show_bug.cgi?id=256313
    rdar://108897052

    Reviewed by Myles C. Maxfield.

    We are now rejecting unsupported formats at parsing time.
    We are also moving the code fragment that parses the font-format to
    its own function, so it can be used by both @font-face src and @supports
    parsers.

    * LayoutTests/imported/w3c/web-platform-tests/css/css-fonts/parsing/font-face-src-format-expected.txt:
    * Source/WebCore/css/CSSFontFaceSrcValue.cpp:
    (WebCore::CSSFontFaceSrcResourceValue::fontLoadRequest):
    * Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp:
    (WebCore::CSSPropertyParserHelpers::consumeFontFormat):
    (WebCore::CSSPropertyParserHelpers::identMatchesSupportedFontFormat): Deleted.
    * Source/WebCore/css/parser/CSSPropertyParserHelpers.h:
    * Source/WebCore/css/parser/CSSPropertyParserWorkerSafe.cpp:
    (WebCore::CSSPropertyParserHelpersWorkerSafe::consumeFontFaceSrcURI):
    * Source/WebCore/css/parser/CSSSupportsParser.cpp:
    (WebCore::CSSSupportsParser::consumeSupportsFontFormatFunction):
    * Source/WebCore/platform/graphics/coretext/FontCustomPlatformDataCoreText.cpp:
    (WebCore::FontCustomPlatformData::supportsFormat):
    * Source/WebCore/platform/graphics/freetype/FontCustomPlatformDataFreeType.cpp:
    (WebCore::FontCustomPlatformData::supportsFormat):
    * Source/WebCore/platform/graphics/win/FontCustomPlatformDataWin.cpp:
    (WebCore::FontCustomPlatformData::supportsFormat):

    Canonical link: https://commits.webkit.org/263914@main

Identifier: 263769.21@safari-7616.1.14-branch
  • Loading branch information
vitorroriz authored and MyahCobbs committed May 10, 2023
1 parent ba47a0d commit 6c0c822
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 39 deletions.
Expand Up @@ -11,7 +11,7 @@ PASS Check that src: url("foo.ttf") format("truetype") is valid
PASS Check that src: url("foo.ttf") format("woff") is valid
PASS Check that src: url("foo.ttf") format("woff2") is valid
PASS Check that src: url("foo.ttf") format("opentype", "truetype") is invalid
PASS Check that src: url("foo.ttf") format(collection) is valid
FAIL Check that src: url("foo.ttf") format(collection) is valid assert_equals: expected true but got false
PASS Check that src: url("foo.ttf") format(opentype) is valid
PASS Check that src: url("foo.ttf") format(truetype) is valid
PASS Check that src: url("foo.ttf") format(woff) is valid
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/css/CSSFontFaceSrcValue.cpp
Expand Up @@ -100,7 +100,7 @@ std::unique_ptr<FontLoadRequest> CSSFontFaceSrcResourceValue::fontLoadRequest(Sc
isFormatSVG = false;
} else {
isFormatSVG = equalLettersIgnoringASCIICase(m_format, "svg"_s);
if (!isFormatSVG && !FontCustomPlatformData::supportsFormat(m_format))
if (!FontCustomPlatformData::supportsFormat(m_format))
return nullptr;
}

Expand Down
29 changes: 16 additions & 13 deletions Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp
Expand Up @@ -8202,19 +8202,6 @@ RefPtr<CSSValue> consumeFontFaceFontFamily(CSSParserTokenRange& range)
return CSSValueList::createCommaSeparated(name.releaseNonNull());
}

bool identMatchesSupportedFontFormat(CSSValueID id)
{
return identMatches<
CSSValueCollection,
CSSValueEmbeddedOpentype,
CSSValueOpentype,
CSSValueSvg,
CSSValueTruetype,
CSSValueWoff,
CSSValueWoff2
>(id);
}


Vector<FontTechnology> consumeFontTech(CSSParserTokenRange& range, bool singleValue)
{
Expand All @@ -8233,6 +8220,22 @@ Vector<FontTechnology> consumeFontTech(CSSParserTokenRange& range, bool singleVa
return technologies;
}

String consumeFontFormat(CSSParserTokenRange& range, bool rejectStringValues)
{
// https://drafts.csswg.org/css-fonts/#descdef-font-face-src
// FIXME: We allow any identifier here and convert to strings; specification calls for certain keywords and legacy compatibility strings.
auto args = CSSPropertyParserHelpers::consumeFunction(range);
auto& arg = args.consumeIncludingWhitespace();
if (!args.atEnd())
return nullString();
if (arg.type() != IdentToken && (rejectStringValues || arg.type() != StringToken))
return nullString();
auto format = arg.value().toString();
if (arg.type() == IdentToken && !FontCustomPlatformData::supportsFormat(format))
return nullString();
return format;
}

// MARK: @font-palette-values

RefPtr<CSSValue> consumeFontPaletteValuesOverrideColors(CSSParserTokenRange& range, const CSSParserContext& context)
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/css/parser/CSSPropertyParserHelpers.h
Expand Up @@ -321,7 +321,7 @@ RefPtr<CSSValue> consumeDeclarationValue(CSSParserTokenRange&, const CSSParserCo

RefPtr<CSSValue> consumeFontFaceFontFamily(CSSParserTokenRange&);
Vector<FontTechnology> consumeFontTech(CSSParserTokenRange&, bool singleValue = false);
bool identMatchesSupportedFontFormat(CSSValueID);
String consumeFontFormat(CSSParserTokenRange&, bool rejectStringValues = false);

// @font-palette-values descriptor consumers:

Expand Down
14 changes: 3 additions & 11 deletions Source/WebCore/css/parser/CSSPropertyParserWorkerSafe.cpp
Expand Up @@ -44,6 +44,7 @@
#include "CSSTokenizer.h"
#include "CSSUnicodeRangeValue.h"
#include "Document.h"
#include "FontCustomPlatformData.h"
#include "ParsingUtilities.h"
#include "ScriptExecutionContext.h"
#include "StyleSheetContents.h"
Expand Down Expand Up @@ -235,18 +236,9 @@ static RefPtr<CSSFontFaceSrcResourceValue> consumeFontFaceSrcURI(CSSParserTokenR
String format;
Vector<FontTechnology> supportedTechnologies;
if (range.peek().functionId() == CSSValueFormat) {
// https://drafts.csswg.org/css-fonts/#descdef-font-face-src
// FIXME: We allow any identifier here and convert to strings; specification calls for certain keywords and legacy compatibility strings.
auto args = CSSPropertyParserHelpers::consumeFunction(range);
auto& arg = args.consumeIncludingWhitespace();
if (!args.atEnd())
return nullptr;
if (arg.type() == IdentToken) {
if (!CSSPropertyParserHelpers::identMatchesSupportedFontFormat(arg.id()))
return nullptr;
} else if (arg.type() != StringToken)
format = CSSPropertyParserHelpers::consumeFontFormat(range);
if (format.isNull())
return nullptr;
format = arg.value().toString();
}
if (range.peek().functionId() == CSSValueTech) {
supportedTechnologies = CSSPropertyParserHelpers::consumeFontTech(range);
Expand Down
11 changes: 2 additions & 9 deletions Source/WebCore/css/parser/CSSSupportsParser.cpp
Expand Up @@ -169,15 +169,8 @@ CSSSupportsParser::SupportsResult CSSSupportsParser::consumeSupportsSelectorFunc
CSSSupportsParser::SupportsResult CSSSupportsParser::consumeSupportsFontFormatFunction(CSSParserTokenRange& range)
{
ASSERT(range.peek().type() == FunctionToken && range.peek().functionId() == CSSValueFontFormat);

auto block = range.consumeBlock();
block.consumeWhitespace();

auto isSupported = CSSPropertyParserHelpers::consumeIdent(block, CSSPropertyParserHelpers::identMatchesSupportedFontFormat) ? Supported : Unsupported;
if (!block.atEnd())
return Unsupported;

return isSupported;
auto format = CSSPropertyParserHelpers::consumeFontFormat(range, true);
return format.isNull() ? Unsupported : Supported;
}

// <supports-font-tech-fn>
Expand Down
Expand Up @@ -99,7 +99,8 @@ bool FontCustomPlatformData::supportsFormat(const String& format)
|| equalLettersIgnoringASCIICase(format, "woff-variations"_s)
|| equalLettersIgnoringASCIICase(format, "truetype-variations"_s)
|| equalLettersIgnoringASCIICase(format, "opentype-variations"_s)
|| equalLettersIgnoringASCIICase(format, "woff"_s);
|| equalLettersIgnoringASCIICase(format, "woff"_s)
|| equalLettersIgnoringASCIICase(format, "svg"_s);
}

bool FontCustomPlatformData::supportsTechnology(const FontTechnology& tech)
Expand Down
Expand Up @@ -161,7 +161,8 @@ bool FontCustomPlatformData::supportsFormat(const String& format)
|| equalLettersIgnoringASCIICase(format, "truetype-variations"_s)
|| equalLettersIgnoringASCIICase(format, "opentype-variations"_s)
#endif
|| equalLettersIgnoringASCIICase(format, "woff"_s);
|| equalLettersIgnoringASCIICase(format, "woff"_s)
|| equalLettersIgnoringASCIICase(format, "svg"_s);
}

bool FontCustomPlatformData::supportsTechnology(const FontTechnology&)
Expand Down
Expand Up @@ -105,7 +105,8 @@ bool FontCustomPlatformData::supportsFormat(const String& format)
#if USE(WOFF2)
|| equalLettersIgnoringASCIICase(format, "woff2"_s)
#endif
|| equalLettersIgnoringASCIICase(format, "woff"_s);
|| equalLettersIgnoringASCIICase(format, "woff"_s)
|| equalLettersIgnoringASCIICase(format, "svg"_s);
}

bool FontCustomPlatformData::supportsTechnology(const FontTechnology&)
Expand Down

0 comments on commit 6c0c822

Please sign in to comment.