Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.DOMConverter.js |
— | — | @@ -35,7 +35,7 @@ |
36 | 36 | case 'dl': |
37 | 37 | case 'dd': |
38 | 38 | return { |
39 | | - handler: this._convertHTMLLeaf, |
| 39 | + handler: this._convertHTMLBranch, |
40 | 40 | type: 'listItem' |
41 | 41 | }; |
42 | 42 | case 'pre': |
— | — | @@ -57,11 +57,10 @@ |
58 | 58 | handler: this._convertHTMLBranch, |
59 | 59 | type: nodeName.toLowerCase() |
60 | 60 | }; |
61 | | - break; |
62 | 61 | } |
63 | 62 | }; |
64 | 63 | |
65 | | -DOMConverter.prototype.getHTMLAnnotationType = function ( nodeName ) { |
| 64 | +DOMConverter.prototype.getHTMLAnnotationType = function ( nodeName, warn ) { |
66 | 65 | switch ( nodeName.toLowerCase() ) { |
67 | 66 | case 'i': |
68 | 67 | return 'textStyle/italic'; |
— | — | @@ -72,10 +71,11 @@ |
73 | 72 | case 'a': |
74 | 73 | return 'link/unknown'; // XXX: distinguish internal / external etc |
75 | 74 | 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 | + } |
78 | 79 | return undefined; |
79 | | - break; |
80 | 80 | } |
81 | 81 | }; |
82 | 82 | |
— | — | @@ -132,18 +132,54 @@ |
133 | 133 | attributes: this._HTMLPropertiesToWikiAttributes( node ), |
134 | 134 | children: [] |
135 | 135 | }; |
| 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 | + |
136 | 150 | for ( var i = 0, l = children.length; i < l; i++ ) { |
137 | 151 | var cnode = children[i]; |
138 | 152 | switch ( cnode.nodeType ) { |
139 | 153 | 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 | + } |
146 | 178 | 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; |
148 | 184 | break; |
149 | 185 | case Node.COMMENT_NODE: |
150 | 186 | // add a comment node. |
— | — | @@ -183,7 +219,7 @@ |
184 | 220 | switch ( cnode.nodeType ) { |
185 | 221 | case Node.ELEMENT_NODE: |
186 | 222 | // Call a handler for the particular annotation node type |
187 | | - var annotationtype = this.getHTMLAnnotationType( cnode.nodeName ); |
| 223 | + var annotationtype = this.getHTMLAnnotationType( cnode.nodeName, true ); |
188 | 224 | if ( annotationtype ) { |
189 | 225 | var res = this._convertHTMLAnnotation( cnode, offset, annotationtype ); |
190 | 226 | //console.log( 'res leaf: ' + JSON.stringify(res, null, 2)); |
— | — | @@ -232,7 +268,7 @@ |
233 | 269 | switch ( cnode.nodeType ) { |
234 | 270 | case Node.ELEMENT_NODE: |
235 | 271 | // Call a handler for the particular annotation node type |
236 | | - var annotationtype = this.getHTMLAnnotationType(cnode.nodeName); |
| 272 | + var annotationtype = this.getHTMLAnnotationType(cnode.nodeName, true); |
237 | 273 | if ( annotationtype ) { |
238 | 274 | var res = this._convertHTMLAnnotation( cnode, offset, annotationtype ); |
239 | 275 | //console.log( 'res annotations 2: ' + JSON.stringify(res, null, 2)); |