Skip to content

Commit

Permalink
Bumped version to 0.9.7.
Browse files Browse the repository at this point in the history
Updated some tests to use module format.
Added more tests to getVal().
Added tests for $.fn.closest() and $.fn.parentsUntil().
Fixed bugs in $.fn.parentsUntil() where it returned the matching element, and didn't filter the nodes correctly.
  • Loading branch information
hexydec committed Dec 19, 2018
1 parent 6b908d3 commit a1b202f
Show file tree
Hide file tree
Showing 17 changed files with 380 additions and 221 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A lightweight modular jQuery clone/alternative library built for modern browsers
![Licence](https://img.shields.io/badge/Licence-MIT-lightgrey.svg)
![Project Status](https://img.shields.io/badge/Project%20Status-Beta-yellow.svg)
![Size Minified](https://img.shields.io/badge/Size%20(Minified)-16.6kb-brightgreen.svg)
![Size Gzipped](https://img.shields.io/badge/Size%20(Gzipped)-6.07kb-brightgreen.svg)
![Size Gzipped](https://img.shields.io/badge/Size%20(Gzipped)-6.09kb-brightgreen.svg)

**This project is now in beta, make sure to test your integration with this code thoroughly before deploying**

Expand Down
20 changes: 14 additions & 6 deletions dist/dabby.es5.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/dabby.es5.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/dabby.es5.min.js

Large diffs are not rendered by default.

17 changes: 11 additions & 6 deletions dist/dabby.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! dabbyjs v0.9.6 by Will Earp - https://github.com/hexydec/dabby */
/*! dabbyjs v0.9.7 by Will Earp - https://github.com/hexydec/dabby */

const $ = function dabby(selector, context) {

Expand Down Expand Up @@ -563,15 +563,20 @@ $.fn.add = function (nodes, context) {
while (i--) {
parent = this[i].parentNode;
while (parent && parent.nodeType === Node.ELEMENT_NODE) {
if (until && filterNodes(parent, selector).length) {
break;
}
nodes.push(parent);
if (!all || (until && filterNodes(parent, selector).length)) {
if (!all) {
break;
} else {
parent = parent.parentNode;
}
parent = parent.parentNode;
}
}
return $(selector ? filterNodes(nodes, selector) : nodes);
if (!until) {
filter = selector;
}
return $(filter ? filterNodes(nodes, filter) : nodes);
};
});

Expand Down Expand Up @@ -1373,7 +1378,7 @@ $.fn.closest = function (selector, context) {
while (i--) {
parents = [];
node = this[i];
while (node) {
while (node && node.nodeType === Node.ELEMENT_NODE) {
parents.push(node);
node = node.parentNode;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/dabby.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dabbyjs",
"version": "0.9.6",
"version": "0.9.7",
"homepage": "https://hexydec.github.io/dabby",
"author": "Will Earp <will@hexydec.com>",
"description": "A lightweight modular jQuery clone library built for modern browsers",
Expand Down
87 changes: 47 additions & 40 deletions src/attributes/attr/test.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,53 @@
import $ from "../../../dist/dabby.js";

QUnit.module("Attributes");

QUnit.test("$.fn.attr", function (assert) {
QUnit.module("Attributes", hooks => {
var test = document.getElementsByClassName("test")[0];
test.innerHTML = '<div class="testtemp"></div>';
var main = $(".testtemp"),
rmain = document.getElementsByClassName("testtemp")[0],
style = "padding-top: 10px;",
correct = true;

// set and get class
assert.deepEqual(main.attr("class", "testtemp testclass"), main, "Returns itself when setting class");
assert.equal(rmain.className, "testtemp testclass", "Can set class");
assert.equal(main.attr("class"), "testtemp testclass", "Can retrieve class");
main.attr("class", "testtemp");
assert.equal(main.attr("class"), "testtemp", "Can remove class");

// set and get style
assert.deepEqual(main.attr("style", style), main, "Returns itself when setting style");
assert.equal(rmain.style.cssText, style, "Can set style");
assert.equal(main.attr("style"), style, "Can retrieve style");

// set and get attribute
assert.deepEqual(main.attr("itemprop", "articleBody"), main, "Returns itself when setting property");
assert.equal(rmain.getAttribute("itemprop"), "articleBody", "Can set property");
assert.equal(main.attr("itemprop"), "articleBody", "Can retrieve property");
main.attr("itemprop", null);
assert.equal(main.attr("itemprop"), undefined, "Can remove property");

// set attributes using a callback
test.innerHTML = '<div class="testtemp"></div><div class="testtemp"></div><div class="testtemp"></div>';
main = $(".testtemp");
assert.deepEqual(main.attr("data-test", function (i, el) {return "test-"+i;}), main, "Returns itself when setting attribute using callback");
main.each(function (i) {
if (this.getAttribute("data-test") !== "test-"+i) {
correct = false;
return false;
}

hooks.before(() => {
test.innerHTML = '<div class="testtemp"></div>';
});

QUnit.test("$.fn.attr", function (assert) {
var main = $(".testtemp"),
rmain = document.getElementsByClassName("testtemp")[0],
style = "padding-top: 10px;",
correct = true;

// set and get class
assert.deepEqual(main.attr("class", "testtemp testclass"), main, "Returns itself when setting class");
assert.equal(rmain.className, "testtemp testclass", "Can set class");
assert.equal(main.attr("class"), "testtemp testclass", "Can retrieve class");
main.attr("class", "testtemp");
assert.equal(main.attr("class"), "testtemp", "Can remove class");

// set and get style
assert.deepEqual(main.attr("style", style), main, "Returns itself when setting style");
assert.equal(rmain.style.cssText, style, "Can set style");
assert.equal(main.attr("style"), style, "Can retrieve style");

// set and get attribute
assert.deepEqual(main.attr("itemprop", "articleBody"), main, "Returns itself when setting property");
assert.equal(rmain.getAttribute("itemprop"), "articleBody", "Can set property");
assert.equal(main.attr("itemprop"), "articleBody", "Can retrieve property");
main.attr("itemprop", null);
assert.equal(main.attr("itemprop"), undefined, "Can remove property");

// set attributes using a callback
test.innerHTML = '<div class="testtemp"></div><div class="testtemp"></div><div class="testtemp"></div>';
main = $(".testtemp");
assert.deepEqual(main.attr("data-test", function (i, el) {return "test-"+i;}), main, "Returns itself when setting attribute using callback");
main.each(function (i) {
if (this.getAttribute("data-test") !== "test-"+i) {
correct = false;
return false;
}
});
assert.equal(correct, true, "Can set property with callback");

// reset
});
assert.equal(correct, true, "Can set property with callback");

// reset
test.innerHTML = "";
hooks.after(() => {
test.innerHTML = "";
});
});
71 changes: 36 additions & 35 deletions src/attributes/show-hide/test.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
import $ from "../../../dist/dabby.js";

QUnit.module("Attributes");
QUnit.module("Attributes", hooks => {

QUnit.test("$.fn.show", function (assert) {
var test = document.getElementsByClassName("test")[0],
obj;
test.innerHTML = '<div class="testtemp"><div style="display: none;"></div><div style="display: none;"></div><div style="display: none;"><div style="display: none;"></div></div><div style="display: none;"><div style="display: none;"></div></div></div>';
obj = $(".testtemp div");
QUnit.test("$.fn.show", function (assert) {
var test = document.getElementsByClassName("test")[0],
obj;
test.innerHTML = '<div class="testtemp"><div style="display: none;"></div><div style="display: none;"></div><div style="display: none;"><div style="display: none;"></div></div><div style="display: none;"><div style="display: none;"></div></div></div>';
obj = $(".testtemp div");

assert.deepEqual(obj.show(), obj, "Returns self on set");
let show = 0;
obj.get().forEach(item => {
show += item.style.display !== "none";
assert.deepEqual(obj.show(), obj, "Returns self on set");
let show = 0;
obj.get().forEach(item => {
show += item.style.display !== "none";
});
assert.equal(obj.length, show, "Showed the requested elements");
});
assert.equal(obj.length, show, "Showed the requested elements");
});

QUnit.test("$.fn.hide", function (assert) {
var test = document.getElementsByClassName("test")[0],
obj;
test.innerHTML = '<div class="testtemp"><div></div><div></div><div><div></div></div><div><div></div></div></div>';
obj = $(".testtemp div");
QUnit.test("$.fn.hide", function (assert) {
var test = document.getElementsByClassName("test")[0],
obj;
test.innerHTML = '<div class="testtemp"><div></div><div></div><div><div></div></div><div><div></div></div></div>';
obj = $(".testtemp div");

assert.deepEqual(obj.hide(), obj, "Returns self on set");
let hide = 0;
obj.get().forEach(item => {
hide += item.style.display === "none";
assert.deepEqual(obj.hide(), obj, "Returns self on set");
let hide = 0;
obj.get().forEach(item => {
hide += item.style.display === "none";
});
assert.equal(obj.length, hide, "Hid the requested elements");
});
assert.equal(obj.length, hide, "Hid the requested elements");
});

QUnit.test("$.fn.toggle", function (assert) {
var test = document.getElementsByClassName("test")[0],
obj;
test.innerHTML = '<div class="testtemp"><div style="display: none;"></div><div style="display: inline-block;"></div><div style="display: flex;"><div></div></div><div style="display: none;"><div style="display: none;"></div></div></div>';
obj = $(".testtemp div");
QUnit.test("$.fn.toggle", function (assert) {
var test = document.getElementsByClassName("test")[0],
obj;
test.innerHTML = '<div class="testtemp"><div style="display: none;"></div><div style="display: inline-block;"></div><div style="display: flex;"><div></div></div><div style="display: none;"><div style="display: none;"></div></div></div>';
obj = $(".testtemp div");

assert.deepEqual(obj.toggle(), obj, "Returns self on set");
let show = 0, hide = 0;
obj.get().forEach(item => {
hide += item.style.display === "none";
show += item.style.display !== "none";
assert.deepEqual(obj.toggle(), obj, "Returns self on set");
let show = 0, hide = 0;
obj.get().forEach(item => {
hide += item.style.display === "none";
show += item.style.display !== "none";
});
assert.equal(3, show, "Showed the requested elements");
assert.equal(3, hide, "Hid the requested elements");
});
assert.equal(3, show, "Showed the requested elements");
assert.equal(3, hide, "Hid the requested elements");
});
31 changes: 25 additions & 6 deletions src/internal/getval/test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import $ from "../../../dist/dabby.js";

import getVal from "./getval.js";

QUnit.module("Internal");
QUnit.module("Internal", hooks => {
const test = document.getElementsByClassName("test")[0];

hooks.before(() => {
test.innerHTML = '<div class="testtemp"></div><div class="testtemp2"></div><div class="testtemp3"></div>';
});

QUnit.test("getVal", assert => {
const obj = $(".test div");
assert.deepEqual(getVal(obj, "test"), ["test", "test", "test"], "Can pass-through a value");
assert.deepEqual(getVal(obj, function () {return $(this).attr("class");}), ["testtemp", "testtemp2", "testtemp3"], "Can use function as value");
assert.deepEqual(getVal(obj, function (i, current) {return current;}, obj => obj.className), ["testtemp", "testtemp2", "testtemp3"], "Can use function as value and return original value");

let clone = {foo: "bar", bar: "foo"};
const val = getVal(obj, clone);
val.map((item, i) => {
item.foo = "foo" + i;
return item;
});
assert.deepEqual(val, [{foo: "foo0", bar: "foo"}, {foo: "foo1", bar: "foo"}, {foo: "foo2", bar: "foo"}], "Objects are cloned onto each output");
assert.deepEqual(clone, {foo: "bar", bar: "foo"}, "Original object was not changed when object was copied to each val");
});

QUnit.test("getVal", function (assert) {
var obj = $(".test");
assert.deepEqual(getVal(obj, "test"), ["test"], "Can pass-through a value");
assert.deepEqual(getVal(obj, function (i) {return this === obj[0] && !i ? "test" : false;}), ["test"], "When passing function as value, variables and context is correct");
hooks.after(() => {
test.innerHTML = "";
});
});
3 changes: 1 addition & 2 deletions src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import "./manipulation/wrapall/test.js";
// traversal
import "./traversal/add/test.js";
import "./traversal/children/test.js";
//import "./traversal/closest/test.js";
import "./traversal/closest/test.js";
import "./traversal/eq/test.js";
import "./traversal/filter/test.js";
import "./traversal/find/test.js";
Expand All @@ -77,7 +77,6 @@ import "./traversal/siblings/test.js";
// utilities
import "./utils/each/test.js";
import "./utils/extend/test.js";
//import "./utils/isarray/test.js";
import "./utils/isfunction/test.js";
import "./utils/isplainobject/test.js";
import "./utils/iswindow/test.js";
Expand Down
2 changes: 1 addition & 1 deletion src/traversal/closest/closest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ $.fn.closest = function (selector, context) {
while (i--) {
parents = [];
node = this[i];
while (node) {
while (node && node.nodeType === Node.ELEMENT_NODE) {
parents.push(node);
node = node.parentNode;
}
Expand Down
18 changes: 18 additions & 0 deletions src/traversal/closest/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import $ from "../../../dist/dabby.js";

QUnit.module("Traversal", hooks => {
var test = document.getElementsByClassName("test")[0];

hooks.before(() => {
test.innerHTML = '<div class="testtemp"><div class="testtemp2"><div class="testtemp3">test</div></div></div>';
});

QUnit.test("$.fn.closest", function (assert) {
const obj = $(".testtemp3, .testtemp2, .testtemp");
assert.deepEqual(obj.closest(".test").get(), [test, test, test], "Can select parents until a particular node");
});

hooks.after(() => {
test.innerHTML = "";
});
});
13 changes: 9 additions & 4 deletions src/traversal/parents/parents.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ import filterNodes from "../../internal/filternodes/filternodes.js";
while (i--) {
parent = this[i].parentNode;
while (parent && parent.nodeType === Node.ELEMENT_NODE) {
if (until && filterNodes(parent, selector).length) {
break;
}
nodes.push(parent);
if (!all || (until && filterNodes(parent, selector).length)) {
if (!all) {
break;
} else {
parent = parent.parentNode;
}
parent = parent.parentNode;
}
}
return $(selector ? filterNodes(nodes, selector) : nodes);
if (!until) {
filter = selector;
}
return $(filter ? filterNodes(nodes, filter) : nodes);
}
});
Loading

0 comments on commit a1b202f

Please sign in to comment.