Skip to content
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

fix: shouldn't htmlEntities and htmlNumbers pass ? #130

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export function escapeHtml(unsafe) {
}

// Used to match HTML entities and HTML characters.
const reUnescapedHtml = /[&<>"']/g
const reUnescapedHtml = /&(?![\w\#]+;)|[<>"']/g
// Cast (null,undefined,[] and 0 to empty string => '')
const reHasUnescapedHtml = RegExp(reUnescapedHtml.source)

Expand Down
22 changes: 22 additions & 0 deletions test/specs/i18n.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ describe('i18n', () => {
'<span>This is a <strong>success</strong>.</span>'
)
})
it('should skip html entities/numbers using Markdown in translation messages', () => {
const t = i18n('Entity: &bull; & number: a&#768;', {
test: 'success',
markdown: true,
})
expect(React.isValidElement(t)).toBe(true)
const renderedHtml = ReactDOMServer.renderToStaticMarkup(t)
expect(renderedHtml).toBe(
'<span>Entity: &bull; &amp; number: a&#768;</span>'
)
})

it('should correctly escape interpolations when used with Markdown', () => {
const t = i18n('This is a **__test__**.', {
Expand Down Expand Up @@ -251,6 +262,17 @@ describe('i18n', () => {
)
})

it('should not escape html entities or html numbers', () => {
const str = '<div> & but also html entity_name &amp;<p> and &#60; are so called \'html entity_number"'
expect(escapeHtml(str)).toBe(
'&lt;div&gt; &amp; but also html entity_name &amp;&lt;p&gt; and &#60; are so called &#039;html entity_number&quot;'
)
})
it('should not escape the already escaped', () => {
const str = '&lt;div&gt; &amp; &lt;p&gt; are so called &#039;html tags&quot;'
expect(escapeHtml(str)).toBe(str)
})

it('should handle undefined values with markdown', () => {
expect(
i18n(undefined, {
Expand Down