Omit closing tags for html, head, body

This commit is contained in:
Wilson Lin 2020-07-24 19:05:29 +10:00
parent 4fcb36f59b
commit 9f40000527
3 changed files with 42 additions and 2 deletions

View File

@ -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": {

View File

@ -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);

View File

@ -69,6 +69,15 @@ fn test_removal_of_optional_tags() {
eval(b"<rt></rt>", b"<rt>");
eval(b"<rt></rt><rp>1</rp><div></div>", b"<rt><rp>1</rp><div></div>");
eval(b"<div><rt></rt></div>", b"<div><rt></div>");
eval(br#"
<html>
<head>
</head>
<body>
</body>
</html>
"#, b"<html><head><body>");
}
#[test]