Hello everyone,
I ran into an issue that I could use a little help with. I need to search a large string, match certain parts of it, and replace a certain part of the certain part. The entire string is made up of spans, tables, and images, to build a tree. The string is what's returned from the xmlParser. What I need to do is like this:

<span id="node1" style="blah:10px" />

There are a few of these buried within the rest of the structure. I need to search the structure, and change the above line to this:

<span id="node1" style="blah:10px"[B]></span>[/B]

The reason for this, is when i do treePanel.innerHTML = myBigString;
in Firefox, these spans become nested because the '/' is stripped out. (I verified this by passing in a modified version where I manually changed the /> to ></span> and it worked). So what I need, is to come up with the regex to find this. I think I can get what I need to match, but I'm unsure how to go about replacing. Here's what I have to match:

myRegexp = /<span.*(\/>)/gi;

I believe that will start matching when it finds <span and then stop when it encounters /> . The parens will save the ordered variable as $1. How do I replace that with ></span>?

Thank you,
jmasta

Maybe something like [untested]:

var reg = /(<span[^/>]+)\/>/gi;
var str = "<span id='d'/><div id='3'>d</div>" +
          "<span id='j' style='blah: 10px'/>";
var a = str.replace(reg, "$1><\/span>");
alert(a);

But this would mean looking away from the actual problem; the problem here is that SPAN HTML tags need to have a ending tag. What is xmlParser here? How come xmlParser returns an invalid span tag; can't it be fixed to return proper spans?

And BTW, this isn't a Firefox problem, it is actually doing what it should do. Since SPAN tags need to have content, the /> is ignored and <span id="something" /> is treated as the start of a SPAN tag.

IMO, the solution you are seeking seems more like a hack.

Thank you for your input, ~S.O.S~. It was going to be a hack, yes. It's an internal web-app that was written for IE6, and now they wish to have it work in IE7 and Firefox. The two parsers used here are xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); and xmlDoc=new DOMParser(); . According to W3Schools, this is the way to handle parsing for IE/FF. So if these parsers handle things the same, then it's the responseText coming into them that is slightly different. I'm going with your suggestion of finding the root of the problem and trying to fix it there, instead of implementing a regex hack. I'll keep this thread updated with what I find out.

Thanks again,

Update: I checked the response.responseText coming into the w parsers. These are the same in IE and FF. They both have the correct ></span> ending. For some reason, the DOMParser changes that into /> . Then, innerHTML correctly strips the / off because spans need to have content.

So the root of the issue is the code surrounding the DOMParser. I'll look into this some more.

Here's what I have discovered. The content for both Internet Explorer and Firefox are the same up until:

treeContent = spanNodes.item(0).xml || (new XMLSerializer()).serializeToString(spanNodes.item(0));

Following this, treeContent has stripped out the </span> and changed it to /> . I'm now looking into what the XMLSerializer does and trying to discover why it works differently than .xml. Any ideas?
Thanks,

I need a bit more background on the requirement/problem here as in what is the source of the XML you are talking about? Is is shipped over the network using an async call in the form of AJAX or read from an external file? Also posting some sample code which others can hack on and reproduce your problem might just help in faster resolution since "works differently" is kinda difficult to pinpoint given there is no context established in the form of a code snippet.

Also, given that XMLSerializer is specific to Gecko based browsers [not a standard], reading the API or asking in the Mozilla forums seems to be your best bet.

Hello again,
~s.o.s~ I implemented your solution for the regex hack for now, I was told to go this route so the project can move forward until I can research the issue with the serializer. Thanks for your help and input.

I'll try and post some of the content and explain more in depth when I get a chance.

If anyone else has ever run into issues with the serializer, i'd love to hear your issues or solutions.

Thanks,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.