Fixed README formatting, added start of dom.children method

This commit is contained in:
Timothy Warren 2011-07-27 12:06:56 -04:00
parent 19de9f1409
commit 980410b901
7 changed files with 258 additions and 195 deletions

View File

@ -2,7 +2,7 @@
Kis JS Keep It Simple JS Library
Copyright Timothy J. Warren
License Public Domain
Version 0.2.0
Version 0.3.0
*/
(function (){
@ -180,13 +180,6 @@
// --------------------------------------------------------------------------
/**
* Dom manipulation object
*
*/
(function (){
var d;
/*
* classList.js: Cross-browser full element.classList implementation.
* 2011-06-15
@ -343,6 +336,18 @@
// --------------------------------------------------------------------------
/**
* Dom manipulation object
*
*/
(function (){
var d, tag_reg, id_reg, class_reg;
tag_reg = /^([\w\-]+)$/;
id_reg = /#([\w\-]+$)/;
class_reg = /\.([\w\-]+)$/;
//Private function for getting/setting attributes
function _attr(sel, name, value)
{
@ -462,6 +467,64 @@
console.log("Property " + prop + " nor an equivalent seems to exist");
}
function _sel_filter(filter, curr_sel)
{
var i,
len = curr_sel.length,
matches = [];
if(typeof filter !== "string")
{
return filter;
}
//Filter by tag
if(filter.match(tag_reg))
{
for(i=0;i<len;i++)
{
if(curr_sell[i].tagName.toLowerCase() == filter.toLowerCase())
{
matches.push(curr_sel[i]);
}
}
}
else if(filter.match(class_reg))
{
for(i=0;i<len;i++)
{
if(curr_sel[i].classList.contains(filter))
{
matches.push(curr_sel[i]);
}
}
}
else if(filter.match(id_reg))
{
return document.getElementById(filter);
}
else
{
console.log(filter+" is not a valid filter");
}
return (matches.length === 1) ? matches[0] : matches;
}
function _set_sel(sel)
{
for(var i in $_)
{
if(typeof $_[i] === "object" || typeof $_[i] === "function")
{
$_[i].el = sel;
}
}
return $_;
}
// --------------------------------------------------------------------------
d = {
@ -540,6 +603,7 @@
},
css: function (prop, val)
{
//Return the current value if a value is not set
if(typeof val === "undefined")
{
return _css(this.el, prop);
@ -548,6 +612,25 @@
$_.each(function (e){
_css(e, prop, val);
});
},
children: function(filter)
{
var sel;
if(typeof sel === "undefined")
{
sel = this.el.children;
}
else
{
sel = _sel_filter(filter, this.el.children);
}
//Update the $_ object to reflect the new selector
$_ = _set_sel(sel);
//Return the $_ object for chaining
return $_;
}
};
@ -786,7 +869,7 @@
(function (){
// Property name for expandos on DOM objects
var kis_expando = "KIS_0_2_0";
var kis_expando = "KIS_0_3_0";
var attach, remove, add_remove, e;

View File

@ -334,11 +334,13 @@ if (typeof document !== "undefined" && !("classList" in document.createElement("
{
for(var i in $_)
{
if(typeof $_[i] === "object")
if(typeof $_[i] === "object" || typeof $_[i] === "function")
{
$_[i].el = sel;
}
}
return $_;
}
// --------------------------------------------------------------------------
@ -443,10 +445,10 @@ if (typeof document !== "undefined" && !("classList" in document.createElement("
}
//Update the $_ object to reflect the new selector
_set_sel(sel);
$_ = _set_sel(sel);
//Return the $_ object for chaining
return $_
return $_;
}
};

View File

@ -2,7 +2,7 @@
Kis JS Keep It Simple JS Library
Copyright Timothy J. Warren
License Public Domain
Version 0.2.0
Version 0.3.0
*/
(function (){

View File

@ -6,7 +6,7 @@
(function (){
// Property name for expandos on DOM objects
var kis_expando = "KIS_0_2_0";
var kis_expando = "KIS_0_3_0";
var attach, remove, add_remove, e;

View File

@ -1,30 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<link rel="stylesheet" href="qunit/qunit.css" type="text/css" />
<title>Kis-js test app</title>
</head>
<body>
<h1 id="qunit-header">Kis-js Test Suite</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">
test markup, will be hidden
<span id="testSpan"></span>
</div>
<section hidden="hidden">
<article id="r14">
This is important text!
</article>
<aside id="classChild">
<div class="child"></div>
</aside>
</section>
<script src="../kis-all.js"></script>
<script src="qunit/qunit.js"></script>
<script src="./tests.js"></script>
</body>
</html>

View File

@ -166,4 +166,12 @@
equal($test.dom.css("display"), "block", "Getting CSS");
});
test("Children", function(){
var $test = $_("section");
var ele = $test.el;
equal($test.dom.children().el, ele.children, "Returns children without parameters");
equal($test.dom.children('#r14').el, document.getElementById('r14'), "Finds id");
});
}());