Index: trunk/parsers/wikidom/tests/hype/es.DocumentModel.test.js |
— | — | @@ -120,8 +120,6 @@ |
121 | 121 | */ |
122 | 122 | var data = [ |
123 | 123 | // 0 - Beginning of paragraph |
124 | | - { 'type': 'document' }, |
125 | | - // 0 - Beginning of paragraph |
126 | 124 | { 'type': 'paragraph' }, |
127 | 125 | // 1 - Plain content |
128 | 126 | 'a', |
— | — | @@ -176,9 +174,7 @@ |
177 | 175 | // 26 - Plain content |
178 | 176 | 'a', |
179 | 177 | // 27 - End of paragraph |
180 | | - { 'type': '/paragraph' }, |
181 | | - // 27 - End of paragraph |
182 | | - { 'type': '/document' } |
| 178 | + { 'type': '/paragraph' } |
183 | 179 | ]; |
184 | 180 | |
185 | 181 | test( 'es.ModelNode', function() { |
Index: trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js |
— | — | @@ -7,12 +7,15 @@ |
8 | 8 | * @class |
9 | 9 | * @constructor |
10 | 10 | * @param {Array} data Model data to initialize with, such as data from es.DocumentModel.getData() |
| 11 | + * @param {Object} attributes Document attributes |
11 | 12 | */ |
12 | | -es.DocumentModel = function( data ) { |
| 13 | +es.DocumentModel = function( data, attributes ) { |
13 | 14 | // Inheritance |
14 | 15 | es.DocumentModelNode.call( this, length ); |
15 | 16 | |
| 17 | + // Properties |
16 | 18 | this.data = $.isArray( data ) ? data : []; |
| 19 | + this.attributes = $.isPlainObject( attributes ) ? attributes : {}; |
17 | 20 | }; |
18 | 21 | |
19 | 22 | /* Static Methods */ |
— | — | @@ -51,10 +54,17 @@ |
52 | 55 | * @returns {es.DocumentModel} Document model created from obj |
53 | 56 | */ |
54 | 57 | es.DocumentModel.newFromPlainObject = function( obj ) { |
55 | | - return new es.DocumentModel( es.DocumentModel.flattenPlainObjectElementNode( obj ) ); |
| 58 | + if ( obj.type === 'document' ) { |
| 59 | + var data = [], |
| 60 | + attributes = $.isPlainObject( obj.attributes ) ? es.copyObject( obj.attributes ) : {}; |
| 61 | + for ( var i = 0; i < obj.children.length; i++ ) { |
| 62 | + data = data.concat( es.DocumentModel.flattenPlainObjectElementNode( obj.children[i] ) ); |
| 63 | + } |
| 64 | + return new es.DocumentModel( data, attributes ); |
| 65 | + } |
| 66 | + throw 'Invalid object error. Object is not a valid document object.'; |
56 | 67 | }; |
57 | 68 | |
58 | | - |
59 | 69 | /** |
60 | 70 | * Creates an es.ContentModel object from a plain content object. |
61 | 71 | * |