Skip to content

DefaultFontSize no longer applied to text loaded with LoadHtml() method #5096

Closed
@smunnik

Description

@smunnik

Is there an existing issue for this?

  • I have searched both open/closed issues, no issue already exists.

CefSharp Version

135.0.220

Operating System

Windows 11

Architecture

x64

.Net Version

.Net 8.0

Implementation

WPF

Reproduction Steps

Set the DefaultFontSize and then load text via the LoadHtml() method. See modifed code snippet from minimal example below.

<wpf:ChromiumWebBrowser x:Name="Browser" 
                        IsBrowserInitializedChanged="Browser_OnIsBrowserInitializedChanged">
using System.Windows;

namespace CefSharp.MinimalExample.Wpf
{
    public partial class MainWindow : Window
    {
        private const int DefaultFontSize = 36;

        public MainWindow()
        {
            InitializeComponent();
            Browser.BrowserSettings.DefaultFontSize = DefaultFontSize;
        }

        private void Browser_OnIsBrowserInitializedChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                Browser.LoadHtml(
                    $"This message should be displayed in the default font size of {DefaultFontSize} that was specified.",
                    "http://message/");
            }
        }
    }
}

Expected behavior

The DefaultFontSize should be applied to text loaded with the LoadHtml() method.

Actual behavior

DefaultFontSize is ignored - the font size remains the same no matter what value is supplied.

Regression?

Worked in version 125.0.210 – does not work from version 126.2.70 onwards.

Known Workarounds

Manually set the document.body.style.fontSize using EvaluateScriptAsync() method.

<wpf:ChromiumWebBrowser x:Name="Browser" 
                        IsBrowserInitializedChanged="Browser_OnIsBrowserInitializedChanged" 
                        LoadingStateChanged="Browser_OnLoadingStateChanged">
using System.Windows;

namespace CefSharp.MinimalExample.Wpf
{
    public partial class MainWindow : Window
    {
        private const int DefaultFontSize = 36;

        public MainWindow()
        {
            InitializeComponent();
            Browser.BrowserSettings.DefaultFontSize = DefaultFontSize;
        }

        private void Browser_OnIsBrowserInitializedChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                Browser.LoadHtml(
                    $"This message should be displayed in the default font size of {DefaultFontSize} that was specified.",
                    "http://message/");
            }
        }


        private async void Browser_OnLoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
        {
            if (e.IsLoading)
            {
                return;
            }

            var javascript = $"document.body.style.fontSize = \"{DefaultFontSize}\";";
            await Browser.GetMainFrame().EvaluateScriptAsync($"(function(){{ {javascript} }})();");
        }
    }
}

Does this problem also occur in the CEF Sample Application

Not Tested

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions