Skip to content

Commit

Permalink
add XML formatting targets #944
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed May 14, 2024
1 parent 276f7cd commit 96429a9
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
## 2.42.0
### Features
* add reverse style to formatting and XML [#943](https://github.com/jcubic/jquery.terminal/issues/943)
* add XML formatting targets [#944](https://github.com/jcubic/jquery.terminal/issues/944)
### Bugfix
* fix typing animation on emtpy lines [#946](https://github.com/jcubic/jquery.terminal/issues/946)
* fix skip typing animation [#945](https://github.com/jcubic/jquery.terminal/issues/945)
* fix use of `$.terminal.apply_formatters` with prims

## 2.41.2
### Bugfix
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![npm](https://img.shields.io/badge/npm-DEV-blue.svg)](https://www.npmjs.com/package/jquery.terminal)
![bower](https://img.shields.io/badge/bower-DEV-yellow.svg)
[![Build and test](https://github.com/jcubic/jquery.terminal/actions/workflows/build.yaml/badge.svg?branch=devel&event=push)](https://github.com/jcubic/jquery.terminal/actions/workflows/build.yaml)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/jquery.terminal/badge.svg?branch=devel&55fa1beff371fcfe8bf972eccb9b2346)](https://coveralls.io/github/jcubic/jquery.terminal?branch=devel)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/jquery.terminal/badge.svg?branch=devel&0a0cffb745aa3539685373063c8867d5)](https://coveralls.io/github/jcubic/jquery.terminal?branch=devel)
![NPM Downloads](https://img.shields.io/npm/dm/jquery.terminal.svg?style=flat)
[![jsDelivr Downloads](https://data.jsdelivr.com/v1/package/npm/jquery.terminal/badge?style=rounded&n=1)](https://www.jsdelivr.com/package/npm/jquery.terminal)
[![Paid Support](https://img.shields.io/badge/paid-support-354465.svg)](https://support.jcubic.pl/)
Expand Down
6 changes: 6 additions & 0 deletions __tests__/__snapshots__/terminal.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,12 @@ exports[`Terminal utils xml formatting should format font 1`] = `
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 for give target 1`] = `"[[;white;;;;{}]hello]"`;
exports[`Terminal utils xml formatting should format for give target 2`] = `"[[;white;;;;{}]hello]"`;
exports[`Terminal utils xml formatting should format for give target 3`] = `"[[;white;;;;{}]hello]"`;
exports[`Terminal utils xml formatting should handle invalid xml 1`] = `
"<< First Line [[[[ of Text <><> is little ]]] bit longer
<<<<>>>> Second ]]]] Line of Text
Expand Down
37 changes: 35 additions & 2 deletions __tests__/terminal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,19 @@ describe('Terminal utils', function() {
});
});
});
describe('xml formatting', function() {
fdescribe('xml formatting', function() {
let term;
let keys = {};
beforeEach(() => {
Object.keys($.terminal.xml_formatter).forEach(key => {
keys[key] = $.terminal.xml_formatter[key];
});
term = $('<div/>').terminal({}, {greetings: false});
});
afterEach(() => {
Object.keys(keys).forEach(key => {
$.terminal.xml_formatter[key] = keys[key];
});
term.destroy();
});
it('should render tags', () => {
Expand Down Expand Up @@ -534,10 +541,36 @@ describe('Terminal utils', function() {
'<green>ls</green>',
'<red class="error" style="background:red">Error: invalid message</red>'
].join('\n');
expect($.terminal.apply_formatters(input)).toMatchSnapshot();
expect($.terminal.xml_formatter(input)).toMatchSnapshot();
term.echo(input);
expect(term.find('.terminal-output').html()).toMatchSnapshot();
});
it('should format for give target', () => {
var input = '<white>hello</white>';
var targets = [
{ echo: true },
{ command: true },
{ animation: true }
];
targets.forEach(target => {
expect($.terminal.xml_formatter(input, target)).toMatchSnapshot();
});
});
it('should ignore formatting', () => {
['echo', 'animation', 'prompt', 'command'].forEach(key => {
$.terminal.xml_formatter[key] = false;
});
var input = '<white>hello</white>';
var targets = [
{ echo: true },
{ command: true },
{ prompt: true },
{ animation: true }
];
targets.forEach(target => {
expect($.terminal.xml_formatter(input, target)).toEqual(input);
});
});
it('should render links', () => {
const input = [
'<a href="https://example.com" target="_blank" class="link">example.com</a>',
Expand Down
29 changes: 28 additions & 1 deletion js/xml_formatting.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,28 @@
tags.i = tags.italic;
tags.r = tags.reverse;
var tag_re = /(<\/?\s*[a-zA-Z]+(?:\s?[^>]+)?>)/;
function xml_formatter(string) {
function no_target(options) {
if (!options) {
return true;
}
return Object.keys(options).length === 1 && 'position' in options;
}
function should_render(options) {
if (no_target(options)) {
return true;
}
for (var i = targets.length; i--;) {
var prop = targets[i];
if (options[prop] === true && $.terminal.xml_formatter[prop] === true) {
return true;
}
}
return false;
}
function xml_formatter(string, options) {
if (!should_render(options)) {
return string;
}
return string.split(tag_re).map(function(string) {
if (string.match(tag_re)) {
if (string[1] === '/') {
Expand Down Expand Up @@ -165,6 +186,12 @@
}
xml_formatter.__no_warn__ = true;
xml_formatter.tags = tags;

var targets = ['echo', 'prompt', 'animation', 'command'];
targets.forEach(function(target) {
xml_formatter[target] = true;
});

$.terminal.defaults.allowedAttributes.push('style');
$.terminal.xml_formatter = xml_formatter;
$.terminal.new_formatter(xml_formatter);
Expand Down

0 comments on commit 96429a9

Please sign in to comment.