#[cfg(all(feature = "leptos-0-8", not(feature = "leptos")))]
extern crate leptos_0_8 as leptos;
#[cfg(feature = "leptos")]
extern crate leptos_0_9 as leptos;
#[cfg(all(feature = "leptos-0-8", not(feature = "leptos")))]
extern crate tachys_0_2 as tachys;
#[cfg(feature = "leptos")]
extern crate tachys_0_3 as tachys;
use std::sync::Arc;
use leptos::prelude::*;
use mf2::leptos::{RequestI18n, Setup, install};
use mf2::{
Catalog, Dir, Function, MsgId, Registry, TrArgs, functions, markup, markup_key, tr, tr_rich,
};
use mf2_catalog::writer::{self, Options};
use tachys::html::attribute::AttributeValue;
use tachys::view::RenderHtml;
static FUNCTIONS: [(&str, &dyn Function); 1] = [("string", &functions::STRING)];
static REGISTRY: Registry = Registry::new(&FUNCTIONS);
static LOCALES: &[(&str, Dir)] = &[("en", Dir::Ltr), ("fr", Dir::Ltr), ("ar", Dir::Rtl)];
const ID: MsgId = MsgId::from_raw(0);
fn catalog(source: &str, locale: &str, dir: Dir, lender: Option<&str>) -> Arc<Catalog> {
let parsed = mf2_syntax::parse_model(source);
let model = parsed.message.expect("the message parses");
let analysis = mf2_syntax::analyze(&model);
let slots: Vec<&str> = analysis.externals.iter().map(|n| &*n.nfc).collect();
let mut options = Options::new(locale, dir);
if let Some(lender) = lender {
options.fallback.push((0, lender.to_owned()));
}
let (bytes, manifest) = writer::single(&model, &slots, &options).expect("the catalog writes");
Arc::new(Catalog::new(bytes, manifest.hash()).expect("the catalog reads"))
}
fn in_request<R>(catalog: &Arc<Catalog>, body: impl FnOnce() -> R) -> R {
install(Setup::new(
®ISTRY,
&mf2::host_std::HOST,
0,
"en",
LOCALES,
));
let owner = Owner::new();
owner.with(|| {
RequestI18n::new(Arc::clone(catalog)).provide();
body()
})
}
fn kbd_handlers() -> Box<[(u64, Arc<dyn mf2::MarkupHandler>)]> {
[(
markup_key("kbd"),
markup(|children: AnyView| view! { <kbd>{children}</kbd> }),
)]
.into()
}
#[test]
fn a_borrowed_message_renders_inside_a_span_with_the_lender_s_lang() {
let fr = catalog("Save", "fr", Dir::Ltr, Some("en"));
let html = in_request(&fr, || RenderHtml::to_html(tr(ID)));
assert_eq!(html, "<span lang=\"en\">Save</span>");
}
#[test]
fn a_lender_of_the_other_direction_adds_dir() {
let ar = catalog("Save", "ar", Dir::Rtl, Some("en"));
let html = in_request(&ar, || RenderHtml::to_html(tr(ID)));
assert_eq!(html, "<span lang=\"en\" dir=\"ltr\">Save</span>");
}
#[test]
fn an_own_message_has_no_span() {
let fr = catalog("Enregistrer", "fr", Dir::Ltr, None);
let html = in_request(&fr, || RenderHtml::to_html(tr(ID)));
assert_eq!(html, "Enregistrer");
}
#[test]
fn a_borrowed_rich_message_is_wrapped_whole() {
let source = "Press {#kbd}Esc{/kbd} to close";
let rich = || tr_rich(TrArgs::from(tr(ID)), kbd_handlers());
let own = in_request(&catalog(source, "fr", Dir::Ltr, None), || {
RenderHtml::to_html(rich())
});
let borrowed = in_request(&catalog(source, "fr", Dir::Ltr, Some("en")), || {
RenderHtml::to_html(rich())
});
assert_eq!(own, "Press <kbd>Esc<!></kbd> to close<!>");
assert_eq!(
borrowed,
"<span lang=\"en\">Press <kbd>Esc<!></kbd> to close<!></span><!>"
);
}
#[test]
fn text_around_a_wrapped_message_separates_as_around_an_unwrapped_one() {
let around = || RenderHtml::to_html(view! { <p>{"a"} {tr(ID)} {"b"}</p> });
let own = in_request(&catalog("Save", "fr", Dir::Ltr, None), around);
let borrowed = in_request(&catalog("Save", "fr", Dir::Ltr, Some("en")), around);
assert_eq!(own, "<p>a<!>Save<!>b</p>");
assert_eq!(
borrowed,
own.replace("Save", "<span lang=\"en\">Save</span>")
);
}
#[test]
fn a_text_only_element_gets_no_span() {
let fr = catalog("Save", "fr", Dir::Ltr, Some("en"));
let html = in_request(&fr, || {
RenderHtml::to_html(view! {
<div>
<textarea name="t">{tr(ID)}</textarea>
<select><option value="s">{tr(ID)}</option></select>
<p>{tr(ID)}</p>
</div>
})
});
assert_eq!(
html,
"<div><textarea name=\"t\">Save</textarea><select><option value=\"s\">Save</option></select>\
<p><span lang=\"en\">Save</span></p></div>"
);
let title = in_request(&fr, || {
RenderHtml::to_html(view! { <title>{tr(ID)}</title> })
});
assert_eq!(title, "<title>Save</title>");
}
#[test]
fn an_empty_borrowed_message_keeps_its_text_node() {
let fr = catalog("", "fr", Dir::Ltr, Some("en"));
let html = in_request(&fr, || RenderHtml::to_html(tr(ID)));
assert_eq!(html, "<span lang=\"en\"> </span>");
}
#[test]
fn an_attribute_and_a_string_are_unmarked() {
let fr = catalog("Save", "fr", Dir::Ltr, Some("en"));
let (attr, string) = in_request(&fr, || {
let mut buf = String::new();
AttributeValue::to_html(tr(ID), "title", &mut buf);
(buf, tr(ID).to_string())
});
assert_eq!(attr, " title=\"Save\"");
assert_eq!(string, "Save");
}