ref: master
public/javascripts/tinymce/tests/js/utils.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
(function() { function fontFace(face) { if (tinymce.isOpera) { return "'" + face + "'"; } else { return face; } } function findContainer(selector) { var container; if (tinymce.is(selector, 'string')) { container = editor.dom.select(selector)[0]; } else { container = selector; } if (container.firstChild) { container = container.firstChild; } return container; } function setSelection(startSelector, startOffset, endSelector, endOffset) { if (!endSelector) { endSelector = startSelector; endOffset = startOffset; } var startContainer = findContainer(startSelector); var endContainer = findContainer(endSelector); var rng = editor.dom.createRng(); function setRange(container, offset, start) { offset = offset || 0; if (offset === 'after') { if (start) { rng.setStartAfter(container); } else { rng.setEndAfter(container); } return; } else if (offset === 'afterNextCharacter') { container = container.nextSibling; offset = 1; } if (start) { rng.setStart(container, offset); } else { rng.setEnd(container, offset); } } setRange(startContainer, startOffset, true); setRange(endContainer, endOffset, false); editor.selection.setRng(rng); } function trimContent(content) { return content.replace(/^<p> <\/p>\n?/, '').replace(/\n?<p> <\/p>$/, ''); } /** * Fakes a key event. * * @param {Element/String} e DOM element object or element id to send fake event to. * @param {String} na Event name to fake like "keydown". * @param {Object} o Optional object with data to send with the event like keyCode and charCode. */ function fakeKeyEvent(e, na, o) { var ev; o = tinymce.extend({ keyCode : 13, charCode : 0 }, o); e = tinymce.DOM.get(e); if (e.fireEvent) { ev = document.createEventObject(); tinymce.extend(ev, o); e.fireEvent('on' + na, ev); return; } if (document.createEvent) { try { // Fails in Safari ev = document.createEvent('KeyEvents'); ev.initKeyEvent(na, true, true, window, false, false, false, false, o.keyCode, o.charCode); } catch (ex) { ev = document.createEvent('Events'); ev.initEvent(na, true, true); ev.keyCode = o.keyCode; ev.charCode = o.charCode; } } else { ev = document.createEvent('UIEvents'); if (ev.initUIEvent) { ev.initUIEvent(na, true, true, window, 1); } ev.keyCode = o.keyCode; ev.charCode = o.charCode; } e.dispatchEvent(ev); } function normalizeRng(rng) { if (rng.startContainer.nodeType == 3) { if (rng.startOffset === 0) { rng.setStartBefore(rng.startContainer); } else if (rng.startOffset >= rng.startContainer.nodeValue.length - 1) { rng.setStartAfter(rng.startContainer); } } if (rng.endContainer.nodeType == 3) { if (rng.endOffset === 0) { rng.setEndBefore(rng.endContainer); } else if (rng.endOffset >= rng.endContainer.nodeValue.length - 1) { rng.setEndAfter(rng.endContainer); } } return rng; } // TODO: Replace this with the new event logic in 3.5 function type(chr) { var editor = tinymce.activeEditor, keyCode, charCode, evt, startElm, rng; function charCodeToKeyCode(charCode) { var lookup = { '0': 48, '1': 49, '2': 50, '3': 51, '4': 52, '5': 53, '6': 54, '7': 55, '8': 56, '9': 57,'a': 65, 'b': 66, 'c': 67, 'd': 68, 'e': 69, 'f': 70, 'g': 71, 'h': 72, 'i': 73, 'j': 74, 'k': 75, 'l': 76, 'm': 77, 'n': 78, 'o': 79, 'p': 80, 'q': 81, 'r': 82, 's': 83, 't': 84, 'u': 85, 'v': 86, 'w': 87, 'x': 88, 'y': 89, ' ': 32, ',': 188, '-': 189, '.': 190, '/': 191, '\\': 220, '[': 219, ']': 221, '\'': 222, ';': 186, '=': 187, ')': 41 }; return lookup[String.fromCharCode(charCode)]; } function fakeEvent(target, type, evt) { editor.dom.fire(target, type, evt); } // Numeric keyCode if (typeof(chr) == "number") { charCode = chr; keyCode = charCodeToKeyCode(charCode); } else if (typeof(chr) == "string") { // String value if (chr == '\b') { keyCode = 8; charCode = chr.charCodeAt(0); } else if (chr == '\n') { keyCode = 13; charCode = chr.charCodeAt(0); } else { charCode = chr.charCodeAt(0); keyCode = charCodeToKeyCode(charCode); } } else { evt = chr; if (evt.charCode) { chr = String.fromCharCode(evt.charCode); } if (evt.keyCode) { keyCode = evt.keyCode; } } evt = evt || {keyCode: keyCode, charCode: charCode}; startElm = editor.selection.getStart(); fakeEvent(startElm, 'keydown', evt); fakeEvent(startElm, 'keypress', evt); if (!evt.isDefaultPrevented()) { if (keyCode == 8) { if (editor.getDoc().selection) { rng = editor.getDoc().selection.createRange(); if (rng.text.length === 0) { rng.moveStart('character', -1); rng.select(); } rng.execCommand('Delete', false, null); } else { rng = editor.selection.getRng(); if (rng.startContainer.nodeType == 1 && rng.collapsed) { var nodes = rng.startContainer.childNodes, lastNode = nodes[nodes.length - 1]; // If caret is at <p>abc|</p> and after the abc text node then move it to the end of the text node // Expand the range to include the last char <p>ab[c]</p> since IE 11 doesn't delete otherwise if (rng.startOffset >= nodes.length - 1 && lastNode && lastNode.nodeType == 3 && lastNode.data.length > 0) { rng.setStart(lastNode, lastNode.data.length - 1); rng.setEnd(lastNode, lastNode.data.length); editor.selection.setRng(rng); } } editor.getDoc().execCommand('Delete', false, null); } } else if (typeof(chr) == 'string') { rng = editor.selection.getRng(true); if (rng.startContainer.nodeType == 3 && rng.collapsed) { rng.startContainer.insertData(rng.startOffset, chr); rng.setStart(rng.startContainer, rng.startOffset + 1); rng.collapse(true); editor.selection.setRng(rng); } else { rng.insertNode(editor.getDoc().createTextNode(chr)); } } } fakeEvent(startElm, 'keyup', evt); } function cleanHtml(html) { html = html.toLowerCase().replace(/[\r\n]+/gi, ''); html = html.replace(/ (sizcache[0-9]+|sizcache|nodeindex|sizset[0-9]+|sizset|data\-mce\-expando|data\-mce\-selected)="[^"]*"/gi, ''); html = html.replace(/<span[^>]+data-mce-bogus[^>]+>[\u200B\uFEFF]+<\/span>|<div[^>]+data-mce-bogus[^>]+><\/div>/gi, ''); html = html.replace(/ style="([^"]+)"/gi, function(val1, val2) { val2 = val2.replace(/;$/, ''); return ' style="' + val2.replace(/\:([^ ])/g, ': $1') + ';"'; }); return html; } function normalizeHtml(html) { var writer = new tinymce.html.Writer(); new tinymce.html.SaxParser({ validate: false, comment: writer.comment, cdata: writer.cdata, text: writer.text, end: writer.end, pi: writer.pi, doctype: writer.doctype, start: function(name, attrs, empty) { attrs.sort(function(a, b) { if (a.name === b.name) { return 0; } return a.name > b.name ? 1 : -1; }); writer.start(name, attrs, empty); } }).parse(html); return writer.getContent(); } /** * Measures the x, y, w, h of the specified element/control relative to the view element. */ function rect(ctrl) { var outerRect, innerRect; if (ctrl.nodeType) { innerRect = ctrl.getBoundingClientRect(); } else { innerRect = ctrl.getEl().getBoundingClientRect(); } outerRect = document.getElementById('view').getBoundingClientRect(); return [ Math.round(innerRect.left - outerRect.left), Math.round(innerRect.top - outerRect.top), Math.round(innerRect.right - innerRect.left), Math.round(innerRect.bottom - innerRect.top) ]; } function size(ctrl) { return rect(ctrl).slice(2); } function resetScroll(elm) { elm.scrollTop = 0; elm.scrollLeft = 0; } // Needed since fonts render differently on different platforms function nearlyEqualRects(rect1, rect2, diff) { diff = diff || 1; for (var i = 0; i < 4; i++) { if (Math.abs(rect1[i] - rect2[i]) > diff) { deepEqual(rect1, rect2); return; } } ok(true); } function getFrontmostWindow() { return editor.windowManager.windows[editor.windowManager.windows.length - 1]; } function pressArrowKey(evt) { var dom = editor.dom, target = editor.selection.getNode(); evt = tinymce.extend({keyCode: 37}, evt); dom.fire(target, 'keydown', evt); dom.fire(target, 'keypress', evt); dom.fire(target, 'keyup', evt); } function pressEnter(evt) { var dom = editor.dom, target = editor.selection.getNode(); evt = tinymce.extend({keyCode: 13}, evt); dom.fire(target, 'keydown', evt); dom.fire(target, 'keypress', evt); dom.fire(target, 'keyup', evt); } function trimBrsOnIE(html) { return html.replace(/<br[^>]*>/gi, ''); } function patch(proto, name, patchFunc) { var originalFunc = proto[name]; var originalFuncs = proto.__originalFuncs; if (!originalFuncs) { proto.__originalFuncs = originalFuncs = {}; } if (!originalFuncs[name]) { originalFuncs[name] = originalFunc; } else { originalFunc = originalFuncs[name]; } proto[name] = function() { var args = Array.prototype.slice.call(arguments); args.unshift(originalFunc); return patchFunc.apply(this, args); }; } function unpatch(proto, name) { var originalFuncs = proto.__originalFuncs; if (!originalFuncs) { return; } if (name) { proto[name] = originalFuncs[name]; delete originalFuncs[name]; } else { for (var key in originalFuncs) { proto[key] = originalFuncs[key]; } delete proto.__originalFuncs; } } function triggerElementChange(element){ var evt; if ("createEvent" in document) { evt = document.createEvent("HTMLEvents"); evt.initEvent("change", false, true); element.dispatchEvent(evt); } else { element.fireEvent("onchange"); } } window.Utils = { fontFace: fontFace, findContainer: findContainer, setSelection: setSelection, trimContent: trimContent, fakeKeyEvent: fakeKeyEvent, normalizeRng: normalizeRng, type: type, cleanHtml: cleanHtml, normalizeHtml: normalizeHtml, rect: rect, size: size, resetScroll: resetScroll, nearlyEqualRects: nearlyEqualRects, getFrontmostWindow: getFrontmostWindow, pressArrowKey: pressArrowKey, pressEnter: pressEnter, trimBrsOnIE: trimBrsOnIE, patch: patch, unpatch: unpatch, triggerElementChange: triggerElementChange }; })(); |