Arlolra has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/198001

Change subject: Associate data-parsoid with duplicated ids
......................................................................

Associate data-parsoid with duplicated ids

 * Not sure this is actually a good thing. When we discussed stable-ids,
   it was pointed out that clients should take care not to duplicate
   ids. Ids are unique to a page so this content isn't well formed.

 * Also, see caveats about ids present in the original wikitext and
   reused.

Change-Id: I0c2f91ac961ac7471c9836c7b543ade894c3147b
---
M lib/mediawiki.DOMUtils.js
M tests/mocha/api.js
2 files changed, 39 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/parsoid 
refs/changes/01/198001/1

diff --git a/lib/mediawiki.DOMUtils.js b/lib/mediawiki.DOMUtils.js
index 7227fcf..53fe9b8 100644
--- a/lib/mediawiki.DOMUtils.js
+++ b/lib/mediawiki.DOMUtils.js
@@ -2011,14 +2011,20 @@
        // and adds back the data-parsoid attributes.
        applyDataParsoid: function ( document, dp ) {
                Object.keys( dp.ids ).forEach(function ( key ) {
-                       var el = document.getElementById( key );
-                       if ( el ) {
-                               this.setJSONAttribute( el, 'data-parsoid', 
dp.ids[key] );
+                       var el;
+                       while ( true ) {
+                               el = document.getElementById( key );
+                               if ( !el ) { return; }
+                               DU.setJSONAttribute( el, 'data-parsoid', 
dp.ids[key] );
                                if ( /^mw[\w-]{2,}$/.test( key ) ) {
                                        el.removeAttribute( 'id' );
+                               } else {
+                                       // Avoid an infinite loop for ids that 
already existed
+                                       // and were reused to store 
data-parsoid.
+                                       return;
                                }
                        }
-               }.bind( this ));
+               });
        },
 
        // Removes the data-parsoid attribute from a node,
diff --git a/tests/mocha/api.js b/tests/mocha/api.js
index 6af7585..4051bb8 100644
--- a/tests/mocha/api.js
+++ b/tests/mocha/api.js
@@ -510,6 +510,35 @@
                                .end(done);
                        });
 
+                       it('should apply data-parsoid to duplicated ids', 
function(done) {
+                               // Note: This won't work for non-mw prefixed 
ids.
+                               request(api)
+                               .post('v2/' + mockHost + '/wt/')
+                               .send({
+                                       html: "<html><body id=\"mwAA\"><div 
id=\"mwBB\">data-parsoid test</div><div id=\"mwBB\">data-parsoid 
test</div></body></html>",
+                                       original: {
+                                               title: "Doesnotexist",
+                                               html: {
+                                                       body: "<html><body 
id=\"mwAA\"><div id=\"mwBB\">data-parsoid test</div></body></html>",
+                                               },
+                                               "data-parsoid": {
+                                                       body: {
+                                                               "ids": {
+                                                                       mwAA: 
{},
+                                                                       mwBB: { 
"autoInsertedEnd": true, "stx": "html" }
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               })
+                               .expect(200)
+                               .expect(function(res) {
+                                       
res.body.should.have.property("wikitext");
+                                       
res.body.wikitext.body.should.equal("<div>data-parsoid test<div>data-parsoid 
test");
+                               })
+                               .end(done);
+                       });
+
                }); // end html2wt
 
        });

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0c2f91ac961ac7471c9836c7b543ade894c3147b
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

Reply via email to