Skip to content

Commit

Permalink
Fix unicode rendering, handle checked attribute, string escaping, arr…
Browse files Browse the repository at this point in the history
…ay rendering
  • Loading branch information
voltrevo committed Mar 1, 2024
1 parent 6d83ec2 commit f866fb1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
4 changes: 4 additions & 0 deletions .vscode/deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "https://esm.sh/[email protected]"
},
"lint": {
"rules": {
"exclude": ["require-await", "prefer-const"]
Expand Down
50 changes: 31 additions & 19 deletions valuescript_vm/src/jsx_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -119,43 +119,55 @@ 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)
}
}
}

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 {
let val_str = match key.as_str() {
"style" => render_css(val),
_ => val.to_string(),
};

write!(f, " {}=\"{}\"", key, val_str)?;
}
if key == "checked" {
match val.is_truthy() {
true => write!(f, " checked")?,
false => {}
}

Ok(())
}
continue;
}

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(())
Expand Down
11 changes: 10 additions & 1 deletion valuescript_vm/src/vs_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
Expand All @@ -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,
}
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion vstc/src/db_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
}
Expand Down

0 comments on commit f866fb1

Please sign in to comment.