jenkins-bot has submitted this change and it was merged.

Change subject: JsApi: Add `PNodeList.fromWikitext()` helper method.
......................................................................


JsApi: Add `PNodeList.fromWikitext()` helper method.

Change-Id: I28c1ce6d4f8b5920b032bc71e88cbcf0e2c3e4c9
---
M .jsduck/categories.json
M .jsduck/external.js
M lib/index.js
M tests/mocha/jsapi.js
4 files changed, 68 insertions(+), 0 deletions(-)

Approvals:
  Arlolra: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/.jsduck/categories.json b/.jsduck/categories.json
index 7d8f247..f5c45f0 100644
--- a/.jsduck/categories.json
+++ b/.jsduck/categories.json
@@ -91,6 +91,7 @@
                                        "Map",
                                        "Number",
                                        "Object",
+                                       "Promise",
                                        "RegExp",
                                        "Set",
                                        "String"
diff --git a/.jsduck/external.js b/.jsduck/external.js
index 25e4d0b..451177e 100644
--- a/.jsduck/external.js
+++ b/.jsduck/external.js
@@ -72,3 +72,12 @@
  * primitive values or object references.
  * [See MDN for more 
information](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).
  */
+
+/**
+ * @class Promise
+ *
+ * The Promise object is used for deferred and asynchronous
+ * computations. A Promise represents an operation that hasn't
+ * completed yet, but is expected to in the future.
+ * [See MDN for more 
information](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise).
+ */
diff --git a/lib/index.js b/lib/index.js
index bc82d91..da89050 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -44,6 +44,26 @@
  *        var templates = pdoc.filterTemplates();
  *        console.log(templates[0].name);
  *     }).done();
+ *
+ * @param {String} input
+ *    The input wikitext or HTML (depending on conversion direction).
+ * @param {Object} options
+ * @param {Boolean} [options.document=false]
+ *    Return a DOM {@link Document} (instead of a string)
+ * @param {Boolean} [options.pdoc=false]
+ *    Return a {@link PDoc} object (instead of a string)
+ * @param {Boolean} [options.wt2html=true]
+ *    Convert wikitext to HTML.
+ * @param {Boolean} [options.html2wt=false]
+ *    Convert HTML to wikitext.
+ * @param {ParsoidConfig} [options.parsoidConfig]
+ *    A {@link ParsoidConfig} object to use during parsing.
+ *    If not provided one will be constructed using `options.config`.
+ * @param {Object} [options.config]
+ *    A set of options which will be passed to the {@link ParsoidConfig}
+ *    constructor.
+ * @return {Promise}
+ *   Fulfilled with the result of the parse.
  */
 Parsoid.parse = function(input, options, optCb) {
        options = options || {};
@@ -79,6 +99,31 @@
        }).nodify(optCb);
 };
 
+// Add a helper method to PNodeList, based on Parsoid.parse.
+
+/** @class PNodeList */
+/**
+ * Create a {@link PNodeList} belonging to the given {@link PDoc}
+ * from a string containing wikitext.
+ * @param {PDoc} pdoc
+ *   The {@link PDoc} which will own the result.
+ * @param {String} wikitext
+ *   The wikitext to convert.
+ * @param {Object} options
+ *   Options which are passed to {@link Parsoid#parse}.
+ * @return {Promise}
+ *    Fulfilled by a {@link PNodeList} representing the given wikitext.
+ * @static
+ */
+JsApi.PNodeList.fromWikitext = function(pdoc, wikitext, options) {
+       options = Object.assign({}, options);
+       options.pdoc = true;
+       return Parsoid.parse(wikitext, options).then(function(pdoc2) {
+               var node = pdoc.document.adoptNode(pdoc2.document.body);
+               return new JsApi.PNodeList(pdoc, null, node);
+       });
+};
+
 // Expose other helpful objects.
 Object.keys(JsApi).forEach(function(k) {
        Parsoid[k] = JsApi[k];
diff --git a/tests/mocha/jsapi.js b/tests/mocha/jsapi.js
index de71321..9e93944 100644
--- a/tests/mocha/jsapi.js
+++ b/tests/mocha/jsapi.js
@@ -210,4 +210,17 @@
                        texts[0].value.should.equal(' foo');
                });
        });
+       it('allows mutation using wikitext', function() {
+               var text = '== heading ==';
+               return Parsoid.parse(text, { pdoc: true }).then(function(pdoc) {
+                       var headings = pdoc.filterHeadings();
+                       headings.length.should.equal(1);
+                       // Note that even if the wikitext is unbalanced, the 
result
+                       // will be balanced.  The bold face doesn't escape the 
heading!
+                       return Parsoid.PNodeList.fromWikitext(pdoc, 
"'''bold").then(function(pnl) {
+                               headings[0].title = pnl;
+                               String(pdoc).should.equal("== '''bold''' ==\n");
+                       });
+               });
+       });
 });

-- 
To view, visit https://gerrit.wikimedia.org/r/228005
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I28c1ce6d4f8b5920b032bc71e88cbcf0e2c3e4c9
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Cscott <[email protected]>
Gerrit-Reviewer: Arlolra <[email protected]>
Gerrit-Reviewer: Cscott <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to