Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix @font-face url resolve with different url bases. #426

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions integration_tests/specs/css/css-fonts/relative-path-font-face.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!doctype html>
<html lang="en" fit="true">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="https://andycall.oss-accelerate.aliyuncs.com/css/font.css">
<style>
div {
font-family: Google Sans;
}
</style>
</head>
<body>
<div>
AAAAAAAAA
</div>
</body>
<script>
await sleep(2);
await snapshotAction();
</script>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@

#include "generated_plugin_registrant.h"

#include <connectivity_plus/connectivity_plus_windows_plugin.h>
#include <webf/webf_plugin.h>

void RegisterPlugins(flutter::PluginRegistry* registry) {
ConnectivityPlusWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("ConnectivityPlusWindowsPlugin"));
WebfPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("WebfPlugin"));
}
1 change: 0 additions & 1 deletion integration_tests/windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#

list(APPEND FLUTTER_PLUGIN_LIST
connectivity_plus
webf
)

Expand Down
10 changes: 5 additions & 5 deletions webf/lib/src/css/font_face.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ class _Font {
}

class CSSFontFace {
static Uri? _resolveFontSource(int contextId, String source) {
static Uri? _resolveFontSource(int contextId, String source, String? base) {
WebFController controller = WebFController.getControllerOfJSContextId(contextId)!;
String base = controller.url;
base = base ?? controller.url;
try {
return controller.uriParser!.resolve(Uri.parse(base), Uri.parse(source));
} catch (_) {
return null;
}
}
static void resolveFontFaceRules(CSSFontFaceRule fontFaceRule, int contextId) async {
static void resolveFontFaceRules(CSSFontFaceRule fontFaceRule, int contextId, String? baseHref) async {
CSSStyleDeclaration declaration = fontFaceRule.declarations;
String fontFamily = declaration.getPropertyValue('fontFamily');
String url = declaration.getPropertyValue('src');
Expand Down Expand Up @@ -89,12 +89,12 @@ class CSSFontFace {
loader.addFont(bytes);
loader.load();
} else {
Uri? uri = _resolveFontSource(contextId, targetFont.src);
Uri? uri = _resolveFontSource(contextId, targetFont.src, baseHref);
if (uri == null) return;
WebFBundle bundle = WebFBundle.fromUrl(uri.toString());
await bundle.resolve(contextId);
assert(bundle.isResolved, 'Failed to obtain $url');
FontLoader loader = FontLoader(fontFamily);
FontLoader loader = FontLoader(removeQuotationMark(fontFamily));
Future<ByteData> bytes = Future.value(bundle.data?.buffer.asByteData());
loader.addFont(bytes);
loader.load();
Expand Down
8 changes: 4 additions & 4 deletions webf/lib/src/css/rule_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ class RuleSet {

int _lastPosition = 0;

void addRules(List<CSSRule> rules) {
void addRules(List<CSSRule> rules, { required String? baseHref }) {
for (CSSRule rule in rules) {
addRule(rule);
addRule(rule, baseHref: baseHref);
}
}

void addRule(CSSRule rule) {
void addRule(CSSRule rule, { required String? baseHref }) {
rule.position = _lastPosition++;
if (rule is CSSStyleRule) {
for (final selector in rule.selectorGroup.selectors) {
Expand All @@ -48,7 +48,7 @@ class RuleSet {
} else if (rule is CSSKeyframesRule) {
keyframesRules[rule.name] = rule;
} else if (rule is CSSFontFaceRule) {
CSSFontFace.resolveFontFaceRules(rule, ownerDocument.contextId!);
CSSFontFace.resolveFontFaceRules(rule, ownerDocument.contextId!, baseHref);
} else {
assert(false, 'Unsupported rule type: ${rule.runtimeType}');
}
Expand Down
2 changes: 1 addition & 1 deletion webf/lib/src/dom/document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ class Document extends ContainerNode {
styleSheets.addAll(sheets.map((e) => e.clone()));
ruleSet.reset();
for (var sheet in sheets) {
ruleSet.addRules(sheet.cssRules);
ruleSet.addRules(sheet.cssRules, baseHref: sheet.href);
}
}

Expand Down
14 changes: 7 additions & 7 deletions webf/lib/src/dom/style_node_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,20 @@ class StyleNodeManager {
if (equals(oldSheets[index].cssRules, newSheets[index].cssRules)) {
continue;
}
ruleSet.addRules(newSheets[index].cssRules);
ruleSet.addRules(oldSheets[index].cssRules);
ruleSet.addRules(newSheets[index].cssRules, baseHref: newSheets[index].href);
ruleSet.addRules(oldSheets[index].cssRules, baseHref: oldSheets[index].href);
}

if (index == oldSheetsCount) {
for (; index < newSheetsCount; index++) {
ruleSet.addRules(newSheets[index].cssRules);
ruleSet.addRules(newSheets[index].cssRules, baseHref: newSheets[index].href);
}
return ruleSet;
}

if (index == newSheetsCount) {
for (; index < oldSheetsCount; index++) {
ruleSet.addRules(oldSheets[index].cssRules);
ruleSet.addRules(oldSheets[index].cssRules, baseHref: oldSheets[index].href);
}
return ruleSet;
}
Expand All @@ -166,7 +166,7 @@ class StyleNodeManager {
}
CSSStyleSheet sheet1 = mergeSorted[index];
if (index == mergeSorted.length - 1 || sheet != sheet1) {
ruleSet.addRules(sheet1.cssRules);
ruleSet.addRules(sheet1.cssRules, baseHref: sheet1.href);
continue;
}

Expand All @@ -178,8 +178,8 @@ class StyleNodeManager {
continue;
}

ruleSet.addRules(sheet1.cssRules);
ruleSet.addRules(sheet2.cssRules);
ruleSet.addRules(sheet1.cssRules, baseHref: sheet1.href);
ruleSet.addRules(sheet2.cssRules, baseHref: sheet2.href);
}

return ruleSet;
Expand Down
Loading