/// <summary> /// Handle the related attribute types which exist upon the element exposed by the given context, if any. /// </summary> /// <returns>A response type providing information about the result of this operation.</returns> /// <param name="context">The rendering context, which exposes a ZPT element.</param> public AttributeHandlingResult Handle(IRenderingContext context) { if(context == null) { throw new ArgumentNullException(nameof(context)); } AttributeHandlingResult output; var attrib = context.GetTalAttribute(ZptConstants.Tal.OmitTagAttribute); if(attrib != null) { if(attrib.Value.Length == 0) { var children = context.Element.Omit(); output = new AttributeHandlingResult(new RenderingContext[0], false, children.Select(x => context.CreateSiblingContext(x)).ToArray()); } else { // Normal handling by detecting an attribute and using its value var result = context.TalModel.Evaluate(attrib.Value, context); if(!result.CancelsAction && result.GetValueAsBoolean()) { var children = context.Element.Omit(); output = new AttributeHandlingResult(new RenderingContext[0], false, children.Select(x => context.CreateSiblingContext(x)).ToArray()); } else { output = new AttributeHandlingResult(new [] { context }, true); } } } else { output = new AttributeHandlingResult(new [] { context }, true); } return output; }
/// <summary> /// Replaces the defined macro on the source context with the macro context and returns the result. /// </summary> /// <returns>The rendering context for the replacement.</returns> /// <param name="sourceContext">The source context (which uses a macro).</param> /// <param name="macroContext">The macro context (which defines the macro).</param> public virtual IRenderingContext ReplaceMacroElement(IRenderingContext sourceContext, IRenderingContext macroContext) { var replacedSourceElement = sourceContext.Element.ReplaceWith(macroContext.Element); return sourceContext.CreateSiblingContext(replacedSourceElement); }
/// <summary> /// Gets a collection of <see cref="SlotToFill"/> from the given source information. /// </summary> /// <remarks> /// <para> /// This operation constructs and executes a query across the slots and fillers provided, which matches each filler /// to a slot. /// </para> /// </remarks> /// <returns>A collection of the slots matched with fillers.</returns> /// <param name="sourceContext">The source rendering context.</param> /// <param name="availableFillers">A collection of elements in the source context representing slot fillers.</param> /// <param name="macroContext">The macro rendering context.</param> /// <param name="availableSlotDefinitions">A collection of elements in the macro context representing slot definitions.</param> public virtual IEnumerable<SlotToFill> GetSlotsToFill(IRenderingContext sourceContext, IDictionary<string,IZptElement> availableFillers, IRenderingContext macroContext, IDictionary<string,IZptElement> availableSlotDefinitions) { return (from slotElement in availableSlotDefinitions join fillerElement in availableFillers on slotElement.Key equals fillerElement.Key let slot = sourceContext.CreateSiblingContext(slotElement.Value) let filler = macroContext.CreateSiblingContext(fillerElement.Value) select new SlotToFill(slot, filler, slotElement.Key)) .ToArray(); }
/// <summary> /// Extends a given macro and then makes substitutions into the source context. /// </summary> /// <returns>The resultant rendering context after the substitutions are performed.</returns> /// <param name="context">The rendering context.</param> /// <param name="macro">The macro element to extend and use for substitutions.</param> /// <param name="macroStack">The collection of macros passed through to get to this point.</param> /// <param name="substitutionStrategy">The macro substitution strategy to use.</param> public virtual IRenderingContext ExtendAndSubstitute(IRenderingContext context, IZptElement macro, ref IList<IRenderingContext> macroStack, IMacroSubstituter substitutionStrategy) { if(context == null) { throw new ArgumentNullException(nameof(context)); } if(macro == null) { throw new ArgumentNullException(nameof(macro)); } if(macroStack == null) { throw new ArgumentNullException(nameof(macroStack)); } if(substitutionStrategy == null) { throw new ArgumentNullException(nameof(substitutionStrategy)); } var macroContext = context.CreateSiblingContext(macro); macroStack.Add(macroContext); var extendedContext = GetFullyExtendedContext(macroContext, ref macroStack); return substitutionStrategy.MakeSubstitutions(context, extendedContext, macroStack); }
/// <summary> /// Handles the collection of repetitions, adding the data to the model and inserting new elements, as appropriate. /// </summary> /// <returns>The collection of new rendering contexts which represent new elements added to the DOM.</returns> /// <param name="repetitions">The repetitions.</param> /// <param name="context">The source rendering context.</param> private IRenderingContext[] HandleRepetitions(IRepetitionInfo[] repetitions, IRenderingContext context) { var output = repetitions .Select(x => new { Context = context.CreateSiblingContext(x.AssociatedElement), Repetition = x }) .ToList(); var parent = context.Element.GetParentElement(); foreach(var item in output) { item.Context.TalModel.AddRepetitionInfo(item.Repetition); item.Repetition.AssociatedElement.RemoveAttribute(ZptConstants.Tal.Namespace, ZptConstants.Tal.RepeatAttribute); parent.InsertBefore(context.Element, item.Repetition.AssociatedElement); } context.Element.Remove(); return output .Select(x => x.Context) .ToArray(); }
/// <summary> /// Handle the related attribute types which exist upon the element exposed by the given context, if any. /// </summary> /// <returns>A response type providing information about the result of this operation.</returns> /// <param name="context">The rendering context, which exposes a ZPT element.</param> public AttributeHandlingResult Handle(IRenderingContext context) { if(context == null) { throw new ArgumentNullException(nameof(context)); } AttributeHandlingResult output; string attribName; var attrib = this.GetAttribute(context, out attribName); if(attrib != null) { string mode; var expressionResult = this.GetAttributeResult(attrib, context, out mode); if(expressionResult.CancelsAction && attribName == ZptConstants.Tal.ContentAttribute) { output = new AttributeHandlingResult(new [] { context }, true); } else if(expressionResult.CancelsAction && attribName == ZptConstants.Tal.ReplaceAttribute) { var children = context.Element.Omit(); output = new AttributeHandlingResult(new RenderingContext[0], false, children.Select(x => context.CreateSiblingContext(x)).ToArray()); } else if(expressionResult.Value == null) { if(attribName == ZptConstants.Tal.ContentAttribute) { context.Element.RemoveAllChildren(); output = new AttributeHandlingResult(new [] { context }, true); } else { context.Element.Remove(); output = new AttributeHandlingResult(new RenderingContext[0], false); } } else { if(attribName == ZptConstants.Tal.ContentAttribute) { var resultValue = expressionResult.GetValue<object>(); context.Element.ReplaceChildrenWith((resultValue?? String.Empty).ToString(), mode == STRUCTURE_INDICATOR); output = new AttributeHandlingResult(new [] { context }, true); } else { var resultValue = expressionResult.GetValue<object>(); var elements = context.Element.ReplaceWith((resultValue?? String.Empty).ToString(), mode == STRUCTURE_INDICATOR); output = new AttributeHandlingResult(elements.Select(x => context.CreateSiblingContext(x, true)).ToArray(), true); } } } else { output = new AttributeHandlingResult(new [] { context }, true); } return output; }