示例#1
0
        public void CreateCodeMapping(string padding, string code, Chunk chunk)
        {
            using (CSharpLineMappingWriter mappingWriter = Writer.BuildLineMapping(chunk.Start, code.Length, Context.SourceFile))
            {
                Writer.Write(padding);

                mappingWriter.MarkLineMappingStart();
                Writer.Write(code);
                mappingWriter.MarkLineMappingEnd();
            }
        }
        protected override void Visit(SetBaseTypeChunk chunk)
        {
            if (Context.Host.DesignTimeMode)
            {
                using (CSharpLineMappingWriter lineMappingWriter = Writer.BuildLineMapping(chunk.Start, chunk.TypeName.Length, Context.SourceFile))
                {
                    Writer.Indent(chunk.Start.CharacterIndex);

                    lineMappingWriter.MarkLineMappingStart();
                    Writer.Write(chunk.TypeName);
                    lineMappingWriter.MarkLineMappingEnd();

                    Writer.Write(" ").Write(InheritsHelper).Write(" = null;");
                }
            }
        }
示例#3
0
        private void RenderBoundHTMLAttributes(IDictionary <string, Chunk> chunkAttributes,
                                               string tagHelperVariableName,
                                               IEnumerable <TagHelperAttributeDescriptor> attributeDescriptors,
                                               Dictionary <string, string> htmlAttributeValues)
        {
            foreach (var attributeDescriptor in attributeDescriptors)
            {
                Chunk attributeValueChunk;

                var providedAttribute = chunkAttributes.TryGetValue(attributeDescriptor.Name,
                                                                    out attributeValueChunk);

                if (providedAttribute)
                {
                    var attributeValueRecorded = htmlAttributeValues.ContainsKey(attributeDescriptor.Name);

                    // Bufferable attributes are attributes that can have Razor code inside of them.
                    var bufferableAttribute = IsStringAttribute(attributeDescriptor);

                    // Plain text values are non Razor code (@DateTime.Now) values. If an attribute is bufferable it
                    // may be more than just a plain text value, it may also contain Razor code which is why we attempt
                    // to retrieve a plain text value here.
                    string textValue;
                    var    isPlainTextValue = TryGetPlainTextValue(attributeValueChunk, out textValue);

                    // If we haven't recorded a value and we need to buffer an attribute value and the value is not
                    // plain text then we need to prepare the value prior to setting it below.
                    if (!attributeValueRecorded && bufferableAttribute && !isPlainTextValue)
                    {
                        BuildBufferedWritingScope(attributeValueChunk);
                    }

                    // We capture the tag helpers property value accessor so we can retrieve it later (if we need to).
                    var valueAccessor = string.Format(CultureInfo.InvariantCulture,
                                                      "{0}.{1}",
                                                      tagHelperVariableName,
                                                      attributeDescriptor.PropertyName);

                    // If we haven't recorded this attribute value before then we need to record its value.
                    if (!attributeValueRecorded)
                    {
                        // We only need to create attribute values once per HTML element (not once per tag helper).
                        // We're saving the value accessor so we can retrieve it later if there are more tag
                        // helpers that need the value.
                        htmlAttributeValues.Add(attributeDescriptor.Name, valueAccessor);

                        if (bufferableAttribute)
                        {
                            _writer.WriteStartAssignment(valueAccessor);

                            if (isPlainTextValue)
                            {
                                // If the attribute is bufferable but has a plain text value that means the value
                                // is a string which needs to be surrounded in quotes.
                                RenderQuotedAttributeValue(textValue, attributeDescriptor);
                            }
                            else
                            {
                                // The value contains more than plain text e.g.
                                // stringAttribute ="Time: @DateTime.Now"
                                RenderBufferedAttributeValue(attributeDescriptor);
                            }

                            _writer.WriteLine(";");
                        }
                        else
                        {
                            // Write out simple assignment for non-string property value. Try to keep the whole
                            // statement together and the #line pragma correct to make debugging possible.
                            using (var lineMapper = new CSharpLineMappingWriter(
                                       _writer,
                                       attributeValueChunk.Association.Start,
                                       _context.SourceFile))
                            {
                                // Place the assignment LHS to align RHS with original attribute value's indentation.
                                // Unfortunately originalIndent is incorrect if original line contains tabs. Unable to
                                // use a CSharpPaddingBuilder because the Association has no Previous node; lost the
                                // original Span sequence when the parse tree was rewritten.
                                var originalIndent  = attributeValueChunk.Start.CharacterIndex;
                                var generatedLength = valueAccessor.Length + " = ".Length;
                                var newIndent       = originalIndent - generatedLength;
                                if (newIndent > 0)
                                {
                                    _writer.Indent(newIndent);
                                }

                                _writer.WriteStartAssignment(valueAccessor);
                                lineMapper.MarkLineMappingStart();

                                // Write out bare expression for this attribute value. Property is not a string.
                                // So quoting or buffering are not helpful.
                                RenderRawAttributeValue(attributeValueChunk, attributeDescriptor, isPlainTextValue);

                                // End the assignment to the attribute.
                                lineMapper.MarkLineMappingEnd();
                                _writer.WriteLine(";");
                            }
                        }

                        // Execution contexts are a runtime feature.
                        if (_designTimeMode)
                        {
                            continue;
                        }

                        // We need to inform the context of the attribute value.
                        _writer
                        .WriteStartInstanceMethodInvocation(
                            ExecutionContextVariableName,
                            _tagHelperContext.ExecutionContextAddTagHelperAttributeMethodName)
                        .WriteStringLiteral(attributeDescriptor.Name)
                        .WriteParameterSeparator()
                        .Write(valueAccessor)
                        .WriteEndMethodInvocation();
                    }
                    else
                    {
                        // The attribute value has already been recorded, lets retrieve it from the stored value
                        // accessors.
                        _writer
                        .WriteStartAssignment(valueAccessor)
                        .Write(htmlAttributeValues[attributeDescriptor.Name])
                        .WriteLine(";");
                    }
                }
            }
        }