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

minify-html does not respect whitespaces in <pre> #21

Closed
Keats opened this issue Feb 5, 2021 · 2 comments
Closed

minify-html does not respect whitespaces in <pre> #21

Keats opened this issue Feb 5, 2021 · 2 comments

Comments

@Keats
Copy link

Keats commented Feb 5, 2021

For example:

<pre style="background-color:#2b303b;">
    <code class="language-rust" data-lang="rust"><span style="color:#b48ead;">fn </span><span style="color:#8fa1b3;">main</span><span style="color:#c0c5ce;">() {
      println!(&quot;</span><span style="color:#a3be8c;">Hello, world!</span><span style="color:#c0c5ce;">&quot;);
    }
    </span></code></pre>

After ./target/debug/minify-html-cli --src test.html -o out.html:

<pre style=background-color:#2b303b;>
    <code class=language-rust data-lang=rust><span style=color:#b48ead;>fn </span><span style=color:#8fa1b3;>main</span><span style=color:#c0c5ce;>() { println!("</span><span style=color:#a3be8c;>Hello, world!</span><span style=color:#c0c5ce;>"); } </span></code></pre>

minify-html should not touch any whitespace inside <pre> since it's already formatted.

@flother
Copy link

flother commented Feb 5, 2021

I think this is due to the context-aware minification being reset when there's a child element within a <pre> or <code> element. For example, this HTML:

<pre><code>fn main() {
  println!("Hello, world!");
  loop {
    println!("Hello, world!");
  }
}
</code></pre>

<pre><code>fn main() {
  println!("Hello, world!");
  <span>loop {
    println!("Hello, world!");
  }</span>
}
</code></pre>

Is converted into this minified HTML:

<pre><code>fn main() {
  println!("Hello, world!");
  loop {
    println!("Hello, world!");
  }
}
</code></pre><pre><code>fn main() {
  println!("Hello, world!");
  <span>loop { println!("Hello, world!"); }</span>
}
</code></pre>

The two code blocks are the same, except the second has a <span> child element. Within that element, all whitespace is collapsed. I think the solution is to keep the <pre> context-awareness unchanged when the parser encounters phrasing content children.

@wilsonzlin
Copy link
Owner

Thanks for raising, I've updated the code to use <pre> whitespace minification rules (i.e. none) for all descendants of <pre>. Using the latest version 0.4.2, the output is:

<pre><code>fn main() {
  println!("Hello, world!");
  loop {
    println!("Hello, world!");
  }
}
</code></pre><pre><code>fn main() {
  println!("Hello, world!");
  <span>loop {
    println!("Hello, world!");
  }</span>
}
</code></pre>

Keats pushed a commit to getzola/zola that referenced this issue Feb 6, 2021
* Update minify-html dependency to version 0.4.2

Fixes #1300. See also wilsonzlin/minify-html#21

* Update minify-html dependency in Cargo.lock

* Add test to check pre whitespace isn't collapsed
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

No branches or pull requests

3 participants