Skip to content

Commit

Permalink
Merge pull request marko-js#734 from marko-js/655-renderToString-browser
Browse files Browse the repository at this point in the history
Fixes marko-js#655 - Implement renderToString in the browser.
  • Loading branch information
patrick-steele-idem authored Jun 13, 2017
2 parents aa6c293 + 8cdb09a commit 2951a6e
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/runtime/vdom/AsyncVDOMBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,17 @@ var proto = AsyncVDOMBuilder.prototype = {
},

toString: function() {
return this.___getNode().outerHTML;
var docFragment = this.___getNode();
var html = '';

if (docFragment.hasChildNodes()) {
var children = docFragment.childNodes;
for (var i = 0; i < children.length; i++) {
html += children[i].outerHTML;
}
}

return html;
},

then: function(fn, fnErr) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var helloComponent = require('./components/hello');

module.exports = {
onMount: function() {
var self = this;
helloComponent.renderToString({
name: this.input.name
}, function (error, html) {
if (error) {
self.emit('renderError', error);
} else {
self.emit('html', html);
}
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
<test-async name=input.name/>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = function(input, out) {
var asyncOut = out.beginAsync();
setTimeout(function() {
asyncOut.write('[async] ' + input.name);
asyncOut.end();
}, 10);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
<div name=input.name/>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var expect = require('chai').expect;

module.exports = function(helpers, done) {
var component = helpers.mount(require('./index'), {
name: 'john'
});

component.on('html', function(renderedHtml) {
expect(renderedHtml).to.equal('<div>[async] john</div>');
done();
});

component.on('error', function (error) {
done(error);
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var helloComponent = require('./components/hello');

module.exports = {
onMount: function() {
this.renderedHtml = helloComponent.renderToString({
name: this.input.name
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="hello">
<div>${input.name}</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
<hello name=input.name/>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var expect = require('chai').expect;

module.exports = function(helpers) {
var component = helpers.mount(require('./index'), {
name: 'john'
});

expect(component.renderedHtml).to.equal('<div class="hello"><div>john</div></div>');
};

0 comments on commit 2951a6e

Please sign in to comment.