public virtual void WriteAttribute( string name, [NotNull] PositionTagged <string> prefix, [NotNull] PositionTagged <string> suffix, params AttributeValue[] values) { WriteAttributeTo(Output, name, prefix, suffix, values); }
public AttributeValue(PositionTagged <string> prefix, PositionTagged <object> value, bool literal) { Prefix = prefix; Value = value; Literal = literal; }
private void WritePositionTaggedLiteral(TextWriter writer, PositionTagged <string> value) { WritePositionTaggedLiteral(writer, value.Value, value.Position); }
public virtual void WriteAttributeTo( [NotNull] TextWriter writer, string name, [NotNull] PositionTagged <string> prefix, [NotNull] PositionTagged <string> suffix, params AttributeValue[] values) { var first = true; var wroteSomething = false; if (values.Length == 0) { // Explicitly empty attribute, so write the prefix and suffix WritePositionTaggedLiteral(writer, prefix); WritePositionTaggedLiteral(writer, suffix); } else { for (var i = 0; i < values.Length; i++) { var attrVal = values[i]; var val = attrVal.Value; var next = i == values.Length - 1 ? suffix : // End of the list, grab the suffix values[i + 1].Prefix; // Still in the list, grab the next prefix if (val.Value == null) { // Nothing to write continue; } // The special cases here are that the value we're writing might already be a string, or that the // value might be a bool. If the value is the bool 'true' we want to write the attribute name // instead of the string 'true'. If the value is the bool 'false' we don't want to write anything. // Otherwise the value is another object (perhaps an HtmlString) and we'll ask it to format itself. string stringValue; // Intentionally using is+cast here for performance reasons. This is more performant than as+bool? // because of boxing. if (val.Value is bool) { if ((bool)val.Value) { stringValue = name; } else { continue; } } else { stringValue = val.Value as string; } if (first) { WritePositionTaggedLiteral(writer, prefix); first = false; } if (!string.IsNullOrEmpty(attrVal.Prefix)) { WritePositionTaggedLiteral(writer, attrVal.Prefix); } // Calculate length of the source span by the position of the next value (or suffix) var sourceLength = next.Position - attrVal.Value.Position; BeginContext(attrVal.Value.Position, sourceLength, isLiteral: attrVal.Literal); // The extra branching here is to ensure that we call the Write*To(string) overload where // possible. if (attrVal.Literal && stringValue != null) { WriteLiteralTo(writer, stringValue); } else if (attrVal.Literal) { WriteLiteralTo(writer, val.Value); } else if (stringValue != null) { WriteTo(writer, stringValue); } else { WriteTo(writer, val.Value); } EndContext(); wroteSomething = true; } if (wroteSomething) { WritePositionTaggedLiteral(writer, suffix); } } }