1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use crate::err::ProcessingResult;
use crate::proc::{Checkpoint, Processor, ProcessorRange};
use crate::spec::codepoint::is_whitespace;
use crate::spec::tag::content::CONTENT_TAGS;
use crate::spec::tag::contentfirst::CONTENT_FIRST_TAGS;
use crate::spec::tag::formatting::FORMATTING_TAGS;
use crate::spec::tag::wss::WSS_TAGS;
use crate::unit::bang::process_bang;
use crate::unit::comment::process_comment;
use crate::unit::entity::{EntityType, parse_entity};
use crate::unit::tag::process_tag;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
enum ContentType {
Comment,
Bang,
OpeningTag,
Start,
End,
Entity,
Whitespace,
Text,
}
impl ContentType {
fn is_comment_bang_opening_tag(&self) -> bool {
match self {
ContentType::Comment | ContentType::Bang | ContentType::OpeningTag => true,
_ => false,
}
}
fn peek(proc: &mut Processor) -> ContentType {
if proc.at_end() || chain!(proc.match_seq(b"</").matched()) {
return ContentType::End;
};
if chain!(proc.match_pred(is_whitespace).matched()) {
return ContentType::Whitespace;
};
if chain!(proc.match_seq(b"<!--").matched()) {
return ContentType::Comment;
};
if chain!(proc.match_seq(b"<!").matched()) {
return ContentType::Bang;
};
if chain!(proc.match_char(b'<').matched()) {
return ContentType::OpeningTag;
};
if chain!(proc.match_char(b'&').matched()) {
return ContentType::Entity;
};
ContentType::Text
}
}
pub fn process_content(proc: &mut Processor, parent: Option<ProcessorRange>) -> ProcessingResult<()> {
let collapse_whitespace = match parent {
Some(tag_name) => !WSS_TAGS.contains(&proc[tag_name]),
None => true,
};
let destroy_whole_whitespace = match parent {
Some(tag_name) => !WSS_TAGS.contains(&proc[tag_name]) && !CONTENT_TAGS.contains(&proc[tag_name]) && !CONTENT_FIRST_TAGS.contains(&proc[tag_name]) && !FORMATTING_TAGS.contains(&proc[tag_name]),
None => true,
};
let trim_whitespace = match parent {
Some(tag_name) => !WSS_TAGS.contains(&proc[tag_name]) && !FORMATTING_TAGS.contains(&proc[tag_name]),
None => true,
};
let mut last_non_whitespace_content_type = ContentType::Start;
let mut whitespace_checkpoint_opt: Option<Checkpoint> = None;
let mut entity: Option<EntityType> = None;
loop {
let next_content_type = match ContentType::peek(proc) {
ContentType::Entity => {
entity = Some(parse_entity(proc, false)?);
let ws = match entity {
Some(EntityType::Ascii(c)) => is_whitespace(c),
_ => false,
};
if ws {
ContentType::Whitespace
} else {
ContentType::Entity
}
}
ContentType::Whitespace => {
proc.skip_expect();
ContentType::Whitespace
}
other_type => other_type,
};
if next_content_type == ContentType::Whitespace {
match whitespace_checkpoint_opt {
None => {
whitespace_checkpoint_opt = Some(proc.checkpoint());
}
_ => {
}
}
continue;
}
if let Some(ws) = whitespace_checkpoint_opt {
if destroy_whole_whitespace && last_non_whitespace_content_type.is_comment_bang_opening_tag() && next_content_type.is_comment_bang_opening_tag() {
} else if trim_whitespace && (next_content_type == ContentType::End || last_non_whitespace_content_type == ContentType::Start) {
} else if collapse_whitespace {
proc.write(b' ');
} else {
proc.write_skipped(ws);
}
whitespace_checkpoint_opt = None;
};
match next_content_type {
ContentType::Comment => { process_comment(proc)?; }
ContentType::Bang => { process_bang(proc)?; }
ContentType::OpeningTag => { process_tag(proc)?; }
ContentType::End => { break; }
ContentType::Entity => entity.unwrap().keep(proc),
ContentType::Text => { proc.accept()?; }
_ => unreachable!(),
};
last_non_whitespace_content_type = next_content_type;
};
Ok(())
}