mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Fix unicode rendering, handle checked attribute, string escaping, array rendering
This commit is contained in:
4
.vscode/deno.jsonc
vendored
4
.vscode/deno.jsonc
vendored
@@ -1,4 +1,8 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"jsx": "react-jsx",
|
||||
"jsxImportSource": "https://esm.sh/preact@10.11.2"
|
||||
},
|
||||
"lint": {
|
||||
"rules": {
|
||||
"exclude": ["require-await", "prefer-const"]
|
||||
|
||||
@@ -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(())
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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(),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user