ANmaHTMLPadding

Finds the padding or margin from a style statement for an HTML tag.
Passing StyleString as the string starting from "Style=" from inside any html tag
And returns left, right, top, or bottom padding or margin whichever found first using same CSS3 method based on caller request in ReturnPadding_TRBL.

Edit: 2024-02-14: Fixing scenario of searching nested margin of main tag.

CodeFunctionName
What is this?

Public

Tested

Original Work
Function ANmaHTMLPadding_or_Margin(StyleString, Optional ReturnPadding_TRBL = "left")
    ' Finds the padding for a certain CSS.
    ' Pass StyleString as the string from "Style=" inside any heml tag, with or without "Style="
    ' function will read either ...
    ' padding:
    '    or
    ' padding-left;        padding-right:        padding-top:            padding-bottom
    ' and return left, right, top, or bottom padding using same CSS3 method since user defined the return in ReturnPadding
    ' ReturnPadding = "left", "right", "top", "bottom"
    '
    ' Needs: CutString(), VBInstr()
    ' Examples
    ' style="background-color:#cceeff;padding:2px 1pt;text-align:left;vertical-align:bottom"
    ' style="background-color:#cceeff;padding:0 1pt"
    ' style="color:#000000;font-family:'Times New Roman',sans-serif;font-size:8pt;font-weight:400;line-height:120%"
    ' style="background-color:#ffffff;padding:2px 1pt 2px 19pt;text-align:left;vertical-align:bottom"
    '
    ' extract string from = until " or '
    ' If we find padding- then we are great, go ahead and read it according to passed ReturnPadding
    ' otherwise, extract string from padding: until ; or " or '
    '    break that padding into parts with space as separater starting from :
    '    How many parts we found? 1, 2, 3, or 4?
    ' Read based on matrix below (Retrun number part only
    '    padding: 1px 2px 3px 1pt            top right bottom left
    '    padding: 1px 2px 3px                top left=right bottom
    '    padding: 1px 3px                    top=bottom left=right
    '    padding: 1px                            top=right=bottom=left
   
    Rett = 0
    Select Case LCase(ReturnPadding_TRBL)
    Case "top":        Rett2 = 1
    Case "right":    Rett2 = 2
    Case "bottom":    Rett2 = 3
    Case "left":        Rett2 = 4
    End Select
    CSS1 = "" ' Full style statement starting with style=
    CSS2 = "" ' padding only part, starting with padding- or padding:
    CSS3 = "" ' The actual final padding of that direction, with unit, with no padding- nor padding:
    CSS4 = "" ' Numeric part of that padding without unit
    CSS1 = StyleString ' Full style statement starting with style=
    CSS1 = CutString(CSS1, "=")
    If Asc(CSS1) = 34 Or Asc(CSS1) = 39 Then CSS1 = Mid(CSS1, 2)
    CSS1 = CutString(CSS1, , """")
    CSS1 = CutString(CSS1, , "'")
    Padd1 = VBInstr("padding-", CSS1)
    Padd2 = VBInstr("padding:", CSS1)
    Padd3 = VBInstr("padding :", CSS1)
    Marg1 = VBInstr("margin-", CSS1)
    Marg2 = VBInstr("margin:", CSS1)
    Marg3 = VBInstr("margin :", CSS1)
    If Padd1 > 0 Then        ' Return whatever padding user asked
        CSS2 = CutString(CSS1, "padding-" & LCase(ReturnPadding_TRBL) & ":", ";")
        CSS3 = CSS2
    ElseIf Padd2 > 0 Or Padd3 > 0 Then        ' We did not find padding-, we found padding: instead
        Padd4 = IIf(Padd2 > 0, Padd2, Padd3)
        CSS2 = CutString1(CSS1, Padd4)
        CSS2 = Replace(Replace(CSS2, "padding", ""), ":", "")
        CSS2 = Trim(CutString(CSS2, , ";"))
        CSS3 = CSS2 ' assume no spaces found
        SpacesFound = Frequency(CSS2, " ")
        Select Case SpacesFound
        Case 2 ' 1 space found = 2 parts
            If Rett2 = 1 Then CSS3 = CutString3(CSS2, 1, " ")
            If Rett2 = 2 Then CSS3 = CutString3(CSS2, 2, " ")
            If Rett2 = 3 Then CSS3 = CutString3(CSS2, 1, " ")
            If Rett2 = 4 Then CSS3 = CutString3(CSS2, 2, " ")
        Case 3
            If Rett2 = 1 Then CSS3 = CutString3(CSS2, 1, " ")
            If Rett2 = 2 Then CSS3 = CutString3(CSS2, 2, " ")
            If Rett2 = 3 Then CSS3 = CutString3(CSS2, 3, " ")
            If Rett2 = 4 Then CSS3 = CutString3(CSS2, 2, " ")
        Case 4 ' 3 spaces found = 4 parts
            If Rett2 = 1 Then CSS3 = CutString3(CSS2, 1, " ")
            If Rett2 = 2 Then CSS3 = CutString3(CSS2, 2, " ")
            If Rett2 = 3 Then CSS3 = CutString3(CSS2, 3, " ")
            If Rett2 = 4 Then CSS3 = CutString3(CSS2, 4, " ")
        End Select
    ElseIf Marg1 > 0 Then ' Margin found        ' Return whatever margin user asked
        CSS2 = CutString(CSS1, "margin-" & LCase(ReturnPadding_TRBL) & ":", ";")
        CSS3 = CSS2
    ElseIf Marg2 > 0 Or Marg3 > 0 Then        ' We did not find margin-, we found margin: instead
        Marg4 = IIf(Marg2 > 0, Marg2, Padd3)
        CSS2 = CutString1(CSS1, Marg4)
        CSS2 = Replace(Replace(CSS2, "margin", ""), ":", "")
        CSS2 = Trim(CutString(CSS2, , ";"))
        CSS3 = CSS2 ' assume no spaces found
        SpacesFound = Frequency(CSS2, " ")
        Select Case SpacesFound
        Case 2 ' 1 space found = 2 parts
            If Rett2 = 1 Then CSS3 = CutString3(CSS2, 1, " ")
            If Rett2 = 2 Then CSS3 = CutString3(CSS2, 2, " ")
            If Rett2 = 3 Then CSS3 = CutString3(CSS2, 1, " ")
            If Rett2 = 4 Then CSS3 = CutString3(CSS2, 2, " ")
        Case 3
            If Rett2 = 1 Then CSS3 = CutString3(CSS2, 1, " ")
            If Rett2 = 2 Then CSS3 = CutString3(CSS2, 2, " ")
            If Rett2 = 3 Then CSS3 = CutString3(CSS2, 3, " ")
            If Rett2 = 4 Then CSS3 = CutString3(CSS2, 2, " ")
        Case 4 ' 3 spaces found = 4 parts
            If Rett2 = 1 Then CSS3 = CutString3(CSS2, 1, " ")
            If Rett2 = 2 Then CSS3 = CutString3(CSS2, 2, " ")
            If Rett2 = 3 Then CSS3 = CutString3(CSS2, 3, " ")
            If Rett2 = 4 Then CSS3 = CutString3(CSS2, 4, " ")
        End Select
    Else
        ' No Padding found
       
    End If
   
    CSS4 = CutLeftNumber(Trim(CSS3)) ' Gets 1st part of string that is numeric, ignoring spaces at start, until 1st char found that is not a number
    Rett = Val(CSS4)
   
    ANmaHTMLPadding_or_Margin= Rett
End Function

StyleString, Optional ReturnPadding_TRBL = "left"

Views 162

Downloads 48

CodeID
DB ID