﻿//-----------------------------------------------------------------------------
// XHighlight
//
// Copyright 2005-2010 - Xcential Group LLC.
//
//-----------------------------------------------------------------------------

//=============================================================================
// Constructor

function XHighlight()
{

   //--------------------------------------------------------------------------
   // Private Interface

   //--------------------------------------------------------------------------
   // Privileged Interface

   //--------------------------------------------------------------------------
   // Initialization

}

XHighlight.objectClass = "XHighlight";

//=============================================================================
// Static Interface

//-----------------------------------------------------------------------------

XHighlight.clearHighlighting = function(
   inputHTML
)
{

   var outputHTML = "";
   while (XMatch(inputHTML, /<span\s+class=["']?Inline\s+Highlight['"]?\s*>([^<]*)<\/span>/im))
   {
      outputHTML += XMatch.leftContext + XMatch.matches[1];
      inputHTML = XMatch.rightContext;
   }
   outputHTML += inputHTML;

   return outputHTML;
}

//-----------------------------------------------------------------------------

XHighlight.highlightHTML = function(
   inputHTML,
   words
)
{
   words = (words == null) ? new Array() : words;

   //--------------------------------------------------------------------------

   function highlightWord(
      inputHTML,
      word,
      prefix
   )
   {
      prefix = (prefix == null) ? "" : prefix + ":";
      
      var outputHTML = "";
      while (XMatch(inputHTML, "(" + word.replace(/\+/g, "\\s+") + ")", "i"))
      {
         outputHTML += XMatch.leftContext;
         var wordText = XMatch.matches[1];
         inputHTML = XMatch.rightContext;
         if (outputHTML.length > 0 && !(/[>\s\(\[\{\'\"\-\$\u2018\u201C\u00A0]$/).test(outputHTML) && !(/&nbsp;$/).test(outputHTML))
            outputHTML += wordText;
         else if (inputHTML.length > 0 && !(/^[<\s\)\]\}\'\"\-\,\.\;\:\!\?\%\u2019\u201D\u00A0]/).test(inputHTML) && !(/^&nbsp;/).test(outputHTML))
            outputHTML += wordText;
         else if (outputHTML.lastIndexOf(">") < outputHTML.lastIndexOf("<"))
            outputHTML += wordText;
         else if (outputHTML.lastIndexOf(" class=\"DataField\"") > outputHTML.lastIndexOf("<"))
            outputHTML += wordText;
         else if (outputHTML.lastIndexOf("<" + prefix + "script ") > outputHTML.lastIndexOf("</" + prefix + "script>"))
            outputHTML += wordText;
         else
            outputHTML += "<" + prefix + "span class=\"Inline Highlight\">" + wordText + "</" + prefix + "span>";
      }
      outputHTML += inputHTML;

      return outputHTML;
   }

   //--------------------------------------------------------------------------

   outputHTML = XHighlight.clearHighlighting(inputHTML);

   for (var i=0; i<words.length; i++)
   {
      var word = words[i];
      outputHTML = highlightWord(outputHTML, word);
   }

   return outputHTML;
}

//=============================================================================



