// ==UserScript==
// @name Fix font tag underline
// @author Lee Harvey 
// @namespace http://my.opera.com/Lee_Harvey/ 
// @version 1.1
// @description  Fixes the underline colour of links in pages that rely
//			on font tags.
// @ujs:category general: fixes
// @ujs:published 2005-08-29 09:12
// @ujs:modified 2005-09-19 09:19
// @ujs:documentation http://userjs.org/scripts/general/fixes/fix-font-tag-underline 
// @ujs:download http://userjs.org/scripts/download/general/fixes/fix-font-tag-underline.js
// ==/UserScript==


/* 
 * This script is granted to the Public Domain.
 */

document.addEventListener("load",function() {

	//only apply to pages in quirks mode that actually use font tags
	if ( (document.compatMode != 'CSS1Compat') && document.getElementsByTagName('font').length ) {
		for (var i = 0, link; link = document.links[i]; i++) {
			//don't modify links if they already have a specific color
			if (link.style.color) { continue; }
			//get a list of all the font tags inside the link
			var fontTags = link.getElementsByTagName('font');
			var fontColorRegex = /<font[^>]*color=[\w\W]*$/i;
			if (!fontTags.length || !link.innerHTML.match(fontColorRegex,'')) { continue; }
			//do not apply to links that have text before the relevant font tag (cannot do anything about text after it)
			if(link.innerHTML.replace(fontColorRegex,'').replace(/<[^>]*>/g,'').match(/\S/)) { continue; }
			for (var n = 0, m, oCol; m = fontTags[n]; n++) {
				//only use the first font tag inside the link that has a color attribute
				if (oCol = m.getAttribute('color')) {
					//incorrectly reverse-inherit the color into the parent
					link.style.color = oCol;
					break;
				}
			}
		}
	}

},false);
