In my project, I had to "clean" HTML that are generated like this
and having instead of style, CSS.
After some research, I found nothing on the web.
The required result should be something like :
So I wrote this piece of code to implement it dynamically.
The HTML layout generated is always the same.
NB : Class_Collection is an enhanced collection. It could be replaced by something else.
I think it could be optimised, but, this will be for later, if you want to enhance it, post here.
PHP Code:
<table style='margin-left: -.4pt; border-collapse: collapse; table-layout: auto; border: none;'>
<tbody>
<tr style='padding: 0cm 3.6pt 0cm 3.6pt;vertical-align: top;'>
<td style='padding: 0cm 3.6pt 0cm 3.6pt;vertical-align: top;border: solid black 1.0pt;background-color: #E6ECFD;'>
<p style='line-height: normal; text-autospace: none;padding: 0cm 3.6pt 0cm 3.6pt;text-align: left;'>
<strong>Date</strong>
</p>
</td>
<td style='padding: 0cm 3.6pt 0cm 3.6pt;vertical-align: top;border: solid black 1.0pt;background-color: #E6ECFD;'>
<p style='line-height: normal; text-autospace: none;padding: 0cm 3.6pt 0cm 3.6pt;text-align: left;'>
<strong>Infos</strong>
</p>
</td>
...
After some research, I found nothing on the web.
The required result should be something like :
PHP Code:
<style type='text/css'>.s1 {margin-left: -.4pt; border-collapse: collapse; table-layout: auto; border: none;}.s2 {padding: 0cm 3.6pt 0cm 3.6pt;vertical-align: top;}.s3 {padding: 0cm 3.6pt 0cm 3.6pt;vertical-align: top;border: solid black 1.0pt;background-color: #E6ECFD;}.s4 {line-height: normal; text-autospace: none;padding: 0cm 3.6pt 0cm 3.6pt;text-align: left;}.s5 {line-height: normal; text-autospace: none;padding: 0cm 3.6pt 0cm 3.6pt;text-align: right;}.s6 {padding: 0cm 3.6pt 0cm 3.6pt;vertical-align: top;border: solid black 1.0pt;}.s7 {color: #008000;}.s8 {color: #FF0000;}</style>
<table class='s1'>
<tbody>
<tr class='s2'>
<td class='s3'>
<p class='s4'>
<strong>Date</strong>
</p>
</td>
<td class='s3'>
<p class='s4'>
<strong>Infos</strong>
</p>
</td>
...
The HTML layout generated is always the same.
PHP Code:
Public Function HTML_Stylus(sHTML As String) As String
' #VBIDEUtils#************************************************************
' * Author : xxxx
' * Web Site : xxxx
' * E-Mail : xxxx
' * Date : 08/04/2021
' * Time : 14:00
' * Module Name : HTML_Module
' * Module Filename : HTML.bas
' * Procedure Name : HTML_Stylus
' * Purpose :
' * Parameters :
' * sHTML As String
' * Purpose :
' **********************************************************************
' * Comments :
' *
' *
' * Example :
' *
' * See Also :
' *
' * History :
' *
' *
' **********************************************************************
' #VBIDEUtilsERROR#
On Error GoTo ERROR_HTML_Stylus
Dim oXML As New MSXML2.DOMDocument
Dim oNodes As MSXML2.IXMLDOMNodeList
Dim oNode As MSXML2.IXMLDOMNode
Dim sXPath As String
Dim sNewHTML As String
Dim sStyle As String
Dim sStyleName As String
Dim sCSSStyle As String
Dim oColStyles As class_Collection
Dim nPos As String
sNewHTML = sHTML
sCSSStyle = vbNullString
Set oColStyles = New class_Collection
If oXML.LoadXML(sHTML) Then
sXPath = "//*[@style]"
Set oNodes = oXML.selectNodes(sXPath)
For Each oNode In oNodes
sStyle = Trim$(XML_GetAttribute(oNode, "style"))
If Not oColStyles.KeyExists(sStyle) Then
sStyleName = "s" & oColStyles.ItemCount + 1
sCSSStyle = sCSSStyle & "." & sStyleName & " {" & sStyle & "}"
sNewHTML = Replace(sNewHTML, "style='" & sStyle & "'", "class='" & sStyleName & "'")
oColStyles.AddItem sCSSStyle, sStyle
End If
Next
If LenB(sCSSStyle) > 0 Then
sNewHTML = "<style type='text/css'>" & sCSSStyle & "</style>" & sNewHTML
End If
End If
EXIT_HTML_Stylus:
On Error Resume Next
Set oXML = Nothing
Set oColStyles = Nothing
HTML_Stylus = sNewHTML
Exit Function
' #VBIDEUtilsERROR#
ERROR_HTML_Stylus:
Select Case IAErrorHandler(gcError & Err.Number & ": " & Err.Description & vbCrLf & "in HTML_Module.HTML_Stylus" & vbCrLf & gcErrorLine & Erl, vbAbortRetryIgnore + vbCritical, "Error")
Case vbAbort
Screen.MousePointer = vbDefault
Resume EXIT_HTML_Stylus
Case vbRetry
Resume
Case vbIgnore
Resume Next
End Select
Resume EXIT_HTML_Stylus
End Function
I think it could be optimised, but, this will be for later, if you want to enhance it, post here.