From 7cf114e6d8a658abd1fdafbd206d2122bbaed7d1 Mon Sep 17 00:00:00 2001 From: Zach Ahn Date: Thu, 29 Feb 2024 10:53:58 -0500 Subject: [PATCH] Import CSS parsing pattern from PHP Textile I copied the Regex patterns from PHP-Textile, with minor changes only as necessary. See original sources: --- src/block.rs | 10 +++++++--- tests/fixtures/issue-224.yaml | 10 ++++++++++ tests/test_yaml_fixtures.rs | 1 + 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/issue-224.yaml diff --git a/src/block.rs b/src/block.rs index 3e4d568..080ff77 100644 --- a/src/block.rs +++ b/src/block.rs @@ -30,7 +30,7 @@ impl BlockHtmlAttributes { { lazy_static! { static ref CSS_CLASS_NAME_RE: Regex = fregex!( - r"(?i)^-?[_a-z][_a-z0-9-]*$"); + r"^([-a-zA-Z 0-9_\/\[\].:!#]+)$"); } let trimmed_name = name.as_ref().trim(); if CSS_CLASS_NAME_RE.is_match(trimmed_name).unwrap_or_default() { @@ -110,7 +110,7 @@ impl BlockAttributes { static ref ATTR_PADDING_LEFT_RE: Regex = fregex!(r"([(]+)"); static ref ATTR_PADDING_RIGHT_RE: Regex = fregex!(r"([)]+)"); static ref ATTR_COL_RE: Regex = fregex!(r"^(?:\\(\d+)\.?)?\s*(\d+)?"); - static ref CSS_CLASSES_RE: Regex = fregex!(r"^([-a-zA-Z 0-9_\.\/\[\]]*)$"); + static ref CSS_CLASSES_RE: Regex = fregex!(r"^([-a-zA-Z 0-9_\.\/\[\]:!]*)$"); } let mut style = Vec::::new(); @@ -460,7 +460,11 @@ mod test { assert_eq!(atts.to_string(), " class=\"align-left\" id=\"id-value&data\""); assert!(atts.insert_css_class("otherclass")); assert_eq!(atts.to_string(), " class=\"align-left otherclass\" id=\"id-value&data\""); - assert!(!atts.insert_css_class("invalid/class/name")); + assert!(!atts.insert_css_class("invalid@class@name")); assert_eq!(atts.to_string(), " class=\"align-left otherclass\" id=\"id-value&data\""); + assert!(atts.insert_css_class("md:mt-3")); + assert_eq!(atts.to_string(), " class=\"align-left otherclass md:mt-3\" id=\"id-value&data\""); + assert!(atts.insert_css_class("!mt-3")); + assert_eq!(atts.to_string(), " class=\"align-left otherclass md:mt-3 !mt-3\" id=\"id-value&data\""); } } diff --git a/tests/fixtures/issue-224.yaml b/tests/fixtures/issue-224.yaml new file mode 100644 index 0000000..7b00dec --- /dev/null +++ b/tests/fixtures/issue-224.yaml @@ -0,0 +1,10 @@ +Support exclamation marks and colons in class names: + input: | + p(md:mt-3). Paragraph + + p(!mt-3). Paragraph + + expect: | +

Paragraph

+ +

Paragraph

diff --git a/tests/test_yaml_fixtures.rs b/tests/test_yaml_fixtures.rs index 6d90a71..ffed63c 100644 --- a/tests/test_yaml_fixtures.rs +++ b/tests/test_yaml_fixtures.rs @@ -184,5 +184,6 @@ fn test_yaml_fixtures() { "issue-189", "issue-198", "issue-202", + "issue-224", ]); }