Support specifying reference contents with data-mw.body.id

If data-mw.body.html is not set, search for the element
whose ID matches data-mw.body.id and use its contents.

.body.html is still used for new content: new references
use .body.html, and if a reference using .body.id is
changed, the changed HTML is output in .body.html.

Bug: T88650
Change-Id: I5d614927256d59a3ab20b0bd6e882b5cb8fcda20
This commit is contained in:
Roan Kattouw 2015-02-04 16:39:47 -08:00 committed by Jforrester
parent 396eb606a6
commit 26e5a885bc
3 changed files with 32 additions and 15 deletions

View file

@ -56,14 +56,24 @@ ve.dm.MWReferenceNode.static.blacklistedAnnotationTypes = [ 'link' ];
ve.dm.MWReferenceNode.static.listKeyRegex = /^(auto|literal)\/(.*)$/;
ve.dm.MWReferenceNode.static.toDataElement = function ( domElements, converter ) {
function getReflistItemHtml( id ) {
var elem = converter.getHtmlDocument().getElementById( id );
return elem && elem.innerHTML || '';
}
var dataElement,
mwDataJSON = domElements[0].getAttribute( 'data-mw' ),
mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {},
body = mwData.body ? mwData.body.html : '',
reflistItemId = mwData.body && mwData.body.id,
body = ( mwData.body && mwData.body.html ) ||
( reflistItemId && getReflistItemHtml( reflistItemId ) ) ||
'',
refGroup = mwData.attrs && mwData.attrs.group || '',
listGroup = this.name + '/' + refGroup,
autoKeyed = !mwData.attrs || mwData.attrs.name === undefined,
listKey = autoKeyed ? 'auto/' + converter.internalList.getNextUniqueNumber() : 'literal/' + mwData.attrs.name,
listKey = autoKeyed ?
'auto/' + converter.internalList.getNextUniqueNumber() :
'literal/' + mwData.attrs.name,
queueResult = converter.internalList.queueItemHtml( listGroup, listKey, body ),
listIndex = queueResult.index,
contentsUsed = ( body !== '' && queueResult.isNew );
@ -81,6 +91,9 @@ ve.dm.MWReferenceNode.static.toDataElement = function ( domElements, converter )
contentsUsed: contentsUsed
}
};
if ( reflistItemId ) {
dataElement.attributes.refListItemId = reflistItemId;
}
return dataElement;
};
@ -143,7 +156,8 @@ ve.dm.MWReferenceNode.static.toDomElements = function ( dataElement, doc, conver
itemNodeWrapper
);
itemNodeHtml = itemNodeWrapper.innerHTML; // Returns '' if itemNodeWrapper is empty
originalHtml = ve.getProp( mwData, 'body', 'html' ) || '';
originalHtml = ve.getProp( mwData, 'body', 'html' ) ||
itemNode.getAttribute( 'originalHtml' ) || '';
originalHtmlWrapper.innerHTML = originalHtml;
// Only set body.html if itemNodeHtml and originalHtml are actually different
if ( !originalHtmlWrapper.isEqualNode( itemNodeWrapper ) ) {

View file

@ -60,16 +60,15 @@ ve.dm.MWReferencesListNode.static.toDataElement = function ( domElements, conver
}
};
if ( mwData.body && mwData.body.html ) {
// Process the nodes in .body.html as if they were this node's children
contentsDiv = domElements[0].ownerDocument.createElement( 'div' );
contentsDiv.innerHTML = mwData.body.html;
contentsData = converter.getDataFromDomClean( contentsDiv );
return [ referencesListData ]
referencesListData = [ referencesListData ]
.concat( contentsData )
.concat( [ { type: '/' + this.name } ] );
} else {
return referencesListData;
}
return referencesListData;
};
ve.dm.MWReferencesListNode.static.toDomElements = function ( data, doc, converter ) {

View file

@ -303,6 +303,8 @@ ve.dm.mwExample.MWInlineImage = {
};
ve.dm.mwExample.MWReference = {
// The HTML below is enriched to wrap reference contents in <span id="mw-cite-[...]">
// which Parsoid doesn't do yet, but T88290 asks for
referencesList:
'<ol class="references" typeof="mw:Extension/references" about="#mwt7" data-parsoid="{}"' +
'data-mw="{&quot;name&quot;:&quot;references&quot;,&quot;body&quot;:{' +
@ -311,9 +313,9 @@ ve.dm.mwExample.MWReference = {
'&amp;quot;attrs&amp;quot;:{&amp;quot;group&amp;quot;:&amp;quot;g1&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;foo&amp;quot;}}\\&quot; ' +
'rel=\\&quot;dc:references\\&quot; typeof=\\&quot;mw:Extension/ref\\&quot;>' +
'<a href=\\&quot;#cite_note-foo-3\\&quot;>[3]</a></span>&quot;},&quot;attrs&quot;:{&quot;group&quot;:&quot;g1&quot;}}">' +
'<li about="#cite_note-.3A0-2" id="cite_note-.3A0-2"><span rel="mw:referencedBy"><a href="#cite_ref-.3A0_2-0">↑</a></span> Quux</li>' +
'<li about="#cite_note-3" id="cite_note-3"><span rel="mw:referencedBy"><a href="#cite_ref-3">↑</a></span> No name</li>' +
'<li about="#cite_note-foo-4" id="cite_note-foo-4"><span rel="mw:referencedBy"><a href="#cite_ref-foo_4-0">↑</a></span> Ref in refs</li>' +
'<li about="#cite_note-.3A0-2" id="cite_note-.3A0-2"><span rel="mw:referencedBy"><a href="#cite_ref-.3A0_2-0">↑</a></span> <span id="mw-cite-:0">Quux</span></li>' +
'<li about="#cite_note-3" id="cite_note-3"><span rel="mw:referencedBy"><a href="#cite_ref-3">↑</a></span> <span id="mw-cite-3">No name</span></li>' +
'<li about="#cite_note-foo-4" id="cite_note-foo-4"><span rel="mw:referencedBy"><a href="#cite_ref-foo_4-0">↑</a></span> <span id="mw-cite-foo">Ref in refs</span></li>' +
'</ol>'
};
@ -1126,7 +1128,8 @@ ve.dm.mwExample.domToDataCases = {
'<a href="#cite_note-bar-1">[1]</a>' +
'</span>' +
' Yay' +
'<span about="#mwt4" class="reference" data-mw="{&quot;name&quot;:&quot;ref&quot;,&quot;body&quot;:{&quot;html&quot;:&quot;No name&quot;},&quot;attrs&quot;:{&quot;group&quot;:&quot;g1&quot;}}" id="cite_ref-1-0" rel="dc:references" typeof="mw:Extension/ref" data-parsoid="{}">' +
// This reference has .body.id instead of .body.html
'<span about="#mwt4" class="reference" data-mw="{&quot;name&quot;:&quot;ref&quot;,&quot;body&quot;:{&quot;id&quot;:&quot;mw-cite-3&quot;},&quot;attrs&quot;:{&quot;group&quot;:&quot;g1&quot;}}" id="cite_ref-1-0" rel="dc:references" typeof="mw:Extension/ref" data-parsoid="{}">' +
'<a href="#cite_note-3">[g1 2]</a>' +
'</span>' +
' Quux' +
@ -1265,17 +1268,18 @@ ve.dm.mwExample.domToDataCases = {
listGroup: 'mwReference/g1',
listKey: 'auto/0',
refGroup: 'g1',
mw: { name: 'ref', body: { html: 'No name' }, attrs: { group: 'g1' } },
originalMw: '{"name":"ref","body":{"html":"No name"},"attrs":{"group":"g1"}}',
mw: { name: 'ref', body: { id: 'mw-cite-3' }, attrs: { group: 'g1' } },
originalMw: '{"name":"ref","body":{"id":"mw-cite-3"},"attrs":{"group":"g1"}}',
childDomElements: $( '<a href="#cite_note-3">[g1 2]</a>' ).toArray(),
contentsUsed: true
contentsUsed: true,
refListItemId: 'mw-cite-3'
},
htmlAttributes: [
{
values: {
about: '#mwt4',
class: 'reference',
'data-mw': '{"name":"ref","body":{"html":"No name"},"attrs":{"group":"g1"}}',
'data-mw': '{"name":"ref","body":{"id":"mw-cite-3"},"attrs":{"group":"g1"}}',
'data-parsoid': '{}',
id: 'cite_ref-1-0',
rel: 'dc:references',