Compare commits

...

5 Commits

Author SHA1 Message Date
xenia 9fe91ebd2a add support for htmx attributes 2023-08-20 04:42:56 -04:00
ashley williams d2c0379311
Merge pull request #35 from axodotdev/0.5.0
release: 0.5.0
2023-03-30 08:52:49 -05:00
Ashley Williams f6f0a0385d release: 0.5.0 2023-03-30 08:36:22 -05:00
ashley williams 2b6b7cff64
Merge pull request #36 from axodotdev/console
feat(deps): ansi-term -> console
2023-03-29 15:19:04 -05:00
Ashley Williams cd0f7f4eb4 feat(deps): ansi-term -> console 2023-03-29 14:51:27 -05:00
7 changed files with 74 additions and 5 deletions

View File

@ -1,5 +1,28 @@
# Changelog
## 0.5.0 - 2023-03-29
### 🛠️ Fixes
- **✨ Fix broken links and naming issues from fork transition- [adrianheine], [pr29]**
This PR helps clean up a variety of references and links that weren't caught
when the project transitioned from a fork of the `typed-html` crate.
### 🌿 Maintenance
- **✨ Dependency gardening - [ashleygwilliams], [pr32]/[pr33]**
General dependency maintenance with two notable actions:
- replace `ansi_term` with `console` to match the rest of the axo toolchain
- update `lalrpop` to 0.19.9 (latest release) to address warning
[adrianheine]: https://github.com/adrianheine
[pr29]: https://github.com/axodotdev/axohtml/pull/29
[pr32]: https://github.com/axodotdev/axohtml/pull/32
[pr33]: https://github.com/axodotdev/axohtml/pull/33
## 0.4.1 - 2023-01-24
### 🛠️ Fixes

View File

@ -1,6 +1,6 @@
[package]
name = "axohtml-macros"
version = "0.4.1"
version = "0.5.0"
edition = "2018"
authors = ["Axo Developer Co <hello@axo.dev>", "Bodil Stokke <bodil@bodil.org>"]
build = "build.rs"
@ -17,9 +17,9 @@ proc-macro = true
[dependencies]
lalrpop-util = "0.19"
ansi_term = "0.12.1"
proc-macro2 = "1.0.54"
quote = "1.0.26"
console = "0.15.5"
[build-dependencies]
lalrpop = "0.19.9"

View File

@ -103,6 +103,7 @@ impl Declare {
pub attrs: #attr_type_name,
pub data_attributes: Vec<(&'static str, String)>,
pub aria_attributes: Vec<(&'static str, String)>,
pub htmx_attributes: Vec<(&'static str, String)>,
pub events: T::Events,
#body
}
@ -129,6 +130,7 @@ impl Declare {
));
body.extend(quote!(data_attributes: Vec::new(),));
body.extend(quote!(aria_attributes: Vec::new(),));
body.extend(quote!(htmx_attributes: Vec::new(),));
for (child_name, _, _) in self.req_children() {
body.extend(quote!( #child_name, ));
@ -178,6 +180,7 @@ impl Declare {
#push_attrs
attributes.extend(self.data_attributes.clone());
attributes.extend(self.aria_attributes.clone());
attributes.extend(self.htmx_attributes.clone());
let mut children = Vec::new();
#req_children
@ -247,6 +250,10 @@ impl Declare {
for (key, value) in &self.aria_attributes {
out.push((key, value.to_string()));
}
for (key, value) in &self.htmx_attributes {
out.push((key, value.to_string()));
}
out
}
}
@ -364,6 +371,10 @@ impl Declare {
write!(f, " aria-{}=\"{}\"", key,
crate::escape_html_attribute(value.to_string()))?;
}
for (key, value) in &self.htmx_attributes {
write!(f, " {}=\"{}\"", key,
crate::escape_html_attribute(value.to_string()))?;
}
write!(f, "{}", self.events)?;
#print_children
}

View File

@ -1,5 +1,5 @@
use crate::lexer::Token;
use ansi_term::Style;
use console::style;
use lalrpop_util::ParseError::*;
use proc_macro2::{Ident, TokenStream};
use quote::{quote, quote_spanned};
@ -71,7 +71,7 @@ pub fn parse_error(input: &[Token], error: &ParseError) -> TokenStream {
// special case: you probably meant to quote that text
let help_msg = format!(
"text nodes need to be quoted, eg. {}",
Style::new().bold().paint("<p>\"Hello Joe!\"</p>")
style("<p>\"Hello Joe!\"</p>").bold(),
);
Some(quote_spanned! {span=>
compile_error! { #help_msg }

View File

@ -115,6 +115,21 @@ fn extract_aria_attributes(
data
}
fn extract_htmx_attrs(attrs: &mut StringyMap<Ident, TokenTree>) -> StringyMap<String, TokenTree> {
let mut data = StringyMap::new();
let keys: Vec<Ident> = attrs.keys().cloned().collect();
for key in keys {
let key_name = key.to_string();
if key_name.starts_with("hx_") || key_name.starts_with("sse_")
|| key_name.starts_with("ws_") {
let value = attrs.remove(&key).unwrap();
let key = str::replace(&key_name, "_", "-");
data.insert(key.to_string(), value);
}
}
data
}
fn process_value(value: &TokenTree) -> TokenStream {
match value {
TokenTree::Group(g) if g.delimiter() == Delimiter::Bracket => {
@ -165,6 +180,7 @@ impl Element {
let events = extract_event_handlers(&mut self.attributes);
let data_attrs = extract_data_attrs(&mut self.attributes);
let aria_attrs = extract_aria_attributes(&mut self.attributes);
let htmx_attrs = extract_htmx_attrs(&mut self.attributes);
let attrs = self.attributes.iter().map(|(key, value)| {
(
key.to_string(),
@ -246,6 +262,15 @@ impl Element {
));
}
for (key, value) in htmx_attrs
.iter()
.map(|(k, v)| (TokenTree::from(Literal::string(k)), v.clone()))
{
body.extend(quote!(
element.htmx_attributes.push((#key, #value.into()));
));
}
body.extend(opt_children);
for (key, value) in events.iter() {

View File

@ -1,6 +1,6 @@
[package]
name = "axohtml"
version = "0.4.1"
version = "0.5.0"
edition = "2021"
authors = ["Axo Developer Co <hello@axo.dev>", "Bodil Stokke <bodil@bodil.org>"]
license = "MPL-2.0+"

View File

@ -490,6 +490,16 @@ fn test_aria() {
);
}
#[test]
fn test_htmx_attributes() {
use crate as axohtml;
use crate::{dom::DOMTree, html};
let frag: DOMTree<String> = html!(<div hx-ext="sse" sse-connect="/test">"Boo!"</div>);
assert_eq!("<div hx-ext=\"sse\" sse-connect=\"/test\">Boo!</div>", frag.to_string());
}
#[test]
fn test_js() {
use crate as axohtml;