posthtml / htmlnano

Modular HTML minifier, built on top of the PostHTML

Home Page:https://htmlnano.netlify.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Minify default and invalid attribute values

maltsev opened this issue · comments

An invalid keyword and an empty string will be handled as the anonymous keyword.
Source

- <img crossorigin="anonymous">
+ <img crossorigin>

@fstanis, thanks for the idea!

Perhaps we can make the issue more generic? There may be other attributes with a similar behavior (I don't know of any, but probably worth looking into).

As for implementation, do you think that this should be a separate module (e.g. simplifyAttributes) or an extension to collapseBooleanAttributes?

Looking further into this, it seems this is covered by the spec in two forms:

  • missing value default, when the attribute has no value and defaults to something. This is the case with <img crossorigin> being the same as <img crossorigin="anonymous"> because missing value default for crossorigin is anonymous. I think this is something we should add as an optimization.
  • invalid value default, when setting to an invalid value has the same meaning as another value. For example, <track kind="foobar"> is the same as <track kind="metadata"> but is not the same as <track kind> or <track> (they are the same as <track kind="subtitles">). We could add this as an optimization, e.g. replace <track kind="metadata"> with <track kind="m">, but I don't think this is a good idea.

It also seems that there are many cases when missing value default is same as just not specifying the attribute at all, e.g. <track kind="subtitles"> == <track kind> == <track>. Ideally, this optimization would just transform <track kind="subtitles"> to <track kind> and then let removeEmptyAttributes take care of removing kind all together.

When I have some extra time, I'll take a pass at the spec and try to list of all cases of missing value default and invalid value default here.

Perhaps we can make the issue more generic?

Sure. I just renamed it.

As for implementation, do you think that this should be a separate module (e.g. simplifyAttributes) or an extension to collapseBooleanAttributes?

I'm not sure, but perhaps the latter one is better. What do you think?

We could add this as an optimization, e.g. replace <track kind="metadata"> with <track kind="m">, but I don't think this is a good idea.

We might do that in max preset. We could also check if the PostHTML API allows notifying developers about those invalid values.

When I have some extra time, I'll take a pass at the spec and try to list of all cases of missing value default and invalid value default here.

Thanks, Filip! I appreciate your help!

How to disable this behavior ?

I need to keep the attribute crossorigin="use-credentials" in my html for my manifest file :

<link rel="manifest" href="/app.webmanifest" crossorigin="use-credentials">

Without it, it's download fail for website behind credentials, even if file is in same origin.

see mdn

  1. I don't think this project has this behavior (yet)?

  2. If it did, it wouldn't remove crossorigin="use-credentials", it would only replace crossorigin="anonymous" with crossorigin.

We use parcel. HTML minification is handled by htmlnano and current behavior replace crossorigin="use-credentials" with crossorigin=""

when i disable minification i don't have issue

Well it's strange but adding "collapseBooleanAttributes": false to .htmlnanorc solved my issue.

I don't think crossorigin is a boolean attribute.

I will open new issue.

Ugh, you're right. Created #79 to fix.

commented

Related PRs:

#107 #114