From 7ebd067a95886392b3894bed0d4a0c50de475b59 Mon Sep 17 00:00:00 2001 From: poire-z Date: Fri, 12 Jul 2024 13:34:47 +0200 Subject: [PATCH] Footnote popup: fix XHTML handling (#12158) Latest MuPDF update changed HTML parsing, and use a better HTML5 parser, which may cause some issues with the XHTML we get from crengine. So, for footnote popups, be sure we use MuPDF's XHTML parser. --- frontend/ui/widget/footnotewidget.lua | 3 +++ frontend/ui/widget/htmlboxwidget.lua | 10 ++++++++-- frontend/ui/widget/scrollhtmlwidget.lua | 3 ++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/frontend/ui/widget/footnotewidget.lua b/frontend/ui/widget/footnotewidget.lua index 22b13625f..2ca88535d 100644 --- a/frontend/ui/widget/footnotewidget.lua +++ b/frontend/ui/widget/footnotewidget.lua @@ -293,8 +293,11 @@ function FootnoteWidget:init() local padding_bottom = Size.padding.large local htmlwidget_height = self.height - padding_top - padding_bottom + -- We always get balanced XHTML from crengine for HTML snippets, so we + -- pass is_xhtml=true to avoid side effects from MuPDF's HTML5 parser. self.htmlwidget = ScrollHtmlWidget:new{ html_body = self.html, + is_xhtml = true, css = css, default_font_size = font_size, width = htmlwidget_width, diff --git a/frontend/ui/widget/htmlboxwidget.lua b/frontend/ui/widget/htmlboxwidget.lua index 02a1cc48c..447d89472 100644 --- a/frontend/ui/widget/htmlboxwidget.lua +++ b/frontend/ui/widget/htmlboxwidget.lua @@ -36,7 +36,7 @@ function HtmlBoxWidget:init() end end -function HtmlBoxWidget:setContent(body, css, default_font_size) +function HtmlBoxWidget:setContent(body, css, default_font_size, is_xhtml) -- fz_set_user_css is tied to the context instead of the document so to easily support multiple -- HTML dictionaries with different CSS, we embed the stylesheet into the HTML instead of using -- that function. @@ -51,8 +51,14 @@ function HtmlBoxWidget:setContent(body, css, default_font_size) -- https://bugs.ghostscript.com/show_bug.cgi?id=698351 html = html:gsub("%
", " 
") + -- We can provide some "magic"/"mimetype" to Mupdf.openDocumentFromText(): + -- - "html" will get MuPDF to use its bundled gumbo-parser to parse HTML5 according to the specs. + -- - "xhtml" will get MuPDF to use its own XML parser, and if it fails, to switch to gumbo-parser. + -- When we know the body is balanced XHTML, it's safer to use "xhtml" to avoid the HTML5 + -- rules to trigger (ie. <p>123</p>, which is valid in FB2 snippets, parsed + -- as title>p, while gumbo-parse would consider "

123

" as being plain text). local ok - ok, self.document = pcall(Mupdf.openDocumentFromText, html, "html") + ok, self.document = pcall(Mupdf.openDocumentFromText, html, is_xhtml and "xhtml" or "html") if not ok then -- self.document contains the error logger.warn("HTML loading error:", self.document) diff --git a/frontend/ui/widget/scrollhtmlwidget.lua b/frontend/ui/widget/scrollhtmlwidget.lua index ce5e5372a..038bd84ac 100644 --- a/frontend/ui/widget/scrollhtmlwidget.lua +++ b/frontend/ui/widget/scrollhtmlwidget.lua @@ -17,6 +17,7 @@ local Screen = Device.screen local ScrollHtmlWidget = InputContainer:extend{ html_body = nil, + is_xhtml = false, css = nil, default_font_size = Screen:scaleBySize(24), -- same as infofont htmlbox_widget = nil, @@ -39,7 +40,7 @@ function ScrollHtmlWidget:init() html_link_tapped_callback = self.html_link_tapped_callback, } - self.htmlbox_widget:setContent(self.html_body, self.css, self.default_font_size) + self.htmlbox_widget:setContent(self.html_body, self.css, self.default_font_size, self.is_xhtml) self.v_scroll_bar = VerticalScrollBar:new{ enable = self.htmlbox_widget.page_count > 1,