Skip to content

Commit

Permalink
fix formatting JSON attributes when image url have html entites #937
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Apr 29, 2024
1 parent a68a635 commit 425d3b1
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 38 deletions.
4 changes: 2 additions & 2 deletions __tests__/__snapshots__/terminal.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ exports[`Terminal utils xml formatting should format font 1`] = `
[[;;;;;{\\"style\\":\\"--size:1.2;letter-spacing:2\\"}]Hello World]"
`;
exports[`Terminal utils xml formatting should format font 2`] = `"<div data-index=\\"0\\"><div style=\\"width: 100%;\\"><span style=\\"color:white;--color:white;--original-color:white;background-color:red;--background:red;--size:1.2;letter-spacing:2\\" data-text=\\"Hello&nbsp;World\\"><span>Hello&nbsp;World</span></span></div><div style=\\"width: 100%;\\"><span style=\\"background-color:red;--background:red\\" data-text=\\"Hello World\\"><span>Hello&nbsp;World</span></span></div><div class=\\"cmd-end-line\\" style=\\"width: 100%;\\"><span style=\\";--size:1.2;letter-spacing:2\\" data-text=\\"Hello&nbsp;World\\"><span>Hello&nbsp;World</span></span></div></div>"`;
exports[`Terminal utils xml formatting should format font 2`] = `"<div data-index=\\"0\\"><div style=\\"width: 100%;\\"><span style=\\"color:white;--color:white;--original-color:white;background-color:red;--background:red;--size:1.2;letter-spacing:2\\" data-text=\\"Hello&nbsp;World\\"><span>Hello&nbsp;World</span></span></div><div style=\\"width: 100%;\\"><span style=\\"background-color:red;--background:red\\" data-text=\\"Hello World\\"><span>Hello&nbsp;World</span></span></div><div class=\\"cmd-end-line\\" style=\\"width: 100%;\\"><span style=\\"--size:1.2;letter-spacing:2\\" data-text=\\"Hello&nbsp;World\\"><span>Hello&nbsp;World</span></span></div></div>"`;
exports[`Terminal utils xml formatting should handle invalid xml 1`] = `
"<< First Line [[[[ of Text <><> is little ]]] bit longer
Expand All @@ -803,7 +803,7 @@ exports[`Terminal utils xml formatting should render colors 1`] = `
exports[`Terminal utils xml formatting should render colors 2`] = `"<div data-index=\\"0\\"><div style=\\"width: 100%;\\"><span style=\\"color:white;--color:white;--original-color:white\\" class=\\"command\\" data-text=\\"ls\\"><span>ls</span></span></div><div style=\\"width: 100%;\\"><span style=\\"color:green;--color:green;--original-color:green\\" data-text=\\"ls\\"><span>ls</span></span></div><div class=\\"cmd-end-line\\" style=\\"width: 100%;\\"><span style=\\"color:red;--color:red;--original-color:red;background:red\\" class=\\"error\\" data-text=\\"Error:&nbsp;invalid&nbsp;message\\"><span>Error:&nbsp;invalid&nbsp;message</span></span></div></div>"`;
exports[`Terminal utils xml formatting should render links 1`] = `"<div data-index=\\"0\\"><div style=\\"width: 100%;\\"><a href=\\"https://example.com\\" target=\\"_blank\\" class=\\"link\\" data-text=\\"\\">example.com</a></div><div style=\\"width: 100%;\\"><a href=\\"example.com\\" data-text=\\"\\">example.com</a></div><div class=\\"cmd-end-line\\" style=\\"width: 100%;\\"><a href=\\"https://example.com\\" style=\\";background:red\\" data-text=\\"\\">Error:&nbsp;invalid&nbsp;message</a></div></div>"`;
exports[`Terminal utils xml formatting should render links 1`] = `"<div data-index=\\"0\\"><div style=\\"width: 100%;\\"><a href=\\"https://example.com\\" target=\\"_blank\\" class=\\"link\\" data-text=\\"\\">example.com</a></div><div style=\\"width: 100%;\\"><a href=\\"example.com\\" data-text=\\"\\">example.com</a></div><div class=\\"cmd-end-line\\" style=\\"width: 100%;\\"><a href=\\"https://example.com\\" style=\\"background:red\\" data-text=\\"\\">Error:&nbsp;invalid&nbsp;message</a></div></div>"`;
exports[`Terminal utils xml formatting should render tags 1`] = `
"[[;;;foo;;{\\"style\\":\\"color:red\\"}]text]
Expand Down
42 changes: 31 additions & 11 deletions js/jquery.terminal-2.40.6.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
*
* broken image by Sophia Bai from the Noun Project (CC-BY)
*
* Date: Mon, 29 Apr 2024 13:58:36 +0000
* Date: Mon, 29 Apr 2024 14:58:28 +0000
*/
/* global define, Map, BigInt */
/* eslint-disable */
Expand Down Expand Up @@ -5312,7 +5312,7 @@
// -------------------------------------------------------------------------
$.terminal = {
version: 'DEV',
date: 'Mon, 29 Apr 2024 13:58:36 +0000',
date: 'Mon, 29 Apr 2024 14:58:28 +0000',
// colors from https://www.w3.org/wiki/CSS/Properties/color/keywords
color_names: [
'transparent', 'currentcolor', 'black', 'silver', 'gray', 'white',
Expand Down Expand Up @@ -6929,20 +6929,40 @@
return result;
}
// -----------------------------------------------------------------
function find_semicolon(text) {
var index = 0;
var inside_entity = false;

while (index < text.length) {
if (text[index] === '&') {
inside_entity = true;
} else if (text[index] === ';' && !inside_entity) {
return index;
} else if (text[index] === ';') {
inside_entity = false;
}
index++;
}

return -1;
}
// -----------------------------------------------------------------
function format(s, style, color, background, _class, data_text, text) {
var attrs;
var valid_attrs = [];
if (data_text.match(/;/)) {
try {
var splitted = data_text.split(';');
var str = splitted.slice(1).join(';')
.replace(/&nbsp;/g, ' ')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>');
if (str.match(/^\s*\{[^}]*\}\s*$/)) {
attrs = JSON.parse(str);
valid_attrs = filter_attr_names(Object.keys(attrs));
data_text = splitted[0];
var semicolon = find_semicolon(data_text);
if (semicolon !== -1) {
var json = data_text.substring(semicolon + 1);
json = json.replace(/&nbsp;/g, ' ')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>');
if (json.match(/^\s*\{[^}]*\}\s*$/)) {
attrs = JSON.parse(json);
valid_attrs = filter_attr_names(Object.keys(attrs));
data_text = data_text.substring(0, semicolon);
}
}
} catch (e) {
}
Expand Down
4 changes: 2 additions & 2 deletions js/jquery.terminal-2.40.6.min.js

Large diffs are not rendered by default.

38 changes: 29 additions & 9 deletions js/jquery.terminal-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -6929,20 +6929,40 @@
return result;
}
// -----------------------------------------------------------------
function find_semicolon(text) {
var index = 0;
var inside_entity = false;

while (index < text.length) {
if (text[index] === '&') {
inside_entity = true;
} else if (text[index] === ';' && !inside_entity) {
return index;
} else if (text[index] === ';') {
inside_entity = false;
}
index++;
}

return -1;
}
// -----------------------------------------------------------------
function format(s, style, color, background, _class, data_text, text) {
var attrs;
var valid_attrs = [];
if (data_text.match(/;/)) {
try {
var splitted = data_text.split(';');
var str = splitted.slice(1).join(';')
.replace(/&nbsp;/g, ' ')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>');
if (str.match(/^\s*\{[^}]*\}\s*$/)) {
attrs = JSON.parse(str);
valid_attrs = filter_attr_names(Object.keys(attrs));
data_text = splitted[0];
var semicolon = find_semicolon(data_text);
if (semicolon !== -1) {
var json = data_text.substring(semicolon + 1);
json = json.replace(/&nbsp;/g, ' ')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>');
if (json.match(/^\s*\{[^}]*\}\s*$/)) {
attrs = JSON.parse(json);
valid_attrs = filter_attr_names(Object.keys(attrs));
data_text = data_text.substring(0, semicolon);
}
}
} catch (e) {
}
Expand Down
42 changes: 31 additions & 11 deletions js/jquery.terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
*
* broken image by Sophia Bai from the Noun Project (CC-BY)
*
* Date: Mon, 29 Apr 2024 13:58:36 +0000
* Date: Mon, 29 Apr 2024 14:58:28 +0000
*/
/* global define, Map, BigInt */
/* eslint-disable */
Expand Down Expand Up @@ -5312,7 +5312,7 @@
// -------------------------------------------------------------------------
$.terminal = {
version: 'DEV',
date: 'Mon, 29 Apr 2024 13:58:36 +0000',
date: 'Mon, 29 Apr 2024 14:58:28 +0000',
// colors from https://www.w3.org/wiki/CSS/Properties/color/keywords
color_names: [
'transparent', 'currentcolor', 'black', 'silver', 'gray', 'white',
Expand Down Expand Up @@ -6929,20 +6929,40 @@
return result;
}
// -----------------------------------------------------------------
function find_semicolon(text) {
var index = 0;
var inside_entity = false;

while (index < text.length) {
if (text[index] === '&') {
inside_entity = true;
} else if (text[index] === ';' && !inside_entity) {
return index;
} else if (text[index] === ';') {
inside_entity = false;
}
index++;
}

return -1;
}
// -----------------------------------------------------------------
function format(s, style, color, background, _class, data_text, text) {
var attrs;
var valid_attrs = [];
if (data_text.match(/;/)) {
try {
var splitted = data_text.split(';');
var str = splitted.slice(1).join(';')
.replace(/&nbsp;/g, ' ')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>');
if (str.match(/^\s*\{[^}]*\}\s*$/)) {
attrs = JSON.parse(str);
valid_attrs = filter_attr_names(Object.keys(attrs));
data_text = splitted[0];
var semicolon = find_semicolon(data_text);
if (semicolon !== -1) {
var json = data_text.substring(semicolon + 1);
json = json.replace(/&nbsp;/g, ' ')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>');
if (json.match(/^\s*\{[^}]*\}\s*$/)) {
attrs = JSON.parse(json);
valid_attrs = filter_attr_names(Object.keys(attrs));
data_text = data_text.substring(0, semicolon);
}
}
} catch (e) {
}
Expand Down
4 changes: 2 additions & 2 deletions js/jquery.terminal.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/jquery.terminal.min.js.map

Large diffs are not rendered by default.

0 comments on commit 425d3b1

Please sign in to comment.