HTML and CSS Reference
In-Depth Information
element.className = cName.replace(/^\s+
|
\s+
$
/g, "");
}
}
function removeClassName(element, cName) {
var r = new RegExp("(^
|
\\s)" + cName + "(\\s
|
$
)");
if (element) {
cName = element.className.replace(r, " ");
element.className = cName.replace(/^\s+
|
\s+
$
/g, "");
}
}
dom.addClassName = addClassName;
dom.removeClassName = removeClassName;
}());
These two methods require the
tddjs
object and its
namespace
method
from Chapter 6,
Applied Functions and Closures.
To code along with this example,
set up a simple JsTestDriver project as described in Chapter 3,
Tools of the Trade,
and
save the
tddjs
object and its
namespace
method along with the above helpers in
lib/tdd.js
. Also save the
Object.create
implementation from Chapter 7,
Objects and Prototypal Inheritance,
in
lib/object.js
.
9.5.2 The
tabController
Object
Listing 9.5 shows the first test case, which covers the
tabController
object's
create method. It accepts a container element for the tab controller. It tests its
markup requirements and throws an exception if the container is not an element
(determined by checking for the properties it's going to use). If the element is deemed
sufficient, the
tabController
object is created and a class name is appended to
the element, allowing CSS to style the tabs as, well, tabs. Note how each of the tests
test a single behavior. This makes for quick feedback loops and reduces the scope
we need to concentrate on at any given time.
The
create
method is going to add an event handler to the element as well, but
we will cheat a little in this example. Event handlers will be discussed in Chapter 10,
Feature Detection,
and testing them will be covered through the example project in
Chapter 15,
TDD and DOM Manipulation: The Chat Client.