Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1450

Extract Styles from HTML as inline CSS

$
0
0
In my project, I had to "clean" HTML that are generated like this
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>
... 

and having instead of style, CSS.
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: -.4ptborder-collapsecollapsetable-layoutautobordernone;}.s2 {padding0cm 3.6pt 0cm 3.6pt;vertical-aligntop;}.s3 {padding0cm 3.6pt 0cm 3.6pt;vertical-aligntop;bordersolid 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'>
                <
class='s4'>
                    <
strong>Date</strong>
                </
p>
            </
td>
            <
td class='s3'>
                <
class='s4'>
                    <
strong>Infos</strong>
                </
p>
            </
td>
... 

So I wrote this piece of code to implement it dynamically.
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(sHTMLThen
      sXPath 
"//*[@style]"
      
Set oNodes oXML.selectNodes(sXPath)

      For 
Each oNode In oNodes
         sStyle 
Trim$(XML_GetAttribute(oNode"style"))
         If 
Not oColStyles.KeyExists(sStyleThen
            sStyleName 
"s" oColStyles.ItemCount 1
            sCSSStyle 
sCSSStyle "." sStyleName " {" sStyle "}"
            
sNewHTML Replace(sNewHTML"style='" sStyle "'""class='" sStyleName "'")
            
oColStyles.AddItem sCSSStylesStyle
         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 

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.

Viewing all articles
Browse latest Browse all 1450

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>