kiwix / libkiwix

Common code base for all Kiwix ports
https://download.kiwix.org/release/libkiwix/
GNU General Public License v3.0
112 stars 54 forks source link

Edge case: kiwix-serve style & script injection fails if optional </head> tag is not present #1082

Closed IMayBeABitShy closed 1 month ago

IMayBeABitShy commented 1 month ago

This is a minor edge case, but the kiwix-serve taskbar won't work due to failed style & script injection when the served page does not contain the optional </HEAD> tag. Yeah, I didn't know either that it was optional, but the current HTML specification says so and apparently modern html minifiers already utilize this part of the spec.

Observed behavior:

The kiwix-serve taskbar looks weird (missing CSS) and won't work (missing js) when there's no </head> tag in the html document served. A HTML inspection revealed that kiwix-serve did not inject the ususal <link type="... into the html.

I believe this may be because kiwix tries to add the relevant tags to the header just before </head> and fails if it is not present. The content of the body tag was still successfully modified.

Expected behavior:

Ideally, the relevant CSS/js/... should still be injected even if there's no </head> tag, as this complies to the standard and would allow further html minification and thus smaller ZIM sizes. Perhaps a check if there's no </head> tag present and then conditionally injecting in front of <body> instead may work.

kelson42 commented 1 month ago

@IMayBeABitShy Do you have a concrete example of zim file? I don't understand how this could work as taskbar is not in the same iframe as content!

IMayBeABitShy commented 1 month ago

Sorry, this was a stupid mistake on my part... A $PATH misconfiguration caused me to use an outdated kiwix-serve without me noticing. Again, sorry about that. It woreks with the new versions.