如何在 Firefox 中将回退编码设置为 UTF-8?

l0b*_*0b0 13 arch-linux character-encoding firefox

我写了一个挪威降价文档:

$ file brukerveiledning.md
brukerveiledning.md: UTF-8 Unicode text
Run Code Online (Sandbox Code Playgroud)

我已使用以下markdown命令将其转换为 HTML :

$ markdown > brukerveiledning.html <  brukerveiledning.md 
$ file brukerveiledning.html 
brukerveiledning.html: UTF-8 Unicode text
Run Code Online (Sandbox Code Playgroud)

但是,Firefox 坚持使用“windows-1252”编码,打破了非 ASCII 字符。我已经尝试将后备文本编码从“当前语言环境的默认值”(在英国应该是 ISO-8859-1 或 UTF-8)更改为“中欧,ISO”,“中欧,微软” ”和“其他(包括西欧)”。这些都不能显示 æ、ø 和 å。没有 Unicode 选项。我还尝试将intl.fallbackCharsetList.ISO-8859-1about:config更改为各种值,例如utf8, utf-8, iso-8859-1,但没有运气。

使用这个markdown包:

$ pacman --query --owns "$(which markdown)"
/usr/bin/markdown is owned by markdown 1.0.1-6
Run Code Online (Sandbox Code Playgroud)

和这个语言环境:

$ locale 
LANG=en_GB.utf8
LC_CTYPE="en_GB.utf8"
LC_NUMERIC="en_GB.utf8"
LC_TIME="en_GB.utf8"
LC_COLLATE="en_GB.utf8"
LC_MONETARY="en_GB.utf8"
LC_MESSAGES="en_GB.utf8"
LC_PAPER="en_GB.utf8"
LC_NAME="en_GB.utf8"
LC_ADDRESS="en_GB.utf8"
LC_TELEPHONE="en_GB.utf8"
LC_MEASUREMENT="en_GB.utf8"
LC_IDENTIFICATION="en_GB.utf8"
LC_ALL=
Run Code Online (Sandbox Code Playgroud)

试图markdown命令级别寻求解决方案,但被拒绝了。

小智 5

在 Firefox 中将回退编码设置为 UTF-8 已被故意阻止 - 请参阅 bugzilla.mozilla.org/show_bug.cgi?id=967981#c4。

我一直在研究的两种解决方法是:

1] 将一些简单的补丁应用到源代码并自己构建 Firefox以在首选项|内容|字体和颜色|高级|“后备文本编码”下拉菜单中添加一个 Unicode[UTF-8] 选项。

2] 运行本地 [Apache] httpd 服务器,并utfx为目录中的 utf-8 编码文件设置一个基于名称的虚拟服务器/my/utf-8/files。然后可以生成 utf-8 字符集 http 标头,Firefox 将识别并显示该文件为 UTF-8 编码。当然,实际的文件编码必须是UTF-8!

a) /etc/httpd/httpd.conf - 添加:

<VirtualHost *:80>
    # This first-listed virtual host is also the default for *:80
    ServerName localhost
    DocumentRoot "/srv/httpd/htdocs"
</VirtualHost>
<VirtualHost *:80>
    ServerName utfx
    DocumentRoot "/my/utf-8/files"
      <Directory "/my/utf-8/files">
          Options Indexes
          Require all granted
      </Directory>
## show UTF-8 characters in file names:
    IndexOptions Charset=UTF-8
## for files with extension html or txt:
    AddCharset UTF-8 txt html
## for extensionless files:
      <Files *>
          ForceType 'text/plain; charset=UTF-8'
      </Files>
      <Files *\.*>
          ForceType None
      </Files>
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

(重新)启动服务器 -apachectl restartapachectl graceful.

b) /etc/hosts - 添加访问utf-8编码文件的域名:

127.0.0.1   utfx
Run Code Online (Sandbox Code Playgroud)

可以使用 wget -S <URL> 检查服务器发送的内容类型信息:

wget -S http://utfx/test{æø,.txt,.html} 2>&1 >/dev/null | grep Content-Type
Run Code Online (Sandbox Code Playgroud)

对于三种文件类型(testæø、test.txt、test.html)。
输出应该是:

内容类型:文本/纯文本;charset=utf-8
内容类型:文本/纯文本;charset=utf-8
内容类型:text/html;字符集=utf-8

c) about:config - 添加 New|Boolean:

browser.fixup.domainwhitelist.utfx  "true"
Run Code Online (Sandbox Code Playgroud)

然后只需utfx在 Firefox 地址栏中输入即可获取文件列表..


mle*_*leu 4

更新:自 Firefox 66 以来已修复此问题

从文件加载 UTF-8 编码的 HTML(和纯文本)文件:现在支持不带<meta charset="utf-8">UTF-8 BOM 的URL

https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Releases/66#HTML


2016年历史信息

这种行为背后的原因似乎在 Mozilla bug 815551(默认情况下自动检测 UTF-8)和 1071816(支持从文件:URL 加载 BOMless UTF-8 文本/纯文件)中进行了描述

据我了解,它基本上可以归结为“应该始终指定编码,因为检测太不可靠”。

  • 对于非本地内容,您应该利用该协议。对于 HTTP,这将charsetContent-Type标头中提供正确的信息
  • 对于 HTML 内容,您还可以使用 Doctype,即<meta charset="utf-8" />
  • 对于其他任何事情,剩下的唯一标准方法就是指定 BOM...

Mozilla 开发人员似乎对添加首选项设置的补丁持开放态度,因此有一天可能可以在 Firefox 中打开本地无 BOM UTF-8 文档。