Support HTMLification

Breaks webapp_server pretty hard
This commit is contained in:
David Greenspan
2014-06-26 15:14:08 -07:00
parent 51a6445e11
commit d7ef9efe12
4 changed files with 106 additions and 7 deletions

View File

@@ -43,6 +43,7 @@ Package.on_use(function (api) {
api.add_files([
'reactivevar.js',
'materializer.js',
'stringifiers.js',
'view.js'
]);
});

View File

@@ -0,0 +1,44 @@
// new Blaze.TextStringifier(options)
//
// An HTML.Visitor that turns HTMLjs into text, which may be
// used as part of an HTML tag attribute or the contents
// of a `<textarea>` or `<style>` tag, for example.
//
// Options: `parentView` (optional), `textMode` (required)
Blaze.TextStringifier = HTML.ToTextVisitor.extend();
Blaze.ToTextVisitor.def({
visitObject: function (x) {
if (x instanceof Blaze.View)
return Blaze.viewToText(x, this.parentView, this.textMode);
throw new Error("Unexpected object in htmljs in Blaze.viewToText: " + x);
},
// see comment in HTML.ToTextVisitor.visitTag
toHTML: function (node) {
var visitor = new Blaze.HTMLStringifier({parentView: this.parentView});
return visitor.visit(node);
}
});
// new Blaze.HTMLStringifier(options)
//
// An HTML.Visitor that turns HTMLjs into an HTML string.
//
// Options: `parentView`
Blaze.HTMLStringifier = HTML.ToHTMLVisitor.extend();
Blaze.HTMLStringifier.def({
visitObject: function (x) {
if (x instanceof Blaze.View)
return Blaze.viewToHTML(x, this.parentView);
throw new Error("Unexpected object in htmljs in Blaze.viewToHTML: " + x);
},
toText: function (node, textMode) {
var visitor = new Blaze.TextStringifier({
parentView: this.parentView,
textMode: HTML.TEXTMODE.STRING
});
return visitor.visit(node);
}
});

View File

@@ -71,7 +71,7 @@ Blaze.materializeView = function (view, parentView) {
view.parentView = (parentView || null);
if (view.isCreated)
throw new Error("Can't materialize the same View twice");
throw new Error("Can't render the same View twice");
view.isCreated = true;
Blaze._fireCallbacks(view, 'created');
@@ -122,6 +122,49 @@ Blaze.materializeView = function (view, parentView) {
return domrange;
};
Blaze._stringifyView = function (view, parentView, stringifier) {
view.parentView = (parentView || null);
if (view.isCreated)
throw new Error("Can't render the same View twice");
view.isCreated = true;
Blaze._fireCallbacks(view, 'created');
var htmljs = view.render();
var result = stringifier.visit(htmljs);
Blaze.destroyView(view);
return result;
};
Blaze.viewToHTML = function (view, parentView) {
return Blaze._stringifyView(
view, parentView,
new Blaze.HTMLStringifier({parentView: parentView}));
};
Blaze.viewToText = function (view, parentView, textMode) {
if ((parentView != null) && ! (parentView instanceof Blaze.View)) {
// omitted parentView argument
textMode = parentView;
parentView = null;
}
if (! textMode)
throw new Error("textMode required");
if (! (textMode === HTML.TEXTMODE.STRING ||
textMode === HTML.TEXTMODE.RCDATA ||
textMode === HTML.TEXTMODE.ATTRIBUTE))
throw new Error("Unknown textMode: " + textMode);
return Blaze._stringifyView(
view, parentView,
new Blaze.TextStringifier({textMode: textMode,
parentView: parentView}));
};
Blaze.destroyView = function (view, _viaTeardown) {
if (view.isDestroyed)
return;
@@ -171,16 +214,26 @@ Blaze.render3 = function (contentFunc) {
return Blaze.materializeView(Blaze.View('render', contentFunc));
};
Blaze.With3 = function (dataFunc, contentFunc) {
Blaze.toHTML3 = function (contentFunc) {
return Blaze.viewToHTML(Blaze.View('toHTML', contentFunc));
};
Blaze.With3 = function (data, contentFunc) {
var view = Blaze.View('with', contentFunc);
view.dataVar = new Blaze.ReactiveVar;
view.onCreated(function () {
this.autorun(function () {
this.dataVar.set(dataFunc());
});
if (typeof data === 'function') {
this.autorun(function () {
this.dataVar.set(data());
});
} else {
this.dataVar.set(data);
}
});
return view;
};
/*Blaze._eachView = function (argFunc, contentFunc, elseContentFunc) {

View File

@@ -490,8 +490,9 @@ var runWebAppServer = function () {
var boilerplateData = _.extend({htmlAttributes: htmlAttributes},
boilerplateBaseData);
boilerplateByAttributes[attributeKey] = "<!DOCTYPE html>\n" +
Blaze.toHTML(function () {
return Blaze.With(boilerplateData, boilerplateFunc);
Blaze.toHTML3(function () {
return boilerplateFunc.toString();
return Blaze.With3(boilerplateData, boilerplateFunc);
});
} catch (e) {
Log.error("Error running template: " + e.stack);