Created
December 16, 2010 12:48
-
-
Save think49/743362 to your computer and use it in GitHub Desktop.
removeNodesAll.js: 対象のノードリストを全て削除する (deep === true で子要素まで削除する)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* removeNodesAll.js | |
* | |
* @version 1.1.1 | |
* @author think49 | |
*/ | |
/** | |
* Remove NodeList. | |
* @function | |
* @param {Object} nodeList [object NodeList] | |
* @param {Boolean} deep childNodes flag (true = remove element.childNodes, false = not remove element.childNodes). | |
*/ | |
function removeNodesAll (nodeList /*, deep*/) { | |
var targetNode, parentNode, df, childNodes, i, l, j; | |
if (!nodeList) { | |
return; | |
} | |
if (arguments[1]) { // If deep is true | |
for (i = nodeList.length - 1; i > -1; i--) { | |
targetNode = nodeList[i]; | |
targetNode.parentNode.removeChild(targetNode); | |
} | |
} else { | |
df = nodeList[0].ownerDocument.createDocumentFragment(); | |
for (i = nodeList.length - 1; i > -1; i--) { | |
targetNode = nodeList[i]; | |
parentNode = targetNode.parentNode; | |
childNodes = targetNode.childNodes; | |
j = childNodes.length - 1; | |
if (j > -1) { | |
for (;j > -1; j--) { | |
df.appendChild(childNodes[j]); | |
} | |
parentNode.replaceChild(df, targetNode); | |
} | |
} | |
} | |
} | |
/** | |
* Remove tag string. | |
* @function | |
* @param {String} string target String. | |
* @param {String} tagName element.tagName | |
* @param {Boolean} deep contents flag (true = remove tag contents, false = not remove tag contents) | |
* @returns {String} removed string. | |
*/ | |
function removeTagsAll (string, tagName /*, deep*/) { | |
var STag, ETag, reg, length; | |
string = string + ''; | |
tagName = tagName + ''; | |
STag = '\u003C' + tagName + '\u003E'; | |
ETag = '\u003C/' + tagName + '\u003E'; | |
reg = new RegExp([STag, ETag, '[\\s\\S]'].join('|'), 'g'); | |
length = 0; | |
if (arguments[2]) { | |
string = string.replace(reg, function (token) { | |
switch (token) { | |
case STag: | |
length++; | |
return ''; | |
break; | |
case ETag: | |
if (length < 1) { | |
throw new Error('not well-formed ETag (' + token + ')'); | |
} else { | |
length--; | |
} | |
return ''; | |
break; | |
default: | |
return length > 0 ? '' : token; | |
} | |
}); | |
} else { | |
string = string.replace(reg, function (token) { | |
switch (token) { | |
case STag: | |
length++; | |
return ''; | |
break; | |
case ETag: | |
if (length < 1) { | |
throw new Error('not well-formed ETag (' + token + ')'); | |
} else { | |
length--; | |
} | |
return ''; | |
break; | |
default: | |
return token; | |
} | |
}); | |
} | |
if (length > 0) { | |
throw new Error('not well-formed STag (' + STag + ')'); | |
} | |
return string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment