Skip to content

Commit 65f7143

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 0986104 commit 65f7143

3 files changed

Lines changed: 31 additions & 4 deletions

File tree

CHANGES.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ New Features
1515
.. _#966: https://github.com/posativ/isso/pull/966
1616
.. _#998: https://github.com/isso-comments/isso/pull/998
1717
.. _#1000: https://github.com/isso-comments/isso/pull/1000
18-
.. _#966: https://github.com/posativ/isso/pull/966
19-
.. _#998: https://github.com/isso-comments/isso/pull/998
2018
.. _#1001: https://github.com/isso-comments/isso/pull/1001
2119

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

3837
.. _#951: https://github.com/posativ/isso/pull/951
3938
.. _#967: https://github.com/posativ/isso/pull/967
@@ -42,6 +41,7 @@ Bugfixes & Improvements
4241
.. _#999: https://github.com/isso-comments/isso/pull/999
4342
.. _#994: https://github.com/isso-comments/isso/pull/994
4443
.. _#997: https://github.com/isso-comments/isso/pull/997
44+
.. _#992: https://github.com/isso-comments/isso/pull/992
4545

4646
0.13.1.dev0 (2023-02-05)
4747
------------------------

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)