osc.lua: escape text

Escape all messages in osc.lua, because other than the title they
weren't being escaped at all. If for example you did mpv foo.mp4
'{\fs50}bar.mp4' and script-message osc-playlist, it would just render
the second entry as bar.mp4 in big text.

The title was escaped partially, now the escaping is complete because:

- It escapes \. Backslashes at the end of the title are escaped instead
  of being stripped, and \n, \N and \h are now printed verbatim. In
  particular, "\\n" is no longer converted to space and is printed
  verbatim instead which is more correct.
- Newlines ("\n", not the "\\n" escape sequence) are converted to spaces
  instead of rendering them and messing up the text positioning within
  the OSC.
- Spaces at the start are preserved.

Fixes #11209, fixes #11275.
This commit is contained in:
Guido Cella 2022-05-11 08:51:39 +02:00 committed by Kacper Michajłow
parent d6610a5b2f
commit c84bb1ce67
2 changed files with 6 additions and 11 deletions

View File

@ -307,7 +307,7 @@ Configurable Options
String that supports property expansion that will be displayed as
OSC title.
ASS tags are escaped, and newlines and trailing slashes are stripped.
ASS tags are escaped and newlines are converted to spaces.
``tooltipborder``
Default: 1

View File

@ -955,10 +955,7 @@ function show_message(text, duration)
-- may slow down massively on huge input
text = string.sub(text, 0, 4000)
-- replace actual linebreaks with ASS linebreaks
text = string.gsub(text, "\n", "\\N")
state.message_text = text
state.message_text = mp.command_native({"escape-ass", text})
if not state.message_hide_timer then
state.message_hide_timer = mp.add_timeout(0, request_tick)
@ -1162,9 +1159,8 @@ function window_controls(topbar)
ne = new_element("wctitle", "button")
ne.content = function ()
local title = mp.command_native({"expand-text", user_opts.windowcontrols_title})
-- escape ASS, and strip newlines and trailing slashes
title = title:gsub("\\n", " "):gsub("\\$", ""):gsub("{","\\{")
return not (title == "") and title or "mpv"
title = title:gsub("\n", " ")
return title ~= "" and mp.command_native({"escape-ass", title}) or "mpv"
end
local left_pad = 5
local right_pad = 10
@ -1792,9 +1788,8 @@ function osc_init()
ne.content = function ()
local title = state.forced_title or
mp.command_native({"expand-text", user_opts.title})
-- escape ASS, and strip newlines and trailing slashes
title = title:gsub("\\n", " "):gsub("\\$", ""):gsub("{","\\{")
return not (title == "") and title or "mpv"
title = title:gsub("\n", " ")
return title ~= "" and mp.command_native({"escape-ass", title}) or "mpv"
end
ne.eventresponder["mbtn_left_up"] = function ()