r98251 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r98250‎ | r98251 | r98252 >
Date:20:37, 27 September 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Added some more "concept" code to hype
Modified paths:
  • /trunk/parsers/wikidom/lib/hype/bases/es.DocumentModelNode.js (added) (history)
  • /trunk/parsers/wikidom/lib/hype/es.DocumentModel.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/hype/es.DocumentModel.js
@@ -1,11 +1,73 @@
22 /**
 3+ * Creates an es.DocumentModel object.
 4+ *
 5+ * es.DocumentModel objects extend the native Array object, so it's contents are directly accessible
 6+ * through the typical methods.
 7+ *
38 * @class
49 * @constructor
 10+ * @param {Array} data Model data to initialize with, such as data from es.DocumentModel.getData()
511 */
6 -es.DocumentModel = function() {
7 - this.data = [];
 12+es.DocumentModel = function( data ) {
 13+ var data = $.isArray( data ) ? data : [];
 14+ return $.extend( data, this );
815 };
916
 17+/* Static Methods */
 18+
 19+es.DocumentModel.isContent = function( content ) {
 20+ return typeof content === 'string' || $.isArray( content );
 21+};
 22+
 23+es.DocumentModel.isElement = function( content ) {
 24+ return content.type !== undefined;
 25+};
 26+
 27+/* Methods */
 28+
 29+es.DocumentModel.prototype.findElement = function( node, root, callback ) {
 30+ for ( var i = 0; i < this.data.length; i++ ) {
 31+ if ( es.DocumentModel.isElement( this.data[i] ) ) {
 32+ if ( content.node === node ) {
 33+ return callback( i );
 34+ }
 35+ // If we are looking for a root node, we can skip over the contents of this one
 36+ if ( root ) {
 37+ i += node.getContentLength() + 2;
 38+ }
 39+ }
 40+ }
 41+ return null;
 42+};
 43+
 44+/**
 45+ * Gets the element object of a node.
 46+ *
 47+ * @method
 48+ * @param {es.DocumentModelNode} node Reference to node object to get element object for
 49+ * @param {Boolean} root Whether to only scan root nodes
 50+ * @returns {Object|null} Element object
 51+ */
 52+es.DocumentModel.prototype.getElement = function( node, root ) {
 53+ return this.findNode( node, root, function( index ) {
 54+ return this.data[index];
 55+ } );
 56+};
 57+
 58+/**
 59+ * Gets the content data of a node.
 60+ *
 61+ * @method
 62+ * @param {es.DocumentModelNode} node Reference to node object to get content data for
 63+ * @param {Boolean} root Whether to only scan root nodes
 64+ * @returns {Array|null} List of content and elements inside node or null if node is not found
 65+ */
 66+es.DocumentModel.prototype.getContent = function( node, root ) {
 67+ return this.findNode( node, root, function( index ) {
 68+ return this.data.slice( index + 1, index + node.getContentLength() );
 69+ } );
 70+};
 71+
1072 /*
1173 // High-level operations
1274
@@ -39,7 +101,7 @@
40102 */
41103
42104 // Low-level operations
43 -
 105+/*
44106 es.DocumentModel.newFromPlainObject = function( obj ) {
45107
46108 };
@@ -85,6 +147,7 @@
86148 }
87149 }
88150 };
 151+*/
89152
90153 /*
91154 * Example of content data
@@ -96,7 +159,7 @@
97160 */
98161 var data = [
99162 // 0 - Beginning of paragraph
100 - { 'type': 'paragraph' },
 163+ { 'type': 'paragraph', 'node': {} },
101164 // 1 - Plain content
102165 'a',
103166 // 2 - Annotated content
@@ -104,53 +167,53 @@
105168 // 3 - Annotated content
106169 ['c', { 'type': 'italic' }],
107170 // 4 - End of paragraph
108 - { 'type': '/paragraph' }
 171+ { 'type': '/paragraph', 'node': {} }
109172 // 5 - Beginning of table
110 - { 'type': 'table' },
 173+ { 'type': 'table', 'node': {} },
111174 // 6 - Beginning of row
112 - { 'type': 'row' },
 175+ { 'type': 'row', 'node': {} },
113176 // 7 - Beginning of cell
114 - { 'type': 'cell' },
 177+ { 'type': 'cell', 'node': {} },
115178 // 8 - Beginning of paragraph
116 - { 'type': 'paragraph' },
 179+ { 'type': 'paragraph', 'node': {} },
117180 // 9 - Plain content
118181 'a',
119182 // 10 - End of paragraph
120 - { 'type': '/paragraph' },
 183+ { 'type': '/paragraph', 'node': {} },
121184 // 11 - Beginning of list
122 - { 'type': 'list' },
 185+ { 'type': 'list', 'node': {} },
123186 // 12 - Beginning of bullet list item
124 - { 'type': 'item', 'styles': ['bullet'] },
 187+ { 'type': 'item', 'styles': ['bullet'], 'node': {} },
125188 // 13 - Plain content
126189 'a',
127190 // 14 - End of item
128 - { 'type': '/item' },
 191+ { 'type': '/item', 'node': {} },
129192 // 15 - Beginning of nested bullet list item
130 - { 'type': 'item', 'styles': ['bullet', 'bullet'] },
 193+ { 'type': 'item', 'styles': ['bullet', 'bullet'], 'node': {} },
131194 // 16 - Plain content
132195 'b',
133196 // 17 - End of item
134 - { 'type': '/item' },
 197+ { 'type': '/item', 'node': {} },
135198 // 18 - Beginning of numbered list item
136 - { 'type': 'item', 'styles': ['number'] },
 199+ { 'type': 'item', 'styles': ['number'], 'node': {} },
137200 // 19 - Plain content
138201 'c',
139202 // 20 - End of item
140 - { 'type': '/item' },
 203+ { 'type': '/item', 'node': {} },
141204 // 21 - End of list
142 - { 'type': '/list' },
 205+ { 'type': '/list', 'node': {} },
143206 // 22 - End of cell
144 - { 'type': '/cell' }
 207+ { 'type': '/cell', 'node': {} }
145208 // 23 - End of row
146 - { 'type': '/row' }
 209+ { 'type': '/row', 'node': {} }
147210 // 24 - End of table
148 - { 'type': '/table' }
 211+ { 'type': '/table', 'node': {} }
149212 // 25 - Beginning of paragraph
150 - { 'type': 'paragraph' },
 213+ { 'type': 'paragraph', 'node': {} },
151214 // 26 - Plain content
152215 'a'
153216 // 27 - End of paragraph
154 - { 'type': '/paragraph' },
 217+ { 'type': '/paragraph', 'node': {} },
155218 ];
156219
157220 /*
Index: trunk/parsers/wikidom/lib/hype/bases/es.DocumentModelNode.js
@@ -0,0 +1,45 @@
 2+/**
 3+ * Creates an es.DocumentModelNode object.
 4+ *
 5+ * @class
 6+ * @constructor
 7+ * @param {es.DocumentModel} documentModel Document model this node is a part of
 8+ * @param {Integer} contentLength Length of contents
 9+ * @property {es.DocumentModel} documentModel Document model this node is a part of
 10+ * @property {Integer} contentLength Length of contents
 11+ */
 12+es.DocumentModelNode = function( documentModel, contentLength ) {
 13+ // Inheritance
 14+ es.ModelList.call( this );
 15+ es.ModelItem.call( this );
 16+ // Properties
 17+ this.documentModel = documentModel;
 18+ this.contentLength = contentLength || 0;
 19+ // Events
 20+ this.on( 'update', function() {
 21+ // Recalculate length when child nodes change
 22+ this.contentLength = 0;
 23+ for ( var i = 0; i < this.length, i++ ) {
 24+ this.contentLength += this[i].getElementLength();
 25+ }
 26+ } );
 27+};
 28+
 29+/* Methods */
 30+
 31+es.DocumentModelNode.getContentLength = function() {
 32+ return this.contentLength;
 33+};
 34+
 35+es.DocumentModelNode.getElementLength = function() {
 36+ return this.contentLength + 2;
 37+};
 38+
 39+es.DocumentModelNode.getContent = function( range ) {
 40+ return this.documentModel.getContent( this );
 41+};
 42+
 43+/* Inheritance */
 44+
 45+es.extend( es.DocumentModelNode, es.ModelList );
 46+es.extend( es.DocumentModelNode, es.ModelItem );

Status & tagging log