Skip to content

Commit

Permalink
🐛 Fix Renderer#addSubtemplate(template, variables) not preparing va…
Browse files Browse the repository at this point in the history
…riables properly
  • Loading branch information
skerit committed Feb 12, 2024
1 parent 166c528 commit 1722a33
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Fix setting `value` not working server-side on `<textarea>` elements
* Add `Renderer#handleError()` method
* Newlines are no longer an expression delimiter, only semicolons are
* Fix `Renderer#addSubtemplate(template, variables)` not preparing variables properly

## 2.3.15 (2023-11-27)

Expand Down
8 changes: 6 additions & 2 deletions lib/core/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3036,7 +3036,7 @@ Renderer.setCommand(function bottom() {
*
* @author Jelle De Loecker <[email protected]>
* @since 1.0.0
* @version 2.3.7
* @version 2.3.16
*
* @param {Array|String} names The partial to render
* @param {Object} options
Expand Down Expand Up @@ -3067,10 +3067,14 @@ Renderer.setMethod(function addSubtemplate(names, options, variables) {
templates.is_subtemplate = true;

// Get the current element
variables.$ancestor_element = this.$0;
const ancestor_element = this.$0;

placeholder = this.async(function renderSubtemplate(next) {

// The Templates render method expects fully prepared variables.
variables = this.prepareVariables(variables);
variables.$ancestor_element = ancestor_element;

templates.render(variables).done(function done(err, block) {

if (err) {
Expand Down
7 changes: 3 additions & 4 deletions lib/element/html_element_extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ Blast.definePrototype(Element, 'incrementIndexInParent', function incrementIndex
*/
Blast.definePrototype(Element, 'transferStylesTo', function transferStylesTo(target, properties) {

var styles = window.getComputedStyle(this),
let styles = window.getComputedStyle(this),
key,
i;

if (!properties) {
Expand Down Expand Up @@ -285,13 +286,11 @@ Blast.definePrototype(Element, 'isFocusable', function isFocusable() {
*/
Blast.definePrototype(Element, 'isTabbable', function isTabbable() {

var tabindex;

if (!ProtoEl.isFocusable.call(this)) {
return false;
}

tabindex = this.getAttribute('tabindex');
let tabindex = this.getAttribute('tabindex');

if (tabindex && tabindex[0] == '-') {
return false;
Expand Down
96 changes: 75 additions & 21 deletions test/30-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ var assert = require('assert'),
Hawkejs = require('../index.js'),
hawkejs;

const Blast = __Protoblast;

const RENDER = (done, ...render_args) => {
let callback = render_args.pop();
return hawkejs.render(...render_args, (...args) => {
try {
callback(...args);
} catch (err) {
done(err);
}
});
}

describe('Renderer', function() {

before(function() {
Expand All @@ -23,7 +36,7 @@ describe('Renderer', function() {

var compiled = hawkejs.compile('async_test_1', code);

hawkejs.render(compiled, {}, function rendered(err, res) {
RENDER(done, compiled, {}, function rendered(err, res) {

if (err) {
throw err;
Expand All @@ -40,7 +53,7 @@ describe('Renderer', function() {
describe('#assign(name)', function() {
it('sets the wanted block content', function(done) {

hawkejs.render('assign_test', function doneAssignTest(err, result) {
RENDER(done, 'assign_test', function doneAssignTest(err, result) {

if (err) {
throw err;
Expand All @@ -59,7 +72,7 @@ This is the internally set main

describe('#assign_end()', function() {
it('should claim the siblings', function(done) {
hawkejs.render('assign_end', function doneAssignTest(err, result) {
RENDER(done, 'assign_end', function doneAssignTest(err, result) {

if (err) {
throw err;
Expand All @@ -74,7 +87,7 @@ This is the internally set main

describe('#expands(template)', function() {
it('expands into the given template', function(done) {
hawkejs.render('expand_test', function doneExpandTest(err, result) {
RENDER(done, 'expand_test', function doneExpandTest(err, result) {

if (err) {
throw err;
Expand All @@ -93,7 +106,7 @@ This is the main content

it('should be possible to use the {% extend %} expression instead', function(done) {

hawkejs.render('extend_test', function doneExpandTest(err, result) {
RENDER(done, 'extend_test', function doneExpandTest(err, result) {

if (err) {
throw err;
Expand All @@ -115,7 +128,7 @@ This is the main content
describe('#start(name, options)', function() {
it('should add block classes if given', function(done) {

hawkejs.render('assign_test_class', function doneExpandTest(err, result) {
RENDER(done, 'assign_test_class', function doneExpandTest(err, result) {

if (err) {
throw err;
Expand All @@ -128,9 +141,50 @@ This is the main content
});
});

describe('#addSubTemplate(template, options)', () => {
it('should correctly convert the variables', (done) => {

let CloneTestClass = Blast.Collection.Function.inherits(null, function CloneTestClass(value) {
this.value = value;
});

CloneTestClass.setMethod(function toHawkejs() {
return new CloneTestClass(this.value + '!!!');
});

let value = new CloneTestClass('test');

let renderer = hawkejs.createRenderer();
renderer.set('test_instance', value);

let source = `The value is: "<%= test_instance.value %>"`;

let compiled = hawkejs.compile(source);
let placeholder = renderer.addSubtemplate(compiled, {print: false});

//let cloned = Blast.Bound.JSON.clone(value, 'toHawkejs')

placeholder.getContent(function gotContent(err, block_buffer) {

if (err) {
return done(err);
}

try {
assert.strictEqual(block_buffer.toHTML(), 'The value is: "test!!!"');
} catch (err) {
return done(err);
}

done();
});

});
});

describe('#implement(name)', function() {
it('should print the given element', function(done) {
hawkejs.render('implement_test', function doneImplement(err, result) {
RENDER(done, 'implement_test', function doneImplement(err, result) {

if (err) {
throw err;
Expand All @@ -142,7 +196,7 @@ This is the main content
});

it('should include assignments', function(done) {
hawkejs.render('implement_blocks', function doneImplement(err, result) {
RENDER(done, 'implement_blocks', function doneImplement(err, result) {

if (err) {
throw err;
Expand All @@ -157,7 +211,7 @@ This is the main content
});

it('should work on assignments', function(done) {
hawkejs.render('expand_start_implement_assign', function doneImplement(err, result) {
RENDER(done, 'expand_start_implement_assign', function doneImplement(err, result) {

if (err) {
throw err;
Expand All @@ -175,7 +229,7 @@ This is the main content

describe('#include(name)', function() {
it('should only be executed if in a used block', function(done) {
hawkejs.render('expand_start_include_assign', function doneImplement(err, result) {
RENDER(done, 'expand_start_include_assign', function doneImplement(err, result) {

if (err) {
throw err;
Expand All @@ -192,7 +246,7 @@ This is the main content

it('should set the correct ancestor element', function(done) {

hawkejs.render('include_parent_test', function doneImplement(err, result) {
RENDER(done, 'include_parent_test', function doneImplement(err, result) {

if (err) {
throw err;
Expand All @@ -213,7 +267,7 @@ This is the main content
count : 0,
};

hawkejs.render('include_with_variables', root_variables, function doneImplement(err, result) {
RENDER(done, 'include_with_variables', root_variables, function doneImplement(err, result) {

if (err) {
throw err;
Expand Down Expand Up @@ -255,7 +309,7 @@ This is the main content

describe('#partial(name)', function() {
it('should render a partial in a new scope', function(done) {
hawkejs.render('print_partial', function donePartial(err, result) {
RENDER(done, 'print_partial', function donePartial(err, result) {

if (err) {
throw err;
Expand All @@ -277,7 +331,7 @@ This is the main content
});

it('should allow using scoped blocks with implements', function(done) {
hawkejs.render('partial_with_implements', function donePartial(err, result) {
RENDER(done, 'partial_with_implements', function donePartial(err, result) {

if (err) {
throw err;
Expand All @@ -293,7 +347,7 @@ This is the main content

describe('Template#switchTemplate(name, variables)', function() {
it('should allow using scoped blocks that switch target blocks', function(done) {
hawkejs.render('partial_with_switch_template', function donePartial(err, result) {
RENDER(done, 'partial_with_switch_template', function donePartial(err, result) {

if (err) {
throw err;
Expand All @@ -307,7 +361,7 @@ This is the main content
});

it('should allow switching to another template in the start template', function(done) {
hawkejs.render('partial_with_root_switch_and_implement', function donePartial(err, result) {
RENDER(done, 'partial_with_root_switch_and_implement', function donePartial(err, result) {

if (err) {
throw err;
Expand All @@ -322,7 +376,7 @@ This is the main content
describe('#foundation()', function() {
it('should not wait for placeholders that are waiting for itself', function(done) {

let renderer = hawkejs.render('implement_with_foundation_extension', function doneImplement(err, result) {
let renderer = RENDER(done, 'implement_with_foundation_extension', function doneImplement(err, result) {

if (err) {
return done(err);
Expand Down Expand Up @@ -356,7 +410,7 @@ This is the main content
describe('#setTheme(name)', function() {
it('should set the theme to use for partials', function(done) {

let renderer = hawkejs.render('implement_blocks', function doneImplement(err, result) {
let renderer = RENDER(done, 'implement_blocks', function doneImplement(err, result) {

if (err) {
throw err;
Expand All @@ -371,7 +425,7 @@ This is the main content

it('should set the theme to use for extensions', function(done) {

let renderer = hawkejs.render('expand_test', function doneImplement(err, result) {
let renderer = RENDER(done, 'expand_test', function doneImplement(err, result) {

if (err) {
throw err;
Expand All @@ -388,7 +442,7 @@ This is the main content
describe('#$0', function() {
it('is a local property that refers to the current element', function(done) {

let renderer = hawkejs.render('local_property_test', function doneTest(err, result) {
let renderer = RENDER(done, 'local_property_test', function doneTest(err, result) {

if (err) {
throw err;
Expand All @@ -412,7 +466,7 @@ This is the main content

describe('#style()', function() {
it('should add stylesheet elements', function(done) {
hawkejs.render('style_test', function doneImplement(err, result) {
RENDER(done, 'style_test', function doneImplement(err, result) {

if (err) {
throw err;
Expand Down

0 comments on commit 1722a33

Please sign in to comment.