Skip to content

Commit

Permalink
Dom & Carousel: Minor post-review changes and tidy-ups.
Browse files Browse the repository at this point in the history
  • Loading branch information
Frances Berriman committed Sep 25, 2009
1 parent 93b9eb9 commit bcb94ed
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 42 deletions.
63 changes: 28 additions & 35 deletions src/dom/dom.js
Expand Up @@ -206,22 +206,16 @@
/**
@name glow.dom-getFirstChildElm
@private
@description
@param {Element}
@description Returns the first leaf of a NodeList
@param {NodeList}
*/
function getFirstChildElm(parent) {
var i=0,
child;

for (child = parent.firstChild; child; child = child.nextSibling) {
function getFirstChildElm(parent) {
for (var child = parent.firstChild; child; child = child.nextSibling) {
if (child.nodeType == 1) {
return child;
}

}

return null;

}
}
return null;
}

/*
Expand Down Expand Up @@ -2134,22 +2128,23 @@
/**
@name glow.dom.NodeList#wrap
@function
@description Wraps the given nodeList with the specified element(s).
The given nodelist items will always be placed in the first child node that contains no further element nodes.
Each item in a given NodeList will be wrapped individually.
@description Wraps the given NodeList with the specified element(s).
The given NodeList items will always be placed in the first child node that contains no further element nodes.
Each item in a given NodeList will be wrapped individually.
@returns {glow.dom.NodeList}
Returns the wrapped NodeList
Returns the NodeList with new wrapper parents
@example
// wrap the given element
glow.dom.get("p").wrap("<div></div>");
<div><p></p></div>
// <div><p></p></div>
*/
wrap: function(wrapper) {
var i = 0,
length = this.length,
var length = this.length,
childElm,
parent,
wrappingNodes;
Expand All @@ -2161,7 +2156,7 @@
wrappingNodes = r.get(wrapper);
}

while (i < length) {
for (i=0; i < length; i++) {
parent = wrappingNodes[0];

while (parent) {
Expand All @@ -2178,14 +2173,13 @@
if (this[i].parentNode) {
wrappingNodes.insertBefore(this[i]);
}

// If wrapping multiple nodes, we need to take a clean copy of the wrapping nodes
if (i != length-1) {
wrappingNodes = wrappingNodes.clone();
}

parent.appendChild(this[i]);

i++;

}

return this;
Expand All @@ -2194,36 +2188,35 @@
/**
@name glow.dom.NodeList#unwrap
@function
@description Removes the first parent of the supplied NodeList
@description Removes the first unique parent for each item of a supplied NodeList
@returns {glow.dom.NodeList}
Returns the unwrapped NodeList
@example
// wrap the given element
// Before: <div><div><p></p></div></div>
// wrap the given element
glow.dom.get("p").unwrap();
// After: <div><p></p></div>
*/
unwrap: function() {
var ret = [],
i = 0,
parent,
toRemove,
var toRemove,
nodesToRemove = this.parent(),
length = nodesToRemove.length;
while (i < length) {

for (i=0; i < length; i++) {
toRemove = nodesToRemove.slice(i, i+1);
// if the item we're removing has no new parent (i.e. is not in document), then we just remove the child and destroy the old parent
if (!toRemove[0].parentNode){
toRemove.children().remove();
toRemove.destroy();
}
else {
toRemove.children().insertBefore(toRemove);
toRemove.destroy();
}

i++
}

}
return this;
},
Expand Down
8 changes: 4 additions & 4 deletions src/widgets/carousel/carousel.js
Expand Up @@ -708,10 +708,10 @@
if (!canGo.apply(this, ["prev"])) this._navPrev.addClass("carousel-prev-disabled");
else if (!canGo.apply(this, [])) this._navNext.addClass("carousel-next-disabled");
}
// need to add back the navigation events on arrows if pageNav is true
if (this._opts.pageNav) {
addMouseNavEvents.call(this);
}
// need to add back the navigation events on arrows if pageNav is true
if (this._opts.pageNav) {
addMouseNavEvents.call(this);
}

}

Expand Down
32 changes: 29 additions & 3 deletions test/glow/dom/dom.js
Expand Up @@ -1791,7 +1791,7 @@ t.test("glow.dom.NodeList#wrap", function () {
});

t.test("glow.dom.NodeList#unwrap", function () {
t.expect(4);
t.expect(6);

var testDoc = glow.dom.create("<div id='giftwrap'><div id='gift'><span>Shiny toys</span></div></div>").appendTo(document.body);
var expectedNodes = testDoc.children();
Expand All @@ -1805,7 +1805,9 @@ t.test("glow.dom.NodeList#unwrap", function () {
testDoc.remove();


var testDoc = glow.dom.create("<div id='box'><div class='tissuepaper'><div class='tissuepaper' id='inner'><span>Shiny toys</span></div></div></div>").appendTo(document.body);
var testDoc = glow.dom.create("<div id='box'>"
+ "<div class='tissuepaper'><div class='tissuepaper' id='inner'><span>Shiny toys</span>"
+ "</div></div></div>").appendTo(document.body);
var toUnwrap = glow.dom.get(".tissuepaper")

var newDoc = toUnwrap.unwrap();
Expand All @@ -1816,7 +1818,9 @@ t.test("glow.dom.NodeList#unwrap", function () {
testDoc.remove();


var testDoc = glow.dom.create("<div id='box'><div class='tissuepaper'><div class='tissuepaper' id='inner'><span>Shiny toys</span></div></div></div>");
var testDoc = glow.dom.create("<div id='box'>"
+ "<div class='tissuepaper'><div class='tissuepaper' id='inner'><span>Shiny toys</span>"
+ "</div></div></div>");


var expectedNodes = glow.dom.get("#inner");
Expand All @@ -1830,6 +1834,28 @@ t.test("glow.dom.NodeList#unwrap", function () {

newDoc.remove();
testDoc.remove();


var testDoc = glow.dom.create("<div id='gift'><span>Shiny toys</span></div>");
var expectedNodes = testDoc.children();
var newDoc = glow.dom.get("#gift").unwrap();

t.ok(glow.dom.get("#gift"), "Orphaned unwrap item returns");

newDoc.remove();
testDoc.remove();

var testDoc = glow.dom.create("<div id='giftwrap'><div class='gift'><span>Shiny toys</span></div><div class='gift'><span>Shiny toys</span></div></div>").appendTo(document.body);
var toUnwrap = glow.dom.get(".gift")

var newDoc = toUnwrap.unwrap();

t.isSet(newDoc, toUnwrap, "Removes only one parent for multiple to-unwrap nodes that share same parent");

newDoc.remove();
testDoc.remove();


});


Expand Down

0 comments on commit bcb94ed

Please sign in to comment.