aboutsummaryrefslogtreecommitdiff
path: root/dev/lib/handlebars/src/spec/index.whitespace_control.test.ts
diff options
context:
space:
mode:
authorDarius Jahandarie <djahandarie@gmail.com>2023-11-04 18:45:57 +0900
committerDarius Jahandarie <djahandarie@gmail.com>2023-11-04 18:45:57 +0900
commitef79eab44bfd000792c610b968b5ceefd41e76a0 (patch)
tree48b04f30f6248caedbd880801aa49402a9e8066a /dev/lib/handlebars/src/spec/index.whitespace_control.test.ts
parent376151096431d4362e4baaacf0cef4a534e169f7 (diff)
Modernize codebase
- Use ES modules - Remove vendored libs and build them from npm using esbuild - Switch from JSZip to zip.js
Diffstat (limited to 'dev/lib/handlebars/src/spec/index.whitespace_control.test.ts')
-rw-r--r--dev/lib/handlebars/src/spec/index.whitespace_control.test.ts88
1 files changed, 88 insertions, 0 deletions
diff --git a/dev/lib/handlebars/src/spec/index.whitespace_control.test.ts b/dev/lib/handlebars/src/spec/index.whitespace_control.test.ts
new file mode 100644
index 00000000..1f7cf019
--- /dev/null
+++ b/dev/lib/handlebars/src/spec/index.whitespace_control.test.ts
@@ -0,0 +1,88 @@
+/*
+ * This file is forked from the handlebars project (https://github.com/handlebars-lang/handlebars.js),
+ * and may include modifications made by Elasticsearch B.V.
+ * Elasticsearch B.V. licenses this file to you under the MIT License.
+ * See `packages/kbn-handlebars/LICENSE` for more information.
+ */
+
+import { expectTemplate } from '../__jest__/test_bench';
+
+describe('whitespace control', () => {
+ it('should strip whitespace around mustache calls', () => {
+ const hash = { foo: 'bar<' };
+ expectTemplate(' {{~foo~}} ').withInput(hash).toCompileTo('bar&lt;');
+ expectTemplate(' {{~foo}} ').withInput(hash).toCompileTo('bar&lt; ');
+ expectTemplate(' {{foo~}} ').withInput(hash).toCompileTo(' bar&lt;');
+ expectTemplate(' {{~&foo~}} ').withInput(hash).toCompileTo('bar<');
+ expectTemplate(' {{~{foo}~}} ').withInput(hash).toCompileTo('bar<');
+ expectTemplate('1\n{{foo~}} \n\n 23\n{{bar}}4').toCompileTo('1\n23\n4');
+ });
+
+ describe('blocks', () => {
+ it('should strip whitespace around simple block calls', () => {
+ const hash = { foo: 'bar<' };
+
+ expectTemplate(' {{~#if foo~}} bar {{~/if~}} ').withInput(hash).toCompileTo('bar');
+ expectTemplate(' {{#if foo~}} bar {{/if~}} ').withInput(hash).toCompileTo(' bar ');
+ expectTemplate(' {{~#if foo}} bar {{~/if}} ').withInput(hash).toCompileTo(' bar ');
+ expectTemplate(' {{#if foo}} bar {{/if}} ').withInput(hash).toCompileTo(' bar ');
+
+ expectTemplate(' \n\n{{~#if foo~}} \n\nbar \n\n{{~/if~}}\n\n ')
+ .withInput(hash)
+ .toCompileTo('bar');
+
+ expectTemplate(' a\n\n{{~#if foo~}} \n\nbar \n\n{{~/if~}}\n\na ')
+ .withInput(hash)
+ .toCompileTo(' abara ');
+ });
+
+ it('should strip whitespace around inverse block calls', () => {
+ expectTemplate(' {{~^if foo~}} bar {{~/if~}} ').toCompileTo('bar');
+ expectTemplate(' {{^if foo~}} bar {{/if~}} ').toCompileTo(' bar ');
+ expectTemplate(' {{~^if foo}} bar {{~/if}} ').toCompileTo(' bar ');
+ expectTemplate(' {{^if foo}} bar {{/if}} ').toCompileTo(' bar ');
+ expectTemplate(' \n\n{{~^if foo~}} \n\nbar \n\n{{~/if~}}\n\n ').toCompileTo('bar');
+ });
+
+ it('should strip whitespace around complex block calls', () => {
+ const hash = { foo: 'bar<' };
+
+ expectTemplate('{{#if foo~}} bar {{~^~}} baz {{~/if}}').withInput(hash).toCompileTo('bar');
+ expectTemplate('{{#if foo~}} bar {{^~}} baz {{/if}}').withInput(hash).toCompileTo('bar ');
+ expectTemplate('{{#if foo}} bar {{~^~}} baz {{~/if}}').withInput(hash).toCompileTo(' bar');
+ expectTemplate('{{#if foo}} bar {{^~}} baz {{/if}}').withInput(hash).toCompileTo(' bar ');
+ expectTemplate('{{#if foo~}} bar {{~else~}} baz {{~/if}}').withInput(hash).toCompileTo('bar');
+
+ expectTemplate('\n\n{{~#if foo~}} \n\nbar \n\n{{~^~}} \n\nbaz \n\n{{~/if~}}\n\n')
+ .withInput(hash)
+ .toCompileTo('bar');
+
+ expectTemplate('\n\n{{~#if foo~}} \n\n{{{foo}}} \n\n{{~^~}} \n\nbaz \n\n{{~/if~}}\n\n')
+ .withInput(hash)
+ .toCompileTo('bar<');
+
+ expectTemplate('{{#if foo~}} bar {{~^~}} baz {{~/if}}').toCompileTo('baz');
+ expectTemplate('{{#if foo}} bar {{~^~}} baz {{/if}}').toCompileTo('baz ');
+ expectTemplate('{{#if foo~}} bar {{~^}} baz {{~/if}}').toCompileTo(' baz');
+ expectTemplate('{{#if foo~}} bar {{~^}} baz {{/if}}').toCompileTo(' baz ');
+ expectTemplate('{{#if foo~}} bar {{~else~}} baz {{~/if}}').toCompileTo('baz');
+ expectTemplate('\n\n{{~#if foo~}} \n\nbar \n\n{{~^~}} \n\nbaz \n\n{{~/if~}}\n\n').toCompileTo(
+ 'baz'
+ );
+ });
+ });
+
+ it('should strip whitespace around partials', () => {
+ expectTemplate('foo {{~> dude~}} ').withPartials({ dude: 'bar' }).toCompileTo('foobar');
+ expectTemplate('foo {{> dude~}} ').withPartials({ dude: 'bar' }).toCompileTo('foo bar');
+ expectTemplate('foo {{> dude}} ').withPartials({ dude: 'bar' }).toCompileTo('foo bar ');
+ expectTemplate('foo\n {{~> dude}} ').withPartials({ dude: 'bar' }).toCompileTo('foobar');
+ expectTemplate('foo\n {{> dude}} ').withPartials({ dude: 'bar' }).toCompileTo('foo\n bar');
+ });
+
+ it('should only strip whitespace once', () => {
+ expectTemplate(' {{~foo~}} {{foo}} {{foo}} ')
+ .withInput({ foo: 'bar' })
+ .toCompileTo('barbar bar ');
+ });
+});