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 |
1 | 170 | + native |
Added: svn:mime-type |
2 | 171 | + text/plain |
Index: trunk/parsers/wikidom/tests/transactions/index.html |
— | — | @@ -2,18 +2,18 @@ |
3 | 3 | "http://www.w3.org/TR/html4/loose.dtd"> |
4 | 4 | <html> |
5 | 5 | <head> |
6 | | - <title>Content Transaction Tests</title> |
| 6 | + <title>Transaction Tests</title> |
7 | 7 | <link rel="stylesheet" href="../../lib/qunit.css" type="text/css" /> |
8 | 8 | </head> |
9 | 9 | <body> |
10 | | - <h1 id="qunit-header">Content Transaction Tests</h1> |
| 10 | + <h1 id="qunit-header">Transaction Tests</h1> |
11 | 11 | <h2 id="qunit-banner"></h2> |
12 | 12 | <h2 id="qunit-userAgent"></h2> |
13 | 13 | <ol id="qunit-tests"></ol> |
14 | 14 | <script src="../../lib/es/es.js"></script> |
15 | 15 | <script src="../../lib/es/es.EventEmitter.js"></script> |
16 | 16 | <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> |
18 | 18 | <script src="../../lib/es/es.Range.js"></script> |
19 | 19 | <script src="../../lib/jquery.js"></script> |
20 | 20 | <script src="../../lib/qunit.js"></script> |
Index: trunk/parsers/wikidom/tests/transactions/test.js |
— | — | @@ -77,7 +77,7 @@ |
78 | 78 | "!" |
79 | 79 | ] ); |
80 | 80 | |
81 | | - var tx = new es.Content.Transaction(), |
| 81 | + var tx = new es.Transaction(), |
82 | 82 | insertion = es.Content.newFromText( 'used to be' ), |
83 | 83 | removal = content.getContent( new es.Range( 5, 7 ) ); |
84 | 84 | |
— | — | @@ -124,7 +124,7 @@ |
125 | 125 | "!" |
126 | 126 | ] ); |
127 | 127 | |
128 | | - var tx = new es.Content.Transaction(), |
| 128 | + var tx = new es.Transaction(), |
129 | 129 | annotation = { 'method': 'add', 'annotation': { 'type': 'italic' } }; |
130 | 130 | |
131 | 131 | tx.add( 'retain', 4 ); |