scratch – Blame information for rev 134

Subversion Repositories:
Rev:
Rev Author Line No. Line
134 office 1 /**
2 * Patch from zombie.js for jsdom https://github.com/assaf/zombie/blob/master/src/zombie/dom_focus.coffee
3 *
4 * Adds focus() and blur() methods and events to dom elements
5 */
6  
7 var FOCUS_ELEMENTS, HTML, elementType, setAttribute, setFocus, _i, _j, _len, _len1, _ref, _ref1;
8  
9 HTML = require("jsdom").dom.level3.html;
10  
11 FOCUS_ELEMENTS = ["INPUT", "SELECT", "TEXTAREA", "BUTTON", "ANCHOR"];
12  
13 HTML.HTMLDocument.prototype.__defineGetter__("activeElement", function() {
14 return this._inFocus || this.body;
15 });
16  
17 setFocus = function(document, element) {
18 var inFocus, onblur, onfocus;
19 inFocus = document._inFocus;
20 if (element !== inFocus) {
21 if (inFocus) {
22 onblur = document.createEvent("HTMLEvents");
23 onblur.initEvent("blur", false, false);
24 inFocus.dispatchEvent(onblur);
25 }
26 if (element) {
27 onfocus = document.createEvent("HTMLEvents");
28 onfocus.initEvent("focus", false, false);
29 element.dispatchEvent(onfocus);
30 document._inFocus = element;
31 }
32 }
33 };
34  
35 HTML.HTMLElement.prototype.focus = function() {};
36  
37 HTML.HTMLElement.prototype.blur = function() {};
38  
39 _ref = [HTML.HTMLInputElement, HTML.HTMLSelectElement, HTML.HTMLTextAreaElement, HTML.HTMLButtonElement, HTML.HTMLAnchorElement];
40 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
41 elementType = _ref[_i];
42 elementType.prototype.focus = function() {
43 return setFocus(this.ownerDocument, this);
44 };
45 elementType.prototype.blur = function() {
46 return setFocus(this.ownerDocument, null);
47 };
48 setAttribute = elementType.prototype.setAttribute;
49 elementType.prototype.setAttribute = function(name, value) {
50 var document;
51 setAttribute.call(this, name, value);
52 if (name === "autofocus") {
53 document = this.ownerDocument;
54 if (~FOCUS_ELEMENTS.indexOf(this.tagName) && !document._inFocus) {
55 return this.focus();
56 }
57 }
58 };
59 }
60  
61 _ref1 = [HTML.HTMLInputElement, HTML.HTMLTextAreaElement, HTML.HTMLSelectElement];
62 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
63 elementType = _ref1[_j];
64 elementType.prototype._eventDefaults.focus = function(event) {
65 var element;
66 element = event.target;
67 return element._focusValue = element.value || '';
68 };
69 elementType.prototype._eventDefaults.blur = function(event) {
70 var change, element, focusValue;
71 element = event.target;
72 focusValue = element._focusValue;
73 if (focusValue !== element.value) {
74 change = element.ownerDocument.createEvent("HTMLEvents");
75 change.initEvent("change", false, false);
76 return element.dispatchEvent(change);
77 }
78 };
79 }