From 96429a98c7038cbd72e027b48331af73a503c179 Mon Sep 17 00:00:00 2001 From: Jakub Jankiewicz Date: Wed, 15 May 2024 00:37:28 +0200 Subject: [PATCH] add XML formatting targets #944 --- CHANGELOG.md | 2 + README.md | 2 +- __tests__/__snapshots__/terminal.spec.js.snap | 6 +++ __tests__/terminal.spec.js | 37 ++++++++++++++++++- js/xml_formatting.js | 29 ++++++++++++++- 5 files changed, 72 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f651ede..532b2a1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 623027d3..e5aafef2 100644 --- a/README.md +++ b/README.md @@ -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/) diff --git a/__tests__/__snapshots__/terminal.spec.js.snap b/__tests__/__snapshots__/terminal.spec.js.snap index 7b233437..9db380a4 100644 --- a/__tests__/__snapshots__/terminal.spec.js.snap +++ b/__tests__/__snapshots__/terminal.spec.js.snap @@ -787,6 +787,12 @@ exports[`Terminal utils xml formatting should format font 1`] = ` exports[`Terminal utils xml formatting should format font 2`] = `"
Hello World
Hello World
Hello World
"`; +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 diff --git a/__tests__/terminal.spec.js b/__tests__/terminal.spec.js index cdf6069e..7eecdb87 100644 --- a/__tests__/terminal.spec.js +++ b/__tests__/terminal.spec.js @@ -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 = $('
').terminal({}, {greetings: false}); }); afterEach(() => { + Object.keys(keys).forEach(key => { + $.terminal.xml_formatter[key] = keys[key]; + }); term.destroy(); }); it('should render tags', () => { @@ -534,10 +541,36 @@ describe('Terminal utils', function() { 'ls', 'Error: invalid message' ].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 = 'hello'; + 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 = 'hello'; + 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 = [ 'example.com', diff --git a/js/xml_formatting.js b/js/xml_formatting.js index 8e692b94..bceb9dab 100644 --- a/js/xml_formatting.js +++ b/js/xml_formatting.js @@ -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] === '/') { @@ -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);