From f48ecebb40ca93f4d9a773baa548c2a177fbd262 Mon Sep 17 00:00:00 2001 From: Gabriel Wicke Date: Thu, 12 Feb 2015 17:01:35 -0800 Subject: [PATCH] Handle and test bodyOnly flag when converting to HTML Bug: T88319 --- mods/parsoid.js | 19 +++++++++++++++++++ test/features/parsoid/transform.js | 25 ++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) 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 = / Heading <\/h2>/; if (!pattern.test(res.body)) { - throw new Error('Expected pattern in response: ' + pattern); + throw new Error('Expected pattern in response: ' + pattern + + '\nSaw: ' + res.body); } + }); + }); + + it('wt2html with bodyOnly', function () { + return preq.post({ + uri: server.config.baseURL + + '/transform/wikitext/to/html/User:GWicke%2F_restbase_test', + body: { + wikitext: '== Heading ==', + bodyOnly: true + } + }) + .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 = /^ Heading <\/h2>$/; + if (!pattern.test(res.body)) { + throw new Error('Expected pattern in response: ' + pattern + + '\nSaw: ' + res.body); + } }); }); + it('html2wt, no-selser', function () { return preq.post({ uri: server.config.baseURL