r106343 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106342‎ | r106343 | r106344 >
Date:17:33, 15 December 2011
Author:gwicke
Status:deferred
Tags:
Comment:
Add some more attribute remapping capabilities to the DOMConverter, and clean
up some grammar formatting.
Modified paths:
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.DOMConverter.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.DOMConverter.js
@@ -35,7 +35,9 @@
3636 * and optionally 'attribs', WikiDom-specific attributes implied by the element name.
3737 */
3838 DOMConverter.prototype._getHTMLtoWikiDomHandlerInfo = function ( nodeName ) {
 39+ var wikiName = '';
3940 switch ( nodeName.toLowerCase() ) {
 41+ // leaf nodes first, with fall-through to last leaf..
4042 case 'p':
4143 return {
4244 handler: this._convertHTMLLeaf,
@@ -121,7 +123,7 @@
122124 type: 'blockquote'
123125 };
124126 default:
125 - console.log( 'HTML to Wiki DOM conversion error. Unsupported node name ' +
 127+ console.log( 'HTML to Wiki DOM conversion warning: Unknown node name ' +
126128 nodeName );
127129 return {
128130 handler: this._convertHTMLBranch,
@@ -139,8 +141,9 @@
140142 * @returns {String} WikiDom annotation type or undefined if element name does
141143 * not map to an annotation.
142144 */
143 -DOMConverter.prototype._getWikiDomAnnotationType = function ( nodeName, warn ) {
144 - switch ( nodeName.toLowerCase() ) {
 145+DOMConverter.prototype._getWikiDomAnnotationType = function ( node, warn ) {
 146+ var name = node.nodeName.toLowerCase();
 147+ switch ( name ) {
145148 case 'i':
146149 return 'textStyle/italic';
147150 case 'b':
@@ -148,7 +151,13 @@
149152 case 'span':
150153 return 'textStyle/span';
151154 case 'a':
152 - return 'link/unknown'; // XXX: distinguish internal / external etc
 155+ var atype = node.getAttribute( 'data-type' );
 156+ if ( atype ) {
 157+ return 'link/' + atype;
 158+ } else {
 159+ return 'link/unknown';
 160+ }
 161+ break; // make JSHint happy
153162 case 'template':
154163 return 'object/template';
155164 case 'ref':
@@ -157,7 +166,7 @@
158167 return 'object/includeonly'; // XXX
159168 default:
160169 if ( warn ) {
161 - console.log( 'HTML to Wiki DOM conversion error. Unsupported html annotation ' +
 170+ console.log( 'HTML to Wiki DOM conversion warning: Unsupported html annotation ' +
162171 nodeName );
163172 }
164173 return undefined;
@@ -201,7 +210,7 @@
202211 switch ( cnode.nodeType ) {
203212 case Node.ELEMENT_NODE:
204213 // Check if element type is an annotation
205 - var annotationtype = this._getWikiDomAnnotationType( cnode.nodeName );
 214+ var annotationtype = this._getWikiDomAnnotationType( cnode );
206215 if ( annotationtype ) {
207216 if ( !parNode ) {
208217 newPara();
@@ -274,7 +283,7 @@
275284 switch ( cnode.nodeType ) {
276285 case Node.ELEMENT_NODE:
277286 // Call a handler for the particular annotation node type
278 - var annotationtype = this._getWikiDomAnnotationType( cnode.nodeName, true );
 287+ var annotationtype = this._getWikiDomAnnotationType( cnode, true );
279288 if ( annotationtype ) {
280289 var res = this._convertHTMLAnnotation( cnode, offset, annotationtype );
281290 //console.log( 'res leaf: ' + JSON.stringify(res, null, 2));
@@ -332,7 +341,7 @@
333342 switch ( cnode.nodeType ) {
334343 case Node.ELEMENT_NODE:
335344 // Call a handler for the particular annotation node type
336 - var annotationtype = this._getWikiDomAnnotationType(cnode.nodeName, true);
 345+ var annotationtype = this._getWikiDomAnnotationType(cnode, true);
337346 if ( annotationtype ) {
338347 var res = this._convertHTMLAnnotation( cnode, offset, annotationtype );
339348 //console.log( 'res annotations 2: ' + JSON.stringify(res, null, 2));
@@ -389,13 +398,24 @@
390399 return out;
391400 };
392401
 402+/**
 403+ * Convert HTML element attributes into WikiDom annotation data attributes.
 404+ *
 405+ * @param {Object} DOM node
 406+ * @return {Object} data object
 407+ */
393408 DOMConverter.prototype._HTMLPropertiesToWikiData = function ( elem ) {
394409 var attribs = elem.attributes,
 410+ name = elem.tagName.toLowerCase();
395411 out = {};
396412 for ( var i = 0, l = attribs.length; i < l; i++ ) {
397413 var attrib = attribs.item(i),
398414 key = attrib.name;
399 - if ( key.match( /^data-json-/ ) ) {
 415+
 416+ if ( this._HTMLPropertiesToWikiAttributesMap[name] &&
 417+ this._HTMLPropertiesToWikiAttributesMap[name][key] ) {
 418+ out[this._HTMLPropertiesToWikiAttributesMap[name][key]] = attrib.value;
 419+ } else if ( key.match( /^data-json-/ ) ) {
400420 // strip data-json- prefix and decode
401421 out[key.replace( /^data-json-/, '' )] = JSON.parse(attrib.value);
402422 } else if ( key.match( /^data-/ ) ) {
@@ -414,7 +434,15 @@
415435 }
416436 return out;
417437 };
 438+// Attribute map html (tagName, attributeName) pairs to WikiDom names for the
 439+// same element
 440+DOMConverter.prototype._HTMLPropertiesToWikiAttributesMap = {
 441+ a: {
 442+ href: 'title'
 443+ }
 444+};
418445
 446+
419447 // Quick HACK: define Node constants locally
420448 // https://developer.mozilla.org/en/nodeType
421449 var Node = {
Index: trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt
@@ -592,11 +592,17 @@
593593 text = [{type: 'TEXT', value: "[" + linkCount + "]"}];
594594 linkCount++;
595595 }
596 - return [ { type: 'TAG',
597 - name: 'a',
598 - attribs: [['href', target]] } ]
599 - .concat( text
600 - , [{type: 'ENDTAG', name: 'a'}]);
 596+ return [
 597+ {
 598+ type: 'TAG',
 599+ name: 'a',
 600+ attribs: [
 601+ ['href', target],
 602+ ['data-type', 'external']
 603+ ],
 604+ }
 605+ ].concat( text
 606+ , [{type: 'ENDTAG', name: 'a'}]);
601607 }
602608 / "[" & { clearFlag('extlink'); return false; }
603609
@@ -719,7 +725,9 @@
720726 var obj = {
721727 type: 'TAG',
722728 name: 'a',
723 - attribs: [['data-type', 'internal']]
 729+ attribs: [
 730+ ['data-type', 'internal']
 731+ ]
724732 };
725733 obj.attribs.push(['href', target]);
726734 if (ltext && ltext.length) {

Status & tagging log