mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
Support HTMLification
Breaks webapp_server pretty hard
This commit is contained in:
@@ -43,6 +43,7 @@ Package.on_use(function (api) {
|
||||
api.add_files([
|
||||
'reactivevar.js',
|
||||
'materializer.js',
|
||||
'stringifiers.js',
|
||||
'view.js'
|
||||
]);
|
||||
});
|
||||
|
||||
44
packages/blaze/stringifiers.js
Normal file
44
packages/blaze/stringifiers.js
Normal 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);
|
||||
}
|
||||
});
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user