me hateses HTML empty tags

2008-08-05 @ 16:11#

!dang-it!

been wrestling with a simple markup editing window in a browser client (javascript) this week and finally narrowed the issue down to mis-handling of 'empty tags' when crossing between HTML and XML/XHTML. i *hate* that tags like br, link, img, etc. leaked into the spec. they have no required *close-tag*!#$?!

anyway, i finally worked up a single regexp pattern that works in javascript to clean up the empty tags before i send them to the XML-compliant data store:

function fixEmptyTags(data)
{
  var tag_list = ['br','hr','meta','link','base','img','embed','param','area','frame','col','input','basefont','bgsound','keygen','sound','spacer','wbr'];
  var reg_match = '<%tag%(\\s+([^>]*))?>';
  var reg_replace = '<%tag% $2 />';
  var i,re;
  
  for(i=0;i<tag_list.length;i++)
  {
    re = new RegExp(reg_match.replace('%tag%',tag_list[i]),'img');
    data = data.replace(re,reg_replace.replace('%tag%',tag_list[i]));
  }
  return data;
};

now i just need to clean up some UI issues and i'll be rid of this pest!

code