const VOID_ELEMENTS: &[&str] = &[
"area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source",
"track", "wbr",
];
const MAX_REPEAT: usize = 1000;
const MAX_ELEMENTS: usize = 100_000;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Profile {
Html,
Xml,
Jsx,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
struct Node {
name: String,
id: Option<String>,
classes: Vec<String>,
attrs: Vec<(String, String)>,
text: Option<String>,
repeat: usize,
children: Vec<Node>,
group: bool,
bare_text: bool,
}
type Alias = (
&'static str,
&'static str,
&'static [(&'static str, &'static str)],
);
const ALIASES: &[Alias] = &[
("input:hidden", "input", &[("type", "hidden"), ("name", "")]),
("input:h", "input", &[("type", "hidden"), ("name", "")]),
(
"input:text",
"input",
&[("type", "text"), ("name", ""), ("id", "")],
),
(
"input:t",
"input",
&[("type", "text"), ("name", ""), ("id", "")],
),
(
"input:search",
"input",
&[("type", "search"), ("name", ""), ("id", "")],
),
(
"input:email",
"input",
&[("type", "email"), ("name", ""), ("id", "")],
),
(
"input:url",
"input",
&[("type", "url"), ("name", ""), ("id", "")],
),
(
"input:password",
"input",
&[("type", "password"), ("name", ""), ("id", "")],
),
(
"input:p",
"input",
&[("type", "password"), ("name", ""), ("id", "")],
),
(
"input:datetime",
"input",
&[("type", "datetime"), ("name", ""), ("id", "")],
),
(
"input:date",
"input",
&[("type", "date"), ("name", ""), ("id", "")],
),
(
"input:datetime-local",
"input",
&[("type", "datetime-local"), ("name", ""), ("id", "")],
),
(
"input:month",
"input",
&[("type", "month"), ("name", ""), ("id", "")],
),
(
"input:week",
"input",
&[("type", "week"), ("name", ""), ("id", "")],
),
(
"input:time",
"input",
&[("type", "time"), ("name", ""), ("id", "")],
),
(
"input:tel",
"input",
&[("type", "tel"), ("name", ""), ("id", "")],
),
(
"input:number",
"input",
&[("type", "number"), ("name", ""), ("id", "")],
),
(
"input:color",
"input",
&[("type", "color"), ("name", ""), ("id", "")],
),
(
"input:checkbox",
"input",
&[("type", "checkbox"), ("name", ""), ("id", "")],
),
(
"input:c",
"input",
&[("type", "checkbox"), ("name", ""), ("id", "")],
),
(
"input:radio",
"input",
&[("type", "radio"), ("name", ""), ("id", "")],
),
(
"input:r",
"input",
&[("type", "radio"), ("name", ""), ("id", "")],
),
(
"input:range",
"input",
&[("type", "range"), ("name", ""), ("id", "")],
),
(
"input:file",
"input",
&[("type", "file"), ("name", ""), ("id", "")],
),
(
"input:f",
"input",
&[("type", "file"), ("name", ""), ("id", "")],
),
(
"input:submit",
"input",
&[("type", "submit"), ("value", "")],
),
("input:s", "input", &[("type", "submit"), ("value", "")]),
(
"input:image",
"input",
&[("type", "image"), ("src", ""), ("alt", "")],
),
(
"input:i",
"input",
&[("type", "image"), ("src", ""), ("alt", "")],
),
(
"input:button",
"input",
&[("type", "button"), ("value", "")],
),
("input:b", "input", &[("type", "button"), ("value", "")]),
("input:reset", "input", &[("type", "reset"), ("value", "")]),
("btn:s", "button", &[("type", "submit")]),
("btn:r", "button", &[("type", "reset")]),
("btn:d", "button", &[("disabled", "")]),
("button:s", "button", &[("type", "submit")]),
("button:submit", "button", &[("type", "submit")]),
("button:r", "button", &[("type", "reset")]),
("button:reset", "button", &[("type", "reset")]),
("button:d", "button", &[("disabled", "")]),
("button:disabled", "button", &[("disabled", "")]),
("a:link", "a", &[("href", "http://")]),
("a:mail", "a", &[("href", "mailto:")]),
("a:tel", "a", &[("href", "tel:+")]),
(
"link:css",
"link",
&[("rel", "stylesheet"), ("href", "style.css")],
),
(
"link:favicon",
"link",
&[
("rel", "shortcut icon"),
("type", "image/x-icon"),
("href", "favicon.ico"),
],
),
("script:src", "script", &[("src", "")]),
("form:get", "form", &[("action", ""), ("method", "get")]),
("form:post", "form", &[("action", ""), ("method", "post")]),
("meta:utf", "meta", &[("charset", "UTF-8")]),
(
"meta:vp",
"meta",
&[
("name", "viewport"),
("content", "width=device-width, initial-scale=1.0"),
],
),
];
fn alias(name: &str) -> Option<&'static Alias> {
ALIASES.iter().find(|(n, _, _)| *n == name)
}
const LOREM: &[&str] = &[
"lorem",
"ipsum",
"dolor",
"sit",
"amet",
"consectetur",
"adipisicing",
"elit",
"sed",
"do",
"eiusmod",
"tempor",
"incididunt",
"ut",
"labore",
"et",
"dolore",
"magna",
"aliqua",
"ut",
"enim",
"ad",
"minim",
"veniam",
"quis",
"nostrud",
"exercitation",
"ullamco",
"laboris",
"nisi",
];
fn lorem_words(name: &str) -> Option<usize> {
let rest = name
.strip_prefix("lorem")
.or_else(|| name.strip_prefix("lipsum"))?;
if rest.is_empty() {
return Some(30);
}
if !rest.bytes().all(|b| b.is_ascii_digit()) {
return None;
}
rest.parse().ok().filter(|n| (1..=MAX_REPEAT).contains(n))
}
fn lorem_text(n: usize) -> String {
let words: Vec<&str> = LOREM.iter().copied().cycle().take(n).collect();
let mut text = words.join(" ");
if let Some(first) = text.get_mut(0..1) {
first.make_ascii_uppercase();
}
text.push('.');
text
}
fn resolve(nodes: &mut [Node], profile: Profile) -> Option<()> {
for node in nodes.iter_mut() {
resolve(&mut node.children, profile)?;
if node.group {
continue;
}
if let Some(n) = lorem_words(&node.name)
&& node.is_lone_word()
{
node.name.clear();
node.text = Some(lorem_text(n));
node.bare_text = true;
continue;
}
if profile == Profile::Xml || !node.name.contains(':') {
continue;
}
let (_, tag, defaults) = alias(&node.name)?;
node.name = tag.to_string();
let mut attrs: Vec<(String, String)> = defaults
.iter()
.filter(|(k, _)| !(*k == "id" && node.id.is_some()))
.filter(|(k, _)| !(*k == "class" && !node.classes.is_empty()))
.map(|(k, v)| match node.attrs.iter().position(|(a, _)| a == k) {
Some(i) => node.attrs.remove(i),
None => (k.to_string(), v.to_string()),
})
.collect();
attrs.append(&mut node.attrs);
node.attrs = attrs;
}
for node in nodes.iter_mut() {
if node.text.is_none()
&& node.children.len() == 1
&& node.children[0].bare_text
&& node.children[0].repeat == 1
{
node.text = node.children.pop().and_then(|c| c.text);
}
}
Some(())
}
pub fn expand(abbr: &str, indent: &str, profile: Profile) -> Option<(String, usize)> {
if profile == Profile::Html
&& let Some(page) = document_snippet(abbr, indent)
{
return Some(page);
}
let mut nodes = parse(abbr)?;
if profile == Profile::Xml
&& nodes.len() == 1
&& nodes[0].is_lone_word()
&& alias(&nodes[0].name).is_some()
{
return None;
}
resolve(&mut nodes, profile)?;
if element_count(&nodes) > MAX_ELEMENTS {
return None;
}
let mut r = Renderer {
indent,
profile,
out: String::new(),
caret: None,
};
r.render_all(&nodes, None, 0, 1)?;
if r.out.is_empty() {
return None;
}
let caret = r.caret.unwrap_or(r.out.len() - 1);
Some((r.out, caret))
}
fn document_snippet(abbr: &str, indent: &str) -> Option<(String, usize)> {
const DOCTYPE: &str = "<!DOCTYPE html>\n";
let doctype = match abbr {
"!" | "html:5" => true,
"doc" => false,
"!!!" => return Some((DOCTYPE.to_string(), DOCTYPE.len() - 1)),
_ => return None,
};
let mut out = String::new();
if doctype {
out.push_str(DOCTYPE);
}
out.push_str("<html lang=\"en\">\n<head>\n");
for line in [
"<meta charset=\"UTF-8\">",
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">",
"<title>Document</title>",
] {
out.push_str(indent);
out.push_str(line);
out.push('\n');
}
out.push_str("</head>\n<body>\n");
out.push_str(indent);
let caret = out.len();
out.push_str("\n</body>\n</html>\n");
Some((out, caret))
}
fn element_count(nodes: &[Node]) -> usize {
nodes.iter().fold(0usize, |acc, n| {
let own = usize::from(!n.group).saturating_add(element_count(&n.children));
acc.saturating_add(n.repeat.saturating_mul(own))
})
}
pub fn abbreviation_before(line: &str, col: usize) -> Option<(usize, String)> {
let chars: Vec<char> = line.chars().collect();
let end = col.min(chars.len());
let mut start = end;
let mut depth = 0usize;
while start > 0 {
let c = chars[start - 1];
match c {
']' | '}' => depth += 1,
'[' | '{' => depth = depth.saturating_sub(1),
_ if depth > 0 => {}
_ if !is_abbr_char(c) => break,
_ => {}
}
start -= 1;
}
start = start.max(last_tag_end(&chars[..end]));
if start >= end {
return None;
}
let abbr: String = chars[start..end].iter().collect();
Some((start, abbr))
}
fn last_tag_end(chars: &[char]) -> usize {
let mut in_tag = false;
let mut out = 0;
for (i, &c) in chars.iter().enumerate() {
match c {
'<' => in_tag = true,
'>' if in_tag => {
in_tag = false;
out = i + 1;
}
_ => {}
}
}
out
}
fn is_abbr_char(c: char) -> bool {
c.is_ascii_alphanumeric()
|| matches!(
c,
'.' | '#'
| '>'
| '+'
| '*'
| '('
| ')'
| '['
| ']'
| '{'
| '}'
| '='
| '-'
| '_'
| '"'
| '\''
| '$'
| ':'
| '/'
| '@'
| '!'
| '?'
| '&'
| ','
| '^'
)
}
const KNOWN_TAGS: &[&str] = &[
"a",
"abbr",
"address",
"area",
"article",
"aside",
"audio",
"b",
"base",
"bdi",
"bdo",
"blockquote",
"body",
"br",
"button",
"canvas",
"caption",
"cite",
"code",
"col",
"colgroup",
"data",
"datalist",
"dd",
"del",
"details",
"dfn",
"dialog",
"div",
"dl",
"dt",
"em",
"embed",
"fieldset",
"figcaption",
"figure",
"footer",
"form",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"head",
"header",
"hgroup",
"hr",
"html",
"i",
"iframe",
"img",
"input",
"ins",
"kbd",
"label",
"legend",
"li",
"link",
"main",
"map",
"mark",
"menu",
"meta",
"meter",
"nav",
"noscript",
"object",
"ol",
"optgroup",
"option",
"output",
"p",
"param",
"picture",
"pre",
"progress",
"q",
"rp",
"rt",
"ruby",
"s",
"samp",
"script",
"search",
"section",
"select",
"slot",
"small",
"source",
"span",
"strong",
"style",
"sub",
"summary",
"sup",
"table",
"tbody",
"td",
"template",
"textarea",
"tfoot",
"th",
"thead",
"time",
"title",
"tr",
"track",
"u",
"ul",
"var",
"video",
"wbr",
];
struct Parser<'a> {
src: &'a [char],
pos: usize,
climb: usize,
stray_caret: bool,
}
fn parse(abbr: &str) -> Option<Vec<Node>> {
let chars: Vec<char> = abbr.trim().chars().collect();
if chars.is_empty() {
return None;
}
let mut p = Parser {
src: &chars,
pos: 0,
climb: 0,
stray_caret: false,
};
let nodes = p.sequence(true)?;
if p.pos != p.src.len() || p.stray_caret {
return None;
}
if nodes.len() == 1 && nodes[0].is_bare() {
return None;
}
if nodes.len() == 1 && nodes[0].is_lone_word() {
let name = nodes[0].name.as_str();
if !name.contains('-')
&& !KNOWN_TAGS.contains(&name)
&& alias(name).is_none()
&& lorem_words(name).is_none()
{
return None;
}
}
Some(nodes)
}
impl Node {
fn is_lone_word(&self) -> bool {
self.repeat == 1
&& !self.group
&& self.id.is_none()
&& self.classes.is_empty()
&& self.attrs.is_empty()
&& self.text.is_none()
&& self.children.is_empty()
}
fn is_text_only(&self) -> bool {
!self.group
&& self.name.is_empty()
&& self.id.is_none()
&& self.classes.is_empty()
&& self.attrs.is_empty()
&& self.text.is_some()
&& self.children.is_empty()
}
fn is_bare(&self) -> bool {
self.name.is_empty()
&& self.id.is_none()
&& self.classes.is_empty()
&& self.attrs.is_empty()
&& self.text.is_none()
&& self.children.is_empty()
}
}
impl Parser<'_> {
fn peek(&self) -> Option<char> {
self.src.get(self.pos).copied()
}
fn eat(&mut self, c: char) -> bool {
if self.peek() == Some(c) {
self.pos += 1;
true
} else {
false
}
}
fn sequence(&mut self, root: bool) -> Option<Vec<Node>> {
let mut out = Vec::new();
loop {
out.push(self.item()?);
if self.climb > 0 {
self.climb -= 1;
if self.climb == 0 || root {
self.climb = 0;
continue;
}
return Some(out);
}
if self.eat('+') {
continue;
}
let mut carets = 0;
while self.eat('^') {
carets += 1;
}
if carets == 0 {
return Some(out);
}
if root {
self.stray_caret = true;
continue;
}
self.climb = carets;
return Some(out);
}
}
fn item(&mut self) -> Option<Node> {
let mut node = if self.eat('(') {
let inner = self.sequence(true)?;
if !self.eat(')') {
return None;
}
Node {
group: true,
children: inner,
repeat: 1,
..Node::default()
}
} else {
self.atom()?
};
if self.eat('*') {
node.repeat = self.number()?;
if node.repeat == 0 || node.repeat > MAX_REPEAT {
return None;
}
if !node.group {
self.suffixes(&mut node)?;
}
}
if self.eat('>') {
let kids = self.sequence(false)?;
if node.group {
return None;
}
node.children = kids;
}
Some(node)
}
fn number(&mut self) -> Option<usize> {
let start = self.pos;
while self.peek().is_some_and(|c| c.is_ascii_digit()) {
self.pos += 1;
}
if start == self.pos {
return None;
}
self.src[start..self.pos]
.iter()
.collect::<String>()
.parse()
.ok()
}
fn atom(&mut self) -> Option<Node> {
let mut node = Node {
repeat: 1,
..Node::default()
};
node.name = self.ident();
self.suffixes(&mut node)?;
if node.is_bare() {
return None;
}
Some(node)
}
fn suffixes(&mut self, node: &mut Node) -> Option<()> {
loop {
match self.peek() {
Some('#') => {
self.pos += 1;
let v = self.ident();
if v.is_empty() {
return None;
}
node.id = Some(v);
}
Some('.') => {
self.pos += 1;
let v = self.ident();
if v.is_empty() {
return None;
}
node.classes.push(v);
}
Some('[') => {
self.pos += 1;
let attrs = self.attrs()?;
node.attrs.extend(attrs);
}
Some('{') => {
self.pos += 1;
node.text = Some(self.text()?);
}
_ => return Some(()),
}
}
}
fn ident(&mut self) -> String {
let start = self.pos;
while self
.peek()
.is_some_and(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '$' | ':' | '!'))
{
self.pos += 1;
}
self.src[start..self.pos].iter().collect()
}
fn attrs(&mut self) -> Option<Vec<(String, String)>> {
let mut out = Vec::new();
loop {
while self.eat(' ') {}
if self.eat(']') {
return Some(out);
}
let name = self.ident();
if name.is_empty() {
return None;
}
let value = if self.eat('=') {
match self.peek() {
Some(q @ ('"' | '\'')) => {
self.pos += 1;
let start = self.pos;
while self.peek().is_some_and(|c| c != q) {
self.pos += 1;
}
let v: String = self.src[start..self.pos].iter().collect();
if !self.eat(q) {
return None;
}
v
}
_ => {
let start = self.pos;
while self.peek().is_some_and(|c| c != ' ' && c != ']') {
self.pos += 1;
}
self.src[start..self.pos].iter().collect()
}
}
} else {
String::new()
};
out.push((name, value));
}
}
fn text(&mut self) -> Option<String> {
let start = self.pos;
while self.peek().is_some_and(|c| c != '}') {
self.pos += 1;
}
let t: String = self.src[start..self.pos].iter().collect();
if !self.eat('}') {
return None;
}
Some(t)
}
}
fn implicit_tag(parent: Option<&str>) -> &'static str {
match parent {
Some("ul" | "ol") => "li",
Some("table" | "tbody" | "thead" | "tfoot") => "tr",
Some("tr") => "td",
Some("select" | "optgroup") => "option",
Some("dl") => "dt",
Some("map") => "area",
_ => "div",
}
}
struct Renderer<'a> {
indent: &'a str,
profile: Profile,
out: String,
caret: Option<usize>,
}
impl Renderer<'_> {
fn render_all(
&mut self,
nodes: &[Node],
parent: Option<&str>,
depth: usize,
inherited: usize,
) -> Option<()> {
for node in nodes {
for i in 1..=node.repeat {
let index = if node.repeat > 1 { i } else { inherited };
self.render(node, parent, depth, index)?;
}
}
Some(())
}
fn render(
&mut self,
node: &Node,
parent: Option<&str>,
depth: usize,
index: usize,
) -> Option<()> {
if node.group {
return self.render_all(&node.children, parent, depth, index);
}
if node.bare_text {
self.out.push_str(&self.indent.repeat(depth));
self.out.push_str(node.text.as_deref().unwrap_or(""));
self.out.push('\n');
return Some(());
}
if node.is_text_only() {
let text = number(node.text.as_deref().unwrap_or(""), index);
self.out.push_str(&self.indent.repeat(depth));
self.out.push_str(&text);
self.out.push('\n');
return Some(());
}
let name = if node.name.is_empty() {
implicit_tag(parent).to_string()
} else {
number(&node.name, index)
};
let void = VOID_ELEMENTS.contains(&name.as_str());
if void && (!node.children.is_empty() || node.text.is_some()) {
return None;
}
let jsx = self.profile == Profile::Jsx;
let pad = self.indent.repeat(depth);
self.out.push_str(&pad);
self.out.push('<');
self.out.push_str(&name);
if name == "a" && !node.attrs.iter().any(|(k, _)| k == "href") {
self.out.push_str(" href=\"\"");
}
if let Some(id) = &node.id {
self.out.push_str(&format!(" id=\"{}\"", number(id, index)));
}
if !node.classes.is_empty() {
let classes: Vec<String> = node.classes.iter().map(|c| number(c, index)).collect();
let key = if jsx { "className" } else { "class" };
self.out
.push_str(&format!(" {key}=\"{}\"", classes.join(" ")));
}
for (k, v) in &node.attrs {
let k = match (jsx, k.as_str()) {
(true, "class") => "className",
(true, "for") => "htmlFor",
_ => k,
};
let v = number(v, index).replace('"', """);
self.out.push_str(&format!(" {k}=\"{v}\""));
}
if void {
self.out.push_str(match self.profile {
Profile::Html => ">\n",
Profile::Xml => "/>\n",
Profile::Jsx => " />\n",
});
return Some(());
}
self.out.push('>');
if !node.children.is_empty() {
self.out.push('\n');
self.render_all(&node.children, Some(&name), depth + 1, index)?;
self.out.push_str(&pad);
} else {
let text = node.text.as_deref().map(|t| number(t, index));
if text.as_deref().unwrap_or("").is_empty() && self.caret.is_none() {
self.caret = Some(self.out.len());
}
if let Some(t) = text {
self.out.push_str(&t);
}
}
self.out.push_str(&format!("</{name}>\n"));
Some(())
}
}
fn number(s: &str, index: usize) -> String {
if !s.contains('$') {
return s.to_string();
}
let mut out = String::with_capacity(s.len());
let chars: Vec<char> = s.chars().collect();
let mut i = 0;
while i < chars.len() {
if chars[i] == '$' {
let start = i;
while i < chars.len() && chars[i] == '$' {
i += 1;
}
let width = i - start;
out.push_str(&format!("{index:0width$}"));
} else {
out.push(chars[i]);
i += 1;
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_quote_in_an_attribute_value_is_escaped() {
let out = super::expand("a[title='say \"hi\"']", " ", Profile::Html)
.unwrap()
.0;
assert!(out.contains("title=\"say "hi"\""), "{out}");
}
fn expand(abbr: &str, indent: &str) -> Option<(String, usize)> {
super::expand(abbr, indent, Profile::Html)
}
fn ex(abbr: &str) -> String {
expand(abbr, " ").expect("should expand").0
}
#[test]
fn a_bare_tag_expands_to_a_tag_pair() {
assert_eq!(ex("div"), "<div></div>\n");
}
#[test]
fn a_class_without_a_tag_implies_a_div() {
assert_eq!(ex(".wrapper"), "<div class=\"wrapper\"></div>\n");
}
#[test]
fn ids_and_several_classes_combine() {
assert_eq!(
ex("section#hero.a.b"),
"<section id=\"hero\" class=\"a b\"></section>\n"
);
}
#[test]
fn the_child_operator_nests_and_indents() {
assert_eq!(
ex("div>span"),
"<div>\n <span></span>\n</div>\n",
"one indent level per depth"
);
}
#[test]
fn the_sibling_operator_keeps_nodes_at_the_same_level() {
assert_eq!(ex("h1+p"), "<h1></h1>\n<p></p>\n");
}
#[test]
fn a_sibling_after_a_child_stays_a_child_of_the_same_parent() {
assert_eq!(ex("div>h1+p"), "<div>\n <h1></h1>\n <p></p>\n</div>\n");
}
#[test]
fn multiplication_repeats_a_node() {
assert_eq!(
ex("ul>li*3"),
"<ul>\n <li></li>\n <li></li>\n <li></li>\n</ul>\n"
);
}
#[test]
fn an_implicit_child_of_a_list_is_an_li() {
assert_eq!(
ex("ul>.item*2"),
"<ul>\n <li class=\"item\"></li>\n <li class=\"item\"></li>\n</ul>\n"
);
}
#[test]
fn an_implicit_child_of_a_row_is_a_cell() {
assert_eq!(ex("tr>.cell"), "<tr>\n <td class=\"cell\"></td>\n</tr>\n");
}
#[test]
fn text_after_the_multiplier_belongs_to_the_element() {
assert_eq!(
ex("li*3{Item $}"),
"<li>Item 1</li>\n<li>Item 2</li>\n<li>Item 3</li>\n"
);
assert_eq!(
ex("ul>li.item$*2{Item $}"),
"<ul>\n <li class=\"item1\">Item 1</li>\n <li class=\"item2\">Item 2</li>\n</ul>\n"
);
assert_eq!(
ex("li*2.x"),
"<li class=\"x\"></li>\n<li class=\"x\"></li>\n"
);
}
#[test]
fn a_text_node_is_bare_text() {
assert_eq!(
ex("p>{Click }+a{here}+{ to continue}"),
"<p>\n Click \n <a href=\"\">here</a>\n to continue\n</p>\n"
);
assert_eq!(ex("ul>{Item $}*2"), "<ul>\n Item 1\n Item 2\n</ul>\n");
}
#[test]
fn a_link_gets_an_empty_href_unless_it_names_one() {
assert_eq!(ex("a"), "<a href=\"\"></a>\n");
assert_eq!(ex("a[href=#top]"), "<a href=\"#top\"></a>\n");
}
#[test]
fn text_on_an_element_and_bad_multipliers_are_unchanged() {
assert_eq!(ex(".x{hi}"), "<div class=\"x\">hi</div>\n");
assert_eq!(ex("ul>.x{hi}"), "<ul>\n <li class=\"x\">hi</li>\n</ul>\n");
assert_eq!(ex("li{Item $}*2"), "<li>Item 1</li>\n<li>Item 2</li>\n");
assert!(expand("(li)*2{x}", " ").is_none());
assert!(expand("li*3x", " ").is_none());
}
#[test]
fn the_dollar_placeholder_numbers_each_repetition() {
assert_eq!(
ex("li.item$*3"),
"<li class=\"item1\"></li>\n<li class=\"item2\"></li>\n<li class=\"item3\"></li>\n"
);
}
#[test]
fn a_run_of_dollars_zero_pads_to_its_own_width() {
assert_eq!(
ex("li#row$$$*2"),
"<li id=\"row001\"></li>\n<li id=\"row002\"></li>\n"
);
}
#[test]
fn text_in_braces_renders_inline() {
assert_eq!(ex("p{Hello}"), "<p>Hello</p>\n");
}
#[test]
fn text_is_numbered_too() {
assert_eq!(ex("li{Item $}*2"), "<li>Item 1</li>\n<li>Item 2</li>\n");
}
#[test]
fn attributes_parse_bare_quoted_and_valueless() {
assert_eq!(
ex("a[href=# title=\"Go home\" download]"),
"<a href=\"#\" title=\"Go home\" download=\"\"></a>\n"
);
}
#[test]
fn groups_repeat_as_a_unit() {
assert_eq!(
ex("(li>a)*2"),
"<li>\n <a href=\"\"></a>\n</li>\n<li>\n <a href=\"\"></a>\n</li>\n"
);
}
#[test]
fn a_group_can_sit_beside_other_nodes() {
assert_eq!(
ex("(h1+p)+footer"),
"<h1></h1>\n<p></p>\n<footer></footer>\n"
);
}
#[test]
fn void_elements_get_no_closing_tag() {
assert_eq!(ex("br"), "<br>\n");
assert_eq!(ex("img[src=a.png]"), "<img src=\"a.png\">\n");
}
#[test]
fn a_realistic_abbreviation_builds_a_whole_block() {
assert_eq!(
ex("nav.menu>ul>li.item$*3>a[href=#]{Link $}"),
concat!(
"<nav class=\"menu\">\n",
" <ul>\n",
" <li class=\"item1\">\n",
" <a href=\"#\">Link 1</a>\n",
" </li>\n",
" <li class=\"item2\">\n",
" <a href=\"#\">Link 2</a>\n",
" </li>\n",
" <li class=\"item3\">\n",
" <a href=\"#\">Link 3</a>\n",
" </li>\n",
" </ul>\n",
"</nav>\n"
)
);
}
#[test]
fn the_indent_string_is_the_callers() {
assert_eq!(
expand("div>p", "\t").unwrap().0,
"<div>\n\t<p></p>\n</div>\n"
);
}
#[test]
fn the_caret_lands_inside_the_first_empty_element() {
let (out, caret) = expand("div>span", " ").unwrap();
assert_eq!(&out[caret..caret + 7], "</span>");
}
#[test]
fn the_caret_falls_to_the_end_when_nothing_is_empty() {
let (out, caret) = expand("p{hi}", " ").unwrap();
assert_eq!(caret, out.len() - 1);
assert_eq!(&out[caret..], "\n");
}
#[test]
fn markup_inside_text_does_not_capture_the_caret() {
let (out, caret) = expand("p{a></b}+span", " ").unwrap();
assert_eq!(&out[caret..], "</span>\n");
}
#[test]
fn markup_inside_an_attribute_value_does_not_capture_the_caret() {
let (out, caret) = expand("a[title=\"></\"]", " ").unwrap();
assert_eq!(out, "<a href=\"\" title=\"></\"></a>\n");
assert_eq!(&out[caret..], "</a>\n");
}
#[test]
fn every_attribute_group_is_kept() {
assert_eq!(ex("a[href=x][title=y]"), "<a href=\"x\" title=\"y\"></a>\n");
}
#[test]
fn a_void_element_with_content_does_not_expand() {
assert!(expand("img>span", " ").is_none());
assert!(expand("br{text}", " ").is_none());
assert!(expand("map>.x>span", " ").is_none());
assert!(expand("img+span", " ").is_some());
}
#[test]
fn xml_self_closes_void_elements() {
assert_eq!(
super::expand("br+img[src=a]", " ", Profile::Xml)
.unwrap()
.0,
"<br/>\n<img src=\"a\"/>\n"
);
}
#[test]
fn jsx_self_closes_and_renames_attributes() {
assert_eq!(
super::expand("img.hero+label[for=q]", " ", Profile::Jsx)
.unwrap()
.0,
"<img className=\"hero\" />\n<label htmlFor=\"q\"></label>\n"
);
}
#[test]
fn prose_does_not_expand() {
assert!(expand("just some words", " ").is_none());
}
#[test]
fn a_bare_word_that_is_not_a_tag_does_not_expand() {
assert!(expand("fox", " ").is_none());
assert!(expand("section", " ").is_some());
}
#[test]
fn a_hyphenated_custom_element_expands() {
assert_eq!(ex("my-widget"), "<my-widget></my-widget>\n");
}
#[test]
fn an_unknown_name_with_other_syntax_still_expands() {
assert_eq!(ex("fox.red"), "<fox class=\"red\"></fox>\n");
assert_eq!(ex("fox>cub"), "<fox>\n <cub></cub>\n</fox>\n");
}
#[test]
fn a_caret_climbs_out_of_the_child_list() {
assert_eq!(
ex("header>nav>a*2^h1"),
"<header>\n <nav>\n <a href=\"\"></a>\n <a href=\"\"></a>\n </nav>\n <h1></h1>\n</header>\n"
);
}
#[test]
fn each_caret_climbs_one_level() {
assert_eq!(
ex("div>ul>li^p"),
"<div>\n <ul>\n <li></li>\n </ul>\n <p></p>\n</div>\n"
);
assert_eq!(
ex("div>ul>li^^p"),
"<div>\n <ul>\n <li></li>\n </ul>\n</div>\n<p></p>\n"
);
assert_eq!(
ex("div.card>h2^div.card>h2"),
"<div class=\"card\">\n <h2></h2>\n</div>\n<div class=\"card\">\n <h2></h2>\n</div>\n"
);
}
#[test]
fn extra_carets_stop_at_the_top_and_at_a_group() {
assert_eq!(
ex("div>p^^span"),
"<div>\n <p></p>\n</div>\n<span></span>\n"
);
assert_eq!(
ex("section>(ul>li^^^p)+footer"),
"<section>\n <ul>\n <li></li>\n </ul>\n <p></p>\n <footer></footer>\n</section>\n"
);
}
#[test]
fn siblings_and_repeats_follow_a_climb() {
assert_eq!(
ex("ul>li^p*2+hr"),
"<ul>\n <li></li>\n</ul>\n<p></p>\n<p></p>\n<hr>\n"
);
}
#[test]
fn the_abbreviation_before_the_caret_includes_its_carets() {
let line = " header>nav>a*2^h1";
let (start, abbr) = abbreviation_before(line, line.chars().count()).unwrap();
assert_eq!(start, 2);
assert_eq!(abbr, "header>nav>a*2^h1");
}
#[test]
fn a_caret_that_climbs_nothing_does_not_expand() {
assert!(expand("ul>li^", " ").is_none(), "nothing after the climb");
assert!(expand("x^2", " ").is_none(), "math, not markup");
assert!(expand("2^10", " ").is_none());
assert!(
expand("(p^span)", " ").is_none(),
"a group's top climbs nowhere"
);
}
#[test]
fn abbreviations_without_a_caret_are_unchanged() {
assert_eq!(
ex("ul>li*3"),
"<ul>\n <li></li>\n <li></li>\n <li></li>\n</ul>\n"
);
assert_eq!(ex("div>h1+p"), "<div>\n <h1></h1>\n <p></p>\n</div>\n");
assert_eq!(ex("p{2^10}"), "<p>2^10</p>\n", "a caret in text is text");
}
#[test]
fn an_unbalanced_abbreviation_does_not_expand() {
assert!(expand("div>(span", " ").is_none());
assert!(expand("a[href=#", " ").is_none());
assert!(expand("p{text", " ").is_none());
}
#[test]
fn a_dangling_operator_does_not_expand() {
assert!(expand("div>", " ").is_none());
assert!(expand("div+", " ").is_none());
assert!(expand("*3", " ").is_none());
}
#[test]
fn an_empty_abbreviation_does_not_expand() {
assert!(expand("", " ").is_none());
assert!(expand(" ", " ").is_none());
}
#[test]
fn an_absurd_repeat_count_is_refused() {
assert!(expand("div*1000", " ").is_some());
assert!(expand("div*1001", " ").is_none());
assert!(expand("div*0", " ").is_none());
}
#[test]
fn a_nested_product_of_repeats_is_refused() {
assert!(expand("div*1000>span*1000", " ").is_none());
assert!(expand("(div>(span*1000))*1000", " ").is_none());
assert!(expand("div*100>span*100", " ").is_some());
}
#[test]
fn the_abbreviation_is_taken_from_the_caret_leftwards() {
let (start, abbr) = abbreviation_before(" ul>li*3", 9).unwrap();
assert_eq!(start, 2);
assert_eq!(abbr, "ul>li*3");
}
#[test]
fn leading_indentation_is_not_part_of_the_abbreviation() {
let (start, _) = abbreviation_before("\t\tdiv", 5).unwrap();
assert_eq!(start, 2);
}
#[test]
fn an_earlier_tag_on_the_line_is_not_swallowed() {
let (start, abbr) = abbreviation_before("<div>span", 9).unwrap();
assert_eq!(start, 5);
assert_eq!(abbr, "span");
}
#[test]
fn nothing_to_the_left_means_no_abbreviation() {
assert!(abbreviation_before(" ", 3).is_none());
assert!(abbreviation_before("div", 0).is_none());
}
#[test]
fn colon_aliases_and_lorem_expand_inside_a_compound() {
let out = expand("form>input:email+input:password+btn:s+p>lorem5", " ")
.unwrap()
.0;
assert_eq!(
out,
"<form>\n <input type=\"email\" name=\"\" id=\"\">\n \
<input type=\"password\" name=\"\" id=\"\">\n \
<button type=\"submit\"></button>\n \
<p>Lorem ipsum dolor sit amet.</p>\n</form>\n"
);
}
#[test]
fn a_lone_alias_expands() {
assert_eq!(
expand("input:text", " ").unwrap().0,
"<input type=\"text\" name=\"\" id=\"\">\n"
);
assert_eq!(
expand("a:link", " ").unwrap().0,
"<a href=\"http://\"></a>\n"
);
assert_eq!(
expand("link:css", " ").unwrap().0,
"<link rel=\"stylesheet\" href=\"style.css\">\n"
);
assert_eq!(
expand("form:post", " ").unwrap().0,
"<form action=\"\" method=\"post\"></form>\n"
);
}
#[test]
fn an_alias_takes_the_abbreviations_own_id_and_attributes() {
assert_eq!(
expand("input:text#q[name=query]", " ").unwrap().0,
"<input id=\"q\" type=\"text\" name=\"query\">\n"
);
}
#[test]
fn an_alias_in_jsx_is_self_closed() {
assert_eq!(
super::expand("input:checkbox", " ", Profile::Jsx)
.unwrap()
.0,
"<input type=\"checkbox\" name=\"\" id=\"\" />\n"
);
}
#[test]
fn lorem_is_text() {
assert_eq!(expand("lorem3", " ").unwrap().0, "Lorem ipsum dolor.\n");
assert_eq!(
expand("div>h1+lorem4", " ").unwrap().0,
"<div>\n <h1></h1>\n Lorem ipsum dolor sit.\n</div>\n"
);
let words = expand("lorem", " ").unwrap().0;
assert_eq!(words.split_whitespace().count(), 30, "{words}");
}
#[test]
fn an_unknown_colon_name_refuses_in_html() {
assert!(expand("form>input:bogus", " ").is_none());
assert!(expand("div>svg:rect", " ").is_none());
assert!(super::expand("form>btn:x", " ", Profile::Jsx).is_none());
}
#[test]
fn xml_keeps_namespaced_names() {
assert_eq!(
super::expand("xsl:template>xsl:value-of", " ", Profile::Xml)
.unwrap()
.0,
"<xsl:template>\n <xsl:value-of></xsl:value-of>\n</xsl:template>\n"
);
assert!(super::expand("input:email", " ", Profile::Xml).is_none());
}
#[test]
fn a_word_like_lorem_is_not_lorem() {
assert!(expand("loremipsum", " ").is_none());
assert!(expand("lorem0", " ").is_none());
}
const PAGE: &str = "<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"UTF-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
<title>Document</title>
</head>
<body>
|
</body>
</html>
";
fn with_caret(abbr: &str) -> Option<String> {
expand(abbr, " ").map(|(mut out, caret)| {
out.insert(caret, '|');
out
})
}
#[test]
fn an_exclamation_mark_writes_the_page_skeleton() {
assert_eq!(with_caret("!").as_deref(), Some(PAGE));
}
#[test]
fn html5_writes_the_same_page() {
assert_eq!(with_caret("html:5").as_deref(), Some(PAGE));
}
#[test]
fn doc_and_triple_bang_write_their_parts_of_the_page() {
let doc = PAGE.strip_prefix("<!DOCTYPE html>\n").unwrap();
assert_eq!(with_caret("doc").as_deref(), Some(doc));
assert_eq!(with_caret("!!!").as_deref(), Some("<!DOCTYPE html>|\n"));
}
#[test]
fn the_page_is_indented_with_the_buffers_unit() {
let (page, _) = expand("!", "\t").unwrap();
assert!(page.contains("\n\t<meta charset=\"UTF-8\">\n"), "{page}");
assert!(page.contains("<body>\n\t\n</body>"), "{page}");
}
#[test]
fn near_misses_and_other_dialects_write_no_page() {
for abbr in ["!!", "!!!!", "html:4", "html:5x", "docs", "!div"] {
assert!(
!expand(abbr, " ").is_some_and(|(o, _)| o.contains("<head>")),
"{abbr:?} wrote a page"
);
}
for profile in [Profile::Xml, Profile::Jsx] {
for abbr in ["!", "html:5", "!!!"] {
assert_eq!(
super::expand(abbr, " ", profile),
None,
"{abbr:?} in {profile:?}"
);
}
}
}
#[test]
fn abbreviations_that_are_no_snippet_still_expand_as_before() {
assert_eq!(ex("html"), "<html></html>\n");
assert_eq!(ex("head>title"), "<head>\n <title></title>\n</head>\n");
}
}