Merge branch 'flash' into connect
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 · parents cdf49dc, 3c06c82 · Browse files · 6 files·+137 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+137 added−1 removed
- tests:+49 added−1 removed
- other:+88 added−0 removed
Provenance
- Commit
c180923- Tree
a7be5bd- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
cdf49dc- Parent 2
3c06c82
6 files changed+137 added−1 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 filemodified:support/connectnot shown
Its text is not shown, modified.
modified:test/request.test.js+32 added-1 removed
2 unmodified lines3 * Module dependencies.4 */5 6 var express = require('express'),7- connect = require('connect');7+ connect = require('connect'),8+ MemoryStore = require('connect/middleware/session/memory');9 10+// Prevent reap timer11+var memoryStore = new MemoryStore({ reapInterval: -1 });12+13 module.exports = {14 '#isXMLHttpRequest': function(assert){15 var app = express.createServer();16 91 unmodified lines108 109 assert.response(app,110 { url: '/user', method: 'POST', data: 'id=1', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }},111 { body: 'user 1' });112+ },113+ 114+ '#flash()': function(assert){115+ var app = express.createServer(116+ connect.cookieDecoder(),117+ connect.session({ store: memoryStore })118+ );119+120+ app.get('/', function(req, res){121+ assert.eql([], req.flash('info'));122+ assert.eql([], req.flash('error'));123+ assert.eql({}, req.flash());124+ assert.eql({}, req.session.flash);125+ 126+ assert.equal(1, req.flash('info', 'one'));127+ assert.equal(2, req.flash('info', 'two'));128+ assert.eql(['one', 'two'], req.flash('info'));129+ assert.eql([], req.flash('info'));130+131+ assert.equal(1, req.flash('info', 'one'));132+ assert.eql({ info: ['one'] }, req.flash());133+ res.send('ok');134+ });135+ 136+ assert.response(app,137+ { url: '/' },138+ { body: 'ok' });139 }140 };added: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 filemodified:.pomo+5 added
57 unmodified lines58 complete: false59 description: 60 length: 2561 name: connect-form support for methodOverride?62+- !ruby/object:Pomo::Task 63+ complete: false64+ description: 65+ length: 2566+ name: app.error() etc