void ReadStyleRule(XElement reader, IStylePropertyContext context, List <IStyleRule> rules, IStyleSelector parent = null, bool directChild = false) { var element = reader.AttributeLocal("element")?.Value; var selector = CreateSelector(reader, parent, directChild, element, context); var style = new PredefinedStyle(StyleSystem); var hasStyle = false; foreach (var propertyNode in reader.Elements().Where(pn => pn.Name.LocalName == "property")) { var p = ReadProperty(propertyNode, context); style.SetValue(p.Key, p.Value); hasStyle = true; } if (hasStyle) { var rule = new StyleRule(selector, style); rules.Add(rule); } foreach (var propertyNode in reader.Elements().Where(pn => pn.Name.LocalName == "style")) { var attr = propertyNode.AttributeLocal("direct-child"); ReadStyleRule(propertyNode, context, rules, selector, attr?.Value == "true"); } }
public BuilderTransitionData(StyleSheet styleSheet, StyleRule styleRule, VisualElement element, bool editorExtensionMode) { transitionProperty = styleSheet.GetStylePropertyManipulator(element, styleRule, StylePropertyId.TransitionProperty.UssName(), editorExtensionMode); transitionDuration = styleSheet.GetStylePropertyManipulator(element, styleRule, StylePropertyId.TransitionDuration.UssName(), editorExtensionMode); transitionTimingFunction = styleSheet.GetStylePropertyManipulator(element, styleRule, StylePropertyId.TransitionTimingFunction.UssName(), editorExtensionMode); transitionDelay = styleSheet.GetStylePropertyManipulator(element, styleRule, StylePropertyId.TransitionDelay.UssName(), editorExtensionMode); }
public static StyleProperty AddProperty( this StyleSheet styleSheet, StyleRule rule, string name, string undoMessage = null) { // Undo/Redo if (string.IsNullOrEmpty(undoMessage)) { undoMessage = "Change UI Style Value"; } Undo.RegisterCompleteObjectUndo(styleSheet, undoMessage); var newProperty = new StyleProperty { name = name }; // Create empty values array. newProperty.values = new StyleValueHandle[0]; // Add property to selector's rule's properties. var properties = rule.properties.ToList(); properties.Add(newProperty); rule.properties = properties.ToArray(); StyleSheetCache.ClearCaches(); return(newProperty); }
// StyleSheet Value Setters static public void SetStyleSheetRuleValue(StyleSheet styleSheet, StyleRule styleRule, string styleName, float value) { var styleProperty = GetOrCreateStylePropertyByStyleName(styleSheet, styleRule, styleName); var isNewValue = styleProperty.values.Length == 0; // If the current style property is saved as a float instead of a dimension, // it means it's a user file where they left out the unit. We need to resave // it here as a dimension to create final proper uss. if (!isNewValue && styleProperty.values[0].valueType != StyleValueType.Dimension) { styleProperty.values = new StyleValueHandle[0]; isNewValue = true; } var dimension = new Dimension(); dimension.unit = Dimension.Unit.Pixel; dimension.value = value; if (isNewValue) { styleSheet.AddValue(styleProperty, dimension); } else // TODO: Assume only one value. { styleSheet.SetValue(styleProperty.values[0], dimension); } }
/// <summary> /// Gets or sets background color. /// </summary> /// <param name="value">New value.</param> /// <returns>Value.</returns> private string QueryBackgroundColor(string value = null) { Property prop = null; StyleRule rule = null; string ret = string.Empty; rule = _stylesheet.Rules.OfType <StyleRule>() .Where(r => string.Compare(r.Selector.ToString(), string.Concat(".theme-", _id), true) == 0) .FirstOrDefault(); if (rule != null) { prop = rule.Declarations.Where(d => string.Compare(d.Name, "background-color", true) == 0) .FirstOrDefault(); if (prop != null) { ret = prop.Term.ToString(); } if (value != null) { ret = value; rule.Declarations.Add(NewProperty("background-color", value)); } } return(ret); }
public void AddComment(StyleRule rule, string comment) { if (!string.IsNullOrEmpty(comment)) { ruleComments.Add(rule, comment); } }
public List <RuleTreeNode <StyleData> > AddStyle(StyleRule rule, int importanceOffset = 0, bool mergeLayouts = false) { var added = AddSelector(rule.SelectorText, importanceOffset); var addedList = added.ToList(); foreach (var leaf in addedList) { var style = rule.Style; if (leaf.Data == null) { leaf.Data = new StyleData(); } var dic = RuleHelpers.GetRuleDic(style, false); leaf.Data.Rules.Add(dic); var lay = RuleHelpers.GetLayoutDic(style, false); if (lay != null) { if (mergeLayouts) { leaf.Data.Rules.Add(lay.ToDictionary(x => x.prop.name, x => x.value)); } else { leaf.Data.Layouts.AddRange(lay); } } var importantDic = RuleHelpers.GetRuleDic(style, true); var importantLay = RuleHelpers.GetLayoutDic(style, true); if (importantDic.Count > 0 || importantLay != null) { var importantLeaf = leaf.AddChildCascading("** !"); importantLeaf.Specifity = leaf.Specifity + RuleHelpers.ImportantSpecifity; if (importantLeaf.Data == null) { importantLeaf.Data = new StyleData(); } importantLeaf.Data.Rules.Add(importantDic); if (importantLay != null) { if (mergeLayouts) { leaf.Data.Rules.Add(importantLay.ToDictionary(x => x.prop.name, x => x.value)); } else { importantLeaf.Data.Layouts.AddRange(importantLay); } } added.Add(importantLeaf); LeafNodes.InsertIntoSortedList(importantLeaf); } } return(added); }
public void Remove(StyleRule o) { if (o == null) { return; } base.InnerList.Remove(o); }
public bool Contains(StyleRule o) { if (o == null) { return(false); } return(InnerList.Contains(o)); }
public void Add(StyleRule o) { if (o == null) { return; } base.InnerList.Add(o); }
public void ToStringTest() { var styleRule = new StyleRule(); var toString = styleRule.ToString(); Assert.IsNull(toString); }
private static void GetStyleRules(StyleSheet styleSheet, CssNode astRule) { var astStyleDeclarationBlock = astRule.Children .Single(x => x.Type == CssNodeType.StyleDeclarationBlock); var styleDeclarations = astStyleDeclarationBlock.Children .Where(x => x.Type == CssNodeType.StyleDeclaration) .Select(x => { var keyAst = x.Children .Single(y => y.Type == CssNodeType.Key); var valueAst = x.Children .Single(y => y.Type == CssNodeType.Value); return(new StyleDeclaration { Property = keyAst.Text, Value = valueAst.Text != "" ? valueAst.Text.Trim() : valueAst.Children .Select(y => y.Type == CssNodeType.VariableReference ? GetVariableValue(y) : y.Text) .Aggregate("", (a, b) => a + (a != "" ? " " : "") + b).Trim() }); }) .ToList(); var parentSelectorList = GetParentsSelectorAsts(astRule); var parentSelectors = (parentSelectorList?.Select(x => GetSelectorStringsFromSelectorsCssNode(x)) ?? new List <List <string> >()).ToList(); var currentLevelSelectors = astRule.Children .Single(x => x.Type == CssNodeType.Selectors); // add current level to parentlevels var allSelectorLayers = parentSelectors.Concat(new[] { GetSelectorStringsFromSelectorsCssNode(currentLevelSelectors) }) .ToList(); var allSelectorsToUse = GetAllRuleSelectors(allSelectorLayers); foreach (var ruleSelectorToUse in allSelectorsToUse) { var rule = new StyleRule(); rule.SelectorType = SelectorType.LogicalTree; rule.Selectors = new List <Selector>(new[] { new Selector() { Value = ruleSelectorToUse } }); rule.SelectorString = string.Join(",", rule.Selectors.Select(x => x.Value)); rule.DeclarationBlock.AddRange(styleDeclarations); styleSheet.LocalRules.Add(rule); } ResolveSubRules(styleSheet, astStyleDeclarationBlock); }
internal static StylePropertyId[] GetPropertyIds(StyleRule rule) { StylePropertyId[] array = new StylePropertyId[rule.properties.Length]; for (int i = 0; i < array.Length; i++) { array[i] = StyleSheetCache.GetPropertyId(rule, i); } return(array); }
public void BeginRule(int ruleLine) { StyleSheetBuilder.Log("Beginning rule"); this.m_BuilderState = StyleSheetBuilder.BuilderState.Rule; this.m_CurrentRule = new StyleRule { line = ruleLine }; }
// This is for UXML inline sheet public void SetInlineContext(StyleSheet sheet, StyleRule rule, int ruleIndex) { m_Sheet = sheet; m_Properties = rule.properties; m_PropertyIDs = StyleSheetCache.GetPropertyIDs(sheet, ruleIndex); specificity = StyleValueExtensions.InlineSpecificity; LoadProperties(); }
public void TryGet(StyleRule rule, Action <string> next) { string comment; if (ruleComments.TryGetValue(rule, out comment)) { next(comment); } }
public void EndRule() { StyleSheetBuilder.Log("Ending rule"); this.m_BuilderState = StyleSheetBuilder.BuilderState.Init; this.m_CurrentRule.properties = this.m_CurrentProperties.ToArray(); this.m_Rules.Add(this.m_CurrentRule); this.m_CurrentRule = null; this.m_CurrentProperties.Clear(); }
/// <summary> /// Gets or sets font family. /// </summary> /// <param name="value">New value.</param> /// <returns>Value.</returns> private string QueryFontFamily(string value = null) { int trimIndex = -1; Property prop = null; StyleRule rule = null; string ret = string.Empty; ImportRule fontImport = null; rule = _stylesheet.Rules.OfType <StyleRule>() .Where(r => string.Compare(r.Selector.ToString(), string.Concat(".theme-", _id), true) == 0) .FirstOrDefault(); if (rule != null) { prop = rule.Declarations.Where(d => string.Compare(d.Name, "font-family", true) == 0) .FirstOrDefault(); if (prop != null) { // Removing everyting except of the font name itself (assuming sans-serif). ret = value != null ? value : prop.Term.ToString(); trimIndex = ret.IndexOf('\''); if (trimIndex < 0) { trimIndex = ret.IndexOf(' '); } if (trimIndex > 0) { ret = ret.Substring(0, trimIndex).Trim('\'').Trim(); } if (value != null) { rule.Declarations.Add(NewProperty("font-family", string.Format("'{0}', sans-serif", value))); _stylesheet.Rules.OfType <ImportRule>().ToList().ForEach(ir => { if (ir.Href.ToLowerInvariant().IndexOf("fonts.googleapis.com") >= 0) { _stylesheet.Rules.Remove(ir); } }); fontImport = new ImportRule(); fontImport.RuleType = RuleType.Import; fontImport.Href = string.Format("http://fonts.googleapis.com/css?family={0}", HttpUtility.UrlEncode(value)); _stylesheet.Rules.Add(fontImport); } } } return(ret); }
public void BeginRule(int ruleLine) { Log("Beginning rule"); Debug.Assert(m_BuilderState == BuilderState.Init); m_BuilderState = BuilderState.Rule; m_CurrentRule = new StyleRule { line = ruleLine }; }
public static IEnumerable <string> GetAllSetStyleProperties(this StyleRule styleRule) { foreach (var property in styleRule.properties) { if (StylePropertyUtil.s_NameToId.ContainsKey(property.name)) { yield return(property.name); } } }
// This is for UXML inline sheet public void SetInlineContext(StyleSheet sheet, StyleRule rule, int ruleIndex) { m_Sheet = sheet; m_Rule = rule; m_PropertyIDs = StyleSheetCache.GetPropertyIDs(sheet, ruleIndex); m_CurrentPropertyIndex = 0; specificity = StyleValueExtensions.InlineSpecificity; SetCurrentProperty(); }
public string Get(StyleRule rule) { string comment; if (!ruleComments.TryGetValue(rule, out comment)) { comment = ""; } return(comment); }
public void AddRule(StyleRule rule) { if (_rules.ContainsKey(rule.Name)) { _rules[rule.Name] = rule; return; } _rules.Add(rule.Name, rule); }
static StylePropertyManipulator ResolveVariableFromRootSelectorInStyleSheet( VisualElement currentVisualElement, StyleSheet sheet, StyleRule styleRule, string variableName, bool editorExtensionMode) { for (var selectorIndex = sheet.complexSelectors.Length - 1; selectorIndex >= 0; --selectorIndex) { var complexSelector = sheet.complexSelectors[selectorIndex]; if (!complexSelector.isSimple) { continue; } var simpleSelector = complexSelector.selectors[0]; var selectorPart = simpleSelector.parts[0]; if (selectorPart.type != StyleSelectorType.Wildcard && selectorPart.type != StyleSelectorType.PseudoClass) { continue; } if (selectorPart.type == StyleSelectorType.PseudoClass && selectorPart.value != "root") { continue; } var rule = complexSelector.rule; for (var propertyIndex = rule.properties.Length - 1; propertyIndex >= 0; --propertyIndex) { var property = rule.properties[propertyIndex]; if (property.name != variableName) { continue; } var manipulator = GetPooled(); for (var i = 0; i < property.values.Length; ++i) { var index = i; var newPart = ResolveValueOrVariable(sheet, currentVisualElement, styleRule, property, ref i, editorExtensionMode); newPart.offset = index; newPart.isVariable = true; newPart.variableName = variableName; manipulator.stylePropertyParts.Add(newPart); } return(manipulator); } } return(null); }
public void SetContext(StyleSheet sheet, StyleComplexSelector selector, StyleVariableContext varContext) { m_Sheet = sheet; m_Rule = selector.rule; m_PropertyIDs = StyleSheetCache.GetPropertyIDs(sheet, selector.ruleIndex); m_Resolver.variableContext = varContext; m_CurrentPropertyIndex = 0; specificity = sheet.isUnityStyleSheet ? StyleValueExtensions.UnitySpecificity : selector.specificity; SetCurrentProperty(); }
internal static StylePropertyId[] GetPropertyIds(StyleRule rule) { var propertyIds = new StylePropertyId[rule.properties.Length]; for (int i = 0; i < propertyIds.Length; i++) { propertyIds[i] = GetPropertyId(rule, i); } return(propertyIds); }
public void ToStringWhenHueTest() { var styleRule = new StyleRule { Hue = "hue" }; var toString = styleRule.ToString(); Assert.AreEqual($"hue:{styleRule.Hue}", toString); }
public void ToStringWhenWeightTest() { var styleRule = new StyleRule { Weight = 1 }; var toString = styleRule.ToString(); Assert.AreEqual($"weight:{styleRule.Weight}", toString); }
public void ToStringWhenLightnessTest() { var styleRule = new StyleRule { Lightness = 10 }; var toString = styleRule.ToString(); Assert.AreEqual($"lightness:{styleRule.Lightness}", toString); }
public void ToStringWhenSaturationTest() { var styleRule = new StyleRule { Saturation = 10 }; var toString = styleRule.ToString(); Assert.AreEqual($"saturation:{styleRule.Saturation}", toString); }
public void StyleRule1() { var pieceStyle = new StyleRule("#piece_1") { { "max-width", "960px" } }; var sb = new StringBuilder(); using (var output = new StringWriter(sb)) { pieceStyle.WriteTo(output); } Assert.Equal("#piece_1 { max-width: 960px; }", sb.ToString()); }
private StyleRule ParseRule(BaseTree tree) { var rule = new StyleRule(); foreach (BaseTree child in tree.Children) { switch (child.Text) { case "PROPERTY": rule.Properties.Add(ParseProperty(child)); break; case "RULE": rule.Rules.Add(ParseRule(child)); break; case "SELECTOR": rule.Selectors.Add(ParseSelector(child)); break; case "MIXIN": rule.Mixins.Add(ParseSelector(child)); break; } } return rule; }
public CssRule ReadRuleBlock(CssSelector selector) { var rule = new StyleRule(selector); ReadBlock(rule); return rule; }
public StyleRule ReadStyleRule() { var rule = new StyleRule(ReadSelector()); ReadBlock(rule); return rule; }