2019-12-25 21:47:18 -05:00
|
|
|
use crate::err::{ErrorType, ProcessingResult};
|
2019-12-27 05:52:49 -05:00
|
|
|
use crate::proc::{Processor, ProcessorRange};
|
2019-12-23 06:48:41 -05:00
|
|
|
use crate::spec::codepoint::{is_alphanumeric, is_whitespace};
|
|
|
|
use crate::spec::tag::void::VOID_TAGS;
|
2019-12-27 05:52:49 -05:00
|
|
|
use crate::unit::attr::{AttrType, process_attr, ProcessedAttr};
|
2019-12-25 04:44:51 -05:00
|
|
|
use crate::unit::content::process_content;
|
|
|
|
use crate::unit::style::process_style;
|
2019-12-27 05:52:49 -05:00
|
|
|
use phf::{Set, phf_set};
|
|
|
|
use crate::unit::script::text::process_text_script;
|
|
|
|
use crate::unit::script::js::process_js_script;
|
|
|
|
|
|
|
|
pub static JAVASCRIPT_MIME_TYPES: Set<&'static [u8]> = phf_set! {
|
|
|
|
b"application/ecmascript",
|
|
|
|
b"application/javascript",
|
|
|
|
b"application/x-ecmascript",
|
|
|
|
b"application/x-javascript",
|
|
|
|
b"text/ecmascript",
|
|
|
|
b"text/javascript",
|
|
|
|
b"text/javascript1.0",
|
|
|
|
b"text/javascript1.1",
|
|
|
|
b"text/javascript1.2",
|
|
|
|
b"text/javascript1.3",
|
|
|
|
b"text/javascript1.4",
|
|
|
|
b"text/javascript1.5",
|
|
|
|
b"text/jscript",
|
|
|
|
b"text/livescript",
|
|
|
|
b"text/x-ecmascript",
|
|
|
|
b"text/x-javascript",
|
|
|
|
};
|
2019-12-23 06:48:41 -05:00
|
|
|
|
|
|
|
// Tag names may only use ASCII alphanumerics. However, some people also use `:` and `-`.
|
|
|
|
// See https://html.spec.whatwg.org/multipage/syntax.html#syntax-tag-name for spec.
|
|
|
|
fn is_valid_tag_name_char(c: u8) -> bool {
|
|
|
|
is_alphanumeric(c) || c == b':' || c == b'-'
|
|
|
|
}
|
|
|
|
|
2019-12-26 01:16:13 -05:00
|
|
|
enum TagType {
|
|
|
|
Script,
|
|
|
|
Style,
|
|
|
|
Other,
|
|
|
|
}
|
|
|
|
|
2019-12-25 21:47:18 -05:00
|
|
|
pub fn process_tag(proc: &mut Processor) -> ProcessingResult<()> {
|
2019-12-25 07:29:18 -05:00
|
|
|
// TODO Minify opening and closing tag whitespace before name and after name/last attr.
|
|
|
|
// TODO DOC No checking if opening and closing names match.
|
2019-12-25 04:44:51 -05:00
|
|
|
// Expect to be currently at an opening tag.
|
2019-12-25 07:29:18 -05:00
|
|
|
chain!(proc.match_char(b'<').expect().keep());
|
2019-12-25 04:44:51 -05:00
|
|
|
// May not be valid tag name at current position, so require instead of expect.
|
2019-12-26 01:16:13 -05:00
|
|
|
let opening_name_range = chain!(proc.match_while_pred(is_valid_tag_name_char).require_with_reason("tag name")?.keep().out_range());
|
|
|
|
|
|
|
|
// TODO DOC: Tags must be case sensitive.
|
|
|
|
let tag_type = match &proc[opening_name_range] {
|
|
|
|
b"script" => TagType::Script,
|
|
|
|
b"style" => TagType::Style,
|
|
|
|
_ => TagType::Other,
|
|
|
|
};
|
2019-12-23 06:48:41 -05:00
|
|
|
|
2019-12-26 00:17:57 -05:00
|
|
|
let mut last_attr_type: Option<AttrType> = None;
|
2019-12-23 06:48:41 -05:00
|
|
|
let mut self_closing = false;
|
2019-12-27 05:52:49 -05:00
|
|
|
// Value of any "type" attribute; if multiple, last kept.
|
|
|
|
let mut attr_type_value: Option<ProcessorRange> = None;
|
2019-12-23 06:48:41 -05:00
|
|
|
|
|
|
|
loop {
|
2019-12-26 08:23:33 -05:00
|
|
|
// 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).
|
2019-12-25 07:29:18 -05:00
|
|
|
let ws_accepted = chain!(proc.match_while_pred(is_whitespace).discard().matched());
|
2019-12-23 06:48:41 -05:00
|
|
|
|
2019-12-25 07:29:18 -05:00
|
|
|
if chain!(proc.match_char(b'>').keep().matched()) {
|
2019-12-23 06:48:41 -05:00
|
|
|
// End of tag.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-12-25 07:29:18 -05:00
|
|
|
self_closing = chain!(proc.match_seq(b"/>").keep().matched());
|
2019-12-25 04:44:51 -05:00
|
|
|
if self_closing {
|
2019-12-23 06:48:41 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-12-26 00:17:57 -05:00
|
|
|
// This needs to be enforced as otherwise there would be difficulty in determining what is the end of a tag/attribute name/attribute value.
|
2019-12-23 06:48:41 -05:00
|
|
|
if !ws_accepted {
|
2019-12-25 07:29:18 -05:00
|
|
|
return Err(ErrorType::NoSpaceBeforeAttr);
|
2019-12-23 06:48:41 -05:00
|
|
|
}
|
|
|
|
|
2019-12-26 00:17:57 -05:00
|
|
|
// Write space after tag name or unquoted/valueless attribute.
|
2019-12-27 05:52:49 -05:00
|
|
|
// Don't write after unquoted.
|
2019-12-26 00:17:57 -05:00
|
|
|
match last_attr_type {
|
2019-12-26 08:23:33 -05:00
|
|
|
Some(AttrType::Unquoted) | Some(AttrType::NoValue) | None => proc.write(b' '),
|
|
|
|
_ => {}
|
2019-12-26 00:17:57 -05:00
|
|
|
};
|
2019-12-23 06:48:41 -05:00
|
|
|
|
2019-12-27 05:52:49 -05:00
|
|
|
let ProcessedAttr { name, typ, value } = process_attr(proc)?;
|
|
|
|
match &proc[name] {
|
|
|
|
b"type" => attr_type_value = value,
|
|
|
|
_ => {},
|
|
|
|
};
|
|
|
|
last_attr_type = Some(typ);
|
2019-12-25 04:44:51 -05:00
|
|
|
};
|
2019-12-23 06:48:41 -05:00
|
|
|
|
2019-12-25 07:29:18 -05:00
|
|
|
if self_closing || VOID_TAGS.contains(&proc[opening_name_range]) {
|
2019-12-23 06:48:41 -05:00
|
|
|
return Ok(());
|
2019-12-25 04:44:51 -05:00
|
|
|
};
|
2019-12-23 06:48:41 -05:00
|
|
|
|
2019-12-26 01:16:13 -05:00
|
|
|
match tag_type {
|
2019-12-27 05:52:49 -05:00
|
|
|
TagType::Script => if attr_type_value.is_none() || attr_type_value.filter(|n| JAVASCRIPT_MIME_TYPES.contains(&proc[*n])).is_some() {
|
|
|
|
process_js_script(proc)?;
|
|
|
|
} else {
|
|
|
|
process_text_script(proc)?;
|
|
|
|
},
|
2019-12-26 01:16:13 -05:00
|
|
|
TagType::Style => process_style(proc)?,
|
2019-12-25 07:29:18 -05:00
|
|
|
_ => process_content(proc, Some(opening_name_range))?,
|
2019-12-25 04:44:51 -05:00
|
|
|
};
|
2019-12-23 06:48:41 -05:00
|
|
|
|
|
|
|
// Require closing tag for non-void.
|
2019-12-29 05:39:29 -05:00
|
|
|
chain!(proc.match_seq(b"</").require()?.keep());
|
2019-12-25 07:29:18 -05:00
|
|
|
chain!(proc.match_while_pred(is_valid_tag_name_char).require_with_reason("closing tag name")?.keep());
|
2019-12-29 05:39:29 -05:00
|
|
|
chain!(proc.match_char(b'>').require()?.keep());
|
2019-12-23 06:48:41 -05:00
|
|
|
Ok(())
|
|
|
|
}
|