Skip to content

Commit 8284501

Browse files
committed
escape css class names
1 parent 5ec0335 commit 8284501

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
- This avoids accidental deletion by bots following links, and is more in line with HTTP semantics.
66
- In the table component, the `_col_` prefix is now added to column names in CSS classes. This avoids conflicts with other CSS classes that might be used in the page.
77
- fixes https://github.com/sqlpage/SQLPage/issues/830
8+
- This is a breaking change for custom CSS rules that target table columns by their name.
9+
- Before: `.my_column { ... }`
10+
- After: `._col_my_column { ... }`
811

912
## 0.33.1 (2025-02-25)
1013

sqlpage/templates/table.handlebars

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
{{#each this}}
3535
{{#if (not (starts_with @key '_sqlpage_'))}}
3636
<th class="
37-
_col_{{~@key~}}
37+
_col_{{replace @key ' ' '_'~}}
3838
{{~#if (array_contains_case_insensitive ../../align_right @key)}} text-end {{/if~}}
3939
{{~#if (array_contains_case_insensitive ../../align_center @key)}} text-center {{/if~}}
4040
"
@@ -58,7 +58,7 @@
5858
<tr class="{{_sqlpage_css_class}} {{#if _sqlpage_color}}bg-{{_sqlpage_color}}-lt{{/if}}" {{#if _sqlpage_id}}id="{{_sqlpage_id}}"{{/if}}>
5959
{{~#each this~}}
6060
{{~#if (not (starts_with @key '_sqlpage_'))~}}
61-
<td class="align-middle {{@key~}}
61+
<td class="align-middle _col_{{replace @key ' ' '_'~}}
6262
{{~#if (array_contains_case_insensitive ../../align_right @key)}} text-end {{/if~}}
6363
{{~#if (array_contains_case_insensitive ../../align_center @key)}} text-center {{/if~}}
6464
">

src/template_helpers.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type H = fn(&JsonValue) -> JsonValue;
1616
type EH = fn(&JsonValue) -> anyhow::Result<JsonValue>;
1717
/// Helper that takes two arguments
1818
type HH = fn(&JsonValue, &JsonValue) -> JsonValue;
19+
/// Helper that takes three arguments
20+
type HHH = fn(&JsonValue, &JsonValue, &JsonValue) -> JsonValue;
1921

2022
pub fn register_all_helpers(h: &mut Handlebars<'_>, config: &AppConfig) {
2123
let site_prefix = config.site_prefix.clone();
@@ -27,6 +29,7 @@ pub fn register_all_helpers(h: &mut Handlebars<'_>, config: &AppConfig) {
2729
register_helper(h, "parse_json", parse_json_helper as EH);
2830
register_helper(h, "default", default_helper as HH);
2931
register_helper(h, "entries", entries_helper as H);
32+
register_helper(h, "replace", replace_helper as HHH);
3033
// delay helper: store a piece of information in memory that can be output later with flush_delayed
3134
h.register_helper("delay", Box::new(delay_helper));
3235
h.register_helper("flush_delayed", Box::new(flush_delayed_helper));
@@ -464,6 +467,15 @@ impl CanHelp for HH {
464467
}
465468
}
466469

470+
impl CanHelp for HHH {
471+
fn call(&self, args: &[PathAndJson]) -> Result<JsonValue, String> {
472+
match args {
473+
[a, b, c] => Ok(self(a.value(), b.value(), c.value())),
474+
_ => Err("expected three arguments".to_string()),
475+
}
476+
}
477+
}
478+
467479
struct JFun<F: CanHelp> {
468480
name: &'static str,
469481
fun: F,
@@ -488,6 +500,23 @@ fn register_helper(h: &mut Handlebars, name: &'static str, fun: impl CanHelp) {
488500
h.register_helper(name, Box::new(JFun { name, fun }));
489501
}
490502

503+
fn replace_helper(text: &JsonValue, original: &JsonValue, replacement: &JsonValue) -> JsonValue {
504+
let text_str = match text {
505+
JsonValue::String(s) => s,
506+
other => &other.to_string(),
507+
};
508+
let original_str = match original {
509+
JsonValue::String(s) => s,
510+
other => &other.to_string(),
511+
};
512+
let replacement_str = match replacement {
513+
JsonValue::String(s) => s,
514+
other => &other.to_string(),
515+
};
516+
517+
text_str.replace(original_str, replacement_str).into()
518+
}
519+
491520
#[test]
492521
fn test_rfc2822_date() {
493522
assert_eq!(

0 commit comments

Comments
 (0)