minify-html/rust/onepass/src/unit/attr/mod.rs

86 lines
3.0 KiB
Rust
Raw Normal View History

2021-08-09 05:56:37 -04:00
use crate::common::gen::attrs::ATTRS;
use crate::common::gen::codepoints::{WHATWG_ATTR_NAME_CHAR, WHITESPACE};
use crate::common::spec::tag::ns::Namespace;
use crate::err::ProcessingResult;
2020-07-30 05:51:43 -04:00
use crate::proc::checkpoint::WriteCheckpoint;
2021-08-08 03:58:10 -04:00
use crate::proc::range::ProcessorRange;
2020-01-25 02:04:02 -05:00
use crate::proc::MatchAction::*;
use crate::proc::MatchMode::*;
use crate::proc::Processor;
2021-08-08 03:58:10 -04:00
use crate::unit::attr::value::{
process_attr_value, skip_attr_value, DelimiterType, ProcessedAttrValue,
};
2019-12-25 04:44:51 -05:00
mod value;
#[derive(Clone, Copy, Eq, PartialEq)]
pub enum AttrType {
Quoted,
Unquoted,
NoValue,
}
2019-12-27 05:52:49 -05:00
pub struct ProcessedAttr {
pub name: ProcessorRange,
pub typ: AttrType,
pub value: Option<ProcessorRange>,
}
2021-08-08 03:58:10 -04:00
pub fn process_attr(
proc: &mut Processor,
ns: Namespace,
element: ProcessorRange,
) -> ProcessingResult<ProcessedAttr> {
// It's possible to expect attribute name but not be called at an attribute, e.g. due to whitespace between name and
// value, which causes name to be considered boolean attribute and `=` to be start of new (invalid) attribute name.
2021-08-08 03:58:10 -04:00
let name = proc
2021-08-08 05:00:51 -04:00
.m(WhileInLookup(WHATWG_ATTR_NAME_CHAR), Keep)
2021-08-08 03:58:10 -04:00
.require("attribute name")?;
proc.make_lowercase(name);
let attr_cfg = ATTRS.get(ns, &proc[element], &proc[name]);
let is_boolean = attr_cfg.filter(|attr| attr.boolean).is_some();
2020-07-30 05:51:43 -04:00
let after_name = WriteCheckpoint::new(proc);
2019-12-25 04:44:51 -05:00
2021-08-10 02:12:57 -04:00
// TODO Use attr cfg: collapse, trim, case_sensitive.
2021-08-08 03:58:10 -04:00
let should_collapse_and_trim_value_ws =
2021-08-10 02:12:57 -04:00
attr_cfg.filter(|attr| attr.collapse && attr.trim).is_some();
2020-07-09 03:06:08 -04:00
proc.m(WhileInLookup(WHITESPACE), Discard);
let has_value = proc.m(IsChar(b'='), Keep).nonempty();
2019-12-25 04:44:51 -05:00
2019-12-27 05:52:49 -05:00
let (typ, value) = if !has_value {
(AttrType::NoValue, None)
2019-12-25 04:44:51 -05:00
} else {
2020-07-09 03:06:08 -04:00
proc.m(WhileInLookup(WHITESPACE), Discard);
2020-01-07 08:38:42 -05:00
if is_boolean {
skip_attr_value(proc)?;
// Discard `=`.
debug_assert_eq!(after_name.written_count(proc), 1);
after_name.erase_written(proc);
2020-01-07 08:38:42 -05:00
(AttrType::NoValue, None)
} else {
match process_attr_value(proc, should_collapse_and_trim_value_ws)? {
2020-01-07 08:38:42 -05:00
ProcessedAttrValue { value: None, .. } => {
// Value is empty, which is equivalent to no value, so discard `=`.
debug_assert_eq!(after_name.written_count(proc), 1);
after_name.erase_written(proc);
2020-01-07 08:38:42 -05:00
(AttrType::NoValue, None)
}
2021-08-08 03:58:10 -04:00
ProcessedAttrValue {
delimiter: DelimiterType::Unquoted,
value,
} => (AttrType::Unquoted, value),
ProcessedAttrValue {
delimiter: DelimiterType::Double,
value,
}
| ProcessedAttrValue {
delimiter: DelimiterType::Single,
value,
} => (AttrType::Quoted, value),
}
}
2019-12-27 05:52:49 -05:00
};
Ok(ProcessedAttr { name, typ, value })
2019-12-25 04:44:51 -05:00
}