예제 #1
0
        internal static void Accept(ISelectable selectable, StylesheetStatement statement)
        {
            if (selectable == null)
            {
                return;
            }

            switch (statement)
            {
            case AssignmentStatement assignment:
                if (selectable is IStyleable styleable)
                {
                    styleable.Set(assignment.Key, assignment.Value);
                }

                break;

            case StyleDeclaration style:
                SelectorHelper.Visit(selectable, style.Selector, (current, discard1, discard2) =>
                {
                    foreach (var localStatement in style.Statements)
                    {
                        Accept(current, localStatement);
                    }
                });

                break;

            default:
                throw new NotSupportedException();
            }
        }
예제 #2
0
        private static string Serialize(string id, StylesheetStatement statement)
        {
            switch (statement)
            {
            case AssignmentStatement assignment:
                return($"__set__(__root__, {JsonConvert.SerializeObject(assignment.Key)}, {JsonConvert.SerializeObject(assignment.Value)});");

            case ScriptDeclaration scriptDeclaration:
                return(scriptDeclaration.Script);

            case StyleDeclaration style:
                if (!style.ContainsScripts())
                {
                    return($"__rule__(__root__, '{id}');");
                }

                var predicates = new List <string>();
                var num        = 0;

                string Replace(ScriptSelector script)
                {
                    predicates.Add($@"function () {{ return !!({script.Expression}); }}");
                    return($"<{{{num++}}}>");
                }

                return($@"__visit__(__root__, {JsonConvert.SerializeObject(style.Selector.Serialize(Replace))}, function (__root__) {{
{string.Join("\n", style.Statements.Select((s, i) => Serialize(id + "." + i, s)))}
}}, [{string.Join(", ", predicates)}]);");

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
예제 #3
0
            public void VisitRule(object root, string id)
            {
                var parts = id.Split('.').Select(int.Parse).ToArray();
                StylesheetStatement current = stylesheet.Declarations[parts[0]];

                for (var i = 1; i < parts.Length; i++)
                {
                    current = ((StyleDeclaration)current).Statements[parts[i]];
                }

                SimpleStylesheet.Accept(asSelectable(root), current);
            }