Determine the Encoding of a HTML Byte Stream
-
<h2>Determine the Encoding of a HTML Byte Stream</h2>
<p>This package implements the HTML Standard's <a href="https://html.spec.whatwg.org/multipage/syntax.html#encoding-sniffing-algorithm">encoding sniffing algorithm</a> in all its glory. The most interesting part of this is how it pre-scans the first 1024 bytes in order to search for certain <code><meta charset></code>-related patterns.</p>
<pre><code>
const htmlEncodingSniffer = require("html-encoding-sniffer");
const fs = require("fs");const htmlBuffer = fs.readFileSync("./html-page.html");
const sniffedEncoding = htmlEncodingSniffer(htmlBuffer);
</code></pre><p>The returned value will be a canonical <a href="https://encoding.spec.whatwg.org/#names-and-labels">encoding name</a> (not a label). You might then combine this with the <a href="https://github.com/jsdom/whatwg-encoding">whatwg-encoding</a> package to decode the result:</p>
<pre><code>
const whatwgEncoding = require("whatwg-encoding");
const htmlString = whatwgEncoding.decode(htmlBuffer, sniffedEncoding);
</code></pre><h3>Options</h3>
<p>You can pass two potential options to <code>htmlEncodingSniffer</code>:</p>
<pre><code>
const sniffedEncoding = htmlEncodingSniffer(htmlBuffer, {
transportLayerEncodingLabel,
defaultEncoding
});
</code></pre><p>These represent two possible inputs into the <a href="https://html.spec.whatwg.org/multipage/syntax.html#encoding-sniffing-algorithm">encoding sniffing algorithm</a>:</p>
<ul>
<li><code>transportLayerEncodingLabel</code> is an encoding label that is obtained from the "transport layer" (probably a HTTP <code>Content-Type</code> header), which overrides everything but a BOM.</li>
<li><code>defaultEncoding</code> is the ultimate fallback encoding used if no valid encoding is supplied by the transport layer, and no encoding is sniffed from the bytes. It defaults to <code>"windows-1252"</code>, as recommended by the algorithm's table of suggested defaults for "All other locales" (including the <code>en</code> locale).</li>
</ul><h3>Credits</h3>
<p>This package was originally based on the excellent work of <a href="https://github.com/nicolashenry">@nicolashenry</a>, <a href="https://github.com/tmpvar/jsdom/blob/16fd85618f2705d181232f6552125872a37164bc/lib/jsdom/living/helpers/encoding.js">in jsdom</a>. It has since been pulled out into this separate package.</p>
<hr>
<p>本文由模极社发布</p>
<h4>
内容概览</h4><table>
<tr><td>分类</td><td>要点</td><td>建议/说明</td></tr>
<tr><td>核心概念</td><td>Determine the Encodi</td><td>详见正文</td></tr>
<tr><td>操作步骤</td><td>Determine the Encodi</td><td>详见正文</td></tr>
<tr><td>注意事项</td><td>Determine the Encodi</td><td>详见正文</td></tr>
<tr><td>适用场景</td><td>Determine the Encodi</td><td>详见正文</td></tr>
<tr><td>常见问题</td><td>Determine the Encodi</td><td>详见正文</td></tr>
</table><hr>
<h4>
你可能还感兴趣</h4>
<ul>
<li><a href="/t/4496/4496">@dcloudio/uni-app-plus</a></li>
<li><a href="/t/4434/4434">@babel/plugin-transform-block-scoped-functions</a></li>
<li><a href="/t/2870/2870">酷科机械手产品系列与选型指南</a></li>
</ul><hr>
<p><strong>相关推荐:</strong></p>
<ul>
<li><a href="https://www.mj7.cn/t/topic">注塑产品报价不会算?完整成本计算模板来了(附公式+案例)</a></li>
<li><a href="https://www.mj7.cn/t/pp">PP塑料:性能参数与注塑成型指南</a></li>
<li><a href="https://www.mj7.cn/t/pa">PA塑料:性能参数与注塑成型指南</a></li>
</ul><hr>
<p>新手建议收藏,老手欢迎指点。</p>