mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-05 11:15:01 +03:00
fix(database): preserve table paste after SVG and MathML
This commit is contained in:
1 parent
66357cfabb
commit
e2e6653da7
1 file changed
+575
-5
@@ -1,5 +1,8 @@
|
||||
use blocksuite_editor::{
|
||||
clipboard::{decode_html_snapshot, encode_html_snapshot, table_area_html, write_text_and_html},
|
||||
clipboard::{
|
||||
decode_html_snapshot, encode_html_snapshot, sanitize::decode_html_entities_attr,
|
||||
table_area_html, write_text_and_html,
|
||||
},
|
||||
database::{self, TableClipboardCell},
|
||||
};
|
||||
use ui::{ClipboardEntry, ClipboardItem, Context};
|
||||
@@ -173,9 +176,15 @@ impl TableClipboard {
|
||||
|
||||
/// Read the cells from [`TABLE_ATTR`] when they still give `text`.
|
||||
fn from_html(html: &str, text: &str) -> Option<Vec<Vec<TableClipboardCell>>> {
|
||||
let (_, after) = html.split_once(&format!("{TABLE_ATTR}=\""))?;
|
||||
let (encoded, _) = after.split_once('"')?;
|
||||
let Self { database_table } = serde_json::from_str(&decode_html_snapshot(encoded)?).ok()?;
|
||||
let value = decode_html_entities_attr(table_attribute(html)?);
|
||||
let decoded;
|
||||
let json = if value.trim_start().starts_with('{') {
|
||||
value.as_ref()
|
||||
} else {
|
||||
decoded = decode_html_snapshot(&value)?;
|
||||
&decoded
|
||||
};
|
||||
let Self { database_table } = serde_json::from_str(json).ok()?;
|
||||
let width = database_table.first().map(Vec::len)?;
|
||||
(width > 0
|
||||
&& database_table.iter().all(|row| row.len() == width)
|
||||
@@ -184,6 +193,309 @@ impl TableClipboard {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum ForeignNamespace {
|
||||
Svg,
|
||||
Math,
|
||||
}
|
||||
|
||||
enum Integration {
|
||||
None,
|
||||
Html,
|
||||
MathText,
|
||||
}
|
||||
|
||||
enum Scope<'a> {
|
||||
Div(Option<&'a str>),
|
||||
Template,
|
||||
Html(&'a str),
|
||||
Foreign {
|
||||
tag: &'a str,
|
||||
namespace: ForeignNamespace,
|
||||
integration: Integration,
|
||||
},
|
||||
}
|
||||
|
||||
fn tag_is(tag: &str, names: &[&str]) -> bool {
|
||||
names.iter().any(|name| tag.eq_ignore_ascii_case(name))
|
||||
}
|
||||
|
||||
fn exits_foreign_content(tag: &str, closing: bool, font_format: bool) -> bool {
|
||||
if closing {
|
||||
tag_is(tag, &["br", "p"])
|
||||
} else {
|
||||
font_format
|
||||
|| tag_is(
|
||||
tag,
|
||||
&[
|
||||
"b",
|
||||
"big",
|
||||
"blockquote",
|
||||
"body",
|
||||
"br",
|
||||
"center",
|
||||
"code",
|
||||
"dd",
|
||||
"div",
|
||||
"dl",
|
||||
"dt",
|
||||
"em",
|
||||
"embed",
|
||||
"h1",
|
||||
"h2",
|
||||
"h3",
|
||||
"h4",
|
||||
"h5",
|
||||
"h6",
|
||||
"head",
|
||||
"hr",
|
||||
"i",
|
||||
"img",
|
||||
"li",
|
||||
"listing",
|
||||
"menu",
|
||||
"meta",
|
||||
"nobr",
|
||||
"ol",
|
||||
"p",
|
||||
"pre",
|
||||
"ruby",
|
||||
"s",
|
||||
"small",
|
||||
"span",
|
||||
"strong",
|
||||
"strike",
|
||||
"sub",
|
||||
"sup",
|
||||
"table",
|
||||
"tt",
|
||||
"u",
|
||||
"ul",
|
||||
"var",
|
||||
],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn table_attribute(mut html: &str) -> Option<&str> {
|
||||
let space = |ch| matches!(ch, ' ' | '\t' | '\n' | '\r' | '\x0c');
|
||||
let mut scopes = Vec::new();
|
||||
'tags: while let Some((_, head)) = html.split_once('<') {
|
||||
html = head;
|
||||
if let Some(comment) = head.strip_prefix("!--") {
|
||||
html = comment.split_once("-->")?.1;
|
||||
continue;
|
||||
}
|
||||
if matches!(scopes.last(), Some(Scope::Foreign { .. }))
|
||||
&& let Some(cdata) = head.strip_prefix("![CDATA[")
|
||||
{
|
||||
html = cdata.split_once("]]>")?.1;
|
||||
continue;
|
||||
}
|
||||
if head.starts_with(['!', '?']) {
|
||||
html = head.split_once('>')?.1;
|
||||
continue;
|
||||
}
|
||||
let closing = head.starts_with('/');
|
||||
let head = head.strip_prefix('/').unwrap_or(head);
|
||||
if !head.starts_with(|ch: char| ch.is_ascii_alphabetic()) {
|
||||
continue;
|
||||
}
|
||||
let end = head.find(|ch| space(ch) || matches!(ch, '/' | '>' | '<'))?;
|
||||
let (tag, mut attrs) = head.split_at(end);
|
||||
let table = tag.eq_ignore_ascii_case("table");
|
||||
let div = tag.eq_ignore_ascii_case("div");
|
||||
let template = tag.eq_ignore_ascii_case("template");
|
||||
let svg = tag.eq_ignore_ascii_case("svg");
|
||||
let math = tag.eq_ignore_ascii_case("math");
|
||||
let annotation = tag.eq_ignore_ascii_case("annotation-xml");
|
||||
let font = tag.eq_ignore_ascii_case("font");
|
||||
let mut value = None;
|
||||
let mut encoding = None;
|
||||
let mut font_format = false;
|
||||
let mut self_closing = false;
|
||||
loop {
|
||||
attrs = attrs.trim_start_matches(space);
|
||||
if let Some(rest) = attrs.strip_prefix('/') {
|
||||
attrs = rest;
|
||||
self_closing = attrs.starts_with('>');
|
||||
continue;
|
||||
}
|
||||
if let Some(rest) = attrs.strip_prefix('>') {
|
||||
html = rest;
|
||||
break;
|
||||
}
|
||||
if attrs.starts_with('<') {
|
||||
html = attrs;
|
||||
continue 'tags;
|
||||
}
|
||||
let name_start = usize::from(attrs.starts_with('='));
|
||||
let end = name_start
|
||||
+ attrs[name_start..]
|
||||
.find(|ch| space(ch) || matches!(ch, '=' | '/' | '>' | '<'))?;
|
||||
let (name, rest) = attrs.split_at(end);
|
||||
attrs = rest.trim_start_matches(space);
|
||||
let attr = if let Some(rest) = attrs.strip_prefix('=') {
|
||||
attrs = rest.trim_start_matches(space);
|
||||
let (raw, rest) = match *attrs.as_bytes().first()? {
|
||||
quote @ (b'\'' | b'"') => attrs[1..].split_once(char::from(quote))?,
|
||||
_ => attrs.split_at(attrs.find(|ch| space(ch) || ch == '>')?),
|
||||
};
|
||||
attrs = rest;
|
||||
raw
|
||||
} else {
|
||||
""
|
||||
};
|
||||
if !closing && (table || div) && name.eq_ignore_ascii_case(TABLE_ATTR) {
|
||||
value.get_or_insert(attr);
|
||||
}
|
||||
if annotation && name.eq_ignore_ascii_case("encoding") {
|
||||
encoding.get_or_insert(attr);
|
||||
}
|
||||
font_format |= font && tag_is(name, &["color", "face", "size"]);
|
||||
}
|
||||
let mut foreign = match scopes.last() {
|
||||
Some(Scope::Foreign {
|
||||
namespace,
|
||||
integration,
|
||||
..
|
||||
}) if closing
|
||||
|| matches!(integration, Integration::None)
|
||||
|| (matches!(integration, Integration::MathText)
|
||||
&& tag_is(tag, &["mglyph", "malignmark"])) =>
|
||||
{
|
||||
Some(*namespace)
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
// The HTML parser can pop foreign elements before it inserts a tag.
|
||||
if foreign.is_some() && exits_foreign_content(tag, closing, font_format) {
|
||||
while matches!(
|
||||
scopes.last(),
|
||||
Some(Scope::Foreign {
|
||||
integration: Integration::None,
|
||||
..
|
||||
})
|
||||
) {
|
||||
scopes.pop();
|
||||
}
|
||||
foreign = None;
|
||||
}
|
||||
if closing {
|
||||
let mut scan_foreign = foreign.is_some();
|
||||
let mut integration_boundary = false;
|
||||
for (index, scope) in scopes.iter().enumerate().rev() {
|
||||
let matches = match scope {
|
||||
Scope::Foreign {
|
||||
tag: open,
|
||||
integration,
|
||||
..
|
||||
} if scan_foreign => {
|
||||
integration_boundary |= !matches!(integration, Integration::None);
|
||||
open.eq_ignore_ascii_case(tag)
|
||||
}
|
||||
Scope::Foreign { .. } => break,
|
||||
_ if integration_boundary => break,
|
||||
Scope::Div(_) => div,
|
||||
Scope::Template if !template => break,
|
||||
Scope::Template => true,
|
||||
Scope::Html(open) => open.eq_ignore_ascii_case(tag),
|
||||
};
|
||||
if matches {
|
||||
scopes.truncate(index);
|
||||
break;
|
||||
}
|
||||
scan_foreign &= matches!(scope, Scope::Foreign { .. });
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let namespace = match foreign {
|
||||
Some(ForeignNamespace::Math)
|
||||
if svg
|
||||
&& matches!(scopes.last(), Some(Scope::Foreign { tag, .. })
|
||||
if tag.eq_ignore_ascii_case("annotation-xml")) =>
|
||||
{
|
||||
Some(ForeignNamespace::Svg)
|
||||
}
|
||||
Some(namespace) => Some(namespace),
|
||||
None if svg => Some(ForeignNamespace::Svg),
|
||||
None if math => Some(ForeignNamespace::Math),
|
||||
None => None,
|
||||
};
|
||||
if let Some(namespace) = namespace {
|
||||
if !self_closing {
|
||||
let integration = match namespace {
|
||||
ForeignNamespace::Svg if tag_is(tag, &["foreignobject", "desc", "title"]) => {
|
||||
Integration::Html
|
||||
}
|
||||
ForeignNamespace::Math if tag_is(tag, &["mi", "mo", "mn", "ms", "mtext"]) => {
|
||||
Integration::MathText
|
||||
}
|
||||
ForeignNamespace::Math
|
||||
if annotation
|
||||
&& encoding.is_some_and(|encoding| {
|
||||
let encoding = decode_html_entities_attr(encoding);
|
||||
tag_is(&encoding, &["text/html", "application/xhtml+xml"])
|
||||
}) =>
|
||||
{
|
||||
Integration::Html
|
||||
}
|
||||
_ => Integration::None,
|
||||
};
|
||||
scopes.push(Scope::Foreign {
|
||||
tag,
|
||||
namespace,
|
||||
integration,
|
||||
});
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if tag.eq_ignore_ascii_case("plaintext") {
|
||||
return None;
|
||||
}
|
||||
if tag_is(
|
||||
tag,
|
||||
&[
|
||||
"script", "style", "title", "textarea", "xmp", "iframe", "noembed", "noframes",
|
||||
],
|
||||
) {
|
||||
html = html.match_indices("</").find_map(|(start, close)| {
|
||||
let tail = &html[start + close.len()..];
|
||||
let name = tail.get(..tag.len())?;
|
||||
let boundary =
|
||||
tail[tag.len()..].starts_with(|ch| space(ch) || matches!(ch, '/' | '>'));
|
||||
(name.eq_ignore_ascii_case(tag) && boundary).then_some(&html[start..])
|
||||
})?;
|
||||
continue;
|
||||
}
|
||||
if template {
|
||||
scopes.push(Scope::Template);
|
||||
} else if div {
|
||||
scopes.push(Scope::Div(value));
|
||||
} else if table && !scopes.iter().any(|scope| matches!(scope, Scope::Template)) {
|
||||
return value.or_else(|| {
|
||||
scopes.iter().rev().find_map(|scope| match scope {
|
||||
Scope::Div(value) => *value,
|
||||
_ => None,
|
||||
})
|
||||
});
|
||||
} else if scopes
|
||||
.iter()
|
||||
.any(|scope| matches!(scope, Scope::Foreign { .. }))
|
||||
&& !tag_is(
|
||||
tag,
|
||||
&[
|
||||
"area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta",
|
||||
"param", "source", "track", "wbr",
|
||||
],
|
||||
)
|
||||
{
|
||||
scopes.push(Scope::Html(tag));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn encode_tsv(rows: &[Vec<TableClipboardCell>]) -> String {
|
||||
let mut text = String::new();
|
||||
for (r, row) in rows.iter().enumerate() {
|
||||
@@ -248,7 +560,10 @@ fn decode_tsv(text: &str) -> Vec<Vec<TableClipboardCell>> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{TableClipboard, TableClipboardCell, decode_tsv, encode_tsv};
|
||||
use super::{
|
||||
TABLE_ATTR, TableClipboard, TableClipboardCell, decode_tsv, encode_html_snapshot,
|
||||
encode_tsv,
|
||||
};
|
||||
use serde_json::json;
|
||||
use ui::ClipboardItem;
|
||||
|
||||
@@ -353,4 +668,259 @@ mod tests {
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn html_attribute_syntax_preserves_one_comma_label() {
|
||||
let payload = encode_html_snapshot(
|
||||
r#"{"database_table":[[{"ids":["o1"],"labels":["Sales, APAC"],"source":null}]]}"#,
|
||||
);
|
||||
for head in [
|
||||
format!("<table {TABLE_ATTR}='{payload}'>"),
|
||||
format!("<TABLE DATA-AFFINE-DATABASE-TABLE \n=\t \"{payload}\">"),
|
||||
format!("<table {TABLE_ATTR}={payload}>"),
|
||||
format!("<DIV DATA-AFFINE-DATABASE-TABLE = '{payload}'><table>"),
|
||||
format!("<div {TABLE_ATTR}=fake><table {TABLE_ATTR}='{payload}'>"),
|
||||
format!(
|
||||
"<div title=\"<table {TABLE_ATTR}='fake'>\"><table hidden data-other=\"{TABLE_ATTR}='fake' >\" x-{TABLE_ATTR}=fake {TABLE_ATTR}-extra=fake {TABLE_ATTR}='{payload}'>"
|
||||
),
|
||||
format!(
|
||||
"<!-- <table {TABLE_ATTR}=fake> --><title><table></title><table {TABLE_ATTR}='{payload}'>"
|
||||
),
|
||||
] {
|
||||
let rows = TableClipboard::from_item(&html_item("Sales, APAC", &head)).unwrap();
|
||||
assert!(
|
||||
matches!(&rows[..], [row] if matches!(&row[..],
|
||||
[TableClipboardCell::Options { labels, .. }] if labels == &["Sales, APAC"])),
|
||||
"{head}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn html_attribute_uses_the_first_body_table_and_its_open_divs() {
|
||||
let text = "Sales, APAC";
|
||||
let payload = encode_html_snapshot(
|
||||
r#"{"database_table":[[{"ids":["o1"],"labels":["Sales, APAC"]}]]}"#,
|
||||
);
|
||||
let wrapper = format!("<div {TABLE_ATTR}='{payload}'>");
|
||||
let table = "<table><tr><td>Sales, APAC</td></tr></table>";
|
||||
for html in [
|
||||
format!(
|
||||
"<template><table {TABLE_ATTR}='decoy'><tr><td>draft</td></tr></table></template>{wrapper}{table}</div>"
|
||||
),
|
||||
format!(
|
||||
"<template><table><tr><td>draft</td></tr></table></template>{wrapper}{table}</div>"
|
||||
),
|
||||
format!(
|
||||
"{wrapper}<style>td {{ color: red; }}</style><meta charset=\"utf-8\">{table}</div>"
|
||||
),
|
||||
format!("{wrapper}<template></template>{table}</div>"),
|
||||
format!(
|
||||
"<!DOCTYPE html><!-- comment -->{wrapper}<!-- inner --><div class=\"inner\">{table}</div></div>"
|
||||
),
|
||||
format!(
|
||||
"{wrapper}<template/><template></template><script>'</template>'</script><style>/* </template> */</style><textarea></template></textarea><div {TABLE_ATTR}='decoy'>{table}</div></template>{table}</div>"
|
||||
),
|
||||
format!("{wrapper}<?ignore?><div {TABLE_ATTR}='decoy'></div>{table}</div>"),
|
||||
format!("<div {TABLE_ATTR}='decoy'>{wrapper}{table}</div></div>"),
|
||||
] {
|
||||
let rows = TableClipboard::from_item(&html_item(text, &html)).unwrap();
|
||||
assert!(
|
||||
matches!(&rows[..], [row] if matches!(&row[..],
|
||||
[TableClipboardCell::Options { labels, .. }] if labels == &[text])),
|
||||
"{html}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn database_paste_uses_the_body_table_after_foreign_content() {
|
||||
use super::super::tests_title_menus_and_keys::{Table, fixture};
|
||||
use blocksuite_playground_doc::schema::DatabaseColumnSpec;
|
||||
|
||||
let (mut doc, db) = fixture();
|
||||
let page = doc.workspace.page_mut(crate::HOME_PAGE_ID).unwrap();
|
||||
page.add_database_column(
|
||||
&db,
|
||||
Some("v-table"),
|
||||
DatabaseColumnSpec {
|
||||
id: "target".into(),
|
||||
column_type: "multi-select".into(),
|
||||
name: "Region".into(),
|
||||
data: json!({ "options": [] }),
|
||||
},
|
||||
Some(1),
|
||||
24,
|
||||
)
|
||||
.unwrap();
|
||||
page.stop_capturing().unwrap();
|
||||
let table = Table::open(doc, db, 800);
|
||||
let before = table.read();
|
||||
|
||||
let (text, body) = TableClipboard::encode(vec![vec![TableClipboardCell::Options {
|
||||
ids: vec!["o1".into()],
|
||||
labels: vec!["Sales, APAC".into()],
|
||||
source: None,
|
||||
}]]);
|
||||
for prefix in [
|
||||
r#"<svg xmlns="http://www.w3.org/2000/svg"><title/><path d="M0 0"/></svg>"#,
|
||||
r#"<svg><title></title><path d="M0 0"></path></svg>"#,
|
||||
"<svg/><math/>",
|
||||
"<svg data-x=a/b><title/></svg>",
|
||||
"<svg data-x=a/><title/></svg>",
|
||||
r#"<svg data-x="a/" ><title/></svg>"#,
|
||||
r#"<svg data-x="a/b"/><title/><table>decoy</table></title>"#,
|
||||
"<svg data-x=a /><title/><table>decoy</table></title>",
|
||||
"<math><mrow><math:table data-affine-database-table=decoy/></mrow></math>",
|
||||
"<svg><![CDATA[<table data-affine-database-table=decoy></table></svg>]]><title/></svg>",
|
||||
"<svg><style><![CDATA[/* <table> */]]></style></svg>",
|
||||
"<svg><foreignObject/><plaintext/><style/><template/><svg:div/></svg>",
|
||||
"<math><annotation-xml encoding='text/html'/><title/><math:div><math:table/></math:div></math>",
|
||||
"<template><svg><title/></svg><table>decoy</table></template>",
|
||||
"<svg><foreignObject><div data-affine-database-table=decoy></div></foreignObject></svg>",
|
||||
] {
|
||||
table.select_cell("r2", "target");
|
||||
table.paste_event(html_item(&text, &format!("{prefix}{body}")));
|
||||
let data = table.read();
|
||||
let options = data
|
||||
.columns
|
||||
.iter()
|
||||
.find(|column| column.id == "target")
|
||||
.unwrap()
|
||||
.select_options();
|
||||
let [option] = options.as_slice() else {
|
||||
panic!("{prefix}: expected one option, got {}", options.len());
|
||||
};
|
||||
assert_eq!(option.value, "Sales, APAC", "{prefix}");
|
||||
assert_eq!(
|
||||
data.cells[&("r2".into(), "target".into())].as_string_list(),
|
||||
[option.id.as_str()],
|
||||
"{prefix}"
|
||||
);
|
||||
table.undo();
|
||||
let restored = table.read();
|
||||
assert_eq!(restored.cells, before.cells, "{prefix}");
|
||||
assert_eq!(restored.columns, before.columns, "{prefix}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn html_integration_points_keep_tables_and_their_html_ancestors() {
|
||||
use super::table_attribute;
|
||||
|
||||
for (html, expected) in [
|
||||
(
|
||||
"<svg><g><foreignObject><div data-affine-database-table=inner><table></table></div></foreignObject></g></svg>",
|
||||
Some("inner"),
|
||||
),
|
||||
(
|
||||
"<div data-affine-database-table=outer><svg><foreignObject></div><table></table></foreignObject></svg></div>",
|
||||
Some("outer"),
|
||||
),
|
||||
(
|
||||
"<svg><title><table data-affine-database-table=inner></table></title></svg>",
|
||||
Some("inner"),
|
||||
),
|
||||
(
|
||||
"<svg><desc><title/><table>decoy</table></title><table data-affine-database-table=inner></table></desc></svg>",
|
||||
Some("inner"),
|
||||
),
|
||||
(
|
||||
"<math><annotation-xml encoding='TEXT/HTML'><table data-affine-database-table=inner/></annotation-xml></math>",
|
||||
Some("inner/"),
|
||||
),
|
||||
(
|
||||
"<math><annotation-xml encoding='application/xhtml+xml'><svg><title/></svg><table data-affine-database-table=inner></table></annotation-xml></math>",
|
||||
Some("inner"),
|
||||
),
|
||||
(
|
||||
"<math><annotation-xml encoding='text/plain'><svg><foreignObject><table data-affine-database-table=inner></table></foreignObject></svg></annotation-xml></math>",
|
||||
Some("inner"),
|
||||
),
|
||||
(
|
||||
"<svg><foreignObject><template><table data-affine-database-table=decoy></table></template></foreignObject><svg:div data-affine-database-table=decoy><svg:table></svg:table></svg:div></svg><table></table>",
|
||||
None,
|
||||
),
|
||||
(
|
||||
"<math><annotation-xml encoding='text/plain' encoding='text/html'><title/><table data-affine-database-table=first></table></annotation-xml></math><table></table>",
|
||||
Some("first"),
|
||||
),
|
||||
] {
|
||||
assert_eq!(table_attribute(html), expected, "{html}");
|
||||
}
|
||||
for name in ["mi", "mo", "mn", "ms", "mtext"] {
|
||||
let html = format!(
|
||||
"<math><{name}><title/><table>decoy</table></title><table data-affine-database-table=first></table></{name}></math>"
|
||||
);
|
||||
assert_eq!(table_attribute(&html), Some("first"), "{html}");
|
||||
}
|
||||
for name in ["mglyph", "malignmark"] {
|
||||
let html = format!(
|
||||
"<math><mtext><{name}><title/></{name}><table data-affine-database-table=first></table></mtext></math>"
|
||||
);
|
||||
assert_eq!(table_attribute(&html), Some("first"), "{html}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn html_tables_can_end_foreign_content() {
|
||||
use super::table_attribute;
|
||||
|
||||
let table = "<table data-affine-database-table=first></table>";
|
||||
for html in [
|
||||
format!("<math><mrow>{table}</mrow></math>"),
|
||||
format!("<svg><g>{table}</g></svg>"),
|
||||
format!("<svg><style>/* {table} */</style></svg>"),
|
||||
format!("<svg><g><b><title/><table>ignored</table></title>{table}</b></g></svg>"),
|
||||
format!(
|
||||
"<svg><g><font color=red><title/><table>ignored</table></title>{table}</font></g></svg>"
|
||||
),
|
||||
] {
|
||||
assert_eq!(table_attribute(&html), Some("first"), "{html}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn html_attribute_entities_restore_labels_and_keep_text_validation() {
|
||||
let text = r#""Sales, APAC & <team> 'A' ""B""""#;
|
||||
let label = r#"Sales, APAC & <team> 'A' "B""#;
|
||||
for quote in [""", """, """] {
|
||||
let json = r#"{"database_table":[[{"ids":["o1"],"labels":["Sales, APAC & <team> 'A' \"B\""],"source":null}]]}"#;
|
||||
let html = format!("<table {TABLE_ATTR}='{}'>", json.replace('"', quote));
|
||||
let rows = TableClipboard::from_item(&html_item(text, &html)).unwrap();
|
||||
assert!(
|
||||
matches!(&rows[..], [row] if matches!(&row[..],
|
||||
[TableClipboardCell::Options { labels, .. }] if labels == &[label])),
|
||||
"{quote}"
|
||||
);
|
||||
let rows = TableClipboard::from_item(&html_item("Edited", &html)).unwrap();
|
||||
assert!(matches!(&rows[..], [row] if matches!(&row[..],
|
||||
[TableClipboardCell::Text(value)] if value == "Edited")));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn html_attribute_ignores_decoys_and_invalid_cells() {
|
||||
let text = "Sales, APAC";
|
||||
let payload = encode_html_snapshot(
|
||||
r#"{"database_table":[[{"ids":["o1"],"labels":["Sales, APAC"]}]]}"#,
|
||||
);
|
||||
for html in [
|
||||
format!("<table x-{TABLE_ATTR}=\"{payload}\" {TABLE_ATTR}-extra='{payload}'>"),
|
||||
format!("<table data-note='{TABLE_ATTR}=\"{payload}\"'>"),
|
||||
format!("<div {TABLE_ATTR}=\"{payload}\"></div><table>"),
|
||||
format!("<table><tr><td>{TABLE_ATTR}=\"{payload}\"</td></tr></table>"),
|
||||
format!("<table><tr><td><div {TABLE_ATTR}=\"{payload}\"></div></td></tr></table>"),
|
||||
format!("<table {TABLE_ATTR}=\"{payload}\""),
|
||||
format!("<table {TABLE_ATTR}='{{\"database_table\":[[],[]]}}'>"),
|
||||
format!("<table {TABLE_ATTR}='{{\"database_table\":true}}'>"),
|
||||
] {
|
||||
let rows = TableClipboard::from_item(&html_item(text, &html)).unwrap();
|
||||
assert!(
|
||||
matches!(&rows[..], [row] if matches!(&row[..],
|
||||
[TableClipboardCell::Text(value)] if value == text)),
|
||||
"{html}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user