Repository: microsoft/PowerToys
File: src/modules/previewpane/SvgThumbnailProvider/SvgThumbnailProvider.cs (C#)

Code before:
            _browser.Dock = DockStyle.Fill;
            _browser.Visible = true;
            _browser.Width = (int)cx;
            _browser.Height = (int)cx;
            _browser.NavigationCompleted += async (object sender, CoreWebView2NavigationCompletedEventArgs args) =>
            {
                try
                {
                    // Check if the SVG element is present
                    var a = await _browser.ExecuteScriptAsync($"document.getElementsByTagName('svg')[0].viewBox;");
                    if (a != null)
                    {
                        var svgContent = SvgContents.Substring(SvgContents.IndexOf("<svg", StringComparison.OrdinalIgnoreCase), SvgContents.IndexOf("</svg>", StringComparison.OrdinalIgnoreCase) - SvgContents.IndexOf("<svg", StringComparison.OrdinalIgnoreCase) + "</svg>".Length);

                        Dictionary<string, string> styleDict = new Dictionary<string, string>();

                        // Try to parse the SVG content
                        try
                        {
                            // Attempt to parse the svgContent
                            var svgDocument = XDocument.Parse(svgContent);
                            var svgElement = svgDocument.Root;
                            var currentStyle = svgElement?.Attribute("style")?.Value;

                            // If style attribute exists, preserve existing styles
                            if (!string.IsNullOrEmpty(currentStyle) && currentStyle != "null")
                            {
                                foreach (var stylePart in currentStyle.Split(';'))
                                {
                                    if (!string.IsNullOrEmpty(stylePart))
                                    {
                                        var styleKeyValue = stylePart.Split(':');
                                        if (styleKeyValue.Length == 2)
                                        {
                                            styleDict[styleKeyValue[0].Trim()] = styleKeyValue[1].Trim();
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            // Log the error if the SVG content is not valid or parsing fails
                            Logger.LogError($"Failed to parse SVG content: {ex.Message}");
                        }

                        // Add or replace width and height in the existing style
                        styleDict["width"] = "100%";
                        styleDict["height"] = "100%";

                        // Construct a single JavaScript string to set all properties

Review comment (refactor):
The loop that constructs styleDict could be refactored for readability. Using LINQ to filter and map values directly could make the code shorter and clearer.
 Consider using a LINQ query to process currentStyle.Split(';'), which could simplify the code and make it easier to maintain.

Code after:
            _browser.Dock = DockStyle.Fill;
            _browser.Visible = true;
            _browser.Width = (int)cx;
            _browser.Height = (int)cx;
            _browser.NavigationCompleted += async (object sender, CoreWebView2NavigationCompletedEventArgs args) =>
            {
                try
                {
                    // Check if the SVG element is present
                    var a = await _browser.ExecuteScriptAsync($"document.getElementsByTagName('svg')[0].viewBox;");
                    if (a != null)
                    {
                        var svgContent = SvgContents.Substring(SvgContents.IndexOf("<svg", StringComparison.OrdinalIgnoreCase), SvgContents.IndexOf("</svg>", StringComparison.OrdinalIgnoreCase) - SvgContents.IndexOf("<svg", StringComparison.OrdinalIgnoreCase) + "</svg>".Length);

                        Dictionary<string, string> styleDict = new Dictionary<string, string>();

                        // Try to parse the SVG content
                        try
                        {
                            // Attempt to parse the svgContent
                            var svgDocument = XDocument.Parse(svgContent);
                            var svgElement = svgDocument.Root;
                            var currentStyle = svgElement?.Attribute("style")?.Value;

                            // If style attribute exists, preserve existing styles
                            if (!string.IsNullOrEmpty(currentStyle) && currentStyle != "null")
                            {
                                styleDict = currentStyle
                                    .Split(';', StringSplitOptions.RemoveEmptyEntries)
                                    .Select(stylePart => stylePart.Split(':', 2, StringSplitOptions.TrimEntries))
                                    .Where(styleKeyValue => styleKeyValue.Length == 2 && !string.IsNullOrEmpty(styleKeyValue[0]))
                                    .ToDictionary(
                                        styleKeyValue => styleKeyValue[0],
                                        styleKeyValue => styleKeyValue[1]);
                            }
                        }
                        catch (Exception ex)
                        {
                            // Log the error if the SVG content is not valid or parsing fails
                            Logger.LogError($"Failed to parse SVG content: {ex.Message}");
                        }

                        // Add or replace width and height in the existing style
                        styleDict["width"] = "100%";
                        styleDict["height"] = "100%";

                        // Construct a single JavaScript string to set all properties
                        var styleScript = string.Join(";", styleDict.Select(kv => $"document.getElementsByTagName('svg')[0].style.setProperty('{kv.Key}', '{kv.Value}');"));

                        // Apply the new style attributes using the constructed script
                        await _browser.ExecuteScriptAsync(styleScript);