Clip to Add class=”wrap” to last TD tag in each TR tag

This is written in response to a request from Don Strack on the notetab clips list. His problem is that he wants to have a special class on the last TD tag in each set of TR tags in an existing page. He apparently adds new sets periodically, so he may already have some of them with a class attribute in them.

This clips has the following highlights:

  • it finds matching tr and /tr tag sets
  • it looks backwards through the tr tag to find the last td tag
  • it determines if there is already a class attribute in that td tag … and if yes, move on, if not, it adds it
  • then it steps to the next tr/tr tag set.

    ;*** 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 matching tr tags in a table
    ;*** then find the last td tag and if not yet
    ;*** set, add a class to the last td tag in each row

    ;### initialize everything
    ^!ClearVariables
    ^!Jump Doc_Start
    ; ———————————————-***
    ;line 14
    :FindTR
    ; find first tag
    ^!Find “<” TIS
    ;exit if find fails
    ^!IfError EXIT
    ;figure out where cursor is
    ^!Set %row%=”^$GetRow$”
    ^!Set %column%=”^$GetCol$”
    ; ———————————————-***
    ^!SET %START%=^%row%:^%column%
    ; ADDED THIS TO BE ABLE TO HAVE EVERYTHING
    ; SELECTED AT THE END OF THE CLIP
    ; ———————————————-***
    ^!SetScreenUpdate Off
    ^!SetWordWrap Off

    ;### %TAG% will be empty if cursor is not inside a tag.
    ^!Set %TAG%=”^$GetHtmlTag(TRUE)$”
    ^!IfTrue ^$IsEmpty(^%TAG%)$ NotTag

    ;### So we found a tag.  What tag is it?
    ^!Set %TAGNAME%=”^$GetHtmlTagName(“^%TAG%”;UPPERCASE)$”
    ^!Jump Select_End
    ;if this tag is not a TR, look for next tag
    ^!If “^%TAGNAME%” <> “TR” FindTR

    ;only get here if we are in a TR tag
    ^!Set %TAGCOUNT%=1

    ;line 46
    :FindNextTagForwards
    ^!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 FindNextTagForwards

    ;line 71
    :Match
    ^!SetScreenUpdate On
    ^!Jump SELECT_END
    ; ———————————————-***
    ;^!SelectTo ^%row%:^%column%
    ^!SelectTo ^%START%
    ; ———————————————-***
    ;okay we have a complete TR to /TR Selected
    ;now look inside of it for last TD tag

    :FindTD
    ^!Find “<TD” TIBHS
    ^!Set %thisTD%=”^$GetHtmlTag(“Yes”)$”
    ^!Set %thisTDhasclass%=”^$StrCount(“class=”;”^%thisTD%”;No;No)$”
    ^!If ^%thisTDhasclass% > 0 FindTR
    ^!Replace “^$GetHtmlTagName(“^%thisTD%”)$” >> “^$GetHtmlTagName(“^%thisTD%”)$ class=”wrap”” CIHS
    ^!Goto findTR
    ^!Goto Finish

    ;line 91
    :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

    ;### clean up and go home
    :Finish
    ^!SetScreenUpdate On
    ^!SetWordWrap On
    ^!ClearVariables
    ;line 102 if you have all lines unrapped (including blank lines)

Leave a Reply

Recent Posts

Archives

Topics