jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/387295 )
Change subject: Improve Parsoid section detection
......................................................................
Improve Parsoid section detection
Replaces individual implementations with a common function to detect
Parsoid section tags.
Section tag introduced by Parsoid must have the data-mw-section-id
attribute.
Added unit test.
Change-Id: I478a94ca090ee69e2eb7f0ce3b8aa98f3f919474
---
M lib/parseDefinitions.js
M lib/parsoidSections.js
A test/lib/parsoid/parsoid-sections-test.js
3 files changed, 39 insertions(+), 4 deletions(-)
Approvals:
jenkins-bot: Verified
Mholloway: Looks good to me, approved
diff --git a/lib/parseDefinitions.js b/lib/parseDefinitions.js
index e8c55f9..bd7dec5 100644
--- a/lib/parseDefinitions.js
+++ b/lib/parseDefinitions.js
@@ -3,6 +3,7 @@
const parseDefinitionsUsingDivs = require('./parseDefinitionsUsingDivs');
const parseDefinitionsUsingSectionTags =
require('./parseDefinitionsUsingSectionTags');
const parsoidSectionsUsingDivs = require('./parsoidSectionsUsingDivs');
+const parsoidSections = require('./parsoidSections');
/**
* Parses Wiktionary definitions. Delegates to the correct implementation.
@@ -14,7 +15,7 @@
* Wiktionary page
*/
function parseDefinitions(doc, domain, title) {
- if (doc.querySelector('section')) {
+ if (parsoidSections.hasParsoidSections(doc)) {
return parseDefinitionsUsingSectionTags(doc, domain, title);
} else {
parsoidSectionsUsingDivs.addSectionDivs(doc);
diff --git a/lib/parsoidSections.js b/lib/parsoidSections.js
index da1caa1..dcdbb72 100644
--- a/lib/parsoidSections.js
+++ b/lib/parsoidSections.js
@@ -4,13 +4,21 @@
const parsoidSectionsUsingSectionTags =
require('./parsoidSectionsUsingSectionTags');
/**
+ * Determines if Parsoid added section tags. See T114072.
+ * @param {!document} doc the parsed DOM Document of the Parsoid output
+ * @return {boolean} true if Parsoid added section tags
+ */
+function hasParsoidSections(doc) {
+ return Boolean(doc.querySelector('section[data-mw-section-id]'));
+}
+
+/**
* New sectioning code: wraps sections in <section> tags. Will likely
* be replaced by code in Parsoid.
* @param {!document} doc the parsed DOM Document of the Parsoid output
*/
function addSectionDivs(doc) {
- // in case this is already handled by Parsoid (T114072) don't try again
- if (!doc.querySelector('section')) {
+ if (!hasParsoidSections(doc)) {
parsoidSectionsUsingDivs.addSectionDivs(doc);
}
}
@@ -22,7 +30,7 @@
* @return {!sections[]} an array of section JSON elements
*/
function getSectionsText(doc) {
- if (!doc.querySelector('section')) {
+ if (!hasParsoidSections(doc)) {
return parsoidSectionsUsingDivs.getSectionsText(doc);
} else {
return parsoidSectionsUsingSectionTags.getSectionsText(doc);
@@ -30,6 +38,7 @@
}
module.exports = {
+ hasParsoidSections,
addSectionDivs,
getSectionsText
};
diff --git a/test/lib/parsoid/parsoid-sections-test.js
b/test/lib/parsoid/parsoid-sections-test.js
new file mode 100644
index 0000000..6d64a94
--- /dev/null
+++ b/test/lib/parsoid/parsoid-sections-test.js
@@ -0,0 +1,25 @@
+'use strict';
+
+const assert = require('../../utils/assert.js');
+const domino = require('domino');
+const parsoid = require('../../../lib/parsoidSections');
+
+describe('lib:parsoid-sections', function() {
+
+ this.timeout(20000); // eslint-disable-line no-invalid-this
+
+ it('hasParsoidSections(empty) should return false', () => {
+ const doc = domino.createDocument('');
+ assert.ok(parsoid.hasParsoidSections(doc) === false);
+ });
+
+ it('hasParsoidSections(section) should return false', () => {
+ const doc = domino.createDocument('<section></section>');
+ assert.ok(parsoid.hasParsoidSections(doc) === false);
+ });
+
+ it('hasParsoidSections(section[data-mw-section-id]) should return true',
() => {
+ const doc = domino.createDocument('<section
data-mw-section-id="0"></section>');
+ assert.ok(parsoid.hasParsoidSections(doc) === true);
+ });
+});
--
To view, visit https://gerrit.wikimedia.org/r/387295
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I478a94ca090ee69e2eb7f0ce3b8aa98f3f919474
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/services/mobileapps
Gerrit-Branch: master
Gerrit-Owner: BearND <[email protected]>
Gerrit-Reviewer: Fjalapeno <[email protected]>
Gerrit-Reviewer: GWicke <[email protected]>
Gerrit-Reviewer: Jdlrobson <[email protected]>
Gerrit-Reviewer: Mholloway <[email protected]>
Gerrit-Reviewer: Mhurd <[email protected]>
Gerrit-Reviewer: Mobrovac <[email protected]>
Gerrit-Reviewer: Ppchelko <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits