Skip to content

Commit

Permalink
Fix white-space around phrasing content in head
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 20, 2017
1 parent 8120fcd commit 734d83e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
19 changes: 14 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function format(options) {

function transform(tree) {
var root = minify(tree);
var head = false;

visit(root, visitor);

Expand All @@ -48,8 +49,16 @@ function format(options) {
var child;
var newline;

if (node.type === 'element' && node.tagName === 'head') {
head = true;
}

if (head && node.type === 'element' && node.tagName === 'body') {
head = false;
}

/* Don’t indent content of whitespace-sensitive nodes / inlines. */
if (!length || !padding(node) || ignore(parents.concat(node))) {
if (!length || !padding(node, head) || ignore(parents.concat(node))) {
return;
}

Expand Down Expand Up @@ -78,7 +87,7 @@ function format(options) {
while (++index < length) {
child = children[index];

if (padding(child) || (newline && index === 0)) {
if (padding(child, head) || (newline && index === 0)) {
result.push({
type: 'text',
value: ((prev && blank(prev) && blank(child)) ? double : single) +
Expand All @@ -90,7 +99,7 @@ function format(options) {
result.push(child);
}

if (newline || padding(prev)) {
if (newline || padding(prev, head)) {
result.push({
type: 'text',
value: single + repeat(indent, level - 1)
Expand All @@ -106,13 +115,13 @@ function format(options) {
}
}

function padding(node) {
function padding(node, head) {
if (node.type === 'root') {
return true;
}

if (node.type === 'element') {
return node.tagName === 'script' || !phrasing(node);
return node.tagName === 'script' || !phrasing(node) || head;
}

return false;
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/phrasing-in-head/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!doctype html>
<title>Alpha</title>
<link rel="stylesheet" href="bravo.css">
<h1>Charlie</h1>
10 changes: 10 additions & 0 deletions test/fixtures/phrasing-in-head/output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>Alpha</title>
<link rel="stylesheet" href="bravo.css">
</head>
<body>
<h1>Charlie</h1>
</body>
</html>

0 comments on commit 734d83e

Please sign in to comment.