r106061 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106060‎ | r106061 | r106062 >
Date:18:45, 13 December 2011
Author:gwicke
Status:deferred
Tags:
Comment:
Clean up and comment the Cite extension a bit.
Modified paths:
  • /trunk/extensions/VisualEditor/modules/parser/ext.Cite.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/parser/ext.Cite.js
@@ -1,16 +1,20 @@
22 /**
3 - * The ref / references tags don't do any fancy HTML, so we can actually
4 - * implement this in terms of parse tree manipulations, skipping the need
5 - * for renderer-specific plugins as well.
 3+ * Simple token transform version of the Cite extension.
64 *
7 - * Pretty neat huh!
 5+ * @class
 6+ * @constructor
87 */
9 -
108 function Cite () {
119 this.refGroups = {};
1210 this.refTokens = [];
1311 }
1412
 13+/**
 14+ * Register with dispatcher.
 15+ *
 16+ * @method
 17+ * @param {Object} TokenTransformDispatcher to register to
 18+ */
1519 Cite.prototype.register = function ( dispatcher ) {
1620 // Register for ref and references tag tokens
1721 var self = this;
@@ -27,9 +31,17 @@
2832 };
2933
3034
31 -// Convert list of key-value pairs to object, with first entry for a key
32 -// winning.
33 -// XXX: Move to general util module
 35+/**
 36+ * Convert list of key-value pairs to object, with first entry for a
 37+ * key.
 38+ *
 39+ * XXX: Move to general utils
 40+ *
 41+ * @static
 42+ * @method
 43+ * @param {Array} List of [key, value] pairs
 44+ * @returns {Object} Object with key/values set, first entry wins.
 45+ */
3446 Cite.prototype.attribsToObject = function ( attribs ) {
3547 if ( attribs === undefined ) {
3648 return {};
@@ -44,7 +56,13 @@
4557 return obj;
4658 };
4759
48 -
 60+/**
 61+ * Handle ref tag tokens.
 62+ *
 63+ * @method
 64+ * @param {Object} TokenContext
 65+ * @returns {Object} TokenContext
 66+ */
4967 Cite.prototype.onRef = function ( tokenCTX ) {
5068
5169 var refGroups = this.refGroups;
@@ -131,19 +149,21 @@
132150 {
133151 type: 'TAG',
134152 name: 'span',
135 - attribs: [['id', linkback],
136 - ['class', 'reference'],
137 - // ignore element when serializing back to wikitext
138 - ['data-nosource', '']]
 153+ attribs: [
 154+ ['id', linkback],
 155+ ['class', 'reference'],
 156+ // ignore element when serializing back to wikitext
 157+ ['data-nosource', '']
 158+ ]
139159 },
140160 {
141161 type: 'TAG',
142162 name: 'a',
143 - attribs:
144 - [['data-type', 'hashlink'],
 163+ attribs: [
 164+ ['data-type', 'hashlink'],
145165 ['href', '#' + ref.target]
146166 // XXX: Add round-trip info here?
147 - ]
 167+ ]
148168 },
149169 {
150170 type: 'TEXT',
@@ -161,6 +181,13 @@
162182 return tokenCTX;
163183 };
164184
 185+/**
 186+ * Handle references tag tokens.
 187+ *
 188+ * @method
 189+ * @param {Object} TokenContext
 190+ * @returns {Object} TokenContext
 191+ */
165192 Cite.prototype.onReferences = function ( tokenCTX ) {
166193
167194 var refGroups = this.refGroups;
@@ -174,36 +201,41 @@
175202 attribs: [['id', ref.target]]
176203 }];
177204 if (ref.linkbacks.length == 1) {
178 - out = out.concat([{
179 - type: 'TAG',
180 - name: 'a',
181 - attribs:
182 - [['data-type', 'hashlink'],
183 - ['href', '#' + ref.linkbacks[0]]
184 - ]
185 - },
186 - {type: 'TEXT', value: arrow},
187 - {type: 'ENDTAG', name: 'a'}
188 - ],
189 - ref.tokens // The original content tokens
 205+ out = out.concat([
 206+ {
 207+ type: 'TAG',
 208+ name: 'a',
 209+ attribs: [
 210+ ['data-type', 'hashlink'],
 211+ ['href', '#' + ref.linkbacks[0]]
 212+ ]
 213+ },
 214+ {type: 'TEXT', value: arrow},
 215+ {type: 'ENDTAG', name: 'a'}
 216+ ],
 217+ ref.tokens // The original content tokens
190218 );
191219 } else {
192220 out.content.push({type: 'TEXT', value: arrow});
193221 $.each(ref.linkbacks, function(i, linkback) {
194 - out = out.concat([{
195 - type: 'TAG',
196 - name: 'a',
197 - attribs:
198 - [['data-type', 'hashlink'],
199 - ['href', '#' + ref.linkbacks[0]]
200 - ]
201 - },
202 - // XXX: make formatNum available!
203 - //{type: 'TEXT', value: env.formatNum( ref.groupIndex + '.' + i)},
204 - {type: 'TEXT', value: ref.groupIndex + '.' + i},
205 - {type: 'ENDTAG', name: 'a'}
206 - ],
207 - ref.tokens // The original content tokens
 222+ out = out.concat([
 223+ {
 224+ type: 'TAG',
 225+ name: 'a',
 226+ attribs: [
 227+ ['data-type', 'hashlink'],
 228+ ['href', '#' + ref.linkbacks[0]]
 229+ ]
 230+ },
 231+ // XXX: make formatNum available!
 232+ //{
 233+ // type: 'TEXT',
 234+ // value: env.formatNum( ref.groupIndex + '.' + i)
 235+ //},
 236+ {type: 'TEXT', value: ref.groupIndex + '.' + i},
 237+ {type: 'ENDTAG', name: 'a'}
 238+ ],
 239+ ref.tokens // The original content tokens
208240 );
209241 });
210242 }
@@ -220,22 +252,35 @@
221253 if (options.group in refGroups) {
222254 var group = refGroups[options.group];
223255 var listItems = $.map(group.refs, renderLine);
224 - tokenCTX.token = [{
225 - type: 'TAG',
226 - name: 'ol',
227 - attribs: [['class', 'references']]
228 - }].concat(listItems, {type: 'ENDTAG', name: 'ol'});
 256+ tokenCTX.token = [
 257+ {
 258+ type: 'TAG',
 259+ name: 'ol',
 260+ attribs: [
 261+ ['class', 'references']
 262+ ]
 263+ }
 264+ ].concat( listItems, { type: 'ENDTAG', name: 'ol' } );
229265 } else {
230266 tokenCTX.token = {
231267 type: 'SELFCLOSINGTAG',
232268 name: 'placeholder',
233 - attribs: [['data-origNode', JSON.stringify(token)]]
 269+ attribs: [
 270+ ['data-origNode', JSON.stringify(token)]
 271+ ]
234272 };
235273 }
236274
237275 return tokenCTX;
238276 };
239277
 278+/**
 279+ * Handle end token.
 280+ *
 281+ * @method
 282+ * @param {Object} TokenContext
 283+ * @returns {Object} TokenContext
 284+ */
240285 Cite.prototype.onEnd = function ( tokenCTX ) {
241286 // XXX: Emit error messages if references tag was missing!
242287 // Clean up

Status & tagging log