Skip to content

Commit

Permalink
Merge pull request #33 from gjtorikian/dependabot/cargo/magnus-0.6.2
Browse files Browse the repository at this point in the history
Bump magnus from 0.5.4 to 0.6.2
  • Loading branch information
gjtorikian authored Oct 12, 2023
2 parents 4b5ad6c + 39e84a0 commit 2355ae6
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 163 deletions.
35 changes: 21 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ext/selma/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
[dependencies]
enum-iterator = "1.4"
escapist = "0.0.2"
magnus = "0.5"
magnus = "0.6"
lol_html = "1.2"

[lib]
Expand Down
4 changes: 3 additions & 1 deletion ext/selma/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use magnus::{Error, Module, RModule};
pub(crate) struct SelmaHTML {}

pub fn init(m_selma: RModule) -> Result<(), Error> {
let c_html = m_selma.define_class("HTML", Default::default()).unwrap();
let c_html = m_selma
.define_class("HTML", magnus::class::object())
.expect("cannot define class Selma::HTML");

element::init(c_html).expect("cannot define Selma::HTML::Element class");
end_tag::init(c_html).expect("cannot define Selma::HTML::EndTag class");
Expand Down
4 changes: 2 additions & 2 deletions ext/selma/src/html/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ impl SelmaHTMLElement {

pub fn init(c_html: RClass) -> Result<(), Error> {
let c_element = c_html
.define_class("Element", Default::default())
.expect("cannot find class Selma::HTML::Element");
.define_class("Element", magnus::class::object())
.expect("cannot define class Selma::HTML::Element");

c_element.define_method("tag_name", method!(SelmaHTMLElement::tag_name, 0))?;
c_element.define_method("tag_name=", method!(SelmaHTMLElement::set_tag_name, 1))?;
Expand Down
4 changes: 2 additions & 2 deletions ext/selma/src/html/end_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ impl SelmaHTMLEndTag {

pub fn init(c_html: RClass) -> Result<(), Error> {
let c_end_tag = c_html
.define_class("EndTag", Default::default())
.expect("cannot find class Selma::HTML::EndTag");
.define_class("EndTag", magnus::class::object())
.expect("cannot define class Selma::HTML::EndTag");

c_end_tag.define_method("tag_name", method!(SelmaHTMLEndTag::tag_name, 0))?;

Expand Down
4 changes: 2 additions & 2 deletions ext/selma/src/html/text_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ impl SelmaHTMLTextChunk {

pub fn init(c_html: RClass) -> Result<(), Error> {
let c_text_chunk = c_html
.define_class("TextChunk", Default::default())
.expect("cannot find class Selma::HTML::TextChunk");
.define_class("TextChunk", magnus::class::object())
.expect("cannot define class Selma::HTML::TextChunk");

c_text_chunk.define_method("to_s", method!(SelmaHTMLTextChunk::to_s, 0))?;
c_text_chunk.define_method("content", method!(SelmaHTMLTextChunk::to_s, 0))?;
Expand Down
1 change: 0 additions & 1 deletion ext/selma/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub mod rewriter;
pub mod sanitizer;
pub mod selector;
pub mod tags;
pub mod wrapped_struct;

#[allow(clippy::let_unit_value)]
fn scan_text_args(args: &[Value]) -> Result<(String, ContentType), magnus::Error> {
Expand Down
70 changes: 34 additions & 36 deletions ext/selma/src/rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ use lol_html::{
html_content::{Element, TextChunk},
text, DocumentContentHandlers, ElementContentHandlers, HtmlRewriter, Selector, Settings,
};
use magnus::{exception, function, method, scan_args, Module, Object, RArray, RModule, Value};
use magnus::{
exception, function, method, scan_args,
typed_data::Obj,
value::{Opaque, ReprValue},
Module, Object, RArray, RModule, Ruby, Value,
};

use std::{borrow::Cow, cell::RefCell, primitive::str, rc::Rc};

Expand All @@ -12,13 +17,12 @@ use crate::{
sanitizer::SelmaSanitizer,
selector::SelmaSelector,
tags::Tag,
wrapped_struct::WrappedStruct,
};

#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct Handler {
rb_handler: Value,
rb_selector: WrappedStruct<SelmaSelector>,
rb_handler: Opaque<Value>,
rb_selector: Opaque<Obj<SelmaSelector>>,

total_element_handler_calls: usize,
total_elapsed_element_handlers: f64,
Expand Down Expand Up @@ -53,15 +57,15 @@ impl SelmaRewriter {
let sanitizer = match rb_sanitizer {
None => {
let default_sanitizer = SelmaSanitizer::new(&[])?;
let wrapped_sanitizer = WrappedStruct::from(default_sanitizer);
let wrapped_sanitizer = Obj::wrap(default_sanitizer);
wrapped_sanitizer.funcall::<&str, (), Value>("setup", ())?;
Some(wrapped_sanitizer.get().unwrap().to_owned())
Some(wrapped_sanitizer.get().to_owned())
}
Some(sanitizer_value) => match sanitizer_value {
None => None,
Some(sanitizer) => {
sanitizer.funcall::<&str, (), Value>("setup", ())?;
Some(sanitizer.get().unwrap().to_owned())
Some(sanitizer.get().to_owned())
}
},
};
Expand All @@ -86,19 +90,18 @@ impl SelmaRewriter {
));
}

let rb_selector: WrappedStruct<SelmaSelector> =
match rb_handler.funcall("selector", ()) {
Err(err) => {
return Err(magnus::Error::new(
exception::type_error(),
format!("Error instantiating selector: {err:?}"),
));
}
Ok(rb_selector) => rb_selector,
};
let rb_selector: Obj<SelmaSelector> = match rb_handler.funcall("selector", ()) {
Err(err) => {
return Err(magnus::Error::new(
exception::type_error(),
format!("Error instantiating selector: {err:?}"),
));
}
Ok(rb_selector) => rb_selector,
};
let handler = Handler {
rb_handler,
rb_selector,
rb_handler: Opaque::from(rb_handler),
rb_selector: Opaque::from(rb_selector),
total_element_handler_calls: 0,
total_elapsed_element_handlers: 0.0,

Expand Down Expand Up @@ -128,13 +131,7 @@ impl SelmaRewriter {
#[allow(clippy::let_unit_value)]
fn scan_parse_args(
args: &[Value],
) -> Result<
(
Option<Option<WrappedStruct<SelmaSanitizer>>>,
Option<RArray>,
),
magnus::Error,
> {
) -> Result<(Option<Option<Obj<SelmaSanitizer>>>, Option<RArray>), magnus::Error> {
let args = scan_args::scan_args(args)?;
let _: () = args.required;
let _: () = args.optional;
Expand All @@ -145,10 +142,7 @@ impl SelmaRewriter {
let kwargs = scan_args::get_kwargs::<
_,
(),
(
Option<Option<WrappedStruct<SelmaSanitizer>>>,
Option<RArray>,
),
(Option<Option<Obj<SelmaSanitizer>>>, Option<RArray>),
(),
>(args.keywords, &[], &["sanitizer", "handlers"])?;
let (rb_sanitizer, rb_handlers) = kwargs.optional;
Expand Down Expand Up @@ -270,7 +264,9 @@ impl SelmaRewriter {
handlers.iter().for_each(|handler| {
let element_stack: Rc<RefCell<Vec<String>>> = Rc::new(RefCell::new(vec![]));

let selector = handler.rb_selector.get_static().unwrap();
let ruby = Ruby::get().unwrap();

let selector = ruby.get_inner(handler.rb_selector);

// TODO: test final raise by simulating errors
if selector.match_element().is_some() {
Expand All @@ -280,7 +276,7 @@ impl SelmaRewriter {
selector.match_element().unwrap(),
move |el| {
match Self::process_element_handlers(
handler.rb_handler,
ruby.get_inner(handler.rb_handler),
el,
&closure_element_stack.borrow(),
) {
Expand Down Expand Up @@ -311,7 +307,9 @@ impl SelmaRewriter {
}
}

match Self::process_text_handlers(handler.rb_handler, text) {
let ruby = Ruby::get().unwrap();
match Self::process_text_handlers(ruby.get_inner(handler.rb_handler), text)
{
Ok(_) => Ok(()),
Err(err) => Err(err.to_string().into()),
}
Expand Down Expand Up @@ -421,8 +419,8 @@ impl SelmaRewriter {

pub fn init(m_selma: RModule) -> Result<(), magnus::Error> {
let c_rewriter = m_selma
.define_class("Rewriter", Default::default())
.expect("cannot find class Selma::Rewriter");
.define_class("Rewriter", magnus::class::object())
.expect("cannot define class Selma::Rewriter");

c_rewriter.define_singleton_method("new", function!(SelmaRewriter::new, -1))?;
c_rewriter
Expand Down
26 changes: 15 additions & 11 deletions ext/selma/src/sanitizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ use lol_html::{
errors::AttributeNameError,
html_content::{Comment, ContentType, Doctype, Element, EndTag},
};
use magnus::{class, function, method, scan_args, Module, Object, RArray, RHash, RModule, Value};
use magnus::{
class, function, method, scan_args,
value::{Opaque, ReprValue},
Module, Object, RArray, RHash, RModule, Ruby, Value,
};

#[derive(Clone, Debug)]
#[derive(Default)]
#[derive(Clone, Debug, Default)]
struct ElementSanitizer {
allowed_attrs: Vec<String>,
required_attrs: Vec<String>,
allowed_classes: Vec<String>,
protocol_sanitizers: HashMap<String, Vec<String>>,
}



#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct Sanitizer {
flags: [u8; crate::tags::Tag::TAG_COUNT],
allowed_attrs: Vec<String>,
Expand All @@ -27,10 +28,10 @@ pub struct Sanitizer {
pub escape_tagfilter: bool,
pub allow_comments: bool,
pub allow_doctype: bool,
config: RHash,
config: Opaque<RHash>,
}

#[derive(Clone, Debug)]
#[derive(Clone)]
#[magnus::wrap(class = "Selma::Sanitizer")]
pub struct SelmaSanitizer(std::cell::RefCell<Sanitizer>);

Expand Down Expand Up @@ -68,14 +69,15 @@ impl SelmaSanitizer {
escape_tagfilter: true,
allow_comments: false,
allow_doctype: true,
config,
config: config.into(),
})))
}

fn get_config(&self) -> Result<RHash, magnus::Error> {
let binding = self.0.borrow();
let ruby = Ruby::get().unwrap();

Ok(binding.config)
Ok(ruby.get_inner(binding.config))
}

/// Toggle a sanitizer option on or off.
Expand Down Expand Up @@ -545,7 +547,9 @@ impl SelmaSanitizer {
}

pub fn init(m_selma: RModule) -> Result<(), magnus::Error> {
let c_sanitizer = m_selma.define_class("Sanitizer", Default::default())?;
let c_sanitizer = m_selma
.define_class("Sanitizer", magnus::class::object())
.expect("cannot define class Selma::Sanitizer");

c_sanitizer.define_singleton_method("new", function!(SelmaSanitizer::new, -1))?;
c_sanitizer.define_method("config", method!(SelmaSanitizer::get_config, 0))?;
Expand Down
2 changes: 1 addition & 1 deletion ext/selma/src/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl SelmaSelector {

pub fn init(m_selma: RModule) -> Result<(), Error> {
let c_selector = m_selma
.define_class("Selector", Default::default())
.define_class("Selector", magnus::class::object())
.expect("cannot define class Selma::Selector");

c_selector.define_singleton_method("new", function!(SelmaSelector::new, -1))?;
Expand Down
Loading

0 comments on commit 2355ae6

Please sign in to comment.