EvotecIT / PSWriteHTML

PSWriteHTML is PowerShell Module to generate beautiful HTML reports, pages, emails without any knowledge of HTML, CSS or JavaScript. To get started basics PowerShell knowledge is required.
MIT License
826 stars 106 forks source link

Add-HTMLStyle -Link broken in many versions #396

Closed matt555 closed 1 year ago

matt555 commented 1 year ago

I've used Add-HTMLStyle -Link <link> -Placement <header|footer> in a number scripts over the years and not sure how i missed that it was never working. Generally the CSS I was adding was overkill so likely not noticeable.

Issue

It appears that this was first introduced in commit 1d42199 Where the <style> tag is added to anything going to the header or footer.

When using Add-HTMLStyle to add a CSS link to <head> or <footer> the end result looks like this:

<style>
    <link rel="stylesheet preload prefetch" type="text/css" href="https://unpkg.com/tabulator-tables@5.5.0/dist/css/tabulator.min.css" as="style"/>
</style>

Example code to reproduce

Add-HTMLStyle -Placement Header -Link 'https://unpkg.com/tabulator-tables@5.5.0/dist/css/tabulator.min.css'

Note: -Placement Inline is not affected by this due to the code here: https://github.com/EvotecIT/PSWriteHTML/blob/master/Public/Add-HTMLStyle.ps1#L87-L93

Possible Fix - modify Private\New-HTMLCustomCSS.ps1

I've modified the New-HTMLCustomCSS function with the below code in my $home\Documents\PowerShell\Modules\PSWriteHTML\1.1.0\PSWriteHTML.psm1 and ran various tests. All appear to work without issue.

function New-HTMLCustomCSS {
    [CmdletBinding()]
    param(
        [System.Collections.IDictionary] $CSS,
        [switch] $AddComment
    )

    $Output = foreach ($Key in $CSS.Keys) {
        if ($AddComment) {
            "<!-- CSS $Key AUTOGENERATED on DEMAND START -->" 
        }
        if ($CSS[$Key]) {
            #if ($CSS[$Key] -notlike "*<style *") {

            $CSS[$Key]

            #} else {
            #    $CSS[$Key]
            #}
        }
        if ($AddComment) {
            "<!-- CSS $Key AUTOGENERATED on DEMAND END -->" 
        }
    }
    if ($Output) {
        $Output.Where({ $_ -like '<!-- CSS * START -->' })
        New-HTMLTag -Tag 'style' -Attributes @{ type = 'text/css' } {
            # $Output
            $Output.Where({$_ -notlike '<link *' -and $_ -notlike '<!-- CSS*'})
        } -NewLine
        $Output.Where({ $_ -like '<link *' })
        $Output.Where({ $_ -like '<!-- CSS * END -->' })
        # notate where this may impact other areas via html output.
        '<!-- MONKEYPATCHED_New-HTMLCustomCSS -->'
    }
}

Some noteworthy successful tests are:

Untested:

PrzemyslawKlys commented 1 year ago

I can confirm the behavior, but rather then doing what you did I believe the change should be to remove the style tags and not use -SkipTags internally, and if user wants to skiptags - go ahead, but be aware of consequences.

This affects output a bit because it creates this:

image

Instead of singel style with multiple entries, but I guess it's okish. Not sure if I should complicate any further, maybe internally to prevent this from happening, but not sure if it's worth the effort to remove couple of style tags.