Added Meta, Charset, and Transitional parameters to ConvertTo-HTML#4184
Added Meta, Charset, and Transitional parameters to ConvertTo-HTML#4184TravisEz13 merged 31 commits intoPowerShell:masterfrom
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.
should we encode the key and value?
There was a problem hiding this comment.
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.
Should probably validate or encode the character set.
There was a problem hiding this comment.
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.
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.
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.
Should we allow these or should it be another parameter like charset
There was a problem hiding this comment.
It looks like you can validate the meta tag. w3schools shows the schema for the meta tag.
There was a problem hiding this comment.
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? |
TravisEz13
left a comment
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
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.
We can use an auto property and Meta == null to check if user assign a value.
There was a problem hiding this comment.
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.
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.
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.
This has been changed.
| /// Specifies the charset encoding for the HTML document | ||
| /// </summary> | ||
| [Parameter(ParameterSetName = "Page")] | ||
| [ValidateNotNullOrEmpty] |
There was a problem hiding this comment.
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.
I have removed to the ValidateSet.
- 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; |
There was a problem hiding this comment.
We can use an auto property and Meta == null to check if user assign a value.
| } | ||
| set | ||
| { | ||
| _charset = value.ToUpper(); |
There was a problem hiding this comment.
Why do we convert a charset to upper case?
It seems we can remove this and use auto property.
| } | ||
| set | ||
| { | ||
| _transitional = value; |
There was a problem hiding this comment.
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
| /// </summary> | ||
| /// <returns></returns> | ||
| [Parameter(ParameterSetName = "Page")] | ||
| [ValidateNotNullOrEmpty] |
There was a problem hiding this comment.
I think you want { get; set; } here instead of a field.
| break; | ||
| } | ||
| useditems.Add(s); | ||
| } |
There was a problem hiding this comment.
seems like we should give a warning if we are dropping duplicates
There was a problem hiding this comment.
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.
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.
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.
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