From e6045e633445e9f409ae2546b793c6ec3dd0412a Mon Sep 17 00:00:00 2001 From: Ian Harrigan Date: Wed, 3 Jul 2024 10:00:50 +0200 Subject: [PATCH] allow for LESS style extension syntax in haxeui css --- haxe/ui/styles/CompositeStyleSheet.hx | 15 +++++++++++---- haxe/ui/styles/Parser.hx | 5 +++++ haxe/ui/styles/Style.hx | 9 +++++++++ haxe/ui/styles/StyleSheet.hx | 4 ++-- haxe/ui/styles/elements/DirectiveExtension.hx | 7 +++++++ 5 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 haxe/ui/styles/elements/DirectiveExtension.hx diff --git a/haxe/ui/styles/CompositeStyleSheet.hx b/haxe/ui/styles/CompositeStyleSheet.hx index b460d0b5b..f0cfbd81f 100644 --- a/haxe/ui/styles/CompositeStyleSheet.hx +++ b/haxe/ui/styles/CompositeStyleSheet.hx @@ -98,9 +98,9 @@ class CompositeStyleSheet { return null; } - public function findRule(selector:String):RuleElement { + public function findRule(selector:String, useCache:Bool = false):RuleElement { for (s in _styleSheets) { - var el = s.findRule(selector); + var el = s.findRule(selector, useCache); if (el != null) { return el; } @@ -108,10 +108,17 @@ class CompositeStyleSheet { return null; } - public function findMatchingRules(selector:String):Array { + private var _matchingRuleCache:Map> = new Map>(); + public function findMatchingRules(selector:String, useCache = false):Array { + if (useCache && _matchingRuleCache.exists(selector)) { + return _matchingRuleCache.get(selector); + } var m = []; for (s in _styleSheets) { - m = m.concat(s.findMatchingRules(selector)); + m = m.concat(s.findMatchingRules(selector, useCache)); + } + if (useCache && m.length > 0) { + _matchingRuleCache.set(selector, m); } return m; } diff --git a/haxe/ui/styles/Parser.hx b/haxe/ui/styles/Parser.hx index e9e87f921..b03e3f752 100644 --- a/haxe/ui/styles/Parser.hx +++ b/haxe/ui/styles/Parser.hx @@ -3,6 +3,7 @@ package haxe.ui.styles; import haxe.ui.styles.elements.AnimationKeyFrames; import haxe.ui.styles.elements.AnimationKeyFrame; import haxe.ui.styles.elements.Directive; +import haxe.ui.styles.elements.DirectiveExtension; import haxe.ui.styles.elements.ImportElement; import haxe.ui.styles.elements.MediaQuery; import haxe.ui.styles.elements.RuleElement; @@ -10,6 +11,8 @@ import haxe.ui.themes.ThemeManager; // based on: https://github.com/jotform/css.js/blob/master/css.js +using StringTools; + @:access(haxe.ui.themes.ThemeManager) class Parser { static var cssKeyframesRegex:EReg = ~/@keyframes\s*(\w+?)\s*\{([\s\S]*?\}\s*?)\}/gi; @@ -172,6 +175,8 @@ class Parser { return null; } d = new Directive(cssDirective, ValueTools.parse(cssValue)); + } else if (line.startsWith(".")) { + d = new DirectiveExtension(line); } else { d = new Directive("", ValueTools.parse(line), true); } diff --git a/haxe/ui/styles/Style.hx b/haxe/ui/styles/Style.hx index 5583fdc43..fdbddacf5 100644 --- a/haxe/ui/styles/Style.hx +++ b/haxe/ui/styles/Style.hx @@ -4,6 +4,7 @@ import haxe.ui.filters.Filter; import haxe.ui.filters.FilterParser; import haxe.ui.styles.animation.Animation.AnimationOptions; import haxe.ui.styles.elements.Directive; +import haxe.ui.styles.elements.DirectiveExtension; import haxe.ui.util.Variant; enum StyleBorderType { @@ -197,6 +198,14 @@ class Style { public function mergeDirectives(map:Map) { for (key in map.keys()) { var v = map.get(key); + if ((v is DirectiveExtension)) { + var ruleElements = Toolkit.styleSheet.findMatchingRules(v.directive, true); + if (ruleElements != null) { + for (ruleElement in ruleElements) { + mergeDirectives(ruleElement.directives); + } + } + } switch (key) { case "left": diff --git a/haxe/ui/styles/StyleSheet.hx b/haxe/ui/styles/StyleSheet.hx index 09195ec18..f50b77754 100644 --- a/haxe/ui/styles/StyleSheet.hx +++ b/haxe/ui/styles/StyleSheet.hx @@ -50,7 +50,7 @@ class StyleSheet { return _mediaQueries.length > 0; } - public function findRule(selector:String):RuleElement { + public function findRule(selector:String, useCache = false):RuleElement { for (r in rules) { if (r.selector.toString() == selector) { return r; @@ -59,7 +59,7 @@ class StyleSheet { return null; } - public function findMatchingRules(selector:String):Array { + public function findMatchingRules(selector:String, useCache = false):Array { var m = []; for (r in rules) { if (r.selector.toString() == selector) { diff --git a/haxe/ui/styles/elements/DirectiveExtension.hx b/haxe/ui/styles/elements/DirectiveExtension.hx new file mode 100644 index 000000000..f084bea9e --- /dev/null +++ b/haxe/ui/styles/elements/DirectiveExtension.hx @@ -0,0 +1,7 @@ +package haxe.ui.styles.elements; + +class DirectiveExtension extends Directive { + public function new(directive:String) { + super(directive, null, false); + } +} \ No newline at end of file