r94377 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94376‎ | r94377 | r94378 >
Date:20:08, 12 August 2011
Author:tparscal
Status:deferred
Tags:
Comment:
* Got transactions and operations working
* Added some tests for them
Modified paths:
  • /trunk/parsers/wikidom/lib/es/es.Content.Operation.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.Content.Transaction.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.Content.js (modified) (history)
  • /trunk/parsers/wikidom/tests/transactions (added) (history)
  • /trunk/parsers/wikidom/tests/transactions/index.html (added) (history)
  • /trunk/parsers/wikidom/tests/transactions/test.js (added) (history)

Diff [purge]

Index: trunk/parsers/wikidom/tests/transactions/index.html
@@ -0,0 +1,24 @@
 2+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 3+ "http://www.w3.org/TR/html4/loose.dtd">
 4+<html>
 5+ <head>
 6+ <title>Content Transaction Tests</title>
 7+ <link rel="stylesheet" href="../../lib/qunit.css" type="text/css" />
 8+ </head>
 9+ <body>
 10+ <h1 id="qunit-header">Content Transaction Tests</h1>
 11+ <h2 id="qunit-banner"></h2>
 12+ <h2 id="qunit-userAgent"></h2>
 13+ <ol id="qunit-tests"></ol>
 14+ <script src="../../lib/es/es.js"></script>
 15+ <script src="../../lib/es/es.EventEmitter.js"></script>
 16+ <script src="../../lib/es/es.Content.js"></script>
 17+ <script src="../../lib/es/es.Content.Operation.js"></script>
 18+ <script src="../../lib/es/es.Content.Selection.js"></script>
 19+ <script src="../../lib/es/es.Content.Transaction.js"></script>
 20+ <script src="../../lib/es/es.Range.js"></script>
 21+ <script src="../../lib/jquery.js"></script>
 22+ <script src="../../lib/qunit.js"></script>
 23+ <script src="test.js"></script>
 24+ </body>
 25+</html>
Property changes on: trunk/parsers/wikidom/tests/transactions/index.html
___________________________________________________________________
Added: svn:mime-type
126 + text/plain
Index: trunk/parsers/wikidom/tests/transactions/test.js
@@ -0,0 +1,56 @@
 2+module( 'Content Transactions' );
 3+
 4+var lines = [
 5+ {
 6+ "text": "This is a test paragraph!",
 7+ "annotations": [
 8+ // Make "This" italic
 9+ {
 10+ "type": "italic",
 11+ "range": {
 12+ "start": 0,
 13+ "end": 4
 14+ }
 15+ },
 16+ // Make "a test" a link
 17+ {
 18+ "type": "xlink",
 19+ "data": {
 20+ "url": "http://www.a.com"
 21+ },
 22+ "range": {
 23+ "start": 8,
 24+ "end": 14
 25+ }
 26+ },
 27+ // Make "test" bold
 28+ {
 29+ "type": "bold",
 30+ "range": {
 31+ "start": 10,
 32+ "end": 14
 33+ }
 34+ }
 35+ ]
 36+ }
 37+];
 38+
 39+var content = es.Content.newFromWikiDomLines( lines );
 40+
 41+/* Tests */
 42+
 43+test( 'Insert and delete', 2, function() {
 44+ var Op = es.Content.Operation,
 45+ Tx = es.Content.Transaction;
 46+
 47+ var tx = new Tx(
 48+ content,
 49+ new Op( 'retain', 5 ),
 50+ new Op( 'insert', 'used to be' ),
 51+ new Op( 'delete', 2 ),
 52+ new Op( 'retain', 18 )
 53+ );
 54+
 55+ equal( tx.commit( content ).getText(), 'This used to be a test paragraph!', 'Committing' );
 56+ equal( tx.rollback( content ).getText(), 'This is a test paragraph!', 'Rolling back' );
 57+} );
Property changes on: trunk/parsers/wikidom/tests/transactions/test.js
___________________________________________________________________
Added: svn:mime-type
158 + text/plain
Added: svn:eol-style
259 + native
Index: trunk/parsers/wikidom/lib/es/es.Content.Transaction.js
@@ -6,55 +6,81 @@
77 * @param operations {Array} List of operations
88 * @property operations {Array} List of operations
99 */
10 -es.Content.Transaction = function( operations ) {
11 - this.operations = operations || [];
 10+es.Content.Transaction = function( content, operations ) {
 11+ this.content = content;
 12+ this.operations = [];
 13+ if ( arguments.length > 1 ) {
 14+ var range = new es.Range();
 15+ for ( var i = 1; i < arguments.length; i++ ) {
 16+ var operation = arguments[i];
 17+ switch ( operation.getType() ) {
 18+ case 'retain':
 19+ case 'delete':
 20+ range.to = range.from + operation.getLength();
 21+ if ( !operation.hasContent() ) {
 22+ operation.setContent( content.getContent( range ) );
 23+ }
 24+ range.from = range.to;
 25+ break;
 26+ }
 27+ this.operations.push( operation );
 28+ }
 29+ }
1230 };
1331
1432 es.Content.Transaction.prototype.add = function( operation ) {
1533 this.operations.push( operation );
1634 };
1735
18 -es.Content.Transaction.prototype.commit = function( from ) {
 36+es.Content.Transaction.prototype.commit = function() {
1937 var range = new es.Range(),
2038 to = new es.Content();
2139 for ( var i = 0; i < this.operations.length; i++ ) {
2240 var operation = this.operations[i];
23 - range.to = range.from + operation.getLength();
2441 switch (operation.getType()) {
2542 case 'retain':
26 - to.insert( to.getLength(), from.getData( range ) );
 43+ range.to = range.from + operation.getLength();
 44+ // Automatically add content to operation
 45+ if ( !operation.hasContent() ) {
 46+ operation.setContent( this.content.getContent( range ) );
 47+ }
 48+ to.insert( to.getLength(), operation.getContent().getData() );
2749 range.from = range.to;
2850 break;
2951 case 'insert':
30 - to.insert( to.getLength(), operation.getContent() );
 52+ to.insert( to.getLength(), operation.getContent().getData() );
3153 break;
3254 case 'delete':
33 - // TODO: Validate operation.getContent against content being skipped
34 - offset += operation.getLength();
 55+ range.to = range.from + operation.getLength();
 56+ // Automatically add content to operation
 57+ if ( !operation.hasContent() ) {
 58+ operation.setContent( this.content.getContent( range ) );
 59+ }
 60+ range.from = range.to;
3561 break;
3662 }
3763 }
3864 return to;
3965 };
4066
41 -es.Content.Transaction.prototype.rollback = function( from ) {
 67+es.Content.Transaction.prototype.rollback = function() {
4268 var range = new es.Range(),
4369 to = new es.Content();
4470 for ( var i = 0; i < this.operations.length; i++ ) {
4571 var operation = this.operations[i];
46 - range.to = range.from + operation.getLength();
4772 switch (operation.getType()) {
4873 case 'retain':
49 - to.insert( to.getLength(), from.getData( range ) );
 74+ range.to = range.from + operation.getLength();
 75+ to.insert( to.getLength(), operation.getContent().getData() );
5076 range.from = range.to;
5177 break;
 78+ case 'delete':
 79+ to.insert( to.getLength(), operation.getContent().getData() );
 80+ break;
5281 case 'insert':
53 - // TODO: Validate operation.getContent against content being skipped
54 - offset += operation.getLength();
 82+ range.to = range.from + operation.getLength();
 83+ range.from = range.to;
5584 break;
56 - case 'delete':
57 - to.insert( to.getLength(), operation.getContent() );
58 - break;
5985 }
6086 }
6187 return to;
Index: trunk/parsers/wikidom/lib/es/es.Content.Operation.js
@@ -4,7 +4,7 @@
55 * @class
66 * @constructor
77 * @param type {String} Type of operation, e.g. insert, delete, annotate
8 - * @param content {es.Content} Content being inserted or modified
 8+ * @param content {Mixed} Either {Integer} length, {String} text or {es.Content} content
99 * @param data {Object} Data of operation, e.g. range
1010 * @property type {String} Type of operation
1111 * @property content {es.Content} Content of operation
@@ -12,8 +12,21 @@
1313 */
1414 es.Content.Operation = function( type, content, data ) {
1515 this.type = type;
16 - this.contents = contents;
17 - this.data = data;
 16+ switch ( typeof content ) {
 17+ case 'number':
 18+ this.content = null;
 19+ this.length = content;
 20+ break;
 21+ case 'string':
 22+ this.content = es.Content.newFromText( content );
 23+ this.length = this.content.getLength();
 24+ break;
 25+ case 'object':
 26+ this.content = content;
 27+ this.length = this.content.getLength();
 28+ break;
 29+ }
 30+ this.data = data || {};
1831 };
1932
2033 es.Content.Operation.prototype.getType = function() {
@@ -24,8 +37,16 @@
2538 return this.content;
2639 };
2740
 41+es.Content.Operation.prototype.setContent = function( content ) {
 42+ this.content = content;
 43+};
 44+
 45+es.Content.Operation.prototype.hasContent = function() {
 46+ return !!this.content;
 47+};
 48+
2849 es.Content.Operation.prototype.getLength = function() {
29 - return this.content.getLength();;
 50+ return this.content ? this.content.getLength() : this.length;
3051 };
3152
3253 es.Content.Operation.prototype.getData = function() {
Index: trunk/parsers/wikidom/lib/es/es.Content.js
@@ -90,6 +90,18 @@
9191 /* Static Methods */
9292
9393 /**
 94+ * Creates a new Content object from plain text.
 95+ *
 96+ * @static
 97+ * @method
 98+ * @param text {String} Text to convert
 99+ * @returns {es.Content} EditSurface content object containing converted text
 100+ */
 101+es.Content.newFromText = function( text ) {
 102+ return new es.Content( text.split('') );
 103+};
 104+
 105+/**
94106 * Creates a new Content object from a WikiDom line object.
95107 *
96108 * @static

Status & tagging log