Skip to content

Commit

Permalink
Handle and test bodyOnly flag when converting to HTML
Browse files Browse the repository at this point in the history
Bug: T88319
  • Loading branch information
gwicke committed Feb 13, 2015
1 parent ebc31e2 commit f48eceb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
19 changes: 19 additions & 0 deletions mods/parsoid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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[^>]*>(.*)<\/body>/.exec(html);
if (!match) {
throw new Error('No HTML body found!');
} else {
return match[1];
}
}

PSP.makeTransform = function (from, to) {
var self = this;

Expand All @@ -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;
});
};
Expand Down
25 changes: 24 additions & 1 deletion test/features/parsoid/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = /<h2.*> 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 = /^<h2.*> 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
Expand Down

0 comments on commit f48eceb

Please sign in to comment.