Skip to content

Commit

Permalink
⚡️ Prepare text nodes during compiling
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Apr 29, 2024
1 parent 831af14 commit 43c3a9c
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Use methods for getting (and proxy traps for setting) variables on the `Variables` class
* Refactor variable access in compiled template code
* Only clone certain variables once, when they are initially set
* Prepare text nodes during compiling

## 2.3.19 (2024-04-13)

Expand Down
7 changes: 7 additions & 0 deletions benchmark/05-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const RAW_VARIABLES = {
map: new Map([['a', 1], ['b', 2]]),
},
f_date : new Date('2024-04-27 14:33:01'),
g_string : '<p><br><a>',
h_array : [],
};

const createRawVariables = () => {
Expand Down Expand Up @@ -69,6 +71,11 @@ suite('Renderer', function() {
return renderer.renderHTML(VARIABLE_TEST_TEMPLATE, createRawVariables());
});

bench('renderHTML() (Full example)', function() {
let renderer = hawkejs.createRenderer();
return renderer.renderHTML('bench', createRawVariables());
});

bench('toDry()', function() {
let renderer = hawkejs.createRenderer();
let variables = main.createTestVariables();
Expand Down
16 changes: 16 additions & 0 deletions lib/core/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,22 @@ Renderer.setCommand(function printUnsafe(content) {
return this.actualPrint(content);
});

/**
* Print the string as a text node
*
* @author Jelle De Loecker <[email protected]>
* @since 2.4.0
* @version 2.4.0
*
* @param {String} content
*
* @return {Number} The index of the pushed line
*/
Renderer.setMethod(function printTextNode(content) {
content = Hawkejs.Hawkejs.createTextNode(content);
return this.actualPrint(content);
});

/**
* Safely print the contents
*
Expand Down
5 changes: 3 additions & 2 deletions lib/parser/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ Builder.setMethod(function _compileBody(subroutine, body) {
*
* @author Jelle De Loecker <[email protected]>
* @since 2.2.0
* @version 2.3.8
* @version 2.4.0
*
* @param {Hawkejs.Parser.Subroutine} subroutine
* @param {Object} entry
Expand All @@ -678,7 +678,8 @@ Builder.setMethod(function _compileEntry(subroutine, entry) {
this._compileEntry(subroutine, child);
}
} else if (entry.type == 'text') {
subroutine.appendCall('__render.printUnsafe', [entry.value]);
let value = Bound.String.decodeHTML(entry.value);
subroutine.appendCall('__render.printTextNode', [value]);
} else if (entry.type == 'open_tag') {

let args = [],
Expand Down
2 changes: 1 addition & 1 deletion test/00-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('Hawkejs', function() {

assert.strictEqual(fnc.name, 'compiledView');
assert.strictEqual(fnc.source_name.indexOf('inline'), 0);
assert.strictEqual(code.indexOf('.printUnsafe("this is inline ') > -1, true, 'Print was cut off');
assert.strictEqual(code.indexOf('.printTextNode("this is inline ') > -1, true, 'Print was cut off');
});

it('should add line information to multiline javascript', function() {
Expand Down
40 changes: 40 additions & 0 deletions test/templates/bench.hwk
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{% extend "base_scene" %}

{% set my_value = 47 %}

{% block "main" %}
<h1>Welcome to the Benchmark page!</h1>
<p>
This template is used to test the performance of the framework.
</p>
<p>
Most elements on this page won't do anything.
</p>
<div>
<a id="link-to-welcome" href="/welcome">Go to welcome</a><br>
<a id="link-to-welcome-without-history" href="/welcome" data-he-history="false">
Go to welcome without history
</a><br>
</div>
<div>
<ul>
<li>A simple string: {{ b_string }}</li>
<li>A string containg HTML tags: {{ g_string }}</li>
</ul>
</div>
{% if a_number gt 0 %}
<p>`a_number` is greater than 0: {{ a_number }}</p>
{% /if %}
{% if does.not.exist gt 0 %}
WRONG
{% else %}
<p>`does.not.exist` was successfully detected as not-existing</p>
{% /if %}
{% if h_array %}
WRONG
{% else %}
<p>`h_array` was successfully detected as empty</p>
{% /if %}
<p>Getting `a` from a map: {{ e_object.map.get('a') }}</p>
<p>`my_value` should be 47: {{ my_value }}</p>
{% /block %}

0 comments on commit 43c3a9c

Please sign in to comment.