Skip to content

Commit

Permalink
fix: Properly parse meta tag when parameters are reversed (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsuket authored and Richienb committed Oct 7, 2019
1 parent 47a24a0 commit 6a5d192
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/body.js
Expand Up @@ -306,6 +306,12 @@ function convertBody(buffer, headers) {
// html4
if (!res && str) {
res = /<meta[\s]+?http-equiv=(['"])content-type\1[\s]+?content=(['"])(.+?)\2/i.exec(str);
if (!res) {
res = /<meta[\s]+?content=(['"])(.+?)\1[\s]+?http-equiv=(['"])content-type\3/i.exec(str);
if (res) {
res.pop(); // drop last quote
}
}

if (res) {
res = /charset=(.*)/i.exec(res.pop());
Expand Down
6 changes: 6 additions & 0 deletions test/server.js
Expand Up @@ -181,6 +181,12 @@ export default class TestServer {
res.end(convert('<meta http-equiv="Content-Type" content="text/html; charset=gb2312"><div>中文</div>', 'gb2312'));
}

if (p === '/encoding/gb2312-reverse') {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.end(convert('<meta content="text/html; charset=gb2312" http-equiv="Content-Type"><div>中文</div>', 'gb2312'));
}

if (p === '/encoding/shift-jis') {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html; charset=Shift-JIS');
Expand Down
10 changes: 10 additions & 0 deletions test/test.js
Expand Up @@ -2767,6 +2767,16 @@ describe('external encoding', () => {
});
});

it('should support encoding decode, html4 detect reverse http-equiv', function() {
const url = `${base}encoding/gb2312-reverse`;
return fetch(url).then(res => {
expect(res.status).to.equal(200);
return res.textConverted().then(result => {
expect(result).to.equal('<meta content="text/html; charset=gb2312" http-equiv="Content-Type"><div>中文</div>');
});
});
});

it('should default to utf8 encoding', function() {
const url = `${base}encoding/utf8`;
return fetch(url).then(res => {
Expand Down

0 comments on commit 6a5d192

Please sign in to comment.