From 9f40000527d0c85d4f671c65c85957f86fe8ab66 Mon Sep 17 00:00:00 2001 From: Wilson Lin Date: Fri, 24 Jul 2020 19:05:29 +1000 Subject: [PATCH] Omit closing tags for html, head, body --- nodejs/package.json | 1 + src/spec/tag/omission.rs | 34 ++++++++++++++++++++++++++++++++-- src/tests/mod.rs | 9 +++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/nodejs/package.json b/nodejs/package.json index bebbf18..c9919c2 100644 --- a/nodejs/package.json +++ b/nodejs/package.json @@ -8,6 +8,7 @@ ], "scripts": { "build": "node-gyp build && shx mv build/Release/index.node index.node", + "clean": "cd native && cargo clean && cd .. && node-gyp clean && node-gyp configure && shx rm -f index.node", "postinstall": "node postinstall.js" }, "repository": { diff --git a/src/spec/tag/omission.rs b/src/spec/tag/omission.rs index 64c8e13..89ececf 100644 --- a/src/spec/tag/omission.rs +++ b/src/spec/tag/omission.rs @@ -2,7 +2,6 @@ use lazy_static::lazy_static; use std::collections::{HashSet, HashMap}; // Rules sourced from https://html.spec.whatwg.org/multipage/syntax.html#syntax-tag-omission. -// TODO html, head, body // TODO Opening tags pub enum ClosingTagOmissionRuleIfLast { @@ -38,6 +37,31 @@ impl ClosingTagOmissionRule { } } +lazy_static! { + static ref HTML_CLOSING_TAG_OMISSION_RULE: ClosingTagOmissionRule = ClosingTagOmissionRule { + followed_by: HashSet::new(), + is_last: ClosingTagOmissionRuleIfLast::Always, + }; +} + +lazy_static! { + static ref HEAD_CLOSING_TAG_OMISSION_RULE: ClosingTagOmissionRule = ClosingTagOmissionRule { + followed_by: { + let mut s = HashSet::<&'static [u8]>::new(); + s.insert(b"body"); + s + }, + is_last: ClosingTagOmissionRuleIfLast::Always, + }; +} + +lazy_static! { + static ref BODY_CLOSING_TAG_OMISSION_RULE: ClosingTagOmissionRule = ClosingTagOmissionRule { + followed_by: HashSet::new(), + is_last: ClosingTagOmissionRuleIfLast::Always, + }; +} + lazy_static! { static ref LI_CLOSING_TAG_OMISSION_RULE: ClosingTagOmissionRule = ClosingTagOmissionRule { followed_by: { @@ -116,7 +140,10 @@ lazy_static! { is_last_tags.insert(b"noscript"); is_last_tags.insert(b"video"); - ClosingTagOmissionRule { followed_by, is_last: ClosingTagOmissionRuleIfLast::ParentIsNot(is_last_tags) } + ClosingTagOmissionRule { + followed_by, + is_last: ClosingTagOmissionRuleIfLast::ParentIsNot(is_last_tags), + } }; } @@ -236,6 +263,9 @@ lazy_static! { lazy_static! { pub static ref CLOSING_TAG_OMISSION_RULES: HashMap<&'static [u8], &'static ClosingTagOmissionRule> = { let mut m = HashMap::<&'static [u8], &'static ClosingTagOmissionRule>::new(); + m.insert(b"html", &HTML_CLOSING_TAG_OMISSION_RULE); + m.insert(b"head", &HEAD_CLOSING_TAG_OMISSION_RULE); + m.insert(b"body", &BODY_CLOSING_TAG_OMISSION_RULE); m.insert(b"li", &LI_CLOSING_TAG_OMISSION_RULE); m.insert(b"dt", &DT_CLOSING_TAG_OMISSION_RULE); m.insert(b"dd", &DD_CLOSING_TAG_OMISSION_RULE); diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 4d108c0..264caaf 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -69,6 +69,15 @@ fn test_removal_of_optional_tags() { eval(b"", b""); eval(b"1
", b"1
"); eval(b"
", b"
"); + eval(br#" + + + + + + + + "#, b""); } #[test]