r90636 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90635‎ | r90636 | r90637 >
Date:23:49, 22 June 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Finished the position from location function, still untested.
Modified paths:
  • /trunk/parsers/wikidom/lib/es/es.TextFlow.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/es/es.TextFlow.js
@@ -37,35 +37,65 @@
3838
3939 };
4040
41 -TextFlow.prototype.getPosition = function( start, end ) {
42 - var i = 0,
43 - startLine,
44 - endLine,
 41+TextFlow.prototype.getPosition = function( offset ) {
 42+ if ( offset < 0 ) {
 43+ throw 'Out of range error. Offset is expected to be greater than or equal to 0.';
 44+ }
 45+ var line = 0,
 46+ lineCount = this.lines.length,
4547 position = {
 48+ 'left': 0,
4649 'top': 0,
47 - 'right': 0,
48 - 'bottom': 0,
49 - 'left': 0
 50+ 'bottom': 0
5051 };
51 - while ( i++ < lines.length ) {
52 - if ( start >= lines[i].start && start <= lines[i].end ) {
53 - startLine = i;
 52+
 53+ /*
 54+ * Line finding
 55+ *
 56+ * It's possible that a more efficient method could be used here, but the number of lines to be
 57+ * iterated through will rarely be over 100, so it's unlikely that any significant gains will be
 58+ * had. Plus, as long as we are iterating over each line, we can also sum up the top and bottom
 59+ * positions, which is a nice benefit of this method.
 60+ */
 61+ while ( line < lineCount ) {
 62+ if ( offset >= lines[line].start && offset < lines[line].end ) {
 63+ position.bottom = position.top + lines[line].height;
5464 break;
5565 }
56 - position.top += lines[i].height;
 66+ position.top += lines[line].height;
 67+ line++;
5768 };
58 - position.bottom = top
59 - if ( length !== undefined ) {
60 - i = startLine;
61 - while ( i++ < lines.length ) {
62 - if ( end >= lines[i].start && end <= lines[i].end ) {
63 - startLine = i;
64 - position.bottom += lines[i].height;
65 - break;
66 - }
67 - };
 69+
 70+ /*
 71+ * Virtual n+1 position
 72+ *
 73+ * To allow access to position information of the right side of the last character on the last
 74+ * line, a virtual n+1 position is supported. Offsets beyond this virtual position will cause
 75+ * an exception to be thrown.
 76+ */
 77+ if ( line === lineCount ) {
 78+ if ( offset !== lines[line].end + 1 ) {
 79+ line--;
 80+ position.bottom = position.top;
 81+ position.top -= lines[line].height;
 82+ } else {
 83+ throw 'Out of range error. Offset is expected to be less than or equal to text length.';
 84+ }
6885 }
69 - // TODO: Calculate left and right positions
 86+
 87+ /*
 88+ * Offset measuring
 89+ *
 90+ * Since the left position will be zero for the first character in the line, so we can skip
 91+ * measuring for those cases.
 92+ */
 93+ if ( lines[line].start < offset ) {
 94+ var $lineRuler = $( '<div class="editSurface-line"></div>' ).appendTo( this.$ ),
 95+ lineRuler = $lineRuler[0];
 96+ lineRuler.innerHTML = this.htmlEncode( text.substring( lines[startLine].start, offset ) );
 97+ position.left = lineRuler.clientWidth;
 98+ $lineRuler.remove();
 99+ }
70100 return position;
71101 };
72102

Status & tagging log