Fix unicode rendering, handle checked attribute, string escaping, array rendering

This commit is contained in:
Andrew Morris
2024-03-01 14:45:50 +11:00
parent 6d83ec2079
commit f866fb1c7f
4 changed files with 48 additions and 23 deletions

4
.vscode/deno.jsonc vendored
View File

@@ -1,4 +1,8 @@
{
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "https://esm.sh/preact@10.11.2"
},
"lint": {
"rules": {
"exclude": ["require-await", "prefer-const"]

View File

@@ -6,7 +6,7 @@ use crate::{
builtins::type_error_builtin::ToTypeError,
vs_array::VsArray,
vs_class::VsClass,
vs_value::{Val, VsType},
vs_value::{stringify_string, Val, VsType},
LoadFunctionResult, ValTrait,
};
@@ -86,11 +86,11 @@ impl ValTrait for JsxElement {
if self.children.is_empty() {
write!(f, "\x1b[36m<{}\x1b[39m", tag_str)?;
write_attributes_pretty(f, &self.attrs)?;
write_attributes(f, &self.attrs, true)?;
write!(f, " \x1b[36m/>\x1b[39m")
} else {
write!(f, "\x1b[36m<{}\x1b[39m", tag_str)?;
write_attributes_pretty(f, &self.attrs)?;
write_attributes(f, &self.attrs, true)?;
write!(f, "\x1b[36m>\x1b[39m")?;
for child in &self.children {
@@ -119,15 +119,23 @@ impl fmt::Display for JsxElement {
if self.children.is_empty() {
write!(f, "<{}", tag_str)?;
write_attributes(f, &self.attrs)?;
write_attributes(f, &self.attrs, false)?;
write!(f, " />")
} else {
write!(f, "<{}", tag_str)?;
write_attributes(f, &self.attrs)?;
write_attributes(f, &self.attrs, false)?;
write!(f, ">")?;
for child in &self.children {
write!(f, "{}", child)?;
match child.not_ptr() {
Val::Void | Val::Undefined | Val::Null => {}
Val::Array(arr) => {
for val in &arr.elements {
write!(f, "{}", val)?;
}
}
_ => write!(f, "{}", child)?,
};
}
write!(f, "</{}>", tag_str)
@@ -135,27 +143,31 @@ impl fmt::Display for JsxElement {
}
}
fn write_attributes(f: &mut fmt::Formatter<'_>, attrs: &Vec<(String, Val)>) -> fmt::Result {
fn write_attributes(
f: &mut fmt::Formatter<'_>,
attrs: &Vec<(String, Val)>,
pretty: bool,
) -> fmt::Result {
for (key, val) in attrs {
if key == "checked" {
match val.is_truthy() {
true => write!(f, " checked")?,
false => {}
}
continue;
}
let val_str = match key.as_str() {
"style" => render_css(val),
_ => val.to_string(),
};
write!(f, " {}=\"{}\"", key, val_str)?;
}
Ok(())
}
fn write_attributes_pretty(f: &mut fmt::Formatter<'_>, attrs: &Vec<(String, Val)>) -> fmt::Result {
for (key, val) in attrs {
let val_str = match key.as_str() {
"style" => render_css(val),
_ => val.to_string(),
};
write!(f, " {}=\x1b[33m\"{}\"\x1b[39m", key, val_str)?;
if pretty {
write!(f, " {}=\x1b[33m{}\x1b[39m", key, val_str)?;
} else {
write!(f, " {}={}", key, stringify_string(&val_str))?;
}
}
Ok(())

View File

@@ -246,6 +246,13 @@ impl Val {
Val::StoragePtr(ptr) => ptr.get().to_json(),
}
}
pub fn not_ptr(&self) -> Val {
match self {
Val::StoragePtr(ptr) => ptr.get(),
_ => self.clone(),
}
}
}
impl ValTrait for Val {
@@ -425,6 +432,7 @@ impl ValTrait for Val {
match self {
Array(a) => Some(a.clone()),
Dynamic(val) => val.as_array_data(),
StoragePtr(ptr) => ptr.get().as_array_data(),
_ => None,
}
@@ -437,6 +445,7 @@ impl ValTrait for Val {
Class(class) => Some(class.clone()),
Static(s) => s.as_class_data(),
Dynamic(val) => val.as_class_data(),
StoragePtr(ptr) => ptr.get().as_class_data(),
_ => None,
}
@@ -872,7 +881,7 @@ pub fn number_to_index(x: f64) -> Option<usize> {
Some(x as usize)
}
fn stringify_string(str: &str) -> String {
pub fn stringify_string(str: &str) -> String {
let mut res: String = "\"".into();
for c in str.chars() {

View File

@@ -186,7 +186,7 @@ impl Handler<DbRequest> for DbActor {
if is_jsx_element(&res) {
break 'b Ok(DbResponse {
status: 200,
content_type: "text/html".to_owned(),
content_type: "text/html; charset=UTF-8".to_owned(),
body: res.to_string(),
});
}