-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Added Meta, Charset, and Transitional parameters to ConvertTo-HTML #4184
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
Conversation
…nd transitional DOCTYPEs - Updated Meta parameter to handle http-equiv properties
@ergo3114, |
{ | ||
if(s != "content-type" && s != "default-style" && s !="refresh") | ||
{ | ||
WriteObject("<meta name=\"" + s + "\" content=\"" + _meta[s] + "\">"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we encode the key and value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would encoding gain us here? I am not familiar with that.
WriteObject("<head>"); | ||
if(_charsetSpecified) | ||
{ | ||
WriteObject("<meta charset=\"" + _charset + "\">"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably validate or encode the character set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validation would be great. I don't see too many changes in the charset list but it would be great if the validation were dynamic pulling from w3schools or something. I was trying to find documentation on what the browser does when a non standard charset is specified; such as "foo". I believe it analyzes the html and determines the best or just defaults to UTF-8 for HTML5.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sure there are illegal characters. Even a validation that validates the user hasn't specified something illegal would be better than nothing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will put something in and resubmit it.
WriteObject("<meta name=\"" + s + "\" content=\"" + _meta[s] + "\">"); | ||
} | ||
else { | ||
WriteObject("<meta http-equiv=\"" + s + "\" content=\"" + _meta[s] + "\">"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we allow these or should it be another parameter like charset
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you can validate the meta tag. w3schools shows the schema for the meta tag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use ValidatePattern or ValidateScript to implement it? https://blogs.technet.microsoft.com/heyscriptingguy/2011/01/11/validate-powershell-parameters-before-running-the-script/
@JamesWTruher Can you review the character set parameter here? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR. I have a couple of comments. Please review and respond.
- Set the output of Charset to upper case if a lowercase value is given
set | ||
{ | ||
_meta = value; | ||
_metaSpecified = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the typical pattern is to use InvocationInfo.BoundParameters. This would then allow you to use the automatic properties to simplify the code a bit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was unable to get InvocationInfo.BoundParameters to work. I read over InvocationInfo.cs and ImplicitRemotingCommands.cs to see if I can make sense of it but I am at a loss here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use an auto property and Meta == null
to check if user assign a value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That should be fine, too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ergo3114 Please address this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this as two ways to do the same thing. What are you looking to gain from setting a default value versus checking a flag? Just curious.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A quick search shows we already have inconsistency so I'm fine staying with this pattern
{ | ||
foreach(string s in _meta.Keys) | ||
{ | ||
if(s == "content-type" && s == "default-style" && s =="refresh") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be ||
? In any case, there's more http-equiv
than this list. Perhaps we should just have a separate parameter for http-equiv
meta tags and let the user put whatever they want in it? Also, I believe the names are case-insensitive so Content-Type
is valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been changed.
/// </summary> | ||
[Parameter(ParameterSetName = "Page")] | ||
[ValidateNotNullOrEmpty] | ||
[ValidateSet("UTF-8", "ISO-8859-1","ASCII","ANSI",IgnoreCase=true)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically, the allowed set of (legacy) encodings is quite large although HTML5 I believe has standardized on UTF-8. So perhaps this shouldn't have a ValidateSet
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have removed to the ValidateSet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add some tests
- Removed Validateset from Charset parameter to allow for legacy character sets - Added test for Meta, Charset, and Transitional property on ConvertTo-HTML command
set | ||
{ | ||
_meta = value; | ||
_metaSpecified = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use an auto property and Meta == null
to check if user assign a value.
set | ||
{ | ||
_charset = value.ToUpper(); | ||
_charsetSpecified = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we convert a charset to upper case?
It seems we can remove this and use auto property.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Closed.
set | ||
{ | ||
_transitional = value; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use an auto property for a switch parameter without getter/setter.
…meter - Removed private variable for transitional switch parameter - Removed toUpper() functin from charset
@ergo3114 Thanks for the update. It's helpful for us to know the PR is not abandoned. |
I believe I am back in the game with continuing this PR. I will work on items that were left unattended in the coming days. |
- Set meta hashtable keys to lower before checking in switch statement - Updated test for the transitional parameter to check only the first line of the result - Updated test to be more specific on the error message that is returned when thrown - Added logic to prevent duplicate meta tags
/// <returns></returns> | ||
[Parameter(ParameterSetName = "Page")] | ||
[ValidateNotNullOrEmpty] | ||
public SwitchParameter Transitional; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you want { get; set; } here instead of a field.
break; | ||
} | ||
useditems.Add(s); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems like we should give a warning if we are dropping duplicates
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have changed it to a warning and decided to keep the meta tags that don't match the switch.
ThrowTerminatingError (new ErrorRecord(exc, "MetaPropertyNotFound", ErrorCategory.ParserError, null)); | ||
//Exception exc = new NotSupportedException(StringUtil.Format(ConvertHTMLStrings.MetaPropertyNotFound, s)); | ||
MshCommandRuntime mshCommandRuntime = this.CommandRuntime as MshCommandRuntime; | ||
string Message = "Accepted meta properties are content-type, default-style, application-name, author, description, generator, keywords, x-ua-compatible, and viewport. The meta pair: " + s + " and " + _meta[s] + " may not function correctly."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a new string in the resx file so that it can be localized
It "Test ConvertTo-HTML meta with invalid properties should throw"{ | ||
{ ($customObject | ConvertTo-HTML -Meta @{"authors"="John Doe";"keywords"="PowerShell,PSv6"}) -join $newLine } | Should Throw "authors is not a supported meta property. Accepted meta properties are content-type, default-style, application-name, author, description, generator, keywords, x-ua-compatible, and viewport." | ||
It "Test ConvertTo-HTML meta with invalid properties should throw warning" { | ||
($customObject | ConvertTo-HTML -Meta @{"authors"="John Doe";"keywords"="PowerShell,PSv6"} 3>&1) -match "Accepted meta properties are" | Should Be $true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the message could be localized, you probably just need to check that the warning contains the Meta tags you used in the test
} | ||
|
||
It "Test ConvertTo-HTML meta with invalid properties should throw warning" { | ||
($customObject | ConvertTo-HTML -Meta @{"authors"="John Doe";"keywords"="PowerShell,PSv6"} 3>&1) -match "Accepted meta properties are" | Should Be $true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the warning message can be localized, you shouldn't check against the English string. Instead, check that the warning message contains the meta pair.
…ters to ConvertTo-HTML (PowerShell#4184) (reverted from commit bbc180a)
Related #3267
Added the Meta, Chartset, and Transitional parameters to ConvertTo-HTML
Meta Allows the user to insert
<meta>
tag information such as refresh, author, and keywordsCharset Sets the html encoding of the page, denoted by the
<charset>
html tagTransitional A switch parameter that allows for the insertion of XHTML Transitional DTD DOCTYPE, the default being XHTML Strict DTD
Edit: Documentation can be found here