diff --git a/mods/parsoid.js b/mods/parsoid.js index 6d1da5116..c597df5ea 100644 --- a/mods/parsoid.js +++ b/mods/parsoid.js @@ -227,6 +227,21 @@ PSP.callParsoidTransform = function callParsoidTransform (restbase, req, from, t return restbase.post(parsoidReq); }; +/** + * Cheap body.innerHTML extraction. + * + * This is safe as we know that the HTML we are receiving from Parsoid is + * serialized as XML. + */ +function cheapBodyInnerHTML(html) { + var match = /
]*>(.*)<\/body>/.exec(html); + if (!match) { + throw new Error('No HTML body found!'); + } else { + return match[1]; + } +} + PSP.makeTransform = function (from, to) { var self = this; @@ -252,6 +267,10 @@ PSP.makeTransform = function (from, to) { // Unwrap to the flat response format var innerRes = res.body[to]; innerRes.status = 200; + // Handle bodyOnly flag + if (to === 'html' && req.body.bodyOnly) { + innerRes.body = cheapBodyInnerHTML(innerRes.body); + } return innerRes; }); }; diff --git a/test/features/parsoid/transform.js b/test/features/parsoid/transform.js index a3a7248c3..6d4389b77 100644 --- a/test/features/parsoid/transform.js +++ b/test/features/parsoid/transform.js @@ -50,15 +50,38 @@ describe('transform api', function() { }) .then(function (res) { assert.deepEqual(res.status, 200); + assert.deepEqual(res.headers['content-type'], + 'text/html;profile=mediawiki.org/specs/html/1.0.0'); var pattern = /