home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2006 April / DPPRO0406DVD.ISO / Essentials / Programming / Eclipse SDK / eclipse-SDK-3.1.1-win32.exe / eclipse / plugins / org.eclipse.help.webapp_3.1.0 / advanced / highlight.js < prev    next >
Encoding:
JavaScript  |  2005-09-29  |  3.1 KB  |  97 lines

  1. /*******************************************************************************
  2.  * Copyright (c) 2000, 2005 IBM Corporation and others.
  3.  * All rights reserved. This program and the accompanying materials 
  4.  * are made available under the terms of the Eclipse Public License v1.0
  5.  * which accompanies this distribution, and is available at
  6.  * http://www.eclipse.org/legal/epl-v10.html
  7.  * 
  8.  * Contributors:
  9.  *     IBM Corporation - initial API and implementation
  10.  *******************************************************************************/
  11.  
  12. var isSafari = (navigator.userAgent.indexOf('Safari/') != -1)
  13.             || (navigator.userAgent.indexOf('AppleWebKit/') != -1);
  14. var highlighted=false;
  15. var startTime;
  16. var MAX_DURATION=3000;
  17. onload=highlight;
  18. document.onreadystatechange=highlight;
  19. function highlight(){
  20.     if(highlighted){
  21.         return;
  22.     }
  23.     highlighted=true;
  24.     if (!document.body) return;
  25.     if(document.body.innerHTML.length < 50000){
  26.         for(i=0; i<keywords.length; i++){
  27.             word=keywords[i].toLowerCase();
  28.             highlightWordInNode(word, document.body);
  29.         }
  30.     }else{
  31.         startTime=new Date().getTime();
  32.         for(i=0; i<keywords.length; i++){
  33.             word=keywords[i].toLowerCase();
  34.             highlightWordInNodeTimed(word, document.body);
  35.             if(new Date().getTime()>startTime+MAX_DURATION) return;
  36.         }
  37.     }
  38. }
  39. function highlightWordInNode(aWord, aNode){
  40.     if (aNode.nodeType == 1){
  41.         var children = aNode.childNodes;
  42.         for(var i=0; i < children.length; i++) {
  43.             highlightWordInNode(aWord, children[i]);
  44.         }
  45.     }
  46.     else if(aNode.nodeType==3){
  47.         highlightWordInText(aWord, aNode);
  48.     }
  49.  
  50. }
  51. function highlightWordInNodeTimed(aWord, aNode){
  52.     if (aNode.nodeType == 1){
  53.         var children = aNode.childNodes;
  54.         for(var i=0; i < children.length; i++) {
  55.             highlightWordInNodeTimed(aWord, children[i]);
  56.             if(new Date().getTime()>startTime+MAX_DURATION) return;
  57.         }
  58.     }
  59.     else if(aNode.nodeType==3){
  60.         highlightWordInText(aWord, aNode);
  61.     }
  62.  
  63. }
  64. function highlightWordInText(aWord, textNode){
  65.     allText=new String(textNode.data);
  66.     allTextLowerCase=allText.toLowerCase();
  67.     index=allTextLowerCase.indexOf(aWord);
  68.     if(index>=0){
  69.         // create a node to replace the textNode so we end up
  70.         // not changing number of children of textNode.parent
  71.         replacementNode=document.createElement("span");
  72.         textNode.parentNode.insertBefore(replacementNode, textNode);
  73.         while(index>=0){
  74.             before=allText.substring(0,index);
  75.             newBefore=document.createTextNode(before);
  76.             replacementNode.appendChild(newBefore);
  77.             spanNode=document.createElement("span");
  78.             if(isSafari){
  79.                 spanNode.style.color="#000000";
  80.                 spanNode.style.background="#B5D5FF";
  81.             }else{
  82.                 spanNode.style.background="Highlight";
  83.                 spanNode.style.color="HighlightText";
  84.             }
  85.             replacementNode.appendChild(spanNode);
  86.             boldText=document.createTextNode(allText.substring(index,index+aWord.length));
  87.             spanNode.appendChild(boldText);
  88.             allText=allText.substring(index+aWord.length);
  89.             allTextLowerCase=allText.toLowerCase();
  90.             index=allTextLowerCase.indexOf(aWord);
  91.         }
  92.         newAfter=document.createTextNode(allText);
  93.         replacementNode.appendChild(newAfter);
  94.         textNode.parentNode.removeChild(textNode);
  95.     }
  96. }
  97.