Debian Codegraph @

extensions/Cite/modules/ve-cite/ve.dm.MWReferencesListNode.js

raw symbols SCIP
1 'use strict';
2
3 /*!
4 * VisualEditor DataModel MWReferencesListNode class.
5 *
6 * @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
7 * @license MIT
8 */
9
10 /**
11 * DataModel MediaWiki references list node.
12 *
13 * @constructor
14 * @extends ve.dm.BranchNode
15 * @mixes ve.dm.FocusableNode
16 * @param {Object} [element] Reference to element in linear model
17 * @param {ve.dm.Node[]} [children]
18 */
19 ve.dm.MWReferencesListNode = function VeDmMWReferencesListNode() {
20 // Parent constructor
21 ve.dm.MWReferencesListNode.super.apply( this, arguments );
22
23 // Mixin constructors
24 ve.dm.FocusableNode.call( this );
25 };
26
27 /* Inheritance */
28
29 OO.inheritClass( ve.dm.MWReferencesListNode, ve.dm.BranchNode );
30
31 OO.mixinClass( ve.dm.MWReferencesListNode, ve.dm.FocusableNode );
32
33 /* Methods */
34
35 ve.dm.MWReferencesListNode.prototype.isEditable = function () {
36 return !this.getAttribute( 'templateGenerated' );
37 };
38
39 /* Static members */
40
41 ve.dm.MWReferencesListNode.static.name = 'mwReferencesList';
42
43 ve.dm.MWReferencesListNode.static.handlesOwnChildren = true;
44
45 ve.dm.MWReferencesListNode.static.ignoreChildren = true;
46
47 ve.dm.MWReferencesListNode.static.matchTagNames = null;
48
49 ve.dm.MWReferencesListNode.static.matchRdfaTypes = [ 'mw:Extension/references', 'mw:Transclusion' ];
50
51 // Allow TemplateStyles in the matching element
52 ve.dm.MWReferencesListNode.static.allowedRdfaTypes = [ 'mw:Extension/templatestyles' ];
53
54 // e.g. with a {{reflist}} with TemplateStyles
55 ve.dm.MWReferencesListNode.static.enableAboutGrouping = true;
56
57 // This node has the same specificity as ve.dm.MWTranslcusionNode and only matches
58 // ahead of it because it is registered later (via a dependency in ResourceLoader)
59 // TODO: Make this less fragile.
60 ve.dm.MWReferencesListNode.static.matchFunction = function ( domElement ) {
61 function hasTypeof( el, type ) {
62 return ( el.getAttribute( 'typeof' ) || '' ).indexOf( type ) !== -1;
63 }
64 function isRefList( el ) {
65 return el && el.nodeType === Node.ELEMENT_NODE && hasTypeof( el, 'mw:Extension/references' );
66 }
67 // If the template generated only a reference list, treat it as a ref list (T52769)
68 return isRefList( domElement ) ||
69 // A div-wrapped reference list
70 ( domElement.children.length === 1 && isRefList( domElement.children[ 0 ] ) ) ||
71 // TemplateStyles, about-grouped to a div-wrapped reference list
72 (
73 hasTypeof( domElement, 'mw:Extension/templatestyles' ) &&
74 domElement.hasAttribute( 'about' ) &&
75 domElement.nextElementSibling &&
76 domElement.nextElementSibling.getAttribute( 'about' ) === domElement.getAttribute( 'about' ) &&
77 // A div-wrapped reference list
78 domElement.nextElementSibling.children.length === 1 &&
79 isRefList( domElement.nextElementSibling.children[ 0 ] )
80 // TODO: We should probably check there aren't subsequent elements. This
81 // and the above checks would be easier if the matchFunction was passed
82 // all the elements in the about group.
83 );
84 };
85
86 ve.dm.MWReferencesListNode.static.preserveHtmlAttributes = false;
87
88 ve.dm.MWReferencesListNode.static.toDataElement = function ( domElements, converter ) {
89 const type = domElements[ 0 ].getAttribute( 'typeof' ) || '';
90
91 let refListNode;
92 // We may have matched a mw:Transclusion wrapping a reference list, so pull out the refListNode
93 if ( type.indexOf( 'mw:Extension/references' ) !== -1 ) {
94 refListNode = domElements[ 0 ];
95 } else {
96 refListNode = domElements[ 0 ].querySelector( '[typeof*="mw:Extension/references"]' ) ||
97 // In the TemplateStyles case, the ref list is in the second element
98 domElements[ 1 ].querySelector( '[typeof*="mw:Extension/references"]' );
99 }
100
101 const mwDataJSON = refListNode.getAttribute( 'data-mw' );
102 const mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {};
103 const mwAttrs = mwData.attrs || {};
104 const refGroup = mwAttrs.group || '';
105 const responsiveAttr = mwAttrs.responsive;
106 const listGroup = 'mwReference/' + refGroup;
107 const templateGenerated = type.indexOf( 'mw:Transclusion' ) !== -1;
108 const isResponsiveDefault = mw.config.get( 'wgCiteResponsiveReferences' );
109
110 const referencesListElement = {
111 type: this.name,
112 attributes: {
113 mw: mwData,
114 originalMw: mwDataJSON,
115 refGroup: refGroup,
116 listGroup: listGroup,
117 isResponsive: responsiveAttr !== undefined ? responsiveAttr !== '0' : isResponsiveDefault,
118 templateGenerated: templateGenerated
119 }
120 };
121 if ( mwData.body && mwData.body.html && !templateGenerated ) {
122 // Process the nodes in .body.html as if they were this node's children
123 // Don't process template-generated reflists, that mangles the content (T209493)
124 const contentsDiv = domElements[ 0 ].ownerDocument.createElement( 'div' );
125 contentsDiv.innerHTML = mwData.body.html;
126 const contentsData = converter.getDataFromDomClean( contentsDiv );
127 return [
128 referencesListElement,
129 ...contentsData,
130 { type: '/' + this.name }
131 ];
132 }
133 return referencesListElement;
134 };
135
136 ve.dm.MWReferencesListNode.static.toDomElements = function ( data, doc, converter ) {
137 const isForParser = converter.isForParser();
138 const dataElement = data[ 0 ];
139 const attributes = dataElement.attributes;
140
141 // If we are sending a template generated ref back to Parsoid, output it as a
142 // template. This works because the dataElement already has mw, originalMw
143 // and originalDomIndex properties.
144 if ( attributes.templateGenerated && isForParser ) {
145 return ve.dm.MWTransclusionNode.static
146 .toDomElements.call( this, dataElement, doc, converter );
147 }
148
149 let els;
150 if ( !isForParser ) {
151 // Output needs to be read so re-render
152 const modelNode = ve.dm.nodeFactory.createFromElement( dataElement );
153 // Build from original doc's internal list to get all refs (T186407)
154 modelNode.setDocument( converter.originalDocInternalList.getDocument() );
155 const viewNode = ve.ce.nodeFactory.createFromModel( modelNode );
156 viewNode.modified = true;
157 viewNode.update();
158 els = [ doc.createElement( 'div' ) ];
159 els[ 0 ].appendChild( viewNode.$reflist[ 0 ] );
160 // Destroy the view node so it doesn't try to update the DOM node later
161 // (e.g. updateDebounced)
162 viewNode.destroy();
163 } else if ( dataElement.originalDomElementsHash !== undefined ) {
164 // If there's more than 1 element, preserve entire array, not just first element
165 els = ve.copyDomElements(
166 converter.getStore().value( dataElement.originalDomElementsHash ), doc );
167 } else {
168 els = [ doc.createElement( 'div' ) ];
169 }
170
171 const mwData = attributes.mw ? ve.copy( attributes.mw ) : {};
172
173 mwData.name = 'references';
174
175 if ( attributes.refGroup ) {
176 ve.setProp( mwData, 'attrs', 'group', attributes.refGroup );
177 } else if ( mwData.attrs ) {
178 delete mwData.attrs.group;
179 }
180
181 const originalMw = attributes.originalMw;
182 const originalMwData = originalMw && JSON.parse( originalMw );
183 const originalResponsiveAttr = ve.getProp( originalMwData, 'attrs', 'responsive' );
184 const isResponsiveDefault = mw.config.get( 'wgCiteResponsiveReferences' );
185
186 if ( !(
187 // The original "responsive" attribute hasn't had its meaning changed
188 originalResponsiveAttr !== undefined && ( originalResponsiveAttr !== '0' ) === attributes.isResponsive
189 ) ) {
190 if ( attributes.isResponsive !== isResponsiveDefault ) {
191 ve.setProp( mwData, 'attrs', 'responsive', attributes.isResponsive ? '' : '0' );
192 } else if ( mwData.attrs ) {
193 delete mwData.attrs.responsive;
194 }
195 }
196
197 if ( mwData.autoGenerated ) {
198 // This was an autogenerated reflist. We need to check whether changes
199 // have been made which make that no longer true. The reflist dialog
200 // handles unsetting this if changes to the properties have been made.
201 // Here we want to work out if it has been moved away from the end of
202 // the document.
203 // TODO: it would be better to do this without needing to fish through
204 // the converter's linear data. Use the DM tree instead?
205 let nextIndex = converter.documentData.indexOf( data[ data.length - 1 ] ) + 1;
206 let nextElement;
207 while ( ( nextElement = converter.documentData[ nextIndex ] ) ) {
208 if ( nextElement.type[ 0 ] !== '/' ) {
209 break;
210 }
211 nextIndex++;
212 }
213 if ( nextElement && nextElement.type !== 'internalList' ) {
214 delete mwData.autoGenerated;
215 }
216 }
217
218 const el = els[ 0 ];
219 el.setAttribute( 'typeof', 'mw:Extension/references' );
220
221 const contentsData = data.slice( 1, -1 );
222 if ( contentsData.length > 2 ) {
223 const wrapper = doc.createElement( 'div' );
224 converter.getDomSubtreeFromData( data.slice( 1, -1 ), wrapper );
225 const contentsHtml = wrapper.innerHTML; // Returns '' if wrapper is empty
226 const originalHtml = ve.getProp( mwData, 'body', 'html' ) || '';
227 const originalHtmlWrapper = doc.createElement( 'div' );
228 originalHtmlWrapper.innerHTML = originalHtml;
229 // Only set body.html if contentsHtml and originalHtml are actually different
230 if ( !originalHtmlWrapper.isEqualNode( wrapper ) ) {
231 ve.setProp( mwData, 'body', 'html', contentsHtml );
232 }
233 }
234
235 // If mwData and originalMw are the same, use originalMw to prevent reserialization.
236 // Reserialization has the potential to reorder keys and so change the DOM unnecessarily
237 if ( originalMw && ve.compare( mwData, originalMwData ) ) {
238 el.setAttribute( 'data-mw', originalMw );
239 } else {
240 el.setAttribute( 'data-mw', JSON.stringify( mwData ) );
241 }
242
243 return els;
244 };
245
246 ve.dm.MWReferencesListNode.static.describeChange = function ( key, change ) {
247 if ( key === 'refGroup' ) {
248 if ( !change.from ) {
249 return ve.htmlMsg( 'cite-ve-changedesc-reflist-group-to', this.wrapText( 'ins', change.to ) );
250 } else if ( !change.to ) {
251 return ve.htmlMsg( 'cite-ve-changedesc-reflist-group-from', this.wrapText( 'del', change.from ) );
252 } else {
253 return ve.htmlMsg( 'cite-ve-changedesc-reflist-group-both', this.wrapText( 'del', change.from ), this.wrapText( 'ins', change.to ) );
254 }
255 }
256
257 if ( key === 'isResponsive' ) {
258 if ( change.from ) {
259 return ve.msg( 'cite-ve-changedesc-reflist-responsive-unset' );
260 }
261 return ve.msg( 'cite-ve-changedesc-reflist-responsive-set' );
262 }
263
264 if ( key === 'originalMw' ) {
265 return null;
266 }
267
268 return null;
269 };
270
271 ve.dm.MWReferencesListNode.static.getHashObject = function ( dataElement ) {
272 const attributes = dataElement.attributes;
273 return {
274 type: dataElement.type,
275 attributes: {
276 refGroup: attributes.refGroup,
277 listGroup: attributes.listGroup,
278 isResponsive: attributes.isResponsive,
279 templateGenerated: attributes.templateGenerated
280 }
281 };
282 };
283
284 /* Registration */
285
286 ve.dm.modelRegistry.register( ve.dm.MWReferencesListNode );