Skip to content
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

Do not remove first comment from HTML if it's MOTW (Mark of the WEB) #206

Closed
PrzemyslawKlys opened this issue Nov 30, 2020 · 10 comments · Fixed by #226
Closed

Do not remove first comment from HTML if it's MOTW (Mark of the WEB) #206

PrzemyslawKlys opened this issue Nov 30, 2020 · 10 comments · Fixed by #226

Comments

@PrzemyslawKlys
Copy link

During minify, all comments are removed, but some comments are crucial for Internet Explorer compatibility and JavaScript.
During formatting, the comment is followed by first tag right away which also breaks it.

Reference LINK: https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/compatibility/ms537628(v=vs.85)?redirectedfrom=MSDN

This is the default I have:

<!-- saved from url=(0014)about:internet -->
 <!DOCTYPE html>
 <html><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type"><meta content="width=device-width, initial-scale=1" name="viewport"><meta content="2020-11-30 11:44:27" name="revised"><title></title><link href="https://fonts.googleapis.com/css2?family=Roboto+Condensed&display=swap" as="style" type="text/css" rel="stylesheet preload prefetch"/><script src="https://code.jquery.com/jquery-3.5.1.min.js" type="text/javascript"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js" type="text/javascript"></script><link href="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.min.css" as="style" type="text/css" rel="stylesheet preload prefetch"/><link href="https://cdn.datatables.net/select/1.3.1/css/select.dataTables.min.css" as="style" type="text/css" rel="stylesheet preload prefetch"/><script src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.min.js" type="text/javascript"></script><script src="https://cdn.datatables.net/select/1.3.1/js/dataTables.select.min.js" type="text/javascript"></script><script src="https://cdn.datatables.net/plug-ins/1.10.20/sorting/datetime-moment.js" type="text/javascript"></script><link href="https://cdn.datatables.net/buttons/1.6.5/css/buttons.dataTables.min.css" as="style" type="text/css" rel="stylesheet preload prefetch"/><script src="https://cdn.datatables.net/buttons/1.6.5/js/dataTables.buttons.min.js" type="text/javascript"></script><script src="https://cdn.datatables.net/buttons/1.6.5/js/buttons.html5.min.js" type="text/javascript"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js" type="text/javascript"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js" type="text/javascript"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js" type="text/javascript"></script><link href="https://cdn.datatables.net/responsive/2.2.6/css/responsive.dataTables.min.css" as="style" type="text/css" rel="stylesheet preload prefetch"/><script src="https://cdn.datatables.net/responsive/2.2.6/js/dataTables.responsive.min.js" type="text/javascript"></script><link href="https://cdn.jsdelivr.net/npm/smartwizard@5.1.1/dist/css/smart_wizard_all.min.css" as="style" type="text/css" rel="stylesheet preload prefetch"/><script src="https://cdn.jsdelivr.net/npm/smartwizard@5.1.1/dist/js/jquery.smartWizard.min.js" type="text/javascript"></script><style>body {    font-family: 'Roboto Condensed', sans-serif;    font-size: 8pt;    margin: 0px;}input {    font-size: 8pt;}</style><style>td {    height: 14px;}tfoot input {    width: 100%;    box-sizing: border-box;    padding: -3px;}thead input {    width: 100%;    box-sizing: border-box;    padding: -3px;}td::before {    background: #007bff !important;}td.sorting_1::before {    background: #007bff !important;}button.dt-button {    color: blue !important;    border-radius: 5px;    line-height: 1 !important;}div.dataTables_wrapper {    margin: 5px;}</style><noscript><style type="text/css">table {
    border-collapse: collapse;
    box-sizing: border-box;
    width: 100%;
}

After formatting:

<!-- saved from url=(0014)about:internet --><!DOCTYPE html>
<html>
    <head>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
        <meta content="width=device-width, initial-scale=1" name="viewport" />
        <meta content="2020-11-30 11:44:13" name="revised" />

After minify:

<!DOCTYPE html><html><head><meta content="text/html; charset=utf-8" http-equiv=Content-Type />

If you don't provide saved from URL about:internet when you run HTML file with javascript as a local file in internet explorer it will prevent JS from running and prompt you to click. Just adding this special line (I know, it's weird) and following proper rules around it makes IE 11 behave properly for JavaScript code without useless prompts.

@trullock
Copy link
Owner

trullock commented Dec 28, 2020

Not against this feature but I don't have time to spend on IE11 compatibility, sorry :)

PRs welcome

@PrzemyslawKlys
Copy link
Author

I would love to stop supporting IE11 but unfortunately, users of my projects (https://github.com/EvotecIT/PSWriteHTML) are usually running my tools on servers or are enterprise customers which often have dated defaults. Servers for the next few years will come, by default, with IE11 and until Microsoft auto deploys Edge to those I'm stuck. Not sure if I will be able to fix this in PR, but maybe - some day. Thanks for not closing this issue.

@trullock
Copy link
Owner

trullock commented Dec 30, 2020

OK, seeing as its Christmas I'll take a look!

So this feature already exists!

You need to modify HtmlSettings.KeepCommentsRegex, add a new regex to this which matches your MOTW comments and youre sorted!

@PrzemyslawKlys
Copy link
Author

Hrmm, glad to hear, but will it keep it on a separate line? It can't be touched at all.

@trullock
Copy link
Owner

What do you mean?

write something like this (untested)

settings.KeepCommentsRegex.Add(new Regex("^\ssaved\sfrom\url="))

@PrzemyslawKlys
Copy link
Author

For minimize process:

    $Pattern = "<!-- saved from url=\(0014\)about:internet -->"
    $MOTW = [System.Text.RegularExpressions.Regex]::new($Pattern, [System.Text.RegularExpressions.RegexOptions]::MultiLine)
    $Settings.KeepCommentsRegex.Add($MOTW)

For format process:

    $Pattern = "<!-- saved from url=\(0014\)about:internet -->"
    $MOTW = [System.Text.RegularExpressions.Regex]::new($Pattern) #, [System.Text.RegularExpressions.RegexOptions]::MultiLine)
    $Settings.KeepCommentsRegex.Add($MOTW)

The regex you provided throws an error about digits or so.

Input:

<!-- saved from url=(0014)about:internet -->
 <!DOCTYPE html>
 <html><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type"><meta content="width=device-width, initial-scale=1" name="viewport"><meta content="2020-12-30 14:16:09" name="revised"><title></title><link href="https://fonts.googleapis.com/css2?family=Roboto+Condensed&display=swap" as="style" type="text/css" rel="stylesheet preload prefetch"/><style>body {    font-family: 'Roboto Condensed', sans-serif;    font-size: 8pt;    margin: 0px;}input {    font-size: 8pt;}</style></head><body><div class="main-section"></div></body></html>

Output format:

<!-- saved from url=(0014)about:internet --><!DOCTYPE html>
<html>
    <head>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
        <meta content="width=device-width, initial-scale=1" name="viewport" />
        <meta content="2020-12-30 14:16:10" name="revised" />
        <title></title>
        <link href="https://fonts.googleapis.com/css2?family=Roboto+Condensed&display=swap" as="style" type="text/css" rel="stylesheet preload prefetch" />
        <style>
            body {
                font-family: 'Roboto Condensed',sans-serif;
                font-size: 8pt;
                margin: 0
            }
            input {
                font-size: 8pt
            }
        </style>
    </head>
    <body>
        <div class="main-section"></div>
    </body>
</html>

Output minimize

<!DOCTYPE html><html><head><meta content="text/html; charset=utf-8" http-equiv=Content-Type /><meta content="width=device-width, initial-scale=1" name=viewport /><meta content="2020-12-30 14:16:10" name=revised /><title></title><link href="https://fonts.googleapis.com/css2?family=Roboto+Condensed&display=swap" as=style type=text/css rel="stylesheet preload prefetch" /><style>body{font-family:'Roboto Condensed',sans-serif;font-size:8pt;margin:0}input{font-size:8pt}</style></head><body><div class=main-section></div></body></html>

So minimize ignored setting. Formatting did the same thing it did without the "keepCommentsREgex", as I already have RemoveComments set to $false.

The trick for MOTW is that it can only be the only one in line. Meaning:

Wrong:

<!-- saved from url=(0014)about:internet --><!DOCTYPE html>
<html>
    <head>

Good:

<!-- saved from url=(0014)about:internet -->
<!DOCTYPE html>
<html>
    <head>

I guess one RemoveComments is required to be $false for KeepCommentsRegex to work? Or it doesn't matter for this option?

trullock added a commit that referenced this issue Jan 1, 2021
@trullock
Copy link
Owner

trullock commented Jan 1, 2021

I've updated the XMLDOC Comments on HtmlSettings to show that indeed, KeepCommentsRegex only applies when RemoveComments == true (otherwise all comments would be kept anyways).

With regards the comments remaining on the same line before the doctype, Id say this is really a bug with #190

Ive added a PR to address both

@trullock trullock reopened this Jan 1, 2021
@trullock trullock mentioned this issue Jan 1, 2021
@trullock
Copy link
Owner

trullock commented Jan 2, 2021

fixed in 1.13.1

@PrzemyslawKlys
Copy link
Author

I've updated the XMLDOC Comments on HtmlSettings to show that indeed, KeepCommentsRegex only applies when RemoveComments == true (otherwise all comments would be kept anyways).

With regards the comments remaining on the same line before the doctype, Id say this is really a bug with #190

Ive added a PR to address both

So, I've came back to testing this - and it works fine for formatting

image

For optimize/minify
image

I need new line after that comment. Any way to force it?

@PrzemyslawKlys
Copy link
Author

At this point, I think I could always try and force adding of that comment myself outside of your library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants