r101862 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101861‎ | r101862 | r101863 >
Date:19:39, 3 November 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Fixed lots of bugs in the serializers, Wikitext and HTML look good
Modified paths:
  • /trunk/extensions/VisualEditor/modules/es/models/es.DocumentModel.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/es/serializers/es.HtmlSerializer.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/es/serializers/es.WikitextSerializer.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/es/models/es.DocumentModel.js
@@ -492,12 +492,21 @@
493493 var stack = [];
494494 // Text and annotations
495495 function start( offset, annotation ) {
496 - stack.push( es.extendObject( true, {}, annotation, { 'range': { 'start': offset } } ) );
 496+ // Make a new verion of the annotation object and push it to the stack
 497+ var obj = {
 498+ 'type': annotation.type,
 499+ 'range': { 'start': offset }
 500+ };
 501+ if ( annotation.data ) {
 502+ obj.data = es.copyObject( annotation.data );
 503+ }
 504+ stack.push( obj );
497505 }
498506 function end( offset, annotation ) {
499507 for ( var i = stack.length - 1; i >= 0; i-- ) {
500508 if ( !stack[i].range.end ) {
501509 if ( annotation ) {
 510+ // We would just compare hashes, but the stack doesn't contain any
502511 if ( stack[i].type === annotation.type &&
503512 es.compareObjects( stack[i].data, annotation.data ) ) {
504513 stack[i].range.end = offset;
Index: trunk/extensions/VisualEditor/modules/es/serializers/es.HtmlSerializer.js
@@ -26,6 +26,18 @@
2727 return ( new es.HtmlSerializer( options ) ).document( data );
2828 };
2929
 30+es.HtmlSerializer.getHtmlAttributes = function( attributes ) {
 31+ var htmlAttributes = {},
 32+ count = 0;
 33+ for ( var key in attributes ) {
 34+ if ( key.indexOf( 'html/' ) === 0 ) {
 35+ htmlAttributes[key.substr( 5 )] = attributes[key];
 36+ count++;
 37+ }
 38+ }
 39+ return count ? htmlAttributes : null;
 40+};
 41+
3042 /* Methods */
3143
3244 es.HtmlSerializer.prototype.document = function( node, rawFirstParagraph ) {
@@ -35,7 +47,7 @@
3648 if ( childNode.type in this ) {
3749 // Special case for paragraphs which have particular wrapping needs
3850 if ( childNode.type === 'paragraph' ) {
39 - lines.push( this.paragraph( childNode, rawFirstParagraph && b === 0 ) );
 51+ lines.push( this.paragraph( childNode, rawFirstParagraph && i === 0 ) );
4052 } else {
4153 lines.push( this[childNode.type].call( this, childNode ) );
4254 }
@@ -52,9 +64,9 @@
5365 return es.Html.makeTag( 'hr', {}, false );
5466 };
5567
56 -es.HtmlSerializer.prototype.heading = function( heading ) {
 68+es.HtmlSerializer.prototype.heading = function( node ) {
5769 return es.Html.makeTag(
58 - 'h' + heading.level, {}, this.serializeLine( heading.content )
 70+ 'h' + node.attributes.level, {}, this.content( node.content )
5971 );
6072 };
6173
@@ -72,8 +84,9 @@
7385 };
7486
7587 es.HtmlSerializer.prototype.table = function( node ) {
76 - var lines = [];
77 - lines.push( es.Html.makeOpeningTag( 'table', node.attributes ) );
 88+ var lines = [],
 89+ attributes = es.HtmlSerializer.getHtmlAttributes( node.attributes );
 90+ lines.push( es.Html.makeOpeningTag( 'table', attributes ) );
7891 for ( var i = 0, length = node.children.length; i < length; i++ ) {
7992 lines.push( this.tableRow( node.children[i] ) );
8093 }
@@ -82,8 +95,9 @@
8396 };
8497
8598 es.HtmlSerializer.prototype.tableRow = function( node ) {
86 - var lines = [];
87 - lines.push( es.Html.makeOpeningTag( 'tr', node.attributes ) );
 99+ var lines = [],
 100+ attributes = es.HtmlSerializer.getHtmlAttributes( node.attributes );
 101+ lines.push( es.Html.makeOpeningTag( 'tr', attributes ) );
88102 for ( var i = 0, length = node.children.length; i < length; i++ ) {
89103 lines.push( this.tableCell( node.children[i] ) );
90104 }
@@ -93,10 +107,11 @@
94108
95109 es.HtmlSerializer.prototype.tableCell = function( node ) {
96110 var symbolTable = {
97 - 'tableHeading': 'th',
98 - 'tableCell': 'td'
99 - };
100 - return es.Html.makeTag( symbolTable[node.type], node.attributes, this.document( node, true ) );
 111+ 'tableHeading': 'th',
 112+ 'tableCell': 'td'
 113+ },
 114+ attributes = es.HtmlSerializer.getHtmlAttributes( node.attributes );
 115+ return es.Html.makeTag( symbolTable[node.type], attributes, this.document( node, true ) );
101116 };
102117
103118 es.HtmlSerializer.prototype.transclusion = function( node ) {
Index: trunk/extensions/VisualEditor/modules/es/serializers/es.WikitextSerializer.js
@@ -26,6 +26,18 @@
2727 return ( new es.WikitextSerializer( options ) ).document( data );
2828 };
2929
 30+es.WikitextSerializer.getHtmlAttributes = function( attributes ) {
 31+ var htmlAttributes = {},
 32+ count = 0;
 33+ for ( var key in attributes ) {
 34+ if ( key.indexOf( 'html/' ) === 0 ) {
 35+ htmlAttributes[key.substr( 5 )] = attributes[key];
 36+ count++;
 37+ }
 38+ }
 39+ return count ? htmlAttributes : null;
 40+};
 41+
3042 /* Methods */
3143
3244 es.WikitextSerializer.prototype.document = function( node, rawFirstParagraph ) {
@@ -56,8 +68,8 @@
5769 };
5870
5971 es.WikitextSerializer.prototype.heading = function( node ) {
60 - var symbols = es.repeatString( '=', node.level );
61 - return symbols + this.serializeLine( node.line ) + symbols;
 72+ var symbols = es.repeatString( '=', node.attributes.level );
 73+ return symbols + this.content( node.content ) + symbols;
6274 };
6375
6476 es.WikitextSerializer.prototype.paragraph = function( node ) {
@@ -80,28 +92,37 @@
8193 for ( var i = 0, length = node.children.length; i < length; i++ ) {
8294 var childNode = node.children[i];
8395 lines.push(
84 - convertStyles( childNode.styles ) + ' ' + this.content( childNode.content, path )
 96+ convertStyles( childNode.attributes.styles ) + ' ' +
 97+ this.content( childNode.content )
8598 );
8699 }
87100 return lines.join( '\n' );
88101 };
89102
90 -es.WikitextSerializer.prototype.table = function( table ) {
91 - var lines = [];
92 - lines.push( '{|' + es.Document.Serializer.buildXmlAttributes( table.attributes ) );
 103+es.WikitextSerializer.prototype.table = function( node ) {
 104+ var lines = [],
 105+ attributes = es.WikitextSerializer.getHtmlAttributes( node.attributes );
 106+ if ( attributes ) {
 107+ attributes = es.Html.makeAttributeList( attributes );
 108+ }
 109+ lines.push( '{|' + attributes );
93110 for ( var i = 0, length = node.children.length; i < length; i++ ) {
94 - lines.push( this.tableRow( node.children[i] ) );
 111+ lines.push( this.tableRow( node.children[i], i === 0 ) );
95112 }
96113 lines.push( '|}' );
97114 return lines.join( '\n' );
98115 };
99116
100 -es.WikitextSerializer.prototype.tableRow = function( node ) {
101 - var lines = [];
 117+es.WikitextSerializer.prototype.tableRow = function( node, first ) {
 118+ var lines = [],
 119+ attributes = es.WikitextSerializer.getHtmlAttributes( node.attributes );
 120+ if ( attributes ) {
 121+ attributes = es.Html.makeAttributeList( attributes );
 122+ }
 123+ if ( !first || attributes ) {
 124+ lines.push( '|-' + attributes );
 125+ }
102126 for ( var i = 0, length = node.children.length; i < length; i++ ) {
103 - if ( i > 0 ) {
104 - lines.push( '|-' );
105 - }
106127 lines.push( this.tableCell( node.children[i] ) );
107128 }
108129 return lines.join( '\n' );
@@ -110,11 +131,13 @@
111132 es.WikitextSerializer.prototype.tableCell = function( node ) {
112133 var symbolTable = {
113134 'tableHeading': '!',
114 - 'tableData': '|'
 135+ 'tableCell': '|'
115136 };
116 - return symbolTable[node.type] +
117 - ( node.attributes ? es.Html.makeAttributeList( cell.attributes ) + '|' : '' ) +
118 - this.document( node, true );
 137+ var attributes = es.WikitextSerializer.getHtmlAttributes( node.attributes );
 138+ if ( attributes ) {
 139+ attributes = es.Html.makeAttributeList( attributes ) + '|';
 140+ }
 141+ return symbolTable[node.type] + attributes + this.document( node, true );
119142 };
120143
121144 es.WikitextSerializer.prototype.transclusion = function( node ) {

Status & tagging log