diff --git a/bench/bench.js b/bench/bench.js index cb3dd93..2f5195b 100644 --- a/bench/bench.js +++ b/bench/bench.js @@ -45,8 +45,8 @@ const programs = { removeScriptTypeAttributes: true, removeStyleLinkTypeAttributes: true, removeTagWhitespace: true, - sortAttributes: true, - sortClassName: true, + sortAttributes: false, + sortClassName: false, trimCustomFragments: false, useShortDoctype: true, }).length, @@ -153,9 +153,9 @@ suite .on('complete', async function () { const speedResults = this.map(b => ({ name: b.name, - count: b.count, ops: b.hz, - })).sort((a, b) => a.hz - b.hz); + })); + fs.writeFileSync(path.join(__dirname, "speed.json"), JSON.stringify(speedResults, null, 2)); await renderChart('speed', { type: 'bar', data: { @@ -170,6 +170,7 @@ suite const testNames = Object.keys(sizes); const programNames = Object.keys(programs); + fs.writeFileSync(path.join(__dirname, "minification.json"), JSON.stringify(sizes, null, 2)); await renderChart('minification', { type: 'bar', scaleFontColor: 'red', diff --git a/src/unit/tag.rs b/src/unit/tag.rs index b652d08..31dafca 100644 --- a/src/unit/tag.rs +++ b/src/unit/tag.rs @@ -36,6 +36,7 @@ fn is_valid_tag_name_char(c: u8) -> bool { is_alphanumeric(c) || c == b':' || c == b'-' } +#[derive(Copy, Clone)] enum TagType { Script, Style, @@ -89,8 +90,8 @@ pub fn process_tag(proc: &mut Processor, prev_sibling_closing_tag: Option = None; let mut self_closing = false; - // Value of any "type" attribute; if multiple, last kept. - let mut attr_type_value: Option = None; + // Set to false if `tag_type` is Script and "type" attribute exists and has value that is not empty and not one of `JAVASCRIPT_MIME_TYPES`. + let mut script_tag_type_is_js: bool = true; loop { // At the beginning of this loop, the last parsed unit was either the tag name or an attribute (including its value, if it had one). @@ -111,6 +112,10 @@ pub fn process_tag(proc: &mut Processor, prev_sibling_closing_tag: Option attr_type_value = value, + match (tag_type, &proc[name]) { + (TagType::Script, b"type") => { + // It's JS if the value is empty or one of `JAVASCRIPT_MIME_TYPES`. + script_tag_type_is_js = value.filter(|v| !JAVASCRIPT_MIME_TYPES.contains(&proc[*v])).is_none(); + if script_tag_type_is_js { + erase_attr = true; + }; + }, _ => {} }; - last_attr_type = Some(typ); + if erase_attr { + proc.erase_written(attr_checkpoint); + } else { + last_attr_type = Some(typ); + }; }; if self_closing || VOID_TAGS.contains(&proc[opening_name_range]) { @@ -131,7 +146,7 @@ pub fn process_tag(proc: &mut Processor, prev_sibling_closing_tag: Option if attr_type_value.is_none() || attr_type_value.filter(|n| JAVASCRIPT_MIME_TYPES.contains(&proc[*n])).is_some() { + TagType::Script => if script_tag_type_is_js { process_js_script(proc)?; } else { process_text_script(proc)?;