Skip to content

Commit 0039be6

Browse files
committed
js: config.js: Fix newline character handling in data-isso-* i18n strings
Replace escaped newline characters in data-isso attribute values with actual newline characters to ensure correct parsing and display. Fixes #912
1 parent 6dffaa7 commit 0039be6

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ Bugfixes & Improvements
2727
- Prevent auto creation of invalid links in comments (`#995`_, pkvach)
2828
- Fix W3C Validation issues (`#999`_, pkvach)
2929
- Handle deleted comments in Disqus migration (`#994`_, pkvach)
30+
- Fix newline character handling in data-isso-* i18n strings (`#992`_, pkvach)
3031

3132
.. _#951: https://github.com/posativ/isso/pull/951
3233
.. _#967: https://github.com/posativ/isso/pull/967
3334
.. _#983: https://github.com/posativ/isso/pull/983
3435
.. _#995: https://github.com/isso-comments/isso/pull/995
3536
.. _#999: https://github.com/isso-comments/isso/pull/999
3637
.. _#994: https://github.com/isso-comments/isso/pull/994
38+
.. _#992: https://github.com/isso-comments/isso/pull/992
3739

3840
0.13.1.dev0 (2023-02-05)
3941
------------------------

isso/js/app/config.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ for (var i = 0; i < js.length; i++) {
1616
for (var j = 0; j < js[i].attributes.length; j++) {
1717
var attr = js[i].attributes[j];
1818
if (/^data-isso-/.test(attr.name)) {
19+
20+
// Replace escaped newline characters in the attribute value with actual newline characters
21+
const attrValue = attr.value.replace(/\\n/g, '\n');
22+
1923
try {
2024
// Normalize underscores to dashes so that language-specific
2125
// strings can be caught better later on,
@@ -27,11 +31,11 @@ for (var i = 0; i < js.length; i++) {
2731
// not follow that convention, convert to lowercase here anyway.
2832
config[attr.name.substring(10)
2933
.replace(/_/g, '-')
30-
.toLowerCase()] = JSON.parse(attr.value);
34+
.toLowerCase()] = JSON.parse(attrValue);
3135
} catch (ex) {
3236
config[attr.name.substring(10)
3337
.replace(/_/g, '-')
34-
.toLowerCase()] = attr.value;
38+
.toLowerCase()] = attrValue;
3539
}
3640
}
3741
}

isso/js/tests/unit/config.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
"use strict";
1111

12+
beforeEach(() => {
13+
jest.resetModules();
14+
document.body.innerHTML = '';
15+
});
16+
1217
test("Client configuration - no languages", () => {
1318
// Mock navigator.languages = []
1419
global.languages = jest.spyOn(navigator, "languages", "get")
@@ -35,3 +40,21 @@ test("Client configuration - no languages", () => {
3540

3641
expect(config["langs"]).toStrictEqual(expected_langs);
3742
});
43+
44+
test("data-isso-* i18n strings should be accepted with newline characters", () => {
45+
46+
document.body.innerHTML =
47+
'<div id=isso-thread></div>' +
48+
// Note: `src` and `data-isso` need to be set,
49+
// else `api` fails to initialize!
50+
'<script src="http://isso.api/js/embed.min.js"'
51+
+ ' data-isso="/"'
52+
+ '</script>';
53+
54+
var script_tag = document.getElementsByTagName('script')[0];
55+
script_tag.setAttributeNS(null, 'data-isso-num-comments-text-en', "One comment\\n{{ n }} comments");
56+
57+
const config = require("app/config");
58+
59+
expect(config['num-comments-text-en']).toMatch("One comment\n{{ n }} comments");
60+
});

0 commit comments

Comments
 (0)