r106205 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106204‎ | r106205 | r106206 >
Date:15:40, 14 December 2011
Author:gwicke
Status:deferred
Tags:
Comment:
Improve WikiDom conversion: Handle text and annotations in branch nodes as
paragraphs and treat list items as branches.
Modified paths:
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.DOMConverter.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.DOMConverter.js
@@ -35,7 +35,7 @@
3636 case 'dl':
3737 case 'dd':
3838 return {
39 - handler: this._convertHTMLLeaf,
 39+ handler: this._convertHTMLBranch,
4040 type: 'listItem'
4141 };
4242 case 'pre':
@@ -57,11 +57,10 @@
5858 handler: this._convertHTMLBranch,
5959 type: nodeName.toLowerCase()
6060 };
61 - break;
6261 }
6362 };
6463
65 -DOMConverter.prototype.getHTMLAnnotationType = function ( nodeName ) {
 64+DOMConverter.prototype.getHTMLAnnotationType = function ( nodeName, warn ) {
6665 switch ( nodeName.toLowerCase() ) {
6766 case 'i':
6867 return 'textStyle/italic';
@@ -72,10 +71,11 @@
7372 case 'a':
7473 return 'link/unknown'; // XXX: distinguish internal / external etc
7574 default:
76 - console.log( 'HTML to Wiki DOM conversion error. Unsupported html annotation ' +
77 - nodeName );
 75+ if ( warn ) {
 76+ console.log( 'HTML to Wiki DOM conversion error. Unsupported html annotation ' +
 77+ nodeName );
 78+ }
7879 return undefined;
79 - break;
8080 }
8181 };
8282
@@ -132,18 +132,54 @@
133133 attributes: this._HTMLPropertiesToWikiAttributes( node ),
134134 children: []
135135 };
 136+
 137+ var parNode = null;
 138+
 139+ function newPara () {
 140+ parNode = {
 141+ type: 'paragraph',
 142+ content: {
 143+ text: '',
 144+ annotations: []
 145+ }
 146+ };
 147+ wnode.children.push( parNode );
 148+ }
 149+
136150 for ( var i = 0, l = children.length; i < l; i++ ) {
137151 var cnode = children[i];
138152 switch ( cnode.nodeType ) {
139153 case Node.ELEMENT_NODE:
140 - // Call a handler for the particular node type
141 - var hi = this.getHTMLHandlerInfo( cnode.nodeName );
142 - var res = hi.handler.call(this, cnode, offset + 1, hi.type );
143 - wnode.children.push( res.node );
144 - offset = res.offset;
145 - break;
 154+ // Check if element type is an annotation
 155+ var annotationtype = this.getHTMLAnnotationType( cnode.nodeName );
 156+ if ( annotationtype ) {
 157+ if ( !parNode ) {
 158+ newPara()
 159+ }
 160+ var res = this._convertHTMLAnnotation( cnode, offset, annotationtype );
 161+ //console.log( 'res leaf: ' + JSON.stringify(res, null, 2));
 162+ offset += res.text.length;
 163+ parNode.content.text += res.text;
 164+ //console.log( 'res annotations: ' + JSON.stringify(res, null, 2));
 165+ parNode.content.annotations = parNode.content.annotations
 166+ .concat( res.annotations );
 167+ break;
 168+ } else {
 169+ // Close last paragraph, if still open.
 170+ parNode = null;
 171+ // Call a handler for the particular node type
 172+ var hi = this.getHTMLHandlerInfo( cnode.nodeName );
 173+ var res = hi.handler.call(this, cnode, offset + 1, hi.type );
 174+ wnode.children.push( res.node );
 175+ offset = res.offset;
 176+ break;
 177+ }
146178 case Node.TEXT_NODE:
147 - // Create a paragraph and add it to children?
 179+ if ( !parNode ) {
 180+ newPara();
 181+ }
 182+ parNode.content.text += cnode.data;
 183+ offset += cnode.data.length;
148184 break;
149185 case Node.COMMENT_NODE:
150186 // add a comment node.
@@ -183,7 +219,7 @@
184220 switch ( cnode.nodeType ) {
185221 case Node.ELEMENT_NODE:
186222 // Call a handler for the particular annotation node type
187 - var annotationtype = this.getHTMLAnnotationType( cnode.nodeName );
 223+ var annotationtype = this.getHTMLAnnotationType( cnode.nodeName, true );
188224 if ( annotationtype ) {
189225 var res = this._convertHTMLAnnotation( cnode, offset, annotationtype );
190226 //console.log( 'res leaf: ' + JSON.stringify(res, null, 2));
@@ -232,7 +268,7 @@
233269 switch ( cnode.nodeType ) {
234270 case Node.ELEMENT_NODE:
235271 // Call a handler for the particular annotation node type
236 - var annotationtype = this.getHTMLAnnotationType(cnode.nodeName);
 272+ var annotationtype = this.getHTMLAnnotationType(cnode.nodeName, true);
237273 if ( annotationtype ) {
238274 var res = this._convertHTMLAnnotation( cnode, offset, annotationtype );
239275 //console.log( 'res annotations 2: ' + JSON.stringify(res, null, 2));

Status & tagging log