Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion haxe/ui/styles/StyleSheet.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package haxe.ui.styles;

import haxe.ui.core.Component;
import haxe.ui.styles.elements.AnimationKeyFrames;
import haxe.ui.styles.elements.Directive;
import haxe.ui.styles.elements.ImportElement;
import haxe.ui.styles.elements.MediaQuery;
import haxe.ui.styles.elements.RuleElement;
import haxe.ui.styles.elements.Selector;

class StyleSheet {
public var name:String;
Expand Down Expand Up @@ -125,18 +127,45 @@ class StyleSheet {
}
}

var directives = new Map<String, Directive>();
var selectedSelectors = new Map<String, Selector>();

public function buildStyleFor(c:Component, style:Style = null):Style {
if (style == null) {
style = {};
}

if (rules.length <= 0) {
return style;
}

directives.clear();

for (r in rules) {
if (!r.match(c)) {
continue;
}

style.mergeDirectives(r.directives);
for (k in r.directives.keys()) {
var v = r.directives.get(k);
if (!directives.exists(k)) {
directives[k] = v;
selectedSelectors[k] = r.selector;
} else {
if (r.selector.hasPrecedenceOrEqualTo(selectedSelectors[k])) {
directives[k] = v;
selectedSelectors[k] = r.selector;
if (k == "background-color") {
directives["background-color-end"] = v;
selectedSelectors["background-color-end"] = r.selector;
}
}
}
}
}

style.mergeDirectives(directives);

return style;
}
}
29 changes: 29 additions & 0 deletions haxe/ui/styles/elements/Selector.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package haxe.ui.styles.elements;
class Selector {
public var parts:Array<SelectorPart> = [];

private var weightA = 0;
private var weightB = 0;
private var weightC = 0;

public function new(s:String) {
s = StringTools.replace(s, ">", " > ");
var p = s.split(" ");
Expand Down Expand Up @@ -45,6 +49,31 @@ class Selector {
parts.push(current);
parent = current;
}

// https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
// Combinators, such as +, >, ~, " ", and ||, may make a selector more specific in what is selected
// but they don't add any value to the specificity weight.
for ( p in parts) {
if (p.id != null) {
weightA++;
}
if (p.className != null) {
weightB++;
}
if (p.pseudoClass != null) {
weightC++;
}
}
}

public function hasPrecedenceOrEqualTo(s:Selector) {
if (weightA > s.weightA) return true;
if (weightA < s.weightA) return false;
if (weightB > s.weightB) return true;
if (weightB < s.weightB) return false;
if (weightC > s.weightC) return true;
if (weightC < s.weightC) return false;
return true;
}

public function toString():String {
Expand Down
8 changes: 5 additions & 3 deletions haxe/ui/themes/ThemeManager.hx
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,18 @@ class ThemeManager {
style += "\n" + styleData;
}
if (style != null) {
addStyleString(style);
var p = haxe.io.Path.directory(resourceId);
addStyleString(style, p); //haxe.io.Path.directory(resourceId));
//priority++;
} else {
#if debug
trace("WARNING: could not find " + resourceId);
#end
}
}

public function addStyleString(style:String) {
Toolkit.styleSheet.parse(style);
public function addStyleString(style:String, sheetName:String) {
Toolkit.styleSheet.parse(style, sheetName);
}

private function buildThemeVars(themeName:String, vars:Map<String, String>) {
Expand Down