-
-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UTF-8 BOM is not removed when running on Node.js and input is a file #1344
Comments
What I can't figure out is how to emulate this scenario in Ruby. I can't figure how to get Ruby to report the character code (8-bit unsigned integer) 65279. If I try to pack it, this is what I get: [65279].pack('C*')
# => "\xFF" Here's what I get if I unpack the BOM: "\xFE\xFF".unpack('C*')
=> [254, 255] And if I write it to a file, Ruby always writes the original BOM, "\xEF\xBB\xFB" File.write '/tmp/bom.txt', [65279].pack('U*'), mode: 'wb' We may just have to accept this character code is a weird quirk of Node.js and deal with it as such. There are translations going on we just can't see. |
I'm now convinced it's best to deal with this in Asciidoctor.js. Our custom unpack method is an ideal place for it. Here's the logic we could use that would make this work:
Let me know if you want me to proceed with a fix. |
An alternate way to do this to check if |
Here's something that's interesting: encodeURIComponent(self.charAt())
// => %EF%BB%BF and encodeURIComponent(String.fromCharCode(65279))
// => %EF%BB%BF So it is recognizing 65279 as a UTF-8 BOM. I think checking the char code is the right strategy (as proposed above). It's the same as this, with a bit less overhead: self.charAt() === String.fromCharCode(65279) |
…turn standard BOM
…turn standard BOM
Bypass Ruby method calls to make BOM detection more portable. Port asciidoctor#1344 fix.
Bypass Ruby method calls to make BOM detection more portable. Port asciidoctor#1344 fix.
Although there's a test that covers removing the UTF-8 BOM from the AsciiDoc source, this only works on Node.js when the input is a string created in JavaScript. When the AsciiDoc source is read from a file, or the string is created from Buffer.from, the UTF-8 BOM is not removed.
This snippet explains why we're hitting this problem:
It appears that when Node.js creates a string by way of a Buffer, such as when reading the contents of a file, it changes the UTF-8 BOM into a different BOM character (code: 65279, char ref: 0xFEFF). I have not found any way to disable this behavior. It's basically a quirk of Node.js.
I think Asciidoctor.js should detect this alternate BOM and remove the character. (I'm open to changing Asciidoctor Ruby, if we determine it's necessary).
The text was updated successfully, but these errors were encountered: