diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2024-09-08 03:32:33 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-08 03:32:33 -0700 |
commit | 0cfbc6eafff731d2d2ecce1f6ceb40be340473f3 (patch) | |
tree | fb78aa344c45a9be7f2b88927378267c050621a1 | |
parent | b40ec083ae30eb64ddfec2b238c909116e936e99 (diff) | |
parent | 95b65a7554f1b1041e4f1e7427e540993b68e47e (diff) | |
download | rneovim-0cfbc6eafff731d2d2ecce1f6ceb40be340473f3.tar.gz rneovim-0cfbc6eafff731d2d2ecce1f6ceb40be340473f3.tar.bz2 rneovim-0cfbc6eafff731d2d2ecce1f6ceb40be340473f3.zip |
Merge #30105 fix(tohtml): quote font-family names
-rw-r--r-- | runtime/lua/tohtml.lua | 18 | ||||
-rw-r--r-- | test/functional/plugin/tohtml_spec.lua | 87 |
2 files changed, 70 insertions, 35 deletions
diff --git a/runtime/lua/tohtml.lua b/runtime/lua/tohtml.lua index 6a5bd6de9d..ed42b28725 100644 --- a/runtime/lua/tohtml.lua +++ b/runtime/lua/tohtml.lua @@ -1293,9 +1293,25 @@ local function opt_to_global_state(opt, title) local fonts = {} if opt.font then fonts = type(opt.font) == 'string' and { opt.font } or opt.font --[[@as (string[])]] + for i, v in pairs(fonts) do + fonts[i] = ('"%s"'):format(v) + end elseif vim.o.guifont:match('^[^:]+') then - table.insert(fonts, vim.o.guifont:match('^[^:]+')) + -- Example: + -- Input: "Font,Escape\,comma, Ignore space after comma" + -- Output: { "Font","Escape,comma","Ignore space after comma" } + local prev = '' + for name in vim.gsplit(vim.o.guifont:match('^[^:]+'), ',', { trimempty = true }) do + if vim.endswith(name, '\\') then + prev = prev .. vim.trim(name:sub(1, -2) .. ',') + elseif vim.trim(name) ~= '' then + table.insert(fonts, ('"%s%s"'):format(prev, vim.trim(name))) + prev = '' + end + end end + -- Generic family names (monospace here) must not be quoted + -- because the browser recognizes them as font families. table.insert(fonts, 'monospace') --- @type vim.tohtml.state.global local state = { diff --git a/test/functional/plugin/tohtml_spec.lua b/test/functional/plugin/tohtml_spec.lua index dbf385c0c3..98a422935c 100644 --- a/test/functional/plugin/tohtml_spec.lua +++ b/test/functional/plugin/tohtml_spec.lua @@ -136,6 +136,50 @@ local function run_tohtml_and_assert(screen, func) screen:expect({ grid = expected.grid, attr_ids = expected.attr_ids }) end +---@param guifont boolean +local function test_generates_html(guifont, expect_font) + insert([[line]]) + exec('set termguicolors') + local bg = fn.synIDattr(fn.hlID('Normal'), 'bg#', 'gui') + local fg = fn.synIDattr(fn.hlID('Normal'), 'fg#', 'gui') + local tmpfile = t.tmpname() + + exec_lua( + [[ + local guifont, outfile = ... + local html = (guifont + and require('tohtml').tohtml(0,{title="title"}) + or require('tohtml').tohtml(0,{title="title",font={ "dumyfont","anotherfont" }})) + vim.fn.writefile(html, outfile) + vim.cmd.split(outfile) + ]], + guifont, + tmpfile + ) + + local out_file = api.nvim_buf_get_name(api.nvim_get_current_buf()) + eq({ + '<!DOCTYPE html>', + '<html>', + '<head>', + '<meta charset="UTF-8">', + '<title>title</title>', + ('<meta name="colorscheme" content="%s"></meta>'):format(api.nvim_get_var('colors_name')), + '<style>', + ('* {font-family: %s,monospace}'):format(expect_font), + ('body {background-color: %s; color: %s}'):format(bg, fg), + '</style>', + '</head>', + '<body style="display: flex">', + '<pre>', + 'line', + '', + '</pre>', + '</body>', + '</html>', + }, fn.readfile(out_file)) +end + describe(':TOhtml', function() --- @type test.functional.ui.screen local screen @@ -146,41 +190,16 @@ describe(':TOhtml', function() exec('colorscheme default') end) - it('expected internal html generated', function() - insert([[line]]) - exec('set termguicolors') - local bg = fn.synIDattr(fn.hlID('Normal'), 'bg#', 'gui') - local fg = fn.synIDattr(fn.hlID('Normal'), 'fg#', 'gui') - exec_lua [[ - local outfile = vim.fn.tempname() .. '.html' - local html = require('tohtml').tohtml(0,{title="title",font="dumyfont"}) - vim.fn.writefile(html, outfile) - vim.cmd.split(outfile) - ]] - local out_file = api.nvim_buf_get_name(api.nvim_get_current_buf()) - eq({ - '<!DOCTYPE html>', - '<html>', - '<head>', - '<meta charset="UTF-8">', - '<title>title</title>', - ('<meta name="colorscheme" content="%s"></meta>'):format(api.nvim_get_var('colors_name')), - '<style>', - '* {font-family: dumyfont,monospace}', - ('body {background-color: %s; color: %s}'):format(bg, fg), - '</style>', - '</head>', - '<body style="display: flex">', - '<pre>', - 'line', - '', - '</pre>', - '</body>', - '</html>', - }, fn.readfile(out_file)) + it('generates html with given font', function() + test_generates_html(false, '"dumyfont","anotherfont"') + end) + + it("generates html, respects 'guifont'", function() + exec_lua [[vim.o.guifont='Font,Escape\\,comma, Ignore space after comma']] + test_generates_html(true, '"Font","Escape,comma","Ignore space after comma"') end) - it('expected internal html generated from range', function() + it('generates html from range', function() insert([[ line1 line2 @@ -218,7 +237,7 @@ describe(':TOhtml', function() }, fn.readfile(out_file)) end) - it('highlight attributes generated', function() + it('generates highlight attributes', function() --Make sure to uncomment the attribute in `html_syntax_match()` exec('hi LINE guisp=#00ff00 gui=' .. table.concat({ 'bold', |