Arlolra has uploaded a new change for review.
https://gerrit.wikimedia.org/r/249683
Change subject: T113331: Provide an API flag for fragment parsing
......................................................................
T113331: Provide an API flag for fragment parsing
Change-Id: I25597e92b31164ab7dc84f45a82baf12523aad1c
---
M bin/parse.js
M lib/api/apiUtils.js
M lib/api/routes.js
M lib/config/MWParserEnvironment.js
M lib/ext/cite/processRefs.js
M tests/mocha/api.js
M tests/mockAPI.js
7 files changed, 84 insertions(+), 10 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/parsoid
refs/changes/83/249683/1
diff --git a/bin/parse.js b/bin/parse.js
index 2a0270d..97b759a 100755
--- a/bin/parse.js
+++ b/bin/parse.js
@@ -138,6 +138,11 @@
'boolean': false,
'default': null,
},
+ 'fragment': {
+ description: 'Return a document fragment.',
+ 'boolean': true,
+ 'default': false,
+ },
});
exports.defaultOptions = yargs.options(standardOpts).parse([]);
@@ -188,7 +193,9 @@
// used in Parsoid JS API, return document
out = doc;
} else {
- out = DU.serializeNode(doc).str;
+ out = DU.serializeNode(env.fragment ? doc.body
: doc, {
+ innerXML: env.fragment,
+ }).str;
}
return { trailingNL: true, out: out, env: env };
} else {
@@ -212,6 +219,9 @@
// Enable wikitext scrubbing
env.scrubWikitext = argv.scrubWikitext;
+ // Enable fragment mode
+ env.fragment = argv.fragment;
+
var i, validExtensions;
if (validExtensions !== '') {
validExtensions = argv.extensions.split(',');
diff --git a/lib/api/apiUtils.js b/lib/api/apiUtils.js
index e62b0f7..f53941a 100644
--- a/lib/api/apiUtils.js
+++ b/lib/api/apiUtils.js
@@ -490,10 +490,14 @@
var startTimers = ret.startTimers;
if (doc) {
- output = DU.serializeNode(res.locals.bodyOnly ? doc.body : doc,
{
- // in v3 api, just the children of the body
- innerXML: res.locals.bodyOnly && res.locals.apiVersion
> 2,
- }).str;
+ output = DU.serializeNode(
+ (res.locals.fragment || res.locals.bodyOnly) ? doc.body
: doc,
+ {
+ innerXML: res.locals.fragment ||
+ // in v3 api, just the children of the
body
+ (res.locals.bodyOnly &&
res.locals.apiVersion > 2),
+ }
+ ).str;
apiUtils.setHeader(res, env, 'content-type',
apiUtils.HTML_CONTENT_TYPE);
apiUtils.endResponse(res, env, output);
}
@@ -521,9 +525,10 @@
var opts = res.locals.opts;
if (opts.format === 'pagebundle') {
var out = DU.extractDpAndSerialize(doc, {
- bodyOnly: res.locals.bodyOnly,
- // in v3 api, just the children of the body
- innerXML: res.locals.bodyOnly && res.locals.apiVersion
> 2,
+ bodyOnly: res.locals.fragment || res.locals.bodyOnly,
+ innerXML: res.locals.fragment ||
+ // in v3 api, just the children of the body
+ (res.locals.bodyOnly && res.locals.apiVersion >
2),
});
apiUtils.jsonResponse(res, env, {
html: {
diff --git a/lib/api/routes.js b/lib/api/routes.js
index 087ea3e..c74138b 100644
--- a/lib/api/routes.js
+++ b/lib/api/routes.js
@@ -42,6 +42,7 @@
res.locals.oldid = req.body.oldid || req.query.oldid || null;
// "body" flag to return just the body (instead of the entire
HTML doc)
res.locals.bodyOnly = !!(req.query.body || req.body.body);
+ res.locals.fragment = !!(req.query.fragment ||
req.body.fragment);
// "subst" flag to perform {{subst:}} template expansion
res.locals.subst = !!(req.query.subst || req.body.subst);
next();
@@ -75,6 +76,9 @@
// in v2 this flag was named "body"
res.locals.bodyOnly = !!(req.query.body ||
req.body.body);
}
+
+ // Fragment parsing mode
+ res.locals.fragment = !!(req.query.fragment ||
req.body.fragment);
var opts = Object.assign({ format: req.params.format },
req.body);
var supportedFormats = (version > 2) ?
@@ -155,6 +159,7 @@
env.scrubWikitext = !(!req.query.scrubWikitext
||
req.query.scrubWikitext === 'false');
}
+ env.fragment = res.locals.fragment;
res.locals.env = env;
next();
}).catch(function(err) {
diff --git a/lib/config/MWParserEnvironment.js
b/lib/config/MWParserEnvironment.js
index 74f7095..2cedc88 100644
--- a/lib/config/MWParserEnvironment.js
+++ b/lib/config/MWParserEnvironment.js
@@ -537,6 +537,9 @@
// Apply extra normalizations before serializing DOM.
MWParserEnvironment.prototype.scrubWikitext = false;
+// We're returning a fragment, not a page.
+MWParserEnvironment.prototype.fragment = false;
+
if (typeof module === "object") {
module.exports.MWParserEnvironment = MWParserEnvironment;
}
diff --git a/lib/ext/cite/processRefs.js b/lib/ext/cite/processRefs.js
index d91e049..51e44fa 100644
--- a/lib/ext/cite/processRefs.js
+++ b/lib/ext/cite/processRefs.js
@@ -104,7 +104,9 @@
if (atTopLevel) {
var refsData = new ReferencesData();
_processRefs(cite, refsData, node);
- cite.references.insertMissingReferencesIntoDOM(env, refsData,
node);
+ if (!env.fragment) {
+ cite.references.insertMissingReferencesIntoDOM(env,
refsData, node);
+ }
}
}
diff --git a/tests/mocha/api.js b/tests/mocha/api.js
index e2337b2..a9c2557 100644
--- a/tests/mocha/api.js
+++ b/tests/mocha/api.js
@@ -443,6 +443,49 @@
.end(done);
});
+ it('1. should respect fragment parameter',
function(done) {
+ request(api)
+ .post(version === 3 ?
+ mockDomain +
'/v3/transform/wikitext/to/html/' :
+ 'v2/' + mockDomain + '/html/')
+ .send({
+ wikitext: 'This is a test.
<ref>Yup</ref>',
+ fragment: 1,
+ })
+ .expect(validHtmlResponse(function(doc) {
+ var body = doc.body;
+ // <body> should have one child, <p>
+ body.childElementCount.should.equal(1);
+
body.firstElementChild.nodeName.should.equal('P');
+ }))
+ .expect(function(res) {
+ res.text.should.not.match(/<body/);
+ res.text.should.match(/<p/);
+ })
+ .end(done);
+ });
+
+ it('2. should respect fragment parameter',
function(done) {
+ request(api)
+ .post(version === 3 ?
+ mockDomain +
'/v3/transform/wikitext/to/html/' :
+ 'v2/' + mockDomain + '/html/')
+ .send({
+ wikitext: 'This is a test.
<ref>Yup</ref>',
+ })
+ .expect(validHtmlResponse(function(doc) {
+ var body = doc.body;
+ // <body> should have two children, <p>
and <ol>
+ body.childElementCount.should.equal(2);
+
body.firstElementChild.nodeName.should.equal('P');
+
body.lastElementChild.nodeName.should.equal('OL');
+ }))
+ .expect(function(res) {
+ res.text.should.match(/<body/);
+ })
+ .end(done);
+ });
+
it('should include captured offsets', function(done) {
request(api)
.get(version === 3 ?
diff --git a/tests/mockAPI.js b/tests/mockAPI.js
index e6e4255..1fe12be 100644
--- a/tests/mockAPI.js
+++ b/tests/mockAPI.js
@@ -160,7 +160,13 @@
querySiteinfo: function(body, cb) {
// TODO: Read which language should we use from somewhere.
- cb(null, require('../lib/config/baseconfig/enwiki.json'));
+ var json = require('../lib/config/baseconfig/enwiki.json');
+ // Add the cite extension tags for testing.
+ json.query.extensiontags = json.query.extensiontags.concat([
+ '<ref>',
+ '<references>',
+ ]);
+ cb(null, json);
},
query: function(body, cb) {
--
To view, visit https://gerrit.wikimedia.org/r/249683
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I25597e92b31164ab7dc84f45a82baf12523aad1c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Arlolra <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits