We help IT Professionals succeed at work.
Get Started

Recursive use of replaceWith behaving differently on IE9.0.8112.16421 than FireFox 7.0.1

CAS-IT
CAS-IT asked
on
513 Views
Last Modified: 2012-08-13
We've got a function that we point at a specific element, and it recurses through every child-element, with the intent of removing the tags which do not match our 'approved tag' list.  approved tags are passed in as an argument of type array of string.

In Firefox, it works perfectly.

In IE, it does not fail, but it drops out a lot of the content once it hits the first span.

Specific function is here...
function cleanHTML (theObject,allowedTags){
	for (i=$(theObject).find('*').length-1;i>=0;i--) {
		if ($.inArray($($(theObject).find('*')[i]).get()[0].tagName.toLowerCase(),allowedTags)==-1)
			{ $($(theObject).find('*')[i]).replaceWith($($(theObject).find('*')[i]).html());}
	}
}

Open in new window


Downloadable HTML file you can run to test the function out here...
cleanHTML3-EE.html

HTML Source of our test page is here...
<!DOCTYPE html>
<html>
<head>
	<title>cleanHTML Test v3.0 - Modified 11/3/2011</title>
	<meta charset="utf-8">
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
function cleanHTML (theObject,allowedTags){
	for (i=$(theObject).find('*').length-1;i>=0;i--) {
		if ($.inArray($($(theObject).find('*')[i]).get()[0].tagName.toLowerCase(),allowedTags)==-1)
			{ $($(theObject).find('*')[i]).replaceWith($($(theObject).find('*')[i]).html());}
	}
}
</script>

<div ID="testcontainer">
	<div style="border:1px solid blue;" class="slJobDuties">
		<div style = "border:1px solid red;" class="ExternalClass936C2C07EC22467780317A758292E763">
			<div style="url:'http://www.udel.edu'">
			<p StYle='font-weight:bold;'>
				Text <a href=".">Outside</a> <i>Spans</i>
				<span class="bob">
				Test Inside one span, outside of others
				<span style="border:1px solid black;">
				<span style="border:1px solid orange;" class="bob">
				<span class="bob" style="border:1px solid orange;">
					College <i><a href=".">Accounting</a></i> <span><b>Style</b></span>
					<br />
					More stuff
				</span>
				</span>
				</span>
			</p>
			</div>
		</div>
	</div>
</div>

<script>
console.log($("#testcontainer").html()); //Handy Debugging line - show me the HTML before I do stuff
cleanHTML($("#testcontainer"),["a","br","p"]);
console.log($("#testcontainer").html()); //Handy Debugging line - show me the HTML AFTER I do stuff
</script>

</body>
</html>

Open in new window

Comment
Watch Question
Owner (Aidellio)
CERTIFIED EXPERT
Most Valuable Expert 2015
Commented:
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE