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

Line breaks minifying to a space #84

Closed
safetymonkey opened this issue Mar 16, 2023 · 4 comments
Closed

Line breaks minifying to a space #84

safetymonkey opened this issue Mar 16, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@safetymonkey
Copy link

I think this may be the same issue reported in #47, but it was self-closed and I didn't find the author's solution satisfying.

Basically, I'm seeing confusing behavior about how line breaks are minified. For example, below is the content of my Jinja2 HTML template (leading spaces intentionally kept in to reflect how it looks in my code):

    <nav class="navbar bg-primary fixed-top navbar-expand-md">
      <div class="container-fluid">
        <button
          class="navbar-toggler"
          type="button"
        >
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="nav-collapse">

It got minified to the following, with spaces between the tags:

<nav class="navbar bg-primary fixed-top navbar-expand-md"> <div class="container-fluid"> <button class="navbar-toggler" type="button"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="nav-collapse">

It's not consistent, though. At the top of that same file (my top-level layout template), I have:

<!DOCTYPE html>
<html lang="en">
  <head>
    <link rel="manifest" href="/manifest.json" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="mobile-web-app-capable" content="yes" />
    <meta name="theme-color" content="#2c3e50" />

And that correctly minifies to:

<!DOCTYPE html><html lang="en"> <head><link rel="manifest" href="/manifest.json"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="mobile-web-app-capable" content="yes"><meta name="theme-color" content="#2c3e50">

Any ideas what's going on? It's not the end of the world but as long as I'm saving bytes, I want to save ALL the bytes. Thanks!

@safetymonkey
Copy link
Author

After I posted, this I noticed there's a single space between <html lang="en"> and <head> in my clean example above, so now I'm really confused.

@mrf345
Copy link
Owner

mrf345 commented Mar 16, 2023

It's an option that can be enabled for htmlmin, this should it:

from flask_minify import Minify
from flask_minify.parsers import Html as HtmlParser


class CustomHtmlParser(HtmlParser):
     runtime_options = {**HtmlParser.runtime_options, "remove_empty_space": True}


# initiating the extension
Minify(app=app, parsers={'html': CustomHtmlParser})

checkout https://github.com/mrf345/flask_minify#--parsers for more info

@mrf345 mrf345 closed this as completed Mar 17, 2023
@skiddy784
Copy link

Why isn't that option enabled by default? It seems reasonable to me...

@mrf345 mrf345 added the enhancement New feature or request label May 7, 2023
@mrf345 mrf345 reopened this May 7, 2023
@mrf345 mrf345 closed this as completed in bd0f0a0 May 7, 2023
@mrf345
Copy link
Owner

mrf345 commented May 7, 2023

Should be enabled by default in 0.42

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants