Added req.flash()
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent cdf49dc · Browse files · 3 files·+100 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+100 added−0 removed
- tests:+17 added−0 removed
- other:+83 added−0 removed
Provenance
- Commit
1bcccb3- Tree
4401c85- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
cdf49dc
3 files changed+100 added−0 removed
modified:lib/express/request.js+40 added
8 unmodified lines91011121314151617104 unmodified linesExpand all12212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816911 unmodified lines8 unmodified lines * Module dependencies. */
var http = require('http'), utils = require('./utils'), mime = require('connect/utils').mime;
/** * Return request header or optional default.104 unmodified linesExpand all return this.body[name]; }};
/** * Queue flash `msg` of the given `type`. * * Examples: * * req.flash('info', 'email sent'); * req.flash('error', 'email delivery failed'); * req.flash('info', 'email re-sent'); * // => 2 * * req.flash('info'); * // => ['email sent', 'email re-sent'] * * req.flash('info'); * // => [] * * req.flash(); * // => { error: ['email delivery failed'], info: [] } * * @param {String} type * @param {String} msg * @return {Array|Object|Number} * @api public */
http.IncomingMessage.prototype.flash = function(type, msg){ var msgs = this.session.flash = this.session.flash || {}; if (type && msg) { msg = utils.miniMarkdown(utils.htmlEscape(msg)); return (msgs[type] = msgs[type] || []).push(msg); } else if (type) { var arr = msgs[type]; delete msgs[type]; return arr || []; } else { this.session.flash = {}; return msgs; }};
// Callback for isXMLHttpRequest / xhr
function isxhr() { return this.header('X-Requested-With', '').toLowerCase() === 'xmlhttprequest';11 unmodified linesadded:lib/express/utils.js+43 added
12345678910111213141516171819202122232425262728293031323334353637383940414243
/*! * Express - Utils * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca> * MIT Licensed */
/** * Parse mini markdown implementation. * The following conversions are supported, * primarily for the "flash" middleware: * * _foo_ or *foo* become <em>foo</em> * __foo__ or **foo** become <strong>foo</strong> * [A](B) becomes <a href="B">A</a> * * @param {String} str * @return {String} * @api public */
exports.miniMarkdown = function(str){ return String(str) .replace(/(__|\*\*)(.*?)\1/g, '<strong>$2</strong>') .replace(/(_|\*)(.*?)\1/g, '<em>$2</em>') .replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');};
/** * Escape special characters in the given string of html. * * @param {String} html * @return {String} * @api public */
exports.htmlEscape = function(html) { return String(html) .replace(/&/g, '&') .replace(/"/g, '"') .replace(/</g, '<') .replace(/>/g, '>');};No newline at end of fileadded:test/utils.test.js+17 added
1234567891011121314151617
/** * Module dependencies. */
var utils = require('express/utils');
module.exports = { 'test miniMarkdown': function(assert){ assert.equal('<em>foo</em>', utils.miniMarkdown('_foo_')); assert.equal('<em>foo</em>', utils.miniMarkdown('*foo*')); assert.equal('<strong>foo</strong>', utils.miniMarkdown('**foo**')); assert.equal('<strong>foo</strong>', utils.miniMarkdown('__foo__')); assert.equal('<em>foo</em>_', utils.miniMarkdown('_foo__')); assert.equal('<a href="/login">User Login</a>', utils.miniMarkdown('[User Login](/login)')); }};No newline at end of file