Provide context on desktop again
This commit is contained in:
3 files changed
+5
-104
No files matched your search
@@ -1,82 +0,0 @@
|
|||||||
use std::fmt::{self, Write};
|
|
||||||
|
|
||||||
/// Escape a string to pass it into JavaScript.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust,ignore
|
|
||||||
/// # use web_view::WebView;
|
|
||||||
/// # use std::mem;
|
|
||||||
/// #
|
|
||||||
/// # let mut view: WebView<()> = unsafe { mem::uninitialized() };
|
|
||||||
/// #
|
|
||||||
/// let string = "Hello, world!";
|
|
||||||
///
|
|
||||||
/// // Calls the function callback with "Hello, world!" as its parameter.
|
|
||||||
///
|
|
||||||
/// view.eval(&format!("callback({});", web_view::escape(string)));
|
|
||||||
/// ```
|
|
||||||
#[allow(unused)]
|
|
||||||
pub fn escape_js_string(string: &str) -> Escaper {
|
|
||||||
Escaper(string)
|
|
||||||
}
|
|
||||||
|
|
||||||
// "All code points may appear literally in a string literal except for the
|
|
||||||
// closing quote code points, U+005C (REVERSE SOLIDUS), U+000D (CARRIAGE
|
|
||||||
// RETURN), U+2028 (LINE SEPARATOR), U+2029 (PARAGRAPH SEPARATOR), and U+000A
|
|
||||||
// (LINE FEED)." - ES6 Specification
|
|
||||||
|
|
||||||
pub struct Escaper<'a>(&'a str);
|
|
||||||
|
|
||||||
const SPECIAL: &[char] = &[
|
|
||||||
'\n', // U+000A (LINE FEED)
|
|
||||||
'\r', // U+000D (CARRIAGE RETURN)
|
|
||||||
'\'', // U+0027 (APOSTROPHE)
|
|
||||||
'\\', // U+005C (REVERSE SOLIDUS)
|
|
||||||
'\u{2028}', // U+2028 (LINE SEPARATOR)
|
|
||||||
'\u{2029}', // U+2029 (PARAGRAPH SEPARATOR)
|
|
||||||
];
|
|
||||||
|
|
||||||
impl<'a> fmt::Display for Escaper<'a> {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
let &Escaper(mut string) = self;
|
|
||||||
|
|
||||||
f.write_char('\'')?;
|
|
||||||
|
|
||||||
while !string.is_empty() {
|
|
||||||
if let Some(i) = string.find(SPECIAL) {
|
|
||||||
if i > 0 {
|
|
||||||
f.write_str(&string[..i])?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut chars = string[i..].chars();
|
|
||||||
|
|
||||||
f.write_str(match chars.next().unwrap() {
|
|
||||||
'\n' => "\\n",
|
|
||||||
'\r' => "\\r",
|
|
||||||
'\'' => "\\'",
|
|
||||||
'\\' => "\\\\",
|
|
||||||
'\u{2028}' => "\\u2028",
|
|
||||||
'\u{2029}' => "\\u2029",
|
|
||||||
_ => unreachable!(),
|
|
||||||
})?;
|
|
||||||
|
|
||||||
string = chars.as_str();
|
|
||||||
} else {
|
|
||||||
f.write_str(string)?;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
f.write_char('\'')?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test() {
|
|
||||||
let plain = "ABC \n\r' abc \\ \u{2028} \u{2029}123";
|
|
||||||
let escaped = escape_js_string(plain).to_string();
|
|
||||||
assert!(escaped == "'ABC \\n\\r\\' abc \\\\ \\u2028 \\u2029123'");
|
|
||||||
}
|
|
||||||
@@ -9,7 +9,6 @@ mod config;
|
|||||||
mod desktop_context;
|
mod desktop_context;
|
||||||
mod edits;
|
mod edits;
|
||||||
mod element;
|
mod element;
|
||||||
mod escape;
|
|
||||||
mod eval;
|
mod eval;
|
||||||
mod events;
|
mod events;
|
||||||
mod file_upload;
|
mod file_upload;
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use crate::{
|
|||||||
waker::tao_waker,
|
waker::tao_waker,
|
||||||
Config, DesktopContext, DesktopService,
|
Config, DesktopContext, DesktopService,
|
||||||
};
|
};
|
||||||
use dioxus_core::VirtualDom;
|
use dioxus_core::{ScopeId, VirtualDom};
|
||||||
use dioxus_html::prelude::EvalProvider;
|
use dioxus_html::prelude::EvalProvider;
|
||||||
use futures_util::{pin_mut, FutureExt};
|
use futures_util::{pin_mut, FutureExt};
|
||||||
use std::{any::Any, rc::Rc, task::Waker};
|
use std::{any::Any, rc::Rc, task::Waker};
|
||||||
@@ -148,27 +148,13 @@ impl WebviewInstance {
|
|||||||
asset_handlers,
|
asset_handlers,
|
||||||
));
|
));
|
||||||
|
|
||||||
// Provide the desktop context to the virtualdom
|
|
||||||
// dom.base_scope().provide_context(desktop_context.clone());
|
|
||||||
|
|
||||||
// let query = dom.in_runtime(|| {
|
|
||||||
// let query = ScopeId::ROOT.provide_context(desktop_context.clone());
|
|
||||||
// // Init eval
|
|
||||||
// init_eval();
|
|
||||||
// query
|
|
||||||
// });
|
|
||||||
|
|
||||||
// let desktop_ctx = ScopeId::ROOT.consume_context::<DesktopContext>().unwrap();
|
|
||||||
// let provider: Rc<dyn EvalProvider> = Rc::new(DesktopEvalProvider { desktop_ctx });
|
|
||||||
// ScopeId::ROOT.provide_context(provider);
|
|
||||||
|
|
||||||
// Also set up its eval provider
|
|
||||||
// It's important that we provide as dyn EvalProvider - using the concrete type has
|
|
||||||
// a different TypeId and can not be downcasted as dyn EvalProvider
|
|
||||||
let provider: Rc<dyn EvalProvider> =
|
let provider: Rc<dyn EvalProvider> =
|
||||||
Rc::new(DesktopEvalProvider::new(desktop_context.clone()));
|
Rc::new(DesktopEvalProvider::new(desktop_context.clone()));
|
||||||
|
|
||||||
// dom.base_scope().provide_context(provider);
|
dom.in_runtime(|| {
|
||||||
|
ScopeId::ROOT.provide_context(desktop_context.clone());
|
||||||
|
ScopeId::ROOT.provide_context(provider);
|
||||||
|
});
|
||||||
|
|
||||||
WebviewInstance {
|
WebviewInstance {
|
||||||
waker: tao_waker(shared.proxy.clone(), desktop_context.window.id()),
|
waker: tao_waker(shared.proxy.clone(), desktop_context.window.id()),
|
||||||
@@ -196,8 +182,6 @@ impl WebviewInstance {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// self.desktop_context.send_edits(self.dom.render_immediate());
|
|
||||||
|
|
||||||
self.dom
|
self.dom
|
||||||
.render_immediate(&mut *self.desktop_context.mutation_state.borrow_mut());
|
.render_immediate(&mut *self.desktop_context.mutation_state.borrow_mut());
|
||||||
self.desktop_context.send_edits();
|
self.desktop_context.send_edits();
|
||||||
|
|||||||
Reference in new issue
Block a user