diff --git a/lib/html_tag.js b/lib/html_tag.js index 9811edf4..5f168f94 100644 --- a/lib/html_tag.js +++ b/lib/html_tag.js @@ -3,6 +3,7 @@ const encodeURL = require('./encode_url'); const escapeHTML = require('./escape_html'); const regexUrl = /(cite|download|href|src|url)$/i; +const regexMeta = /^(og:|twitter:)(audio|image|url|video)(:secure_url)?$/i; function encSrcset(str) { str.split(' ') @@ -23,8 +24,10 @@ function htmlTag(tag, attrs, text, escape = true) { for (const i in attrs) { if (attrs[i] === null || typeof attrs[i] === 'undefined') result += ''; else { - if (i.match(regexUrl)) result += ` ${escapeHTML(i)}="${encodeURL(attrs[i])}"`; - else if (attrs[i] === true || i === attrs[i]) result += ` ${escapeHTML(i)}`; + if (i.match(regexUrl) + || (tag === 'meta' && !attrs[i].match(regexMeta) && Object.values(attrs)[0].match(regexMeta))) { + result += ` ${escapeHTML(i)}="${encodeURL(attrs[i])}"`; + } else if (attrs[i] === true || i === attrs[i]) result += ` ${escapeHTML(i)}`; else if (i.match(/srcset$/i)) result += ` ${escapeHTML(i)}="${encSrcset(attrs[i])}"`; else result += ` ${escapeHTML(i)}="${escapeHTML(String(attrs[i]))}"`; } diff --git a/test/html_tag.spec.js b/test/html_tag.spec.js index c205758c..708ce5a5 100644 --- a/test/html_tag.spec.js +++ b/test/html_tag.spec.js @@ -4,6 +4,7 @@ require('chai').should(); describe('htmlTag', () => { const htmlTag = require('../lib/html_tag'); + const encodeURL = require('../lib/encode_url'); it('tag', () => { htmlTag('hr').should.eql('
'); @@ -113,4 +114,41 @@ describe('htmlTag', () => { async: true }, '').should.eql(''); }); + + it('meta tag', () => { + htmlTag('meta', { + property: 'og:title', + content: 'foo & bar' + }).should.eql(''); + + htmlTag('meta', { + name: 'twitter:title', + content: 'foo " bar' + }).should.eql(''); + }); + + it('meta tag - url', () => { + const content = 'https://foo.com/bár.jpg'; + const encoded = encodeURL(content); + + htmlTag('meta', { + property: 'og:url', + content + }).should.eql(``); + + htmlTag('meta', { + property: 'og:image:secure_url', + content + }).should.eql(``); + + htmlTag('meta', { + name: 'twitter:image', + content + }).should.eql(``); + + htmlTag('meta', { + name: 'foo image', + content: 'bar " baz' + }).should.eql(''); + }); });