Find Matching HTML Tags

This works to find a matching html tag. Nesting is fine with this as it will work through it. It will highlight the start and end tags and all content in the middle. There is a find closing html tag in the Notetab Utilities Library, but it is fooled by nested tags.

;*** Effort by Don Passenger
;*** don a-t htmlfixit d-o-t com
;*** discuss things live in chat at http://htmlfixit.com
;*** based on work by Larry and Jody and maybe Wayne
;*******************************************************
;*** will check for a matching html tag

^!Continue [C]If cursor is in the tag you want to find a match for click continue

;### initialize everything
;assume we have our cursor in a tag
;look backwards to find the starting < ^!Find "<" TIBS
;exit if find fails
^!IfError EXIT
^!SetScreenUpdate Off
^!ClearVariables

;### %TAG% will be empty if cursor is not inside a tag.
^!Set %TAG%="^$GetHtmlTag(TRUE)$"
^!IfTrue ^$IsEmpty(^%TAG%)$ NotTag
;if self closing get out too
^!If "/>" = "^$StrCopyRight("^$GetSelection$";2)$" SelfClosing


;### So we found a tag.  What tag is it?
^!Set %TAGNAME%="^$GetHtmlTagName("^%TAG%";UPPERCASE)$"
;got to forwards or backwards routine depending if
;start or finish tag is selected at start of clip
^!If "/" = "^$StrCopyLeft("^%TAGNAME%";1)$" FindNextTagBackwards ELSE FindNextTagForwards

:FindNextTagForwards
^!Jump Select_Start
;figure out where cursor is
^!Set %row%="^$GetRow$"
^!Set %column%="^$GetCol$"
^!Find ">" TIS
^!Set %TAGCOUNT%=1
:TagForwardLoop
^!Find "< " TIS
^!IfError UnMatched
^!Set %TAGTEMP%="^$GetHtmlTag(TRUE)$"
^!IfTrue ^$IsEmpty(^%TAGTEMP%)$ NextTag
^!Set %TAGTEMPNAME%="^$GetHtmlTagName("^%TAGTEMP%";UPPERCASE)$"
^!If "/" = "^$StrCopyLeft("^%TAGTEMPNAME%";1)$" EndTag ELSE BeginTag

:EndTag
^!If "^$StrDeleteLeft("^%TAGTEMPNAME%";1)$" <> "^%TAGNAME%" Skip_2
^!Dec %TAGCOUNT%
^!If "^%TAGCOUNT%" = "0" Match
^!Goto NextTag

:BeginTag
^!If "^%TAGTEMPNAME%" <> "^%TAGNAME%" Skip_2
^!Inc %TAGCOUNT%
^!If "^%TAGCOUNT%" = "0" Match
^!Goto NextTag

:NextTag
^!Jump Select_End
^!Goto TagForwardLoop


:FindNextTagBackwards
^!Jump Select_End
;figure out where cursor is
^!Set %row%="^$GetRow$"
^!Set %column%="^$GetCol$"
^!Find "< " TIBS
^!Set %TAGCOUNT%=1
:TagBackwardLoop
^!Find "<" TIBS
^!IfError UnMatched
^!Set %TAGTEMP%="^$GetHtmlTag(TRUE)$"
^!IfTrue ^$IsEmpty(^%TAGTEMP%)$ NextTag2
^!Set %TAGTEMPNAME%="^$GetHtmlTagName("^%TAGTEMP%";UPPERCASE)$"
^!If "/" = "^$StrCopyLeft("^%TAGTEMPNAME%";1)$" EndTag2 ELSE BeginTag2

:EndTag2
^!If "^%TAGTEMPNAME%" <> "^%TAGNAME%" Skip_2
^!Inc %TAGCOUNT%
^!If "^%TAGCOUNT%" = "0" Match
^!Goto NextTag2

:BeginTag2
^!If "^%TAGTEMPNAME%" <> "^$StrDeleteLeft("^%TAGNAME%";1)$" Skip_2
^!Dec %TAGCOUNT%
^!If "^%TAGCOUNT%" = "0" Match
^!Goto NextTag2

:NextTag2
^!Jump Select_START
^!Goto TagBackwardLoop



:Match
^!SetScreenUpdate On
^!Info [C]Bingo, we have a match!.
^!If "/" = "^$StrCopyLeft("^%TAGNAME%";1)$" MatchBackwardJump ELSE MatchForwardJump
:MatchForwardJump
^!Jump SELECT_END
^!Goto MatchFinish
:MatchBackwardJump
^!Jump SELECT_START
:MatchFinish
^!SelectTo ^%row%:^%column%
^!Goto Finish

:UnMatched
^!SetCursor ^%row%:^%column%
^!Info [C]This appears to be an unmatched Tag.  We made it to the end of your document without finding a match.
^!Goto Finish


:NotTag
^!SetCursor ^%row%:^%column%
^!Info [C]This appears to not be an html tag.  Remember have your cursor inside the tag when starting this clip.
^!Goto Finish


:SelfClosing
^!SetCursor ^%row%:^%column%
^!Info [C]Your tag is self closing - quit messing with me!  Self closing tags end in /> like 
;### clean up and go home :Finish ^!SetScreenUpdate On ^!ClearVariables ;line 131 if you have all lines unrapped (including blank lines)

Leave a Reply

Recent Posts

Archives

Topics