r105209 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r105208‎ | r105209 | r105210 >
Date:19:41, 5 December 2011
Author:tparscal
Status:deferred
Tags:
Comment:
* Moved getContent and getText from leaf nodes to document model nodes
* Renamed getContent to getContentData
* Renamed getText to getContentText
* Added getElementData
Modified paths:
  • /trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelLeafNode.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelNode.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/es/models/es.DocumentModel.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/es/views/es.ContentView.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js (modified) (history)
  • /trunk/extensions/VisualEditor/tests/es/es.DocumentModel.test.js (modified) (history)
  • /trunk/extensions/VisualEditor/tests/es/es.TransactionProcessor.test.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/tests/es/es.TransactionProcessor.test.js
@@ -94,7 +94,7 @@
9595
9696 // Test 6
9797 deepEqual(
98 - documentModel.getChildren()[0].getContent(),
 98+ documentModel.getChildren()[0].getContentData(),
9999 [
100100 'a',
101101 ['b', { 'type': 'textStyle/bold', 'hash': '{"type":"textStyle/bold"}' }],
@@ -120,7 +120,7 @@
121121
122122 // Test 8
123123 deepEqual(
124 - documentModel.getChildren()[0].getContent(),
 124+ documentModel.getChildren()[0].getContentData(),
125125 [
126126 'a',
127127 ['b', { 'type': 'textStyle/bold', 'hash': '{"type":"textStyle/bold"}' }],
@@ -145,7 +145,7 @@
146146
147147 // Test 10
148148 deepEqual(
149 - documentModel.getChildren()[0].getContent(),
 149+ documentModel.getChildren()[0].getContentData(),
150150 ['a'],
151151 'commit keeps model tree up to date with removals'
152152 );
@@ -166,7 +166,7 @@
167167
168168 // Test 12
169169 deepEqual(
170 - documentModel.getChildren()[0].getContent(),
 170+ documentModel.getChildren()[0].getContentData(),
171171 [
172172 'a',
173173 ['b', { 'type': 'textStyle/bold', 'hash': '{"type":"textStyle/bold"}' }],
@@ -197,14 +197,14 @@
198198
199199 // Test 14
200200 deepEqual(
201 - documentModel.getChildren()[0].getContent(),
 201+ documentModel.getChildren()[0].getContentData(),
202202 ['a'],
203203 'commit keeps model tree up to date with paragraph split (paragraph 1)'
204204 );
205205
206206 // Test 15
207207 deepEqual(
208 - documentModel.getChildren()[1].getContent(),
 208+ documentModel.getChildren()[1].getContentData(),
209209 [
210210 ['b', { 'type': 'textStyle/bold', 'hash': '{"type":"textStyle/bold"}' }],
211211 ['c', { 'type': 'textStyle/italic', 'hash': '{"type":"textStyle/italic"}' }]
@@ -228,7 +228,7 @@
229229
230230 // Test 17
231231 deepEqual(
232 - documentModel.getChildren()[0].getContent(),
 232+ documentModel.getChildren()[0].getContentData(),
233233 [
234234 'a',
235235 ['b', { 'type': 'textStyle/bold', 'hash': '{"type":"textStyle/bold"}' }],
@@ -272,21 +272,21 @@
273273
274274 // Test 21
275275 deepEqual(
276 - documentModel.children[1].children[0].children[0].children[1].children[0].children[0].getContent(),
 276+ documentModel.children[1].children[0].children[0].children[1].children[0].children[0].getContentData(),
277277 [ 'f' ],
278278 'removal keeps model tree up to date with list item merge (first list item)'
279279 );
280280
281281 // Test 22
282282 deepEqual(
283 - documentModel.children[1].children[0].children[0].children[1].children[1].children[0].getContent(),
 283+ documentModel.children[1].children[0].children[0].children[1].children[1].children[0].getContentData(),
284284 [ 'g' ],
285285 'removal keeps model tree up to date with list item merge (second list item)'
286286 );
287287
288288 // Test 23
289289 deepEqual(
290 - documentModel.children[2].getContent(),
 290+ documentModel.children[2].getContentData(),
291291 [ 'h' ],
292292 'rollback keeps model tree up to date with list item split (final paragraph)'
293293 );
@@ -322,28 +322,28 @@
323323
324324 // Test 26
325325 deepEqual(
326 - documentModel.children[1].children[0].children[0].children[1].children[0].children[0].getContent(),
 326+ documentModel.children[1].children[0].children[0].children[1].children[0].children[0].getContentData(),
327327 [ 'e' ],
328328 'rollback keeps model tree up to date with list item split (first list item)'
329329 );
330330
331331 // Test 27
332332 deepEqual(
333 - documentModel.children[1].children[0].children[0].children[1].children[1].children[0].getContent(),
 333+ documentModel.children[1].children[0].children[0].children[1].children[1].children[0].getContentData(),
334334 [ 'f' ],
335335 'rollback keeps model tree up to date with list item split (second list item)'
336336 );
337337
338338 // Test 28
339339 deepEqual(
340 - documentModel.children[1].children[0].children[0].children[1].children[2].children[0].getContent(),
 340+ documentModel.children[1].children[0].children[0].children[1].children[2].children[0].getContentData(),
341341 [ 'g' ],
342342 'rollback keeps model tree up to date with list item split (third list item)'
343343 );
344344
345345 // Test 29
346346 deepEqual(
347 - documentModel.children[2].getContent(),
 347+ documentModel.children[2].getContentData(),
348348 [ 'h' ],
349349 'rollback keeps model tree up to date with list item split (final paragraph)'
350350 );
Index: trunk/extensions/VisualEditor/tests/es/es.DocumentModel.test.js
@@ -85,50 +85,50 @@
8686 );
8787 } );
8888
89 -test( 'es.DocumentModel.getContent', 6, function() {
 89+test( 'es.DocumentModel.getContentData', 6, function() {
9090 var documentModel = es.DocumentModel.newFromPlainObject( esTest.obj ),
9191 childNodes = documentModel.getChildren();
9292
9393 // Test 1
9494 deepEqual(
95 - childNodes[0].getContent( new es.Range( 1, 3 ) ),
 95+ childNodes[0].getContentData( new es.Range( 1, 3 ) ),
9696 [
9797 ['b', { 'type': 'textStyle/bold', 'hash': '{"type":"textStyle/bold"}' }],
9898 ['c', { 'type': 'textStyle/italic', 'hash': '{"type":"textStyle/italic"}' }]
9999 ],
100 - 'getContent can return an ending portion of the content'
 100+ 'getContentData can return an ending portion of the content'
101101 );
102102
103103 // Test 2
104104 deepEqual(
105 - childNodes[0].getContent( new es.Range( 0, 2 ) ),
 105+ childNodes[0].getContentData( new es.Range( 0, 2 ) ),
106106 ['a', ['b', { 'type': 'textStyle/bold', 'hash': '{"type":"textStyle/bold"}' }]],
107 - 'getContent can return a beginning portion of the content'
 107+ 'getContentData can return a beginning portion of the content'
108108 );
109109
110110 // Test 3
111111 deepEqual(
112 - childNodes[0].getContent( new es.Range( 1, 2 ) ),
 112+ childNodes[0].getContentData( new es.Range( 1, 2 ) ),
113113 [['b', { 'type': 'textStyle/bold', 'hash': '{"type":"textStyle/bold"}' }]],
114 - 'getContent can return a middle portion of the content'
 114+ 'getContentData can return a middle portion of the content'
115115 );
116116
117117 // Test 4
118118 try {
119 - childNodes[0].getContent( new es.Range( -1, 3 ) );
 119+ childNodes[0].getContentData( new es.Range( -1, 3 ) );
120120 } catch ( negativeIndexError ) {
121 - ok( true, 'getContent throws exceptions when given a range with start < 0' );
 121+ ok( true, 'getContentData throws exceptions when given a range with start < 0' );
122122 }
123123
124124 // Test 5
125125 try {
126 - childNodes[0].getContent( new es.Range( 0, 4 ) );
 126+ childNodes[0].getContentData( new es.Range( 0, 4 ) );
127127 } catch ( outOfRangeError ) {
128 - ok( true, 'getContent throws exceptions when given a range with end > length' );
 128+ ok( true, 'getContentData throws exceptions when given a range with end > length' );
129129 }
130130
131131 // Test 6
132 - deepEqual( childNodes[2].getContent(), ['h'], 'Content can be extracted from nodes' );
 132+ deepEqual( childNodes[2].getContentData(), ['h'], 'Content can be extracted from nodes' );
133133 } );
134134
135135 test( 'es.DocumentModel.getIndexOfAnnotation', 3, function() {
Index: trunk/extensions/VisualEditor/modules/es/models/es.DocumentModel.js
@@ -613,13 +613,28 @@
614614 };
615615
616616 /**
 617+ * Gets the element data of a node.
 618+ *
 619+ * @method
 620+ * @param {es.DocumentModelNode} node Node to get element data for
 621+ */
 622+es.DocumentModel.prototype.getContentDataFromNode = function( node ) {
 623+ var length = node.getElementLength();
 624+ var offset = this.getOffsetFromNode( node );
 625+ if ( offset !== -1 ) {
 626+ return this.data.slice( offset, offset + length );
 627+ }
 628+ return null;
 629+};
 630+
 631+/**
617632 * Gets the content data of a node.
618633 *
619634 * @method
620635 * @param {es.DocumentModelNode} node Node to get content data for
621636 * @returns {Array|null} List of content and elements inside node or null if node is not found
622637 */
623 -es.DocumentModel.prototype.getContentFromNode = function( node, range ) {
 638+es.DocumentModel.prototype.getContentDataFromNode = function( node, range ) {
624639 var length = node.getContentLength();
625640 if ( range ) {
626641 range.normalize();
@@ -1201,7 +1216,7 @@
12021217 txs.push( this.prepareInsertion(
12031218 nodeOffset,
12041219 [ { 'type': type, 'attributes': attributes } ]
1205 - .concat( nodes[i].getContent() )
 1220+ .concat( nodes[i].getContentData() )
12061221 .concat( [ { 'type': '/' + type } ] )
12071222 ) );
12081223 }
Index: trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelNode.js
@@ -218,6 +218,71 @@
219219 return null;
220220 };
221221
 222+/**
 223+ * Gets all element data, including the element opening, closing and it's contents.
 224+ *
 225+ * @method
 226+ * @returns {Array} Element data
 227+ */
 228+es.DocumentModelNode.prototype.getElementData = function() {
 229+ // Get reference to the document, which might be this node but otherwise should be this.root
 230+ var root = this.type === 'document' ?
 231+ this : ( this.root && this.root.type === 'document' ? this.root : null );
 232+ if ( root ) {
 233+ return root.getElementDataFromNode( this );
 234+ }
 235+ return [];
 236+};
 237+
 238+/**
 239+ * Gets content data within a given range.
 240+ *
 241+ * @method
 242+ * @param {es.Range} [range] Range of content to get
 243+ * @returns {Array} Content data
 244+ */
 245+es.DocumentModelNode.prototype.getContentData = function( range ) {
 246+ // Get reference to the document, which might be this node but otherwise should be this.root
 247+ var root = this.type === 'document' ?
 248+ this : ( this.root && this.root.type === 'document' ? this.root : null );
 249+ if ( root ) {
 250+ return root.getContentDataFromNode( this, range );
 251+ }
 252+ return [];
 253+};
 254+
 255+/**
 256+ * Gets plain text version of the content within a specific range.
 257+ *
 258+ * Two newlines are inserted between leaf nodes.
 259+ *
 260+ * TODO: Maybe do something more adaptive with newlines
 261+ *
 262+ * @method
 263+ * @param {es.Range} [range] Range of text to get
 264+ * @returns {String} Text within given range
 265+ */
 266+es.DocumentModelNode.prototype.getContentText = function( range ) {
 267+ var content = this.getContentData( range );
 268+ // Copy characters
 269+ var text = '',
 270+ element = false;
 271+ for ( var i = 0, length = content.length; i < length; i++ ) {
 272+ if ( typeof content[i] === 'object' ) {
 273+ if ( i ) {
 274+ element = true;
 275+ }
 276+ } else {
 277+ if ( element ) {
 278+ text += '\n\n';
 279+ element = false;
 280+ }
 281+ text += typeof content[i] === 'string' ? content[i] : content[i][0];
 282+ }
 283+ }
 284+ return text;
 285+};
 286+
222287 /* Inheritance */
223288
224289 es.extendClass( es.DocumentModelNode, es.DocumentNode );
Index: trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelLeafNode.js
@@ -34,50 +34,10 @@
3535 if ( this.element && this.element.attributes ) {
3636 obj.attributes = es.copyObject( this.element.attributes );
3737 }
38 - obj.content = es.DocumentModel.getExpandedContentData( this.getContent() );
 38+ obj.content = es.DocumentModel.getExpandedContentData( this.getContentData() );
3939 return obj;
4040 };
4141
42 -/**
43 - * Gets the content length.
44 - *
45 - * FIXME: This method makes assumptions that a node with a data property is a DocumentModel, which
46 - * may be an issue if sub-classes of DocumentModelLeafNode other than DocumentModel have a data property
47 - * as well. A safer way of determining this would be helpful in preventing future bugs.
48 - *
49 - * @method
50 - * @param {es.Range} [range] Range of content to get
51 - * @returns {Integer} Length of content
52 - */
53 -es.DocumentModelLeafNode.prototype.getContent = function( range ) {
54 - // Find root
55 - var root = this.data ? this : ( this.root && this.root.data ? this.root : null );
56 - if ( root ) {
57 - return root.getContentFromNode( this, range );
58 - }
59 - return [];
60 -};
61 -
62 -/**
63 - * Gets plain text version of the content within a specific range.
64 - *
65 - * @method
66 - * @param {es.Range} [range] Range of text to get
67 - * @returns {String} Text within given range
68 - */
69 -es.DocumentModelLeafNode.prototype.getText = function( range ) {
70 - var content = this.getContent( range );
71 - // Copy characters
72 - var text = '';
73 - for ( var i = 0, length = content.length; i < length; i++ ) {
74 - // If not using in IE6 or IE7 (which do not support array access for strings) use this..
75 - // text += this.data[i][0];
76 - // Otherwise use this...
77 - text += typeof content[i] === 'string' ? content[i] : content[i][0];
78 - }
79 - return text;
80 -};
81 -
8242 /* Inheritance */
8343
8444 es.extendClass( es.DocumentModelLeafNode, es.DocumentLeafNode );
Index: trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js
@@ -589,7 +589,7 @@
590590 this.model.transact( tx, true );
591591 } else {
592592 tx = this.model.getDocument().prepareInsertion(
593 - targetOffset, sourceNode.model.getContent()
 593+ targetOffset, sourceNode.model.getContentData()
594594 );
595595 this.model.transact( tx, true );
596596
Index: trunk/extensions/VisualEditor/modules/es/views/es.ContentView.js
@@ -500,7 +500,7 @@
501501 * the words.
502502 */
503503 // Get and cache a copy of all content, the make a plain-text version of the cached content
504 - var data = this.contentCache = this.model.getContent(),
 504+ var data = this.contentCache = this.model.getContentData(),
505505 text = '';
506506 for ( var i = 0, length = data.length; i < length; i++ ) {
507507 text += typeof data[i] === 'string' ? data[i] : data[i][0];
@@ -712,7 +712,7 @@
713713 $line[0].innerHTML = this.getHtml( range );
714714 // Overwrite/append line information
715715 this.lines[rs.line] = {
716 - 'text': this.model.getText( range ),
 716+ 'text': this.model.getContentText( range ),
717717 'range': range,
718718 'width': $line.outerWidth(),
719719 'height': $line.outerHeight(),

Status & tagging log