r93232 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r93231‎ | r93232 | r93233 >
Date:19:01, 26 July 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Cleanup
Modified paths:
  • /trunk/parsers/wikidom/lib/es/es.Block.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.ListBlock.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.ParagraphBlock.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/es/es.ParagraphBlock.js
@@ -1,7 +1,8 @@
22 /**
 3+ * es.ParagraphBlock
34 *
45 * @extends {es.Block}
5 - * @param lines {Array} List of line objects
 6+ * @param lines {Array} List of Wikidom line objects
67 * @returns {es.ParagraphBlock}
78 */
89 es.ParagraphBlock = function( lines ) {
@@ -14,12 +15,20 @@
1516 this.flow.on( 'render', function() {
1617 block.emit( 'update' );
1718 } );
18 -}
 19+};
1920
 21+/**
 22+ * Creates a new list block object from Wikidom data.
 23+ *
 24+ * @param wikidomList {Object} Wikidom data to convert from
 25+ */
2026 es.ParagraphBlock.newFromWikidom = function( wikidomBlock ) {
2127 return new es.ParagraphBlock( wikidomBlock.lines );
2228 };
2329
 30+/**
 31+ * Gets the length of all block content.
 32+ */
2433 es.ParagraphBlock.prototype.getLength = function() {
2534 return this.content.getLength();
2635 };
@@ -51,6 +60,20 @@
5261 };
5362
5463 /**
 64+ * Applies an annotation to a given range.
 65+ *
 66+ * If a range arguments are not provided, all content will be annotated.
 67+ *
 68+ * @param method {String} Way to apply annotation ("toggle", "add" or "remove")
 69+ * @param annotation {Object} Annotation to apply
 70+ * @param start {Integer} Offset to begin annotating from
 71+ * @param end {Integer} Offset to stop annotating to
 72+ */
 73+es.ParagraphBlock.prototype.annotateContent = function( method, annotation, start, end ) {
 74+ this.content.annotate( method, annotation, start, end );
 75+};
 76+
 77+/**
5578 * Gets content within a range.
5679 *
5780 * @param start {Integer} Offset to get content from
@@ -99,20 +122,6 @@
100123 };
101124
102125 /**
103 - * Applies an annotation to a given range.
104 - *
105 - * If a range arguments are not provided, all content will be annotated.
106 - *
107 - * @param method {String} Way to apply annotation ("toggle", "add" or "remove")
108 - * @param annotation {Object} Annotation to apply
109 - * @param start {Integer} Offset to begin annotating from
110 - * @param end {Integer} Offset to stop annotating to
111 - */
112 -es.ParagraphBlock.prototype.annotateContent = function( method, annotation, start, end ) {
113 - this.content.annotate( method, annotation, start, end );
114 -};
115 -
116 -/**
117126 * Gets the start and end points of the word closest a given offset.
118127 *
119128 * @param offset {Integer} Offset to find word nearest to
@@ -134,6 +143,9 @@
135144 return new es.Range( 0, this.content.getLength() );
136145 };
137146
138 -es.Block.models['paragraph'] = es.ParagraphBlock;
 147+/**
 148+ * Extend es.Block to support paragraph block creation with es.Block.newFromWikidom
 149+ */
 150+es.Block.models.paragraph = es.ParagraphBlock;
139151
140 -es.extend( es.ParagraphBlock, es.Block );
\ No newline at end of file
 152+es.extend( es.ParagraphBlock, es.Block );
Index: trunk/parsers/wikidom/lib/es/es.ListBlock.js
@@ -1,5 +1,10 @@
22 /**
3 - * es.ListBlock
 3+ * es.ListBlock
 4+ *
 5+ * @extends {es.Block}
 6+ * @param style {String} Type of list, either "number" or "bullet"
 7+ * @param items {Array} List of es.ListBlockItems to append initially to the root list
 8+ * @return {es.ListBlock}
49 */
510 es.ListBlock = function( style, items ) {
611 es.Block.call( this );
@@ -11,28 +16,119 @@
1217 this.list.on( 'update', function() {
1318 block.emit( 'update' );
1419 } );
15 -}
 20+};
1621
 22+/**
 23+ * Creates a new list block object from Wikidom data.
 24+ *
 25+ * @param wikidomList {Object} Wikidom data to convert from
 26+ */
1727 es.ListBlock.newFromWikidom = function( wikidomList ) {
1828 return new es.ListBlock( wikidomList.style, wikidomList.items );
1929 };
2030
21 -es.ListBlock.prototype.getText = function() {
22 - return '';
23 -};
24 -
 31+/**
 32+ * Gets the length of all block content.
 33+ */
2534 es.ListBlock.prototype.getLength = function() {
2635 // Compensate for n+1 virtual position on the last item's content
2736 return this.list.getLength() - 1;
2837 };
2938
3039 /**
 40+ * Inserts content into a block at an offset.
 41+ *
 42+ * @param offset {Integer} Position to insert content at
 43+ * @param content {Object} Content to insert
 44+ */
 45+es.ListBlock.prototype.insertContent = function( offset, content ) {
 46+ var location = this.list.getLocationFromOffset( offset );
 47+ location.item.flow.content.insert( location.offset, content );
 48+};
 49+
 50+/**
 51+ * Deletes content in a block within a range.
 52+ *
 53+ * @param offset {Integer} Offset to start removing content from
 54+ * @param length {Integer} Offset to start removing content to
 55+ */
 56+es.ListBlock.prototype.deleteContent = function( start, end ) {
 57+ // Normalize start/end
 58+ if ( end < start ) {
 59+ var tmp = end;
 60+ end = start;
 61+ start = tmp;
 62+ }
 63+ var location = this.list.getLocationFromOffset( start );
 64+ location.item.flow.content.remove( location.offset, location.offset + end - start );
 65+};
 66+
 67+/**
 68+ * Applies an annotation to a given range.
 69+ *
 70+ * If a range arguments are not provided, all content will be annotated.
 71+ *
 72+ * @param method {String} Way to apply annotation ("toggle", "add" or "remove")
 73+ * @param annotation {Object} Annotation to apply
 74+ * @param start {Integer} Offset to begin annotating from
 75+ * @param end {Integer} Offset to stop annotating to
 76+ */
 77+es.ListBlock.prototype.annotateContent = function( method, annotation, start, end ) {
 78+ throw 'ListBlock.annotateContent not implemented yet.';
 79+};
 80+
 81+/**
 82+ * Gets content within a range.
 83+ *
 84+ * @param start {Integer} Offset to get content from
 85+ * @param end {Integer} Offset to get content to
 86+ */
 87+es.ListBlock.prototype.getContent = function() {
 88+ // TODO: Implement me!
 89+ return new Content();
 90+};
 91+
 92+/**
 93+ * Gets content as plain text within a range.
 94+ *
 95+ * @param start {Integer} Offset to start get text from
 96+ * @param end {Integer} Offset to start get text to
 97+ * @param render {Boolean} If annotations should have any influence on output
 98+ */
 99+es.ListBlock.prototype.getText = function() {
 100+ // TODO: Implement me!
 101+ return '';
 102+};
 103+
 104+/**
31105 * Renders content into a container.
32106 */
33107 es.ListBlock.prototype.renderContent = function( offset ) {
34108 this.list.renderContent( offset );
35109 };
36110
 111+/**
 112+ * Gets the offset of a position.
 113+ *
 114+ * @param position {Integer} Offset to translate
 115+ */
 116+es.ListBlock.prototype.getOffset = function( position ) {
 117+ if ( position.top < 0 ) {
 118+ return 0;
 119+ } else if ( position.top >= this.$.height() ) {
 120+ return this.getLength();
 121+ }
 122+ var blockOffset = this.$.offset();
 123+ position.top += blockOffset.top;
 124+ position.left += blockOffset.left;
 125+ return this.list.getOffsetFromPosition( position );
 126+};
 127+
 128+/**
 129+ * Gets the position of an offset.
 130+ *
 131+ * @param offset {Integer} Offset to translate
 132+ */
37133 es.ListBlock.prototype.getPosition = function( offset ) {
38134 var location = this.list.getLocationFromOffset( offset )
39135 position = location.item.flow.getPosition( location.offset ),
@@ -52,9 +148,42 @@
53149 return position;
54150 };
55151
 152+/**
 153+ * Gets the start and end points of the word closest a given offset.
 154+ *
 155+ * @param offset {Integer} Offset to find word nearest to
 156+ * @return {Object} Range object of boundaries
 157+ */
 158+es.ListBlock.prototype.getWordBoundaries = function( offset ) {
 159+ var location = this.list.getLocationFromOffset( offset );
 160+ var boundaries = location.item.flow.content.getWordBoundaries( location.offset );
 161+ boundaries.start += offset - location.offset;
 162+ boundaries.end += offset - location.offset;
 163+ return boundaries;
 164+};
 165+
 166+/**
 167+ * Gets the start and end points of the section closest a given offset.
 168+ *
 169+ * @param offset {Integer} Offset to find section nearest to
 170+ * @return {Object} Range object of boundaries
 171+ */
 172+es.ListBlock.prototype.getSectionBoundaries = function( offset ) {
 173+ var location = this.list.getLocationFromOffset( offset ),
 174+ start = offset - location.offset;
 175+ return new es.Range( start, start + location.item.content.getLength() );
 176+};
 177+
 178+/**
 179+ * Iteratively execute a callback on each item in the list.
 180+ *
 181+ * Traversal is performed in a depth-first pattern, which is equivilant to a vertical scan of list
 182+ * items. To stop traversal, return false within the callback function.
 183+ *
 184+ * @param callback {Function} Function to execute for each item, accepts an item and index argument
 185+ * @return {Boolean} Whether all items were traversed, or traversal was cut short
 186+ */
56187 es.ListBlock.prototype.traverseItems = function( callback ) {
57 - // Recursively walk the tree of list items and their lists, depth first, until we get to the
58 - // same item as location.item, incrementing position.line for each line that occurs before it
59188 var stack = [{ 'list': this.list, 'index': 0 }],
60189 list,
61190 item,
@@ -89,48 +218,9 @@
90219 return true;
91220 };
92221
93 -es.ListBlock.prototype.getOffset = function( position ) {
94 - if ( position.top < 0 ) {
95 - return 0;
96 - } else if ( position.top >= this.$.height() ) {
97 - return this.getLength();
98 - }
99 - var blockOffset = this.$.offset();
100 - position.top += blockOffset.top;
101 - position.left += blockOffset.left;
102 - return this.list.getOffsetFromPosition( position );
103 -};
 222+/**
 223+ * Extend es.Block to support list block creation with es.Block.newFromWikidom
 224+ */
 225+es.Block.models.list = es.ListBlock;
104226
105 -es.ListBlock.prototype.insertContent = function( offset, content ) {
106 - var location = this.list.getLocationFromOffset( offset );
107 - location.item.flow.content.insert( location.offset, content );
108 -};
109 -
110 -es.ListBlock.prototype.deleteContent = function( start, end ) {
111 - // Normalize start/end
112 - if ( end < start ) {
113 - var tmp = end;
114 - end = start;
115 - start = tmp;
116 - }
117 - var location = this.list.getLocationFromOffset( start );
118 - location.item.flow.content.remove( location.offset, location.offset + end - start );
119 -};
120 -
121 -es.ListBlock.prototype.getWordBoundaries = function( offset ) {
122 - var location = this.list.getLocationFromOffset( offset );
123 - var boundaries = location.item.flow.content.getWordBoundaries( location.offset );
124 - boundaries.start += offset - location.offset;
125 - boundaries.end += offset - location.offset;
126 - return boundaries;
127 -};
128 -
129 -es.ListBlock.prototype.getSectionBoundaries = function( offset ) {
130 - var location = this.list.getLocationFromOffset( offset ),
131 - start = offset - location.offset;
132 - return new es.Range( start, start + location.item.content.getLength() );
133 -};
134 -
135 -es.Block.models['list'] = es.ListBlock;
136 -
137227 es.extend( es.ListBlock, es.Block );
Index: trunk/parsers/wikidom/lib/es/es.Block.js
@@ -1,4 +1,7 @@
22 /**
 3+ * Base object for all blocks, providing basic shared functionality and stubs for required
 4+ * implementations.
 5+ *
36 * @extends {es.EventEmitter}
47 * @returns {es.Block}
58 */
@@ -8,12 +11,17 @@
912 }
1013
1114 /**
12 - * Association between block type name and block class
13 - * Example: "paragraph" => es.ParagraphBlock
 15+ * Association between block type-name and block constructor
1416 *
 17+ * @example "paragraph" => es.ParagraphBlock
1518 */
1619 es.Block.models = {};
1720
 21+/**
 22+ * Creates a new block object from Wikidom data.
 23+ *
 24+ * @param wikidomBlock {Object} Wikidom data to convert from
 25+ */
1826 es.Block.newFromWikidom = function( wikidomBlock ) {
1927 if ( wikidomBlock.type in es.Block.models ) {
2028 return es.Block.models[wikidomBlock.type].newFromWikidom( wikidomBlock );
@@ -22,10 +30,6 @@
2331 }
2432 };
2533
26 -es.Block.prototype.getLength = function() {
27 - throw 'Block.getLength not implemented in this subclass.';
28 -};
29 -
3034 /**
3135 * Gets the index of the block within it's document.
3236 *
@@ -65,6 +69,13 @@
6670 };
6771
6872 /**
 73+ * Gets the length of all block content.
 74+ */
 75+es.Block.prototype.getLength = function() {
 76+ throw 'Block.getLength not implemented in this subclass.';
 77+};
 78+
 79+/**
6980 * Inserts content into a block at an offset.
7081 *
7182 * @param offset {Integer} Position to insert content at
@@ -85,6 +96,20 @@
8697 };
8798
8899 /**
 100+ * Applies an annotation to a given range.
 101+ *
 102+ * If a range arguments are not provided, all content will be annotated.
 103+ *
 104+ * @param method {String} Way to apply annotation ("toggle", "add" or "remove")
 105+ * @param annotation {Object} Annotation to apply
 106+ * @param start {Integer} Offset to begin annotating from
 107+ * @param end {Integer} Offset to stop annotating to
 108+ */
 109+es.Block.prototype.annotateContent = function( method, annotation, start, end ) {
 110+ throw 'Block.annotateContent not implemented in this subclass.';
 111+};
 112+
 113+/**
89114 * Gets content within a range.
90115 *
91116 * @param start {Integer} Offset to get content from
@@ -131,20 +156,6 @@
132157 };
133158
134159 /**
135 - * Applies an annotation to a given range.
136 - *
137 - * If a range arguments are not provided, all content will be annotated.
138 - *
139 - * @param method {String} Way to apply annotation ("toggle", "add" or "remove")
140 - * @param annotation {Object} Annotation to apply
141 - * @param start {Integer} Offset to begin annotating from
142 - * @param end {Integer} Offset to stop annotating to
143 - */
144 -es.Block.prototype.annotateContent = function( method, annotation, start, end ) {
145 - throw 'Block.annotateContent not implemented in this subclass.';
146 -};
147 -
148 -/**
149160 * Gets the start and end points of the word closest a given offset.
150161 *
151162 * @param offset {Integer} Offset to find word nearest to

Status & tagging log