Skip to content

Commit

Permalink
js: config.js: Fix newline character handling in data-isso-* i18n str…
Browse files Browse the repository at this point in the history
…ings

Replace escaped newline characters in data-isso attribute values with actual newline characters to ensure correct parsing and display.

Fixes #912
  • Loading branch information
pkvach committed Apr 14, 2024
1 parent eb35b17 commit 66c7c65
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ Bugfixes & Improvements
- Prevent auto creation of invalid links in comments (`#995`_, pkvach)
- Fix W3C Validation issues (`#999`_, pkvach)
- Handle deleted comments in Disqus migration (`#994`_, 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
.. _#983: https://github.com/posativ/isso/pull/983
.. _#995: https://github.com/isso-comments/isso/pull/995
.. _#999: https://github.com/isso-comments/isso/pull/999
.. _#994: https://github.com/isso-comments/isso/pull/994
.. _#992: https://github.com/isso-comments/isso/pull/992

0.13.1.dev0 (2023-02-05)
------------------------
Expand Down
8 changes: 6 additions & 2 deletions isso/js/app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ 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)) {

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

try {
// Normalize underscores to dashes so that language-specific
// strings can be caught better later on,
Expand All @@ -27,11 +31,11 @@ for (var i = 0; i < js.length; i++) {
// not follow that convention, convert to lowercase here anyway.
config[attr.name.substring(10)
.replace(/_/g, '-')
.toLowerCase()] = JSON.parse(attr.value);
.toLowerCase()] = JSON.parse(attrValue);
} catch (ex) {
config[attr.name.substring(10)
.replace(/_/g, '-')
.toLowerCase()] = attr.value;
.toLowerCase()] = 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 66c7c65

Please sign in to comment.