Highlighting

Highlighting text in HTML block

Page 1 of 1

4 Replies - 11478 Views - Last Post: 13 September 2002 - 04:13 PM Rate Topic: -----

#1 Resonance   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 95
  • Joined: 08-February 02

Highlighting

Posted 12 September 2002 - 09:36 AM

Anyone ever try to code a routine that highlights words ONLY in the text of an HTML block? I've made a UDF that can highlight words in a pure textblock with no HTML, but have a problem when there's a link, or any other HTML tag. Mainly the idea is NOT to highlight anything within the <> characters. I'm not having any luck though with coming up with a good regexp, or any other good ideas for that matter. I have tried several things.I know I can strip out the HTML, but I want to maintain it. Anyone have any ideas? Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Highlighting

#2 supernova333   User is offline

  • D.I.C Addict

Reputation: 4
  • View blog
  • Posts: 590
  • Joined: 12-March 02

Re: Highlighting

Posted 12 September 2002 - 01:21 PM

Here are a few custom tags on devex you can try, I'm not sure if any of them offer an option to protect tags or not:

CF_Highlight

CF_Highlight (a different one, this one looks pretty nice)

CF_Highlight2


Is there a lot of html in your string, or just a link here and there? What about searching the text for the < symbol and if it isn't found, run your UDF on the whole block. If it is found, Run your UDF on the string up to that position, then search for > and < and run your UDF from position of > to the next position of <. I'm not good with regular expressions, but you could use FindNoCase(yourString, "<") to get the position of the brackets in the string.

Let me know if that made any sense. :) Can I see what you currently have in your UDF?
Was This Post Helpful? 0
  • +
  • -

#3 Resonance   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 95
  • Joined: 08-February 02

Re: Highlighting

Posted 12 September 2002 - 07:40 PM

You have no idea all the things I've tried (every udf, custom tag, you name it). All those solutions handle just text. Your solution though, I have not tried - seems too involved though... I think there might be a better way. Think you can code it your solution?

See, the problem with highlighting is, if for example, you make a udf to highlight 1 word. Then you use that udf in a loop where your looping through a list of words, and calling the udf to highlight some text. Now... if the word on your list, happens to be something like "span" or "background" or something you'd use to highlight, the first iteration would go just fine... then the second iteration, it'd highlight the word span again, but the word span in the tag ... so it would try to highlight <span ...> which obviously you don't want or it will ruin the highlight.

The same case goes for any html. Anyways, this is what I have:

<CFSCRIPT>
     function highlight(Data, WordsToHighlight) 
     { 
          /*
               highlight(Data, ListOfWordsToHightLight)                
                    - Where default delimiter is a space. Returns Data, but with highlighted
                      text. Argument delimiters is optional.
                      
               usage:
               
                    Highlight(Stuff, "the,academic,cash,b,for")
                    Highlight(Stuff, "the,academic,cash,b,for")
                    
                    Where Stuff is a textblock - no HTML in it.
          */     

          /* This is so it highlights the first word. Each word in a textblock must be surrounded by
              [^A-Za-z] in order to highlight whole words, and not subwords. */
          Data = " " & Data & " ";
          
           /* Filtering Process:
               These three REReplace statements take out all malicious chars that would break the
               search part of a regular expression. Takes unwanted chars and only lets the chars:
               [^-a-zA-Z0-9&] in. The [:space:] class is used to remove extra whitespace, leaving just 1 space. 
               The     third REReplace sets the search part of a regular expression. */
          ListOfWords = REReplace(WordsToHighlight, "[^-a-zA-Z0-9&']", " ", "ALL");
          ListOfWords = REReplace(ListOfWords, "[[:space:]]+", " ", "ALL");
          ListOfWords = Trim(ListOfWords);

          // This portion of code is to take out single word characters.
          ListOfWords_Array = ArrayNew(1);
          ListOfWords_Array = ListToArray(ListOfWords, " ");

          for (ax=1; ax LE ArrayLen(ListOfWords_Array); ax=ax+1) {
               // Instead of deleting single word chars, just replace it with empty string.
               if(Len(ListOfWords_Array[ax]) EQ 1) {
                    ListOfWords_Array[ax] = "";
               }          
          }          
          ListOfWords = ArrayToList(ListOfWords_Array, " ");
          // Replace multiple white space with just 1 white space.
          ListOfWords = REReplace(ListOfWords, "[[:space:]]+", " ", "ALL");                    
          ListOfWords = Trim(ListOfWords);

          /* Check if there are any chars or words to search for, do this before creating the search
             portion of the regular expression. */
          if (ListOfWords NEQ "") {
               // Creating the search portion of the regexp.
               ListOfWords = "(" & REReplace(ListOfWords, " ", "|", "ALL") & ")";
               return REReplaceNoCase(Data, ListOfWords, "<b span style=""background-color: yellow;""></b>", "ALL");          
          }
          else {
               return data;
          }               
     }
</CFSCRIPT>

<CFSAVECONTENT VARIABLE="Stuff">
I would really like to continue working on this, because I see myself being able to 
implement this in the future. But, I just got swamped, so I'll post the source code 
for you to pick through... no laughing, it's really messy *sorry*

PS If I got it to work with the href tag, I'm sure it could be applied to th span tag also, good luck !
</CFSAVECONTENT>

<CFOUTPUT>#Highlight(Stuff, "really like no be span")#</CFOUTPUT> 

Was This Post Helpful? 0
  • +
  • -

#4 supernova333   User is offline

  • D.I.C Addict

Reputation: 4
  • View blog
  • Posts: 590
  • Joined: 12-March 02

Re: Highlighting

Posted 13 September 2002 - 12:31 AM

I decided to grab the first cf_highlight custom tag above (which worked fine with plain text) and modify it so it can accept a mix of html and text. I'm sure you can improve on this, but it's a start. We could play our own little game of ColdFusion tennis :)

Here's an example: http://www.infidesig...oards/highlight

Here's the custom tag code (highlight.cfm), Sorry, I didn't take the time to add comments:
<CFSET String=Attributes.string>
<CFSET Substring=Attributes.Substring>
<CFIF IsDefined("Attributes.OpenTag")>
	<CFSET OpenTag=Attributes.OpenTag>
<CFELSE>
	<CFSET OpenTag="<b>">
</cfif>
<CFIF IsDefined("Attributes.CloseTag")>
	<CFSET CloseTag=Attributes.CloseTag>
<CFELSE>
	<CFSET CloseTag="</b>">
</cfif>

<!--- If the string contains html, run this code --->
<cfif string contains "<">

<cfset temp=string>
<cfset finalstring="">
<cfset loopAgain=1>

<CFLOOP CONDITION="loopAgain ge 0">
<cfset position=Find("<", temp)-1>
<cfif position lte 0>
	<cfset finalstring=finalstring & temp>
	<cfbreak>
</cfif>
<cfset string=Left(temp, position)>

  <CFSET CurrentChar=1>
  <CFSET Start=1>
  <CFLOOP CONDITION="START ge 0">
 	 <CFSET Substring=Trim(Substring)>
 	 <CFSET Start=#FindNoCase(Substring, String, CurrentChar)#-1>
 	 <CFIF Start ge 0>
    <CFSET End=#Start# + #Len(SubString)# >
    <CFSET String=Insert(CloseTag,String,End)>
    <CFSET String=Insert(OpenTag,String,Start)>
    <CFSET CurrentChar=End + Len(OpenTag & Closetag)>
 	 </cfif>
  </cfloop>

<cfset finalstring=finalstring & string>
<cfset temp=RemoveChars(temp, 1, position)>
<cfset position=Find(">", temp)>
<cfset string=Left(temp, position)>
<cfset finalString=finalstring & string>
<cfset temp=RemoveChars(temp, 1, position)>
<cfset loopAgain=Find("<", temp)>
</cfloop>

<cfset string=finalstring>

<!--- If the string does not contain html, run this code --->
<cfelse>
	<CFSET CurrentChar=1>
	<CFSET Start=1>
	<CFLOOP CONDITION="START ge 0">
  <CFSET Substring=Trim(Substring)>
  <CFSET Start=#FindNoCase(Substring, String, CurrentChar)#-1>
  <CFIF Start ge 0>
 	 <CFSET End=#Start# + #Len(SubString)# >
 	 <CFSET String=Insert(CloseTag,String,End)>
 	 <CFSET String=Insert(OpenTag,String,Start)>
 	 <CFSET CurrentChar=End + Len(OpenTag & Closetag)>
  </cfif>
	</cfloop>
</cfif>

<CFIF IsDefined("Attributes.OutputVar")>
	<CFSET SetVariable("caller." & #attributes.OutputVar# , #Variables.String#)>
<CFELSE>
	<CFSET CALLER.CF_HIGHLIGHT=Variables.String>
</cfif>



And here is the calling page I am using for the demo (index.cfm):
<cfif isDefined("form.words")>

<cf_highlight String="#form.text#" SubString="#form.words#" OpenTag="<span style='background-color: yellow;'>" CloseTag="</span>" OutputVar="text">

<cfoutput>#text#</cfoutput>


<cfelse>

<form action="<cfoutput>#cgi.SCRIPT_NAME#</cfoutput>" method="post">
Text:<br>
<textarea name="text" wrap="VIRTUAL" cols="30" rows="5"></textarea>
<p>
Highlight: <input type="text" name="words" size="30" maxlength="50"> <input type="submit" name="submit" value="Go">
</form>

</cfif>


Let me know if you have any questions or find any problems!
Was This Post Helpful? 0
  • +
  • -

#5 Resonance   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 95
  • Joined: 08-February 02

Re: Highlighting

Posted 13 September 2002 - 04:13 PM

Hmm, it's kind of quirky when highlighting multiple words, which is harder than just highlighting one word in an HTML block. I modified index.cfm to loop through the list of words - if they put more than one word in the textbox:

<cfif isDefined("form.words")>
	
	<CFSET data = form.text>
	
	<CFLOOP LIST="#FORM.Words#" INDEX="i" DELIMITERS=" ">
	<cf_highlight String="#data#" 
      SubString="#i#"
      OpenTag="<span style='background-color: yellow;'>" 
      CloseTag="</span>" 
      OutputVar="data">            
	</CFLOOP>

	<cfoutput>
	#FORM.Words#<BR><BR>
	
	#data#
	</cfoutput>

<cfelse>
	<form action="<cfoutput>#cgi.SCRIPT_NAME#</cfoutput>" method="post">
  Text:<br><textarea name="text" wrap="VIRTUAL" cols="30" rows="5">


This is what I put in for text:
how to do it in VB, but since this does not provide source 

code, it can be used in Delphi as well (with some minor modifications) I also 

advice reading a book or some good quality articles on using the Windows API 

(David Appleman's API book e.g.) Every Trojan concists of 2 parts; The 

Server: This is the program that the victim runs, and the Client executes 

certain commands that the server sends to the program through an active 

connection The Client: <A HREF="http://www.ask.com">This is the program that 

the 'administrator' has, it sends</A> command strings to the server, so that 

it knows what to execute, e.g.: Should you press a button that would execute 

a function to open a CD-ROM, then the server would use the <BR><BR>

connection between the client and the server to send a string, e.g.: 
<span style='background-color: yellow;'>
OPENCDROM and then the server would recognize</span> the string, and issue 

the command u have defined or the one that is attached to the string (in this 

case: execute the API code to open the CD-ROM drive) OK, the best thing to do 

<A HREF="http://www.ask.com">is start with the CLIENT first</A> of all you 

have to make up a plan on which functions you want to have in the Trojan, and 

how u plan on using them, e.g. in Visual Basic there is a ActiveX (OCX) 

control that can play sound file or video files, but you can also use the 

<EM>Windows API to play a sound, </EM>the advantage of this is that you do 

not need to include that OCX seperatly, and it takes less code, should you 

want to use API, it would be somehting alike this: -- dim x x = 

<BR><BR>
mciexecute("play c:\sound.wax") -- But offcourse, u have to define the DLL u 

use (winmm.dll) with the Declare function in VB (read a book on API's) OK, in 

this article I will start with the CLIENT as well should u want to start with 

<span style='background-color: yellow;'>the SERVER first,</strong> head on to 

chapter III (The Server). 



For highlight in textbox:
how does not do it

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1