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: defaultFontFamily in webPreferences #37968

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
44 changes: 26 additions & 18 deletions shell/browser/web_contents_preferences.cc
Original file line number Diff line number Diff line change
Expand Up @@ -430,24 +430,32 @@ void WebContentsPreferences::OverrideWebkitPrefs(
prefs->web_security_enabled = web_security_;
prefs->allow_running_insecure_content = allow_running_insecure_content_;

if (auto font =
default_font_family_.find("standard") != default_font_family_.end())
prefs->standard_font_family_map[blink::web_pref::kCommonScript] = font;
if (auto font =
default_font_family_.find("serif") != default_font_family_.end())
prefs->serif_font_family_map[blink::web_pref::kCommonScript] = font;
if (auto font =
default_font_family_.find("sansSerif") != default_font_family_.end())
prefs->sans_serif_font_family_map[blink::web_pref::kCommonScript] = font;
if (auto font =
default_font_family_.find("monospace") != default_font_family_.end())
prefs->fixed_font_family_map[blink::web_pref::kCommonScript] = font;
if (auto font =
default_font_family_.find("cursive") != default_font_family_.end())
prefs->cursive_font_family_map[blink::web_pref::kCommonScript] = font;
if (auto font =
default_font_family_.find("fantasy") != default_font_family_.end())
prefs->fantasy_font_family_map[blink::web_pref::kCommonScript] = font;
if (!default_font_family_.empty()) {
if (auto iter = default_font_family_.find("standard");
iter != default_font_family_.end())
prefs->standard_font_family_map[blink::web_pref::kCommonScript] =
iter->second;
if (auto iter = default_font_family_.find("serif");
iter != default_font_family_.end())
prefs->serif_font_family_map[blink::web_pref::kCommonScript] =
iter->second;
if (auto iter = default_font_family_.find("sansSerif");
iter != default_font_family_.end())
prefs->sans_serif_font_family_map[blink::web_pref::kCommonScript] =
iter->second;
if (auto iter = default_font_family_.find("monospace");
iter != default_font_family_.end())
prefs->fixed_font_family_map[blink::web_pref::kCommonScript] =
iter->second;
if (auto iter = default_font_family_.find("cursive");
iter != default_font_family_.end())
prefs->cursive_font_family_map[blink::web_pref::kCommonScript] =
iter->second;
if (auto iter = default_font_family_.find("fantasy");
iter != default_font_family_.end())
prefs->fantasy_font_family_map[blink::web_pref::kCommonScript] =
iter->second;
}

if (default_font_size_)
prefs->default_font_size = *default_font_size_;
Expand Down
16 changes: 16 additions & 0 deletions spec/api-browser-window-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3460,6 +3460,22 @@ describe('BrowserWindow module', () => {
expect(w.getSize()).to.deep.equal(size);
});
});

describe('"defaultFontFamily" option', () => {
it('can change the standard font family', async () => {
const w = new BrowserWindow({
show: false,
webPreferences: {
defaultFontFamily: {
standard: 'Impact'
}
}
});
await w.loadFile(path.join(fixtures, 'pages', 'content.html'));
const fontFamily = await w.webContents.executeJavaScript("window.getComputedStyle(document.getElementsByTagName('p')[0])['font-family']", true);
expect(fontFamily).to.equal('Impact');
});
});
});

describe('beforeunload handler', function () {
Expand Down