public TargetExtension(VisitorContext context, ElementNode targetElement, int targetExtensionCount) { _context = context; _targetElement = targetElement; _targetExtensionCount = targetExtensionCount; _idAttribute = _targetElement.Attributes.SingleOrDefault(attr => attr.Name == "id"); _classAttribute = _targetElement.Attributes.SingleOrDefault(attr => attr.Name == "class"); _descriptionAttribute = _targetElement.Attributes.SingleOrDefault(attr => attr.Name == "description"); _targetAttribute = _targetElement.Attributes.SingleOrDefault(attr => attr.Name == "target"); }
internal static AttributeNode AddMethodCallingToAttributeValue(AttributeNode node, string method) { var snippets = new Snippets {new Snippet {Value = method + "("}}; snippets.AddRange(node.AsCodeInverted()); snippets.Add(new Snippet {Value= ")"}); var builder = new ExpressionBuilder(); builder.AppendExpression(snippets); var listNodes=new List<Node> {new ExpressionNode(snippets)}; return new AttributeNode(node.Name, listNodes); }
protected override bool IsSpecialAttribute(ElementNode element, AttributeNode attribute) { var eltName = NameUtility.GetName(element.Name); if (eltName == "for") return false; if (Context.Namespaces == NamespacesType.Unqualified) return attribute.Name == "each"; if (attribute.Namespace != Constants.Namespace) return false; return NameUtility.GetName(attribute.Name) == "each"; }
protected override bool IsSpecialAttribute(ElementNode element, AttributeNode attr) { var eltName = NameUtility.GetName(element.Name); if (eltName == "test" || eltName == "if" || eltName == "elseif" || eltName == "else") return false; if (Context.Namespaces == NamespacesType.Unqualified) return attr.Name == "once"; if (attr.Namespace != Constants.Namespace) return false; var nqName = NameUtility.GetName(attr.Name); return nqName == "once"; }
protected override SpecialNode CreateWrappingNode(AttributeNode attr, ElementNode node) { var attrKey = ExtractFakeAttribute(node, "cache", "key") ?? ExtractFakeAttribute(node, "cache.key", "key"); var attrExpires = ExtractFakeAttribute(node, "cache.expires", "expires"); var attrSignal = ExtractFakeAttribute(node, "cache.signal", "signal"); var attrNodes = new[] { attrKey, attrExpires, attrSignal }.Where(x => x != null).ToList(); var fakeElement = new ElementNode("cache", attrNodes, false) { OriginalNode = attr }; return new SpecialNode(fakeElement); }
private void AddCheckedAttribute(ElementNode elementNode, AttributeNode valueNode) { if (valueNode != null) { string entity = valueNode.Value.Split('.').First(); string code = string.Format("({0}!= null)&&({1})", entity, valueNode.Value); ConditionNode conditionNode = new ConditionNode(code) { Nodes = new List<Node>() { new TextNode("true") } }; elementNode.Attributes.Add(new AttributeNode("checked", new List<Node>{conditionNode} )); } }
Snippets AsTextOrientedCode(AttributeNode attr) { return Context.AttributeBehaviour == AttributeBehaviour.CodeOriented ? AsCode(attr) : attr.AsCodeInverted(); }
protected abstract void Visit(AttributeNode attributeNode);
protected override void Visit(AttributeNode node) { Nodes.Add(node); }
private List<Node> SetNodeNameAndValue(ElementNode elementNode, Node valueNode, Node nameNode, IList<Node> body, AttributeNode originalForAttrib) { List<Node> result = new List<Node>(); if(elementNode.IsTag("textarea")) { if (valueNode != null) { elementNode.IsEmptyElement = false; body.Add(valueNode); } } else { string inputType = elementNode.GetAttributeValue("type"); switch (inputType.ToUpper()) { case "CHECKBOX": elementNode.Attributes.Add(new AttributeNode("value", "true")); // DODGY sorry will figure something decent out soon AddCheckedAttribute(elementNode, originalForAttrib); break; default: if (valueNode != null) elementNode.Attributes.Add(new AttributeNode("value", new List<Node>() { valueNode })); break; } } elementNode.Attributes.Add(new AttributeNode("name", new List<Node>() { nameNode })); return result; }
private void ThenTheElementShouldNotContainAttribute(AttributeNode node) { Context.Target.Unwrap().As<ElementNode>().Attributes.ShouldNotContain(x=>x.Name == node.Name); }
public static ElementNode WithAttribute(this ElementNode elementnode, AttributeNode attributeNode) { elementnode.Attributes.Add(attributeNode); return elementnode; }
private void GivenAnAttribute(AttributeNode node) { Context.Target = new SparkAttributeWrapper(node); }
Snippets AsCode(AttributeNode attr) { var begin = Locate(attr.Nodes.FirstOrDefault()); var end = LocateEnd(attr.Nodes.LastOrDefault()); if (begin == null || end == null) { begin = new Position(new SourceContext(attr.Value)); end = begin.Advance(begin.PotentialLength()); } return Context.SyntaxProvider.ParseFragment(begin, end); }
private string AttrName(AttributeNode attr) { return Context.Namespaces == NamespacesType.Qualified ? NameUtility.GetName(attr.Name) : attr.Name; }
protected override bool IsSpecialAttribute(ElementNode element, AttributeNode attr) { var name = AttrName(attr); return name == "cache" || name == "cache.key" || name == "cache.expires" || name == "cache.signal"; }
protected override SpecialNode CreateWrappingNode(AttributeNode attr, ElementNode node) { var fakeAttribute = new AttributeNode("condition", attr.Nodes); var fakeElement = new ElementNode(NameUtility.GetName(attr.Name), new[] { fakeAttribute }, false) { OriginalNode = attr }; return new SpecialNode(fakeElement); }
protected override void Visit(AttributeNode attributeNode) { Add(attributeNode); }
public void VisitNode(INodeVisitor visitor, IList<Node> body, IList<Chunk> chunks) { var registerTarget = string.Format( @"RegisterTarget(""{0}"", ""{1}"", ""{2}"", __target_{3});", _idAttribute.Value, _classAttribute != null ? _classAttribute.Value : "", _descriptionAttribute != null ? _descriptionAttribute.Value : "", _targetExtensionCount); if (_targetAttribute != null) { registerTarget += Environment.NewLine + string.Format( @"RegisterTarget(""{0}"", ""{1}"", null, null);", _targetAttribute.Value, _idAttribute.Value); } var beginLambda = string.Format( @"__target_{0} = () => {{", _targetExtensionCount); const string endLambda = "};"; var startingTarget = string.Format( @"StartingTarget(""{0}"");", _idAttribute.Value); var nameAttribute = new AttributeNode("name", _idAttribute.QuotChar, _idAttribute.Nodes) { OriginalNode = _idAttribute }; var macroAttributes = _targetElement.Attributes .Where(x => x != _idAttribute && x != _classAttribute && x != _descriptionAttribute) .Concat(new[] { nameAttribute }) .ToList(); var macroElement = new SpecialNode(new ElementNode("macro", macroAttributes, false)); var onceAttribute = new AttributeNode("once", _idAttribute.QuotChar, _idAttribute.Nodes); var testElement = new SpecialNode(new ElementNode("test", new[] { onceAttribute }, false)); macroElement.Body.Add(testElement); testElement.Body = body; testElement.Body.Insert(0, new StatementNode(startingTarget)); visitor.Accept(new StatementNode(beginLambda)); visitor.Accept(testElement); visitor.Accept(new StatementNode(endLambda)); visitor.Accept(new StatementNode(registerTarget)); }
protected override void Visit(AttributeNode attributeNode) { var accumulatedNodes = new List<Node>(); var processedNodes = new List<Node>(); foreach (var node in attributeNode.Nodes) { if (node is ConditionNode) { // condition nodes take the prior unconditional nodes as content var conditionNode = (ConditionNode)node; MovePriorNodesUnderCondition(conditionNode, accumulatedNodes); // prior nodes and condition are set for output processedNodes.AddRange(accumulatedNodes); processedNodes.Add(conditionNode); accumulatedNodes.Clear(); } else { // other types add to the unconditional list accumulatedNodes.Add(node); } } processedNodes.AddRange(accumulatedNodes); var allNodesAreConditional = processedNodes.All(node => node is ConditionNode); if (allNodesAreConditional == false || processedNodes.Any() == false) { // This attribute may not disapper - send it literally AddLiteral(" " + attributeNode.Name + "=\""); foreach (var node in processedNodes) Accept(node); AddLiteral("\""); } else { var scope = new ScopeChunk(); scope.Body.Add(new LocalVariableChunk { Name = "__just__once__", Value = new Snippets("0") }); _sendAttributeOnce = new ConditionalChunk { Type = ConditionalType.If, Condition = new Snippets("__just__once__ < 1") }; _sendAttributeOnce.Body.Add(new SendLiteralChunk { Text = " " + attributeNode.Name + "=\"" }); _sendAttributeIncrement = new AssignVariableChunk { Name = "__just__once__", Value = "1" }; Chunks.Add(scope); using (new Frame(this, scope.Body)) { foreach (var node in processedNodes) Accept(node); } _sendAttributeOnce = null; _sendAttributeIncrement = null; var ifWasSent = new ConditionalChunk { Type = ConditionalType.If, Condition = new Snippets("__just__once__ > 0") }; scope.Body.Add(ifWasSent); ifWasSent.Body.Add(new SendLiteralChunk { Text = "\"" }); } }
private void Process(AttributeNode attribute) { var first = attribute.Nodes.FirstOrDefault() as TextNode; if (first == null || !first.Text.StartsWith("~/")) return; var expr = "SiteResource(\"" + first.Text + "\")"; attribute.Nodes[0] = new ExpressionNode(expr) { OriginalNode = first }; }
protected override SpecialNode CreateWrappingNode(AttributeNode attr, ElementNode node) { var fakeAttribute = new AttributeNode("once", attr.Nodes); var fakeElement = new ElementNode("test", new[] { fakeAttribute }, false) { OriginalNode = attr }; return new SpecialNode(fakeElement); }
private string GenerateFormId() { _lastFormNumKey++; string id=string.Format(CultureInfo.InvariantCulture, "form_{0}", new object[] { _lastFormNumKey }); var idNode=new AttributeNode("id",id); _mNode.Attributes.Add(idNode); return id; }