fix: defaultFontFamily in webPreferences (#37863)

* fix: defaultFontFamily in webPreferences

* chore: check if default_font_family_ is empty
This commit is contained in:
David Sanders 2023-04-13 02:30:25 -07:00 committed by GitHub
parent fceeb64e7c
commit e84bcb39e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 18 deletions

View file

@ -425,24 +425,30 @@ void WebContentsPreferences::OverrideWebkitPrefs(
prefs->web_security_enabled = web_security_; prefs->web_security_enabled = web_security_;
prefs->allow_running_insecure_content = allow_running_insecure_content_; prefs->allow_running_insecure_content = allow_running_insecure_content_;
if (auto font = if (!default_font_family_.empty()) {
default_font_family_.find("standard") != default_font_family_.end()) if (auto iter = default_font_family_.find("standard");
prefs->standard_font_family_map[blink::web_pref::kCommonScript] = font; iter != default_font_family_.end())
if (auto font = prefs->standard_font_family_map[blink::web_pref::kCommonScript] =
default_font_family_.find("serif") != default_font_family_.end()) iter->second;
prefs->serif_font_family_map[blink::web_pref::kCommonScript] = font; if (auto iter = default_font_family_.find("serif");
if (auto font = iter != default_font_family_.end())
default_font_family_.find("sansSerif") != default_font_family_.end()) prefs->serif_font_family_map[blink::web_pref::kCommonScript] = iter->second;
prefs->sans_serif_font_family_map[blink::web_pref::kCommonScript] = font; if (auto iter = default_font_family_.find("sansSerif");
if (auto font = iter != default_font_family_.end())
default_font_family_.find("monospace") != default_font_family_.end()) prefs->sans_serif_font_family_map[blink::web_pref::kCommonScript] =
prefs->fixed_font_family_map[blink::web_pref::kCommonScript] = font; iter->second;
if (auto font = if (auto iter = default_font_family_.find("monospace");
default_font_family_.find("cursive") != default_font_family_.end()) iter != default_font_family_.end())
prefs->cursive_font_family_map[blink::web_pref::kCommonScript] = font; prefs->fixed_font_family_map[blink::web_pref::kCommonScript] = iter->second;
if (auto font = if (auto iter = default_font_family_.find("cursive");
default_font_family_.find("fantasy") != default_font_family_.end()) iter != default_font_family_.end())
prefs->fantasy_font_family_map[blink::web_pref::kCommonScript] = font; 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_) if (default_font_size_)
prefs->default_font_size = *default_font_size_; prefs->default_font_size = *default_font_size_;

View file

@ -3867,6 +3867,22 @@ describe('BrowserWindow module', () => {
expect(w.getSize()).to.deep.equal(size); 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 () { describe('beforeunload handler', function () {