Skip to content

fix(New-HTMLTable): 🐛 Escape HTML characters when InvokeHTMLTags #499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Examples/Example-TableWithTags/Example-TableWithTags.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Import-Module .\PSWriteHTML.psd1 -Force

$test = "<test>"
New-HTML {
New-HTMLTableOption -DataStore JavaScript -BoolAsString -ArrayJoinString '<br>' -ArrayJoin

New-HTMLTab -Name 'Forest' {
New-HTMLSection -HeaderText 'Summary' {
New-HTMLTable -DataTable $test
}
}
} -ShowHTML -FilePath "$PSScriptRoot\Example-TableWithTags.html" -Online
12 changes: 12 additions & 0 deletions Public/New-HTMLTable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,12 @@
ArrayJoinString = $Script:HTMLSchema['TableOptions']['DataStoreOptions'].ArrayJoinString
}
$DataToInsert = $Table | ConvertTo-PrettyObject @convertToPrettyObjectSplat | ConvertTo-Json
if (-not $InvokeHTMLTags) {
# If InvokeHTMLTags is not set, we need to escape HTML characters
# By default HTML tags are escaped when using DataStore HTML, but not when using JavaScript
# So we need to escape them here, so they don't break the JavaScript code
$DataToInsert = $DataToInsert -replace "<", "&lt;" -replace ">"
}
if ($DataToInsert.StartsWith('[')) {
$Script:HTMLSchema.CustomFooterJS[$DataStoreID] = "var $DataStoreID = $DataToInsert;"
} else {
Expand All @@ -1328,6 +1334,12 @@
ArrayJoinString = $Script:HTMLSchema['TableOptions']['DataStoreOptions'].ArrayJoinString
}
$DataToInsert = $Table | ConvertTo-PrettyObject @convertToPrettyObjectSplat | ConvertTo-Json
if (-not $InvokeHTMLTags) {
# If InvokeHTMLTags is not set, we need to escape HTML characters
# By default HTML tags are escaped when using DataStore HTML, but not when using JavaScript
# So we need to escape them here, so they don't break the JavaScript code
$DataToInsert = $DataToInsert -replace "<", "&lt;" -replace ">", "&gt;"
}
if ($DataToInsert.StartsWith('[')) {
$Options = $Options.Replace('"markerForDataReplacement"', $DataToInsert)
} else {
Expand Down