r94847 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94846‎ | r94847 | r94848 >
Date:23:28, 17 August 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Moved es.Content.Transaction to es.Transaction
Modified paths:
  • /trunk/parsers/wikidom/lib/es/es.Content.Transaction.js (deleted) (history)
  • /trunk/parsers/wikidom/lib/es/es.Transaction.js (added) (history)
  • /trunk/parsers/wikidom/tests/transactions/index.html (modified) (history)
  • /trunk/parsers/wikidom/tests/transactions/test.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/es/es.Content.Transaction.js
@@ -1,168 +0,0 @@
2 -/**
3 - * Creates an operation to be applied to a content object.
4 - *
5 - * @class
6 - * @constructor
7 - * @param content {es.Content} Content to operate on
8 - * @property operations {Array} List of operations
9 - */
10 -es.Content.Transaction = function() {
11 - this.operations = [];
12 - this.cursor = 0;
13 -};
14 -
15 -/**
16 - * List of operation implementations.
17 - */
18 -es.Content.Transaction.operations = ( function() {
19 - function annotate( con, add, rem ) {
20 - // Ensure that modifications to annotated characters do not affect other uses of the same
21 - // content by isolating it - performing a deep-slice
22 - con.isolate();
23 - for ( var i = 0; i < add.length; i++ ) {
24 - con.annotate( 'add', add[i] );
25 - }
26 - for ( var i = 0; i < rem.length; i++ ) {
27 - con.annotate( 'remove', rem[i] );
28 - }
29 - }
30 - function retain( val, cur, src, dst, add, rem ) {
31 - var con = src.getContent( new es.Range( cur, cur + val ) );
32 - if ( add.length || rem.length ) {
33 - annotate( con, add, rem );
34 - }
35 - dst.insert( dst.getLength(), con.getData() );
36 - return val;
37 - }
38 - function insert( val, cur, src, dst, add, rem ) {
39 - var con = val.getContent();
40 - if ( add.length || rem.length ) {
41 - annotate( con, add, rem );
42 - }
43 - dst.insert( dst.getLength(), con.getData() );
44 - return 0;
45 - }
46 - function start( val, cur, src, dst, add, rem ) {
47 - if ( val.method === 'add' ) {
48 - add.push( val.annotation );
49 - } else if ( val.method === 'remove' ) {
50 - rem.push( val.annotation );
51 - } else {
52 - throw 'Annotation method error. Unsupported annotation method: ' + val.method;
53 - }
54 - return 0;
55 - }
56 - function end( val, cur, src, dst, add, rem ) {
57 - var stack;
58 - if ( val.method === 'add' ) {
59 - stack = add;
60 - } else if ( val.method === 'remove' ) {
61 - stack = rem;
62 - } else {
63 - throw 'Annotation method error. Unsupported annotation method: ' + val.method;
64 - }
65 - var index;
66 - for ( var i = 0; i < stack.length; i++ ) {
67 - // TODO: Compare data too: es.Content.compareObjects( stack[i], val.annotation )
68 - if ( stack[i].type === val.annotation.type ) {
69 - index = i;
70 - break;
71 - }
72 - }
73 - if ( index === undefined ) {
74 - throw 'Annotation stack error. Annotation is missing.';
75 - }
76 - stack.splice( index, 1 );
77 - return 0;
78 - }
79 - function measure( val ) {
80 - return val.getLength();
81 - }
82 - function pass( val ) {
83 - return val;
84 - }
85 - function zero( val ) {
86 - return 0;
87 - }
88 - return {
89 - 'retain': {
90 - 'commit': retain,
91 - 'rollback': retain,
92 - 'advance': pass
93 - },
94 - 'insert': {
95 - 'commit': insert,
96 - 'rollback': measure,
97 - 'advance': zero
98 - },
99 - 'remove': {
100 - 'commit': measure,
101 - 'rollback': insert,
102 - 'advance': measure
103 - },
104 - 'start': {
105 - 'commit': start,
106 - 'rollback': function( val, cur, src, dst, add, rem ) {
107 - return start( val, cur, src, dst, rem, add );
108 - },
109 - 'advance': zero
110 - },
111 - 'end': {
112 - 'commit': end,
113 - 'rollback': function( val, cur, src, dst, add, rem ) {
114 - return end( val, cur, src, dst, rem, add );
115 - },
116 - 'advance': zero
117 - }
118 - };
119 -} )();
120 -
121 -es.Content.Transaction.prototype.getCursor = function() {
122 - return this.cursor;
123 -};
124 -
125 -es.Content.Transaction.prototype.reset = function() {
126 - this.operations = [];
127 - this.cursor = 0;
128 -};
129 -
130 -es.Content.Transaction.prototype.add = function( type, val ) {
131 - if ( !( type in es.Content.Transaction.operations ) ) {
132 - throw 'Unknown operation error. Operation type is not supported: ' + type;
133 - }
134 - var model = es.Content.Transaction.operations[type];
135 - this.operations.push( {
136 - 'type': type,
137 - 'val': val,
138 - 'model': model
139 - } );
140 - this.cursor += model.advance( val );
141 -};
142 -
143 -es.Content.Transaction.prototype.commit = function( src ) {
144 - var cur = 0,
145 - dst = new es.Content(),
146 - add = [],
147 - rem = [],
148 - adv;
149 - for ( var i = 0; i < this.operations.length; i++ ) {
150 - var op = this.operations[i];
151 - adv = op.model.commit( op.val, cur, src, dst, add, rem );
152 - cur += adv;
153 - }
154 - return dst;
155 -};
156 -
157 -es.Content.Transaction.prototype.rollback = function( src ) {
158 - var cur = 0,
159 - dst = new es.Content(),
160 - add = [],
161 - rem = [],
162 - adv;
163 - for ( var i = 0; i < this.operations.length; i++ ) {
164 - var op = this.operations[i];
165 - adv = op.model.rollback( op.val, cur, src, dst, add, rem );
166 - cur += adv;
167 - }
168 - return dst;
169 -};
Index: trunk/parsers/wikidom/lib/es/es.Transaction.js
@@ -0,0 +1,168 @@
 2+/**
 3+ * Creates an operation to be applied to a content object.
 4+ *
 5+ * @class
 6+ * @constructor
 7+ * @param content {es.Content} Content to operate on
 8+ * @property operations {Array} List of operations
 9+ */
 10+es.Transaction = function() {
 11+ this.operations = [];
 12+ this.cursor = 0;
 13+};
 14+
 15+/**
 16+ * List of operation implementations.
 17+ */
 18+es.Transaction.operations = ( function() {
 19+ function annotate( con, add, rem ) {
 20+ // Ensure that modifications to annotated characters do not affect other uses of the same
 21+ // content by isolating it - performing a deep-slice
 22+ con.isolate();
 23+ for ( var i = 0; i < add.length; i++ ) {
 24+ con.annotate( 'add', add[i] );
 25+ }
 26+ for ( var i = 0; i < rem.length; i++ ) {
 27+ con.annotate( 'remove', rem[i] );
 28+ }
 29+ }
 30+ function retain( val, cur, src, dst, add, rem ) {
 31+ var con = src.getContent( new es.Range( cur, cur + val ) );
 32+ if ( add.length || rem.length ) {
 33+ annotate( con, add, rem );
 34+ }
 35+ dst.insert( dst.getLength(), con.getData() );
 36+ return val;
 37+ }
 38+ function insert( val, cur, src, dst, add, rem ) {
 39+ var con = val.getContent();
 40+ if ( add.length || rem.length ) {
 41+ annotate( con, add, rem );
 42+ }
 43+ dst.insert( dst.getLength(), con.getData() );
 44+ return 0;
 45+ }
 46+ function start( val, cur, src, dst, add, rem ) {
 47+ if ( val.method === 'add' ) {
 48+ add.push( val.annotation );
 49+ } else if ( val.method === 'remove' ) {
 50+ rem.push( val.annotation );
 51+ } else {
 52+ throw 'Annotation method error. Unsupported annotation method: ' + val.method;
 53+ }
 54+ return 0;
 55+ }
 56+ function end( val, cur, src, dst, add, rem ) {
 57+ var stack;
 58+ if ( val.method === 'add' ) {
 59+ stack = add;
 60+ } else if ( val.method === 'remove' ) {
 61+ stack = rem;
 62+ } else {
 63+ throw 'Annotation method error. Unsupported annotation method: ' + val.method;
 64+ }
 65+ var index;
 66+ for ( var i = 0; i < stack.length; i++ ) {
 67+ // TODO: Compare data too: es.Content.compareObjects( stack[i], val.annotation )
 68+ if ( stack[i].type === val.annotation.type ) {
 69+ index = i;
 70+ break;
 71+ }
 72+ }
 73+ if ( index === undefined ) {
 74+ throw 'Annotation stack error. Annotation is missing.';
 75+ }
 76+ stack.splice( index, 1 );
 77+ return 0;
 78+ }
 79+ function measure( val ) {
 80+ return val.getLength();
 81+ }
 82+ function pass( val ) {
 83+ return val;
 84+ }
 85+ function zero( val ) {
 86+ return 0;
 87+ }
 88+ return {
 89+ 'retain': {
 90+ 'commit': retain,
 91+ 'rollback': retain,
 92+ 'advance': pass
 93+ },
 94+ 'insert': {
 95+ 'commit': insert,
 96+ 'rollback': measure,
 97+ 'advance': zero
 98+ },
 99+ 'remove': {
 100+ 'commit': measure,
 101+ 'rollback': insert,
 102+ 'advance': measure
 103+ },
 104+ 'start': {
 105+ 'commit': start,
 106+ 'rollback': function( val, cur, src, dst, add, rem ) {
 107+ return start( val, cur, src, dst, rem, add );
 108+ },
 109+ 'advance': zero
 110+ },
 111+ 'end': {
 112+ 'commit': end,
 113+ 'rollback': function( val, cur, src, dst, add, rem ) {
 114+ return end( val, cur, src, dst, rem, add );
 115+ },
 116+ 'advance': zero
 117+ }
 118+ };
 119+} )();
 120+
 121+es.Transaction.prototype.getCursor = function() {
 122+ return this.cursor;
 123+};
 124+
 125+es.Transaction.prototype.reset = function() {
 126+ this.operations = [];
 127+ this.cursor = 0;
 128+};
 129+
 130+es.Transaction.prototype.add = function( type, val ) {
 131+ if ( !( type in es.Transaction.operations ) ) {
 132+ throw 'Unknown operation error. Operation type is not supported: ' + type;
 133+ }
 134+ var model = es.Transaction.operations[type];
 135+ this.operations.push( {
 136+ 'type': type,
 137+ 'val': val,
 138+ 'model': model
 139+ } );
 140+ this.cursor += model.advance( val );
 141+};
 142+
 143+es.Transaction.prototype.commit = function( src ) {
 144+ var cur = 0,
 145+ dst = new es.Content(),
 146+ add = [],
 147+ rem = [],
 148+ adv;
 149+ for ( var i = 0; i < this.operations.length; i++ ) {
 150+ var op = this.operations[i];
 151+ adv = op.model.commit( op.val, cur, src, dst, add, rem );
 152+ cur += adv;
 153+ }
 154+ return dst;
 155+};
 156+
 157+es.Transaction.prototype.rollback = function( src ) {
 158+ var cur = 0,
 159+ dst = new es.Content(),
 160+ add = [],
 161+ rem = [],
 162+ adv;
 163+ for ( var i = 0; i < this.operations.length; i++ ) {
 164+ var op = this.operations[i];
 165+ adv = op.model.rollback( op.val, cur, src, dst, add, rem );
 166+ cur += adv;
 167+ }
 168+ return dst;
 169+};
Property changes on: trunk/parsers/wikidom/lib/es/es.Transaction.js
___________________________________________________________________
Added: svn:eol-style
1170 + native
Added: svn:mime-type
2171 + text/plain
Index: trunk/parsers/wikidom/tests/transactions/index.html
@@ -2,18 +2,18 @@
33 "http://www.w3.org/TR/html4/loose.dtd">
44 <html>
55 <head>
6 - <title>Content Transaction Tests</title>
 6+ <title>Transaction Tests</title>
77 <link rel="stylesheet" href="../../lib/qunit.css" type="text/css" />
88 </head>
99 <body>
10 - <h1 id="qunit-header">Content Transaction Tests</h1>
 10+ <h1 id="qunit-header">Transaction Tests</h1>
1111 <h2 id="qunit-banner"></h2>
1212 <h2 id="qunit-userAgent"></h2>
1313 <ol id="qunit-tests"></ol>
1414 <script src="../../lib/es/es.js"></script>
1515 <script src="../../lib/es/es.EventEmitter.js"></script>
1616 <script src="../../lib/es/es.Content.js"></script>
17 - <script src="../../lib/es/es.Content.Transaction.js"></script>
 17+ <script src="../../lib/es/es.Transaction.js"></script>
1818 <script src="../../lib/es/es.Range.js"></script>
1919 <script src="../../lib/jquery.js"></script>
2020 <script src="../../lib/qunit.js"></script>
Index: trunk/parsers/wikidom/tests/transactions/test.js
@@ -77,7 +77,7 @@
7878 "!"
7979 ] );
8080
81 - var tx = new es.Content.Transaction(),
 81+ var tx = new es.Transaction(),
8282 insertion = es.Content.newFromText( 'used to be' ),
8383 removal = content.getContent( new es.Range( 5, 7 ) );
8484
@@ -124,7 +124,7 @@
125125 "!"
126126 ] );
127127
128 - var tx = new es.Content.Transaction(),
 128+ var tx = new es.Transaction(),
129129 annotation = { 'method': 'add', 'annotation': { 'type': 'italic' } };
130130
131131 tx.add( 'retain', 4 );

Status & tagging log