http://www.mediawiki.org/wiki/Special:Code/MediaWiki/96064
Revision: 96064
Author: inez
Date: 2011-09-01 23:19:24 +0000 (Thu, 01 Sep 2011)
Log Message:
-----------
Implementation and tests for rangeOf in es.ContentSeries
Modified Paths:
--------------
trunk/parsers/wikidom/lib/synth/bases/es.ContentSeries.js
trunk/parsers/wikidom/tests/synth/index.html
trunk/parsers/wikidom/tests/synth/test.js
Added Paths:
-----------
trunk/parsers/wikidom/lib/synth/es.Range.js
Modified: trunk/parsers/wikidom/lib/synth/bases/es.ContentSeries.js
===================================================================
--- trunk/parsers/wikidom/lib/synth/bases/es.ContentSeries.js 2011-09-01
23:15:27 UTC (rev 96063)
+++ trunk/parsers/wikidom/lib/synth/bases/es.ContentSeries.js 2011-09-01
23:19:24 UTC (rev 96064)
@@ -27,7 +27,7 @@
return null;
};
-es.ContentSeries.prototype.offsetOf = function( item ) {
+es.ContentSeries.prototype.rangeOf = function( item ) {
if ( this.length ) {
var i = 0,
length = this.length,
@@ -36,7 +36,7 @@
if ( this[i] === item ) {
return new es.Range( left, left +
this[i].getLength() );
}
- left += this[i].getLength();
+ left += this[i].getLength() + 1;
i++;
}
}
Copied: trunk/parsers/wikidom/lib/synth/es.Range.js (from rev 96060,
trunk/parsers/wikidom/lib/es/es.Range.js)
===================================================================
--- trunk/parsers/wikidom/lib/synth/es.Range.js (rev 0)
+++ trunk/parsers/wikidom/lib/synth/es.Range.js 2011-09-01 23:19:24 UTC (rev
96064)
@@ -0,0 +1,59 @@
+/**
+ * Range of content.
+ *
+ * @class
+ * @constructor
+ * @param from {Integer} Starting offset
+ * @param to {Integer} Ending offset
+ * @property from {Integer} Starting offset
+ * @property to {Integer} Ending offset
+ * @property start {Integer} Normalized starting offset
+ * @property end {Integer} Normalized ending offset
+ */
+es.Range = function( from, to ) {
+ this.from = from || 0;
+ this.to = to || from;
+ this.normalize();
+};
+
+/* Methods */
+
+/**
+ * Checks if an offset is within this range.
+ *
+ * @method
+ * @param offset {Integer} Offset to check
+ * @returns {Boolean} If offset is within this range
+ */
+es.Range.prototype.containsOffset = function( offset ) {
+ this.normalize();
+ return offset >= this.start && offset < this.end;
+};
+
+/**
+ * Gets the length of the range.
+ *
+ * @method
+ * @returns {Integer} Length of range
+ */
+es.Range.prototype.getLength = function() {
+ return Math.abs( this.from - this.to );
+};
+
+/**
+ * Sets start and end properties, ensuring start is always before end.
+ *
+ * This should always be called before using the start or end properties. Do
not call this unless
+ * you are about to use these properties.
+ *
+ * @method
+ */
+es.Range.prototype.normalize = function() {
+ if ( this.from < this.to ) {
+ this.start = this.from;
+ this.end = this.to;
+ } else {
+ this.start = this.to;
+ this.end = this.from;
+ }
+};
Modified: trunk/parsers/wikidom/tests/synth/index.html
===================================================================
--- trunk/parsers/wikidom/tests/synth/index.html 2011-09-01 23:15:27 UTC
(rev 96063)
+++ trunk/parsers/wikidom/tests/synth/index.html 2011-09-01 23:19:24 UTC
(rev 96064)
@@ -15,6 +15,7 @@
<script src="../../lib/synth/bases/es.Container.js"></script>
<script
src="../../lib/synth/bases/es.ContainerItem.js"></script>
<script
src="../../lib/synth/bases/es.ContentSeries.js"></script>
+ <script src="../../lib/synth/es.Range.js"></script>
<script src="../../lib/jquery.js"></script>
<script src="../../lib/qunit.js"></script>
<script src="test.js"></script>
Modified: trunk/parsers/wikidom/tests/synth/test.js
===================================================================
--- trunk/parsers/wikidom/tests/synth/test.js 2011-09-01 23:15:27 UTC (rev
96063)
+++ trunk/parsers/wikidom/tests/synth/test.js 2011-09-01 23:19:24 UTC (rev
96064)
@@ -166,7 +166,7 @@
return this.size;
};
-test( 'ContentSeries', function() {
+test( 'ContentSeries lookup and rangeOf', function() {
strictEqual(
( new ContentStub( 'a', 0 ) ).getLength(),
0,
@@ -177,34 +177,52 @@
c = new ContentStub( 'c', 2 ),
d = new ContentStub( 'd', 3 ),
e = new ContentStub( 'e', 4 ),
- contentSeries = new es.ContentSeries( [ a, b, c, d, e ] ),
- tests = [
- { 'input' : -1, 'output' : null },
- { 'input' : 0, 'output' : a },
- { 'input' : 1, 'output' : b },
- { 'input' : 2, 'output' : b },
- { 'input' : 3, 'output' : c },
- { 'input' : 4, 'output' : c },
- { 'input' : 5, 'output' : c },
- { 'input' : 6, 'output' : d },
- { 'input' : 7, 'output' : d },
- { 'input' : 8, 'output' : d },
- { 'input' : 9, 'output' : d },
- { 'input' : 10, 'output' : e },
- { 'input' : 11, 'output' : e },
- { 'input' : 12, 'output' : e },
- { 'input' : 13, 'output' : e },
- { 'input' : 14, 'output' : e },
- { 'input' : 15, 'output' : null }
+ contentSeries = new es.ContentSeries( [ a, b, c, d, e ] );
+
+ var lookupTests = [
+ { 'input' : -1, 'output' : null },
+ { 'input' : 0, 'output' : a },
+ { 'input' : 1, 'output' : b },
+ { 'input' : 2, 'output' : b },
+ { 'input' : 3, 'output' : c },
+ { 'input' : 4, 'output' : c },
+ { 'input' : 5, 'output' : c },
+ { 'input' : 6, 'output' : d },
+ { 'input' : 7, 'output' : d },
+ { 'input' : 8, 'output' : d },
+ { 'input' : 9, 'output' : d },
+ { 'input' : 10, 'output' : e },
+ { 'input' : 11, 'output' : e },
+ { 'input' : 12, 'output' : e },
+ { 'input' : 13, 'output' : e },
+ { 'input' : 14, 'output' : e },
+ { 'input' : 15, 'output' : null }
];
- for ( var i = 0; i < tests.length; i++ ) {
+ for ( var i = 0; i < lookupTests.length; i++ ) {
strictEqual(
- contentSeries.lookup( tests[i].input ),
- tests[i].output,
+ contentSeries.lookup( lookupTests[i].input ),
+ lookupTests[i].output,
'es.ContentSeries.lookup finds the right item or
returns null when out of range'
);
}
+
+ var rangeOfTests = [
+ { 'input' : a, 'output' : new es.Range( 0, 0 ) },
+ { 'input' : b, 'output' : new es.Range( 1, 2 ) },
+ { 'input' : c, 'output' : new es.Range( 3, 5 ) },
+ { 'input' : d, 'output' : new es.Range( 6, 9 ) },
+ { 'input' : e, 'output' : new es.Range( 10, 14 ) },
+ { 'input' : null, 'output' : null }
+ ];
+
+ for ( var i = 0; i < rangeOfTests.length; i++ ) {
+ deepEqual(
+ contentSeries.rangeOf( rangeOfTests[i].input ),
+ rangeOfTests[i].output,
+ 'es.ContentSeries.rangeOf returns the correct range or
null if item is not found'
+ );
+ }
} );
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs