Fix content whitespace trimming

This commit is contained in:
Wilson Lin 2020-01-19 11:32:38 +11:00
parent e683eed0b5
commit 9f93a5d6de
7 changed files with 72 additions and 77 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 32 KiB

View File

@ -5,8 +5,8 @@
"relative": 1
},
"hyperbuild-nodejs": {
"absolute": 353722,
"relative": 0.9590744436250054
"absolute": 353540,
"relative": 0.9585809726259165
},
"html-minifier": {
"absolute": 355185,
@ -23,8 +23,8 @@
"relative": 1
},
"hyperbuild-nodejs": {
"absolute": 231728,
"relative": 0.9411535352961006
"absolute": 231718,
"relative": 0.9411129207162787
},
"html-minifier": {
"absolute": 234306,
@ -59,8 +59,8 @@
"relative": 1
},
"hyperbuild-nodejs": {
"absolute": 272551,
"relative": 0.8799291022851277
"absolute": 270869,
"relative": 0.8744987764010047
},
"html-minifier": {
"absolute": 277911,
@ -77,8 +77,8 @@
"relative": 1
},
"hyperbuild-nodejs": {
"absolute": 80117,
"relative": 0.9466961289407761
"absolute": 79807,
"relative": 0.9430330387105922
},
"html-minifier": {
"absolute": 81446,
@ -95,8 +95,8 @@
"relative": 1
},
"hyperbuild-nodejs": {
"absolute": 5761075,
"relative": 0.9120836287948699
"absolute": 5744290,
"relative": 0.9094262560459782
},
"html-minifier": {
"absolute": 5785725,
@ -113,8 +113,8 @@
"relative": 1
},
"hyperbuild-nodejs": {
"absolute": 196585,
"relative": 0.996426580161184
"absolute": 196577,
"relative": 0.9963860307162046
},
"html-minifier": {
"absolute": 196600,
@ -131,8 +131,8 @@
"relative": 1
},
"hyperbuild-nodejs": {
"absolute": 28203,
"relative": 0.8289879780135798
"absolute": 28153,
"relative": 0.8275182975221187
},
"html-minifier": {
"absolute": 29086,
@ -149,8 +149,8 @@
"relative": 1
},
"hyperbuild-nodejs": {
"absolute": 1263691,
"relative": 0.9967322220416869
"absolute": 1263686,
"relative": 0.996728278307728
},
"html-minifier": {
"absolute": 1263150,
@ -167,8 +167,8 @@
"relative": 1
},
"hyperbuild-nodejs": {
"absolute": 648011,
"relative": 0.994527124192536
"absolute": 648004,
"relative": 0.9945163810263407
},
"html-minifier": {
"absolute": 647822,
@ -185,8 +185,8 @@
"relative": 1
},
"hyperbuild-nodejs": {
"absolute": 87177,
"relative": 0.7767778381701699
"absolute": 86691,
"relative": 0.7724474066417771
},
"html-minifier": {
"absolute": 88366,
@ -203,8 +203,8 @@
"relative": 1
},
"hyperbuild-nodejs": {
"absolute": 272916,
"relative": 0.8659766336457732
"absolute": 272478,
"relative": 0.8645868369114782
},
"html-minifier": {
"absolute": 266639,
@ -221,8 +221,8 @@
"relative": 1
},
"hyperbuild-nodejs": {
"absolute": 1320740,
"relative": 0.9359283707317765
"absolute": 1319434,
"relative": 0.9350028877054611
},
"html-minifier": {
"absolute": 1327244,

Binary file not shown.

Before

Width:  |  Height:  |  Size: 57 KiB

After

Width:  |  Height:  |  Size: 57 KiB

View File

@ -1,67 +1,67 @@
{
"Amazon": {
"hyperbuild-nodejs": 496.69767089636576,
"html-minifier": 36.22644969886744,
"minimize": 113.46511363852734
"hyperbuild-nodejs": 502.24664208689205,
"html-minifier": 35.29094514423333,
"minimize": 115.65576358024069
},
"BBC": {
"hyperbuild-nodejs": 530.7581308772171,
"html-minifier": 49.92474956429014,
"minimize": 160.83375271616242
"hyperbuild-nodejs": 534.2273806686738,
"html-minifier": 48.494203597533264,
"minimize": 162.19058006327936
},
"Bing": {
"hyperbuild-nodejs": 2143.2738405584682,
"html-minifier": 224.01168295253845,
"minimize": 548.4301545148764
"hyperbuild-nodejs": 2140.7640403400474,
"html-minifier": 219.51796458703646,
"minimize": 556.4985790478128
},
"Bootstrap": {
"hyperbuild-nodejs": 271.9278016916176,
"html-minifier": 8.063475087253176,
"minimize": 22.267478295105477
"hyperbuild-nodejs": 281.194875239674,
"html-minifier": 7.8415099622038555,
"minimize": 22.529031134122878
},
"Coding Horror": {
"hyperbuild-nodejs": 1023.4279917437968,
"html-minifier": 48.701642742613835,
"minimize": 186.3127301413615
"hyperbuild-nodejs": 1047.0958113343395,
"html-minifier": 47.42413048467408,
"minimize": 192.42462057036215
},
"ECMA-262": {
"hyperbuild-nodejs": 15.516204982806318,
"html-minifier": 0.44813616072549084,
"minimize": 1.3379096629237164
"hyperbuild-nodejs": 15.772555656834477,
"html-minifier": 0.44192845474704334,
"minimize": 1.3294991658715587
},
"Google": {
"hyperbuild-nodejs": 1828.0608850870312,
"html-minifier": 243.05811241714517,
"minimize": 575.3743754787441
"hyperbuild-nodejs": 1835.2139045218082,
"html-minifier": 241.2525641836837,
"minimize": 580.9205037326506
},
"Hacker News": {
"hyperbuild-nodejs": 2098.1972805031955,
"html-minifier": 73.59734557807897,
"minimize": 271.03920934848253
"hyperbuild-nodejs": 2078.0864684534695,
"html-minifier": 73.73992684219166,
"minimize": 275.23770113550273
},
"NY Times": {
"hyperbuild-nodejs": 264.01040398999527,
"html-minifier": 36.483800875972946,
"minimize": 86.74545622195224
"hyperbuild-nodejs": 262.6791315524891,
"html-minifier": 37.21473163759241,
"minimize": 87.5459988860411
},
"Reddit": {
"hyperbuild-nodejs": 402.9146825613306,
"html-minifier": 44.44689052201565,
"minimize": 124.65890370904272
"hyperbuild-nodejs": 392.33718670985326,
"html-minifier": 45.080090941207395,
"minimize": 126.02956065880237
},
"Stack Overflow": {
"hyperbuild-nodejs": 822.1712538713629,
"html-minifier": 40.65363487825213,
"minimize": 158.40795613979014
"hyperbuild-nodejs": 806.413713289417,
"html-minifier": 41.36174599511134,
"minimize": 161.42178263684914
},
"Twitter": {
"hyperbuild-nodejs": 279.88535523093344,
"html-minifier": 36.231829473085334,
"minimize": 167.45777652008428
"hyperbuild-nodejs": 277.80868572702855,
"html-minifier": 37.062907987061436,
"minimize": 168.569458535333
},
"Wikipedia": {
"hyperbuild-nodejs": 54.31371594883309,
"html-minifier": 2.808823542495513,
"minimize": 8.687326663074192
"hyperbuild-nodejs": 54.76838615471663,
"html-minifier": 2.7997721992329714,
"minimize": 8.79398557057751
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 65 KiB

After

Width:  |  Height:  |  Size: 66 KiB

View File

@ -31,13 +31,6 @@ impl ContentType {
}
}
fn is_position(&self) -> bool {
match self {
ContentType::Start | ContentType::End => true,
_ => false,
}
}
fn peek(proc: &mut Processor) -> ContentType {
// Manually write out matching for fast performance as this is hot spot; don't use generated trie.
match proc.peek_offset_eof(0) {
@ -95,7 +88,7 @@ pub fn process_content(proc: &mut Processor, parent: Option<ProcessorRange>) ->
if destroy_whole && last_written.is_tag_like() && next_content_type.is_tag_like() {
// Whitespace is between two tags, comments, instructions, or bangs.
// `destroy_whole` is on, so don't write it.
} else if trim && last_written.is_position() {
} else if trim && (last_written == ContentType::Start || next_content_type == ContentType::End) {
// Whitespace is leading or trailing.
// `trim` is on, so don't write it.
} else if collapse {
@ -115,6 +108,11 @@ pub fn process_content(proc: &mut Processor, parent: Option<ProcessorRange>) ->
// Process and consume next character(s).
match next_content_type {
ContentType::Comment => {
// Comments are completely ignored and do not affect anything (previous element node's closing tag, unintentional entities, whitespace, etc.).
process_comment(proc)?;
continue;
}
ContentType::Tag => {
proc.suspend(uep);
let new_closing_tag = process_tag(
@ -146,10 +144,9 @@ pub fn process_content(proc: &mut Processor, parent: Option<ProcessorRange>) ->
proc.resume(uep);
};
match content_type {
ContentType::Comment | ContentType::Bang | ContentType::Instruction => {
ContentType::Bang | ContentType::Instruction => {
proc.suspend(uep);
match content_type {
ContentType::Comment => { process_comment(proc)?; }
ContentType::Bang => { process_bang(proc)?; }
ContentType::Instruction => { process_instruction(proc)?; }
_ => unreachable!(),
@ -180,10 +177,8 @@ pub fn process_content(proc: &mut Processor, parent: Option<ProcessorRange>) ->
}
};
// Comments are discarded.
if next_content_type != ContentType::Comment {
last_written = next_content_type;
};
// This should not be reached if ContentType::Comment.
last_written = next_content_type;
};
Ok(())