8000 Added Meta, Charset, and Transitional parameters to ConvertTo-HTML by ergo3114 · Pull Request #4184 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 31 commits into from
Sep 11, 2017

Conversation

ergo3114
Copy link
Contributor
@ergo3114 ergo3114 commented Jul 4, 2017

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 keywords
Charset Sets the html encoding of the page, denoted by the <charset> html tag
Transitional 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

@msftclas
Copy link
msftclas commented Jul 4, 2017

@ergo3114,
Thanks for having already signed the Contribution License Agreement. Your agreement was validated by Microsoft. We will now review your pull request.
Thanks,
Microsoft Pull Request Bot

{
if(s != "content-type" && s != "default-style" && s !="refresh")
{
WriteObject("<meta name=\"" + s + "\" content=\"" + _meta[s] + "\">");
Copy link
Member

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?

Copy link
Contributor Author

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 + "\">");
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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] + "\">");
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TravisEz13 TravisEz13 requested a review from JamesWTruher July 11, 2017 19:36
@TravisEz13
Copy link
Member

@JamesWTruher Can you review the character set parameter here?

Copy link
Member
@TravisEz13 TravisEz13 left a 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
{
_meta = value;
_metaSpecified = true;
Copy link
Member
@SteveL-MSFT SteveL-MSFT Jul 16, 2017

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

Copy link
Contributor Author
@ergo3114 ergo3114 Jul 16, 2017

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.

Copy link
Collaborator

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.

Copy link
Member

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

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ergo3114 Please address this.

Copy link
Contributor Author

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.

Copy link
Member

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")
Copy link
Member

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.

Copy link
Contributor Author

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)]
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member
@SteveL-MSFT SteveL-MSFT left a 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

ergo3114 added 2 commits July 16, 2017 13:06
- 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;
Copy link
Collaborator

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;
Copy link
Collaborator

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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closed.

set
{
_transitional = value;
}
Copy link
Collaborator

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.

@TravisEz13
Copy link
Member

@ergo3114 Thanks for the update. It's helpful for us to know the PR is not abandoned.

@ergo3114
Copy link
Contributor Author

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;
Copy link
Contributor

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);
}
Copy link
Member

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

Copy link
Contributor Author

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.";
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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.

@TravisEz13 TravisEz13 merged commit bbc180a into PowerShell:master Sep 11, 2017
TravisEz13 added a commit to TravisEz13/PowerShell that referenced this pull request Sep 20, 2017
daxian-dbw pushed a commit that referenced this pull request Sep 20, 2017
That PR incorrectly included Pester changes. Most likely because that individual did not update his submodules.
@joeyaiello joeyaiello removed Documentation Needed in this repo Documentation is needed in this repo labels Oct 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants
0