Example #1
0
 public virtual void WriteAttribute(string name,
                                    PositionTagged <string> prefix,
                                    PositionTagged <string> suffix,
                                    params AttributeValue[] values)
 {
     WriteAttributeTo(Output, name, prefix, suffix, values);
 }
Example #2
0
        public virtual void WriteAttribute(
            string name,
            PositionTagged <string> prefix,
            PositionTagged <string> suffix,
            params AttributeValue[] values)
        {
            if (prefix == null)
            {
                throw new ArgumentNullException(nameof(prefix));
            }

            if (suffix == null)
            {
                throw new ArgumentNullException(nameof(suffix));
            }

            WriteAttributeTo(Output, name, prefix, suffix, values);
        }
Example #3
0
 private void WritePositionTaggedLiteral(TextWriter writer, PositionTagged <string> value)
 {
     WritePositionTaggedLiteral(writer, value.Value, value.Position);
 }
Example #4
0
        public virtual void WriteAttributeTo(TextWriter writer,
                                             string name,
                                             PositionTagged <string> prefix,
                                             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;
                    var    boolValue = val.Value as bool?;
                    if (boolValue == true)
                    {
                        stringValue = name;
                    }
                    else if (boolValue == false)
                    {
                        continue;
                    }
                    else
                    {
                        stringValue = val.Value as string;
                    }

                    if (first)
                    {
                        WritePositionTaggedLiteral(writer, prefix);
                        first = false;
                    }
                    else
                    {
                        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;

                    if (attrVal.Literal)
                    {
                        WriteLiteralTo(writer, stringValue ?? val.Value);
                    }
                    else
                    {
                        WriteTo(writer, stringValue ?? val.Value); // Write value
                    }
                    wroteSomething = true;
                }
                if (wroteSomething)
                {
                    WritePositionTaggedLiteral(writer, suffix);
                }
            }
        }
Example #5
0
        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;
                    }
                    else
                    {
                        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);
                }
            }
        }
Example #6
0
 public AttributeValue(PositionTagged <string> prefix, PositionTagged <object> value, bool literal)
 {
     Prefix  = prefix;
     Value   = value;
     Literal = literal;
 }
Example #7
0
        public virtual void WriteAttributeTo(
            [NotNull] TextWriter writer,
            string name,
            [NotNull] PositionTagged <string> prefix,
            [NotNull] PositionTagged <string> suffix,
            params AttributeValue[] values)
        {
            if (values.Length == 0)
            {
                // Explicitly empty attribute, so write the prefix
                WritePositionTaggedLiteral(writer, prefix);
            }
            else if (IsSingleBoolFalseOrNullValue(values))
            {
                // Value is either null or the bool 'false' with no prefix; don't render the attribute.
                return;
            }
            else if (UseAttributeNameAsValue(values))
            {
                var attributeValue = values[0];
                var positionTaggedAttributeValue = attributeValue.Value;

                WritePositionTaggedLiteral(writer, prefix);

                var sourceLength       = suffix.Position - positionTaggedAttributeValue.Position;
                var nameAttributeValue = new AttributeValue(
                    attributeValue.Prefix,
                    new PositionTagged <object>(name, attributeValue.Value.Position),
                    literal: attributeValue.Literal);

                // The value is just the bool 'true', write the attribute name instead of the string 'True'.
                WriteAttributeValue(writer, nameAttributeValue, sourceLength);
            }
            else
            {
                // This block handles two cases.
                // 1. Single value with prefix.
                // 2. Multiple values with or without prefix.
                WritePositionTaggedLiteral(writer, prefix);
                for (var i = 0; i < values.Length; i++)
                {
                    var attributeValue = values[i];
                    var positionTaggedAttributeValue = attributeValue.Value;

                    if (positionTaggedAttributeValue.Value == null)
                    {
                        // Nothing to write
                        continue;
                    }

                    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

                    // Calculate length of the source span by the position of the next value (or suffix)
                    var sourceLength = next.Position - attributeValue.Value.Position;

                    WriteAttributeValue(writer, attributeValue, sourceLength);
                }
            }

            WritePositionTaggedLiteral(writer, suffix);
        }