| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
ve.ce.TestOffset = function VeCeTestOffset( direction, offset ) { |
| 22 |
if ( direction !== 'forward' && direction !== 'backward' ) { |
| 23 |
throw new Error( 'Invalid direction "' + direction + '"' ); |
| 24 |
} |
| 25 |
this.direction = direction; |
| 26 |
this.offset = offset; |
| 27 |
this.lastText = null; |
| 28 |
this.lastSel = null; |
| 29 |
}; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
ve.ce.TestOffset.prototype.resolve = function ( node ) { |
| 47 |
const reversed = ( this.direction !== 'forward' ); |
| 48 |
return ve.ce.TestOffset.static.findTextOffset( node, this.offset, reversed ); |
| 49 |
}; |
| 50 |
|
| 51 |
|
| 52 |
ve.ce.TestOffset.static = {}; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
ve.ce.TestOffset.static.findTextOffset = function ( node, n, reversed ) { |
| 66 |
if ( node.nodeType === node.TEXT_NODE ) { |
| 67 |
|
| 68 |
if ( node.textContent.length >= n ) { |
| 69 |
const offset = reversed ? node.textContent.length - n : n; |
| 70 |
const slice = node.textContent.slice( 0, offset ) + '|' + |
| 71 |
node.textContent.slice( offset ); |
| 72 |
return { node: node, offset: offset, slice: slice }; |
| 73 |
} else { |
| 74 |
return { consumed: node.textContent.length + 1 }; |
| 75 |
} |
| 76 |
} |
| 77 |
if ( node.nodeType !== node.ELEMENT_NODE ) { |
| 78 |
return { consumed: 0 }; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
const childNodes = Array.prototype.slice.call( node.childNodes ); |
| 85 |
|
| 86 |
if ( childNodes.length === 0 ) { |
| 87 |
if ( n === 0 ) { |
| 88 |
return { node: node, offset: 0, slice: '|' }; |
| 89 |
} |
| 90 |
return { consumed: 0 }; |
| 91 |
} |
| 92 |
|
| 93 |
if ( reversed ) { |
| 94 |
childNodes.reverse(); |
| 95 |
} |
| 96 |
let consumed = 0; |
| 97 |
for ( let i = 0, len = childNodes.length; i < len; i++ ) { |
| 98 |
const childNode = node.childNodes[ i ]; |
| 99 |
const found = ve.ce.TestOffset.static.findTextOffset( childNode, n - consumed, reversed ); |
| 100 |
if ( found.node ) { |
| 101 |
return found; |
| 102 |
} |
| 103 |
consumed += found.consumed; |
| 104 |
|
| 105 |
if ( childNode.nodeType === node.ELEMENT_NODE ) { |
| 106 |
if ( i + 1 === len || childNodes[ i + 1 ].nodeType !== node.TEXT_NODE ) { |
| 107 |
consumed += 1; |
| 108 |
if ( consumed === n ) { |
| 109 |
|
| 110 |
return { node: node, offset: i + 1, slice: 'XXX' }; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
return { consumed: consumed }; |
| 116 |
}; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
ve.ce.TestRunner = function VeCeTestRunner( surface ) { |
| 126 |
this.view = surface; |
| 127 |
this.model = surface.getModel(); |
| 128 |
this.doc = surface.getElementDocument(); |
| 129 |
this.nativeSelection = surface.nativeSelection; |
| 130 |
|
| 131 |
|
| 132 |
this.lastText = this.getParagraph().textContent; |
| 133 |
|
| 134 |
|
| 135 |
surface.surfaceObserver.pollInterval = null; |
| 136 |
|
| 137 |
|
| 138 |
ve.test.utils.hijackEventSequencerTimeouts( this.view.eventSequencer ); |
| 139 |
}; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
ve.ce.TestRunner.prototype.getParagraph = function () { |
| 151 |
const p = this.view.$element.find( '.ve-ce-attachedRootNode > p' )[ 0 ]; |
| 152 |
if ( p === undefined ) { |
| 153 |
if ( this.view.$element.find( '.ve-ce-attachedRootNode' )[ 0 ] === undefined ) { |
| 154 |
throw new Error( 'no CE div' ); |
| 155 |
} |
| 156 |
throw new Error( 'CE div but no p' ); |
| 157 |
} |
| 158 |
return p; |
| 159 |
}; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
ve.ce.TestRunner.prototype.endLoop = function () { |
| 167 |
this.view.eventSequencer.endLoop(); |
| 168 |
}; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
ve.ce.TestRunner.prototype.sendEvent = function ( eventName, ev ) { |
| 177 |
|
| 178 |
ev.originalEvent = ev.originalEvent || {}; |
| 179 |
this.view.eventSequencer.onEvent( eventName, ev ); |
| 180 |
}; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
ve.ce.TestRunner.prototype.changeText = function ( text ) { |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
const paragraph = this.getParagraph(); |
| 197 |
$( paragraph ).find( '*' ).addBack().contents().each( function () { |
| 198 |
if ( this.nodeType === Node.TEXT_NODE ) { |
| 199 |
this.parentNode.removeChild( this ); |
| 200 |
} |
| 201 |
} ); |
| 202 |
|
| 203 |
const range = document.createRange(); |
| 204 |
if ( text === '' ) { |
| 205 |
range.setStart( paragraph, 0 ); |
| 206 |
} else { |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
const textNode = document.createTextNode( text ); |
| 211 |
paragraph.insertBefore( textNode, paragraph.firstChild ); |
| 212 |
range.setStart( textNode, text.length ); |
| 213 |
} |
| 214 |
this.nativeSelection.removeAllRanges(); |
| 215 |
this.nativeSelection.addRange( range ); |
| 216 |
this.lastText = text; |
| 217 |
}; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
ve.ce.TestRunner.prototype.changeSel = function ( start, end ) { |
| 235 |
if ( typeof start === 'number' ) { |
| 236 |
start = new ve.ce.TestOffset( 'forward', start ); |
| 237 |
} |
| 238 |
if ( typeof end === 'number' ) { |
| 239 |
end = new ve.ce.TestOffset( 'forward', end ); |
| 240 |
} |
| 241 |
|
| 242 |
const foundStart = start.resolve( this.getParagraph() ); |
| 243 |
const foundEnd = start.resolve( this.getParagraph() ); |
| 244 |
if ( !foundStart.node ) { |
| 245 |
throw new Error( 'Bad start offset: ' + start.offset ); |
| 246 |
} |
| 247 |
if ( !foundEnd.node ) { |
| 248 |
throw new Error( 'Bad end offset: ', end.offset ); |
| 249 |
} |
| 250 |
|
| 251 |
const nativeRange = this.doc.createRange(); |
| 252 |
nativeRange.setStart( foundStart.node, foundStart.offset ); |
| 253 |
nativeRange.setEnd( foundEnd.node, foundEnd.offset ); |
| 254 |
this.nativeSelection.removeAllRanges(); |
| 255 |
this.getParagraph().focus(); |
| 256 |
this.nativeSelection.addRange( nativeRange, false ); |
| 257 |
this.lastSel = [ start, end ]; |
| 258 |
|
| 259 |
return { |
| 260 |
startNode: foundStart.node, |
| 261 |
endNode: foundEnd.node, |
| 262 |
startOffset: foundStart.offset, |
| 263 |
endOffset: foundEnd.offset |
| 264 |
}; |
| 265 |
}; |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
ve.ce.TestRunner.prototype.testEqual = function ( assert, testName, sequence ) { |
| 275 |
const comment = testName + ' seq=' + sequence + ': "' + this.lastText + '"'; |
| 276 |
assert.strictEqual( this.model.getDocument().data.getText( false ), this.lastText, comment ); |
| 277 |
}; |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
ve.ce.TestRunner.prototype.testNotEqual = function ( assert, testName, sequence ) { |
| 287 |
const comment = testName + ' seq=' + sequence + ': "' + this.lastText + '"'; |
| 288 |
assert.notStrictEqual( this.model.getDocument().data.getText( false ), this.lastText, comment ); |
| 289 |
}; |
| 290 |
|
| 291 |
ve.ce.TestRunner.prototype.ok = function ( assert, testName, sequence ) { |
| 292 |
const comment = testName + ' seq=' + sequence + ': "' + this.lastText + '"'; |
| 293 |
assert.true( true, comment ); |
| 294 |
}; |
| 295 |
|
| 296 |
ve.ce.TestRunner.prototype.failDied = function ( assert, testName, sequence, ex ) { |
| 297 |
const comment = testName + ' seq=' + sequence + ': "' + this.lastText + |
| 298 |
'" ex=' + ex + ' stack=<' + ex.stack + '>'; |
| 299 |
assert.true( false, comment ); |
| 300 |
}; |