Skip to content

Commit

Permalink
Merge pull request #992 from pkvach/fix/handling-i18n-data-attribute-…
Browse files Browse the repository at this point in the history
…values

js: config.js: Fix newline character handling in data-isso-* i18n strings
  • Loading branch information
ix5 authored Apr 23, 2024
2 parents 0986104 + 953051b commit e3ee8fb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
4 changes: 2 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ New Features
.. _#966: https://github.com/posativ/isso/pull/966
.. _#998: https://github.com/isso-comments/isso/pull/998
.. _#1000: https://github.com/isso-comments/isso/pull/1000
.. _#966: https://github.com/posativ/isso/pull/966
.. _#998: https://github.com/isso-comments/isso/pull/998
.. _#1001: https://github.com/isso-comments/isso/pull/1001

Breaking Changes
Expand All @@ -34,6 +32,7 @@ Bugfixes & Improvements
- Fix W3C Validation issues (`#999`_, pkvach)
- Handle deleted comments in Disqus migration (`#994`_, pkvach)
- Fix total comments count calculation (`#997`_, pkvach)
- Fix newline character handling in data-isso-* i18n strings (`#992`_, pkvach)

.. _#951: https://github.com/posativ/isso/pull/951
.. _#967: https://github.com/posativ/isso/pull/967
Expand All @@ -42,6 +41,7 @@ Bugfixes & Improvements
.. _#999: https://github.com/isso-comments/isso/pull/999
.. _#994: https://github.com/isso-comments/isso/pull/994
.. _#997: https://github.com/isso-comments/isso/pull/997
.. _#992: https://github.com/isso-comments/isso/pull/992

0.13.1.dev0 (2023-02-05)
------------------------
Expand Down
31 changes: 17 additions & 14 deletions isso/js/app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,25 @@ for (var i = 0; i < js.length; i++) {
for (var j = 0; j < js[i].attributes.length; j++) {
var attr = js[i].attributes[j];
if (/^data-isso-/.test(attr.name)) {
try {
// Normalize underscores to dashes so that language-specific
// strings can be caught better later on,
// e.g. data-isso-postbox-text-text-PT_BR becomes
// postbox-text-text-pt-br.
// Also note that attr.name only gives lowercase strings as per
// HTML spec, e.g. data-isso-FOO-Bar becomes foo-bar, but since
// the test environment's jest-environment-jsdom seemingly does
// not follow that convention, convert to lowercase here anyway.
config[attr.name.substring(10)

// Normalize underscores to dashes so that language-specific
// strings can be caught better later on, e.g.
// data-isso-postbox-text-text-PT_BR becomes postbox-text-text-pt-br.
// Also note that attr.name only gives lowercase strings as per HTML
// spec, e.g. data-isso-FOO-Bar becomes foo-bar, but since the test
// environment's jest-environment-jsdom seemingly does not follow
// that convention, convert to lowercase here anyway.
const attrName = attr.name.substring(10)
.replace(/_/g, '-')
.toLowerCase()] = JSON.parse(attr.value);
.toLowerCase()

// Replace escaped newline characters in the attribute value with actual newline characters
const attrValue = attr.value.replace(/\\n/g, '\n');

try {
config[attrName] = JSON.parse(attrValue);
} catch (ex) {
config[attr.name.substring(10)
.replace(/_/g, '-')
.toLowerCase()] = attr.value;
config[attrName] = attrValue;
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions isso/js/tests/unit/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

"use strict";

beforeEach(() => {
jest.resetModules();
document.body.innerHTML = '';
});

test("Client configuration - no languages", () => {
// Mock navigator.languages = []
global.languages = jest.spyOn(navigator, "languages", "get")
Expand All @@ -35,3 +40,21 @@ test("Client configuration - no languages", () => {

expect(config["langs"]).toStrictEqual(expected_langs);
});

test("data-isso-* i18n strings should be accepted with newline characters", () => {

document.body.innerHTML =
'<div id=isso-thread></div>' +
// Note: `src` and `data-isso` need to be set,
// else `api` fails to initialize!
'<script src="http://isso.api/js/embed.min.js"'
+ ' data-isso="/"'
+ '</script>';

var script_tag = document.getElementsByTagName('script')[0];
script_tag.setAttributeNS(null, 'data-isso-num-comments-text-en', "One comment\\n{{ n }} comments");

const config = require("app/config");

expect(config['num-comments-text-en']).toMatch("One comment\n{{ n }} comments");
});

0 comments on commit e3ee8fb

Please sign in to comment.