Skip to content

Commit 50f5658

Browse files
authored
1.3.5
1.3.5
2 parents 0e17dde + e157dff commit 50f5658

13 files changed

Lines changed: 302 additions & 112 deletions

File tree

.jshintrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
"noop" : true,
2323
"getData" : true,
2424
"setData" : true,
25-
"removeData" : true
25+
"removeData" : true,
26+
"getCompareFunction": true
2627
},
28+
"exported": ["getCompareFunction"],
2729

2830
"bitwise" : true,
2931
"camelcase" : true,

dist/cash.js

Lines changed: 142 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

3-
/*! cash-dom 1.3.4, https://github.com/kenwheeler/cash @license MIT */
4-
(function (root, factory) {
3+
/*! cash-dom 1.3.5, https://github.com/kenwheeler/cash @license MIT */
4+
;(function (root, factory) {
55
if (typeof define === "function" && define.amd) {
66
define(factory);
77
} else if (typeof exports !== "undefined") {
@@ -13,7 +13,8 @@
1313
var doc = document, win = window, ArrayProto = Array.prototype, slice = ArrayProto.slice, filter = ArrayProto.filter, push = ArrayProto.push;
1414

1515
var noop = function () {}, isFunction = function (item) {
16-
return typeof item === typeof noop;
16+
// @see https://crbug.com/568448
17+
return typeof item === typeof noop && item.call;
1718
}, isString = function (item) {
1819
return typeof item === typeof "";
1920
};
@@ -26,12 +27,18 @@
2627
return elems;
2728
}
2829

29-
var frag, tmp;
30+
var frag;
3031
function parseHTML(str) {
31-
frag = frag || doc.createDocumentFragment();
32-
tmp = tmp || frag.appendChild(doc.createElement("div"));
33-
tmp.innerHTML = str;
34-
return tmp.childNodes;
32+
if (!frag) {
33+
frag = doc.implementation.createHTMLDocument();
34+
var base = frag.createElement("base");
35+
base.href = doc.location.href;
36+
frag.head.appendChild(base);
37+
}
38+
39+
frag.body.innerHTML = str;
40+
41+
return frag.body.childNodes;
3542
}
3643

3744
function onReady(fn) {
@@ -92,7 +99,6 @@
9299
}
93100

94101
var fn = cash.fn = cash.prototype = Init.prototype = { // jshint ignore:line
95-
constructor: cash,
96102
cash: true,
97103
length: 0,
98104
push: push,
@@ -101,6 +107,8 @@
101107
init: Init
102108
};
103109

110+
Object.defineProperty(fn, "constructor", { value: cash });
111+
104112
cash.parseHTML = parseHTML;
105113
cash.noop = noop;
106114
cash.isFunction = isFunction;
@@ -145,6 +153,20 @@
145153
return !!m && m.call(el, selector);
146154
}
147155

156+
function getCompareFunction(selector) {
157+
return (
158+
/* Use browser's `matches` function if string */
159+
isString(selector) ? matches :
160+
/* Match a cash element */
161+
selector.cash ? function (el) {
162+
return selector.is(el);
163+
} :
164+
/* Direct comparison */
165+
function (el, selector) {
166+
return el === selector;
167+
});
168+
}
169+
148170
function unique(collection) {
149171
return cash(slice.call(collection).filter(function (item, index, self) {
150172
return self.indexOf(item) === index;
@@ -374,9 +396,15 @@
374396
},
375397

376398
filter: function (selector) {
377-
return cash(filter.call(this, (isString(selector) ? function (e) {
378-
return matches(e, selector);
379-
} : selector)));
399+
if (!selector) {
400+
return this;
401+
}
402+
403+
var comparator = (isFunction(selector) ? selector : getCompareFunction(selector));
404+
405+
return cash(filter.call(this, function (e) {
406+
return comparator(e, selector);
407+
}));
380408
},
381409

382410
first: function () {
@@ -481,9 +509,18 @@
481509
}
482510

483511
function removeEvent(node, eventName, callback) {
484-
var eventCache = getData(node, "_cashEvents")[eventName];
512+
var events = getData(node, "_cashEvents"), eventCache = (events && events[eventName]), index;
513+
514+
if (!eventCache) {
515+
return;
516+
}
517+
485518
if (callback) {
486519
node.removeEventListener(eventName, callback);
520+
index = eventCache.indexOf(callback);
521+
if (index >= 0) {
522+
eventCache.splice(index, 1);
523+
}
487524
} else {
488525
each(eventCache, function (event) {
489526
node.removeEventListener(eventName, event);
@@ -571,27 +608,69 @@
571608
function encode(name, value) {
572609
return "&" + encodeURIComponent(name) + "=" + encodeURIComponent(value).replace(/%20/g, "+");
573610
}
574-
function isCheckable(field) {
575-
return field.type === "radio" || field.type === "checkbox";
611+
612+
function getSelectMultiple_(el) {
613+
var values = [];
614+
each(el.options, function (o) {
615+
if (o.selected) {
616+
values.push(o.value);
617+
}
618+
});
619+
return values.length ? values : null;
576620
}
577621

578-
var formExcludes = ["file", "reset", "submit", "button"];
622+
function getSelectSingle_(el) {
623+
var selectedIndex = el.selectedIndex;
624+
return selectedIndex >= 0 ? el.options[selectedIndex].value : null;
625+
}
626+
627+
function getValue(el) {
628+
var type = el.type;
629+
if (!type) {
630+
return null;
631+
}
632+
switch (type.toLowerCase()) {
633+
case "select-one":
634+
return getSelectSingle_(el);
635+
case "select-multiple":
636+
return getSelectMultiple_(el);
637+
case "radio":
638+
return (el.checked) ? el.value : null;
639+
case "checkbox":
640+
return (el.checked) ? el.value : null;
641+
default:
642+
return el.value ? el.value : null;
643+
}
644+
}
579645

580646
fn.extend({
581647
serialize: function () {
582-
var formEl = this[0].elements, query = "";
583-
584-
each(formEl, function (field) {
585-
if (field.name && formExcludes.indexOf(field.type) < 0) {
586-
if (field.type === "select-multiple") {
587-
each(field.options, function (o) {
588-
if (o.selected) {
589-
query += encode(field.name, o.value);
590-
}
591-
});
592-
} else if (!isCheckable(field) || (isCheckable(field) && field.checked)) {
593-
query += encode(field.name, field.value);
594-
}
648+
var query = "";
649+
650+
each(this[0].elements || this, function (el) {
651+
if (el.disabled || el.tagName === "FIELDSET") {
652+
return;
653+
}
654+
var name = el.name;
655+
switch (el.type.toLowerCase()) {
656+
case "file":
657+
case "reset":
658+
case "submit":
659+
case "button":
660+
break;
661+
case "select-multiple":
662+
var values = getValue(el);
663+
if (values !== null) {
664+
each(values, function (value) {
665+
query += encode(name, value);
666+
});
667+
}
668+
break;
669+
default:
670+
var value = getValue(el);
671+
if (value !== null) {
672+
query += encode(name, value);
673+
}
595674
}
596675
});
597676

@@ -600,7 +679,7 @@
600679

601680
val: function (value) {
602681
if (value === undefined) {
603-
return this[0].value;
682+
return getValue(this[0]);
604683
} else {
605684
return this.each(function (v) {
606685
return v.value = value;
@@ -755,10 +834,6 @@
755834

756835
});
757836

758-
function directCompare(el, selector) {
759-
return el === selector;
760-
}
761-
762837
fn.extend({
763838
children: function (selector) {
764839
var elems = [];
@@ -773,8 +848,11 @@
773848
},
774849

775850
closest: function (selector) {
776-
if (!selector || matches(this[0], selector)) {
777-
return this;
851+
if (!selector || this.length < 1) {
852+
return cash();
853+
}
854+
if (this.is(selector)) {
855+
return this.filter(selector);
778856
}
779857
return this.parent().closest(selector);
780858
},
@@ -784,21 +862,19 @@
784862
return false;
785863
}
786864

787-
var match = false, comparator = (isString(selector) ? matches : selector.cash ? function (el) {
788-
return selector.is(el);
789-
} : directCompare);
865+
var match = false, comparator = getCompareFunction(selector);
790866

791-
this.each(function (el, i) {
792-
match = comparator(el, selector, i);
867+
this.each(function (el) {
868+
match = comparator(el, selector);
793869
return !match;
794870
});
795871

796872
return match;
797873
},
798874

799875
find: function (selector) {
800-
if (!selector) {
801-
return cash();
876+
if (!selector || selector.nodeType) {
877+
return cash(selector && this.has(selector).length ? selector : null);
802878
}
803879

804880
var elems = [];
@@ -810,24 +886,38 @@
810886
},
811887

812888
has: function (selector) {
813-
return filter.call(this, function (el) {
814-
return cash(el).find(selector).length !== 0;
889+
var comparator = (isString(selector) ? function (el) {
890+
return find(selector, el).length !== 0;
891+
} : function (el) {
892+
return el.contains(selector);
815893
});
894+
895+
return this.filter(comparator);
816896
},
817897

818898
next: function () {
819899
return cash(this[0].nextElementSibling);
820900
},
821901

822902
not: function (selector) {
823-
return filter.call(this, function (el) {
824-
return !matches(el, selector);
903+
if (!selector) {
904+
return this;
905+
}
906+
907+
var comparator = getCompareFunction(selector);
908+
909+
return this.filter(function (el) {
910+
return !comparator(el, selector);
825911
});
826912
},
827913

828914
parent: function () {
829-
var result = this.map(function (item) {
830-
return item.parentElement || doc.body.parentNode;
915+
var result = [];
916+
917+
this.each(function (item) {
918+
if (item && item.parentNode) {
919+
result.push(item.parentNode);
920+
}
831921
});
832922

833923
return unique(result);
@@ -839,8 +929,8 @@
839929
this.each(function (item) {
840930
last = item;
841931

842-
while (last !== doc.body.parentNode) {
843-
last = last.parentElement;
932+
while (last && last.parentNode && last !== doc.body.parentNode) {
933+
last = last.parentNode;
844934

845935
if (!selector || (selector && matches(last, selector))) {
846936
result.push(last);
@@ -858,7 +948,7 @@
858948
siblings: function () {
859949
var collection = this.parent().children(), el = this[0];
860950

861-
return filter.call(collection, function (i) {
951+
return collection.filter(function (i) {
862952
return i !== el;
863953
});
864954
}

dist/cash.min.js

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cash-dom",
3-
"version": "1.3.4",
3+
"version": "1.3.5",
44
"description": "An absurdly small jQuery alternative for modern browsers.",
55
"main": "./dist/cash.js",
66
"repository": {

src/_wrapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @echo header
2-
(function(root, factory) {
2+
;(function(root, factory) {
33
if (typeof define === 'function' && define.amd) {
44
define(factory);
55
} else if (typeof exports !== 'undefined') {

src/collection.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ fn.extend({
1414
},
1515

1616
filter(selector) {
17-
return cash(filter.call(this, ( isString(selector) ? e => matches(e, selector) : selector )));
17+
if ( !selector ) { return this; }
18+
19+
var comparator = ( isFunction(selector) ? selector : getCompareFunction(selector ));
20+
21+
return cash( filter.call(this, e => comparator(e, selector) ) );
1822
},
1923

2024
first() {

0 commit comments

Comments
 (0)