Commit Graph

88 Commits

Author SHA1 Message Date
Ryan Nowak e4cf7a6d3e Fix #773 (for real this time)
The problem is that the new HTML rewrite pass was traversing into
attributes of all kinds and would turn any HTML content inside those
attributes into elements where possible. The solution is to not do
that.
2018-05-14 10:47:36 -07:00
Ryan Nowak 3142217bec Fix #745 - allow multiple implements directives
The pass for this was 'break'ing after the first directive for no real
reason. Oops.
2018-05-08 14:06:22 -07:00
Ryan Nowak e1c2efb5ce Add tests for #773
This doesn't seem to repro anymore after fixing #772. Adding tests
anyway.
2018-05-08 13:38:23 -07:00
Ryan Nowak 3f5d25d314 Fix 773
We weren't handling a few cases that can occur during typing correctly.
Our passes that look at the content of attributes need to be prepared
for it to be empty in cases where the attribute has been partially
typed in the editor.

I added a smoke test for this that attempts to simulate typing and found
another issue to fix.

The end result of this is that the design for this kind of code is
simpler and takes a more 'brute-force' approach to understanding
attributes. I think this is a good change based on the problems with how
this code has been written today, there are too many possible cases to
try and have the code express and document them all.
2018-05-08 12:39:18 -07:00
Steve Sanderson 37788f3c9d In Blazor cshtml, auto-reference Microsoft.AspNetCore.Blazor and .Components (#751)
* In Blazor cshtml files, auto-import Microsoft.AspNetCore.Blazor and Microsoft.AspNetCore.Blazor.Components. Fixes #749

* Remove redundant @using directives from tests

* Update assertion in test

* Update all affected baselines
2018-05-05 17:55:08 +01:00
Ryan Nowak b390ae0c1c Rewrite of HTML handling for Blazor
This change replaces the parsing of HTML that we perform during the code
generation phase, which parsing of HTML during the IR lowering phase.
The main benefit of this change is that the structure of the HTML is
reflected in the IR tree, allowing us to do more more advance
transformations.

As an example, see how the the handling of `<script>` tags is now a
separate pass.

As an aside from this I also redesigned the structure of component IR
nodes to match the new HTML element nodes. Passes are now more easily
aware of the nodes they are expected to handle and are more easily aware
of the difference between a component and element. This still isn't as
clean as I would like, but I think it's a reasonable improvement.

Another benefit of this is that the code generation is much simpler and
requires much less bookkeeping and statefulness.
2018-05-03 21:56:03 -07:00
Steve Sanderson f61ed4df4f For checkboxes, bind to 'checked'. Fix special property handling in BrowserRenderer.ts. Fixes #659 and #703
* Stop the value<-->checked conversions for checkboxes. Just use native 'checked' property. Fixes #703

* Properly handle removal of 'checked' and 'value' attributes

* E2E coverage for removing 'value'
2018-05-01 16:40:07 +01:00
Steve Sanderson 18b9a70dbe Encourage encapsulation of component parameter properties (#713)
* Before refactoring ParameterCollection assignment logic, add more test coverage

* Begin caching parameter assignment info

* Factor out some reflection code to a reusable location

* Use IPropertySetter to avoid all per-property-assignment reflection

* More error cases and tests for parameter assignment

* Enable binding to nonpublic properties

* Add analyzer to warn and provide fix for public component parameters

* Unit test for analyzer

* Component tag helper now includes private properties if they have [Parameter]

* CR feedback: Remove garbage from csproj

* CR feedback: Rename .Build.Analyzers to .Analyzers

* CR feedback: Move BlazorApi.cs to shared; use it from Analyzers test

* Fix incorrect test name

* Make as many parameters private as possible. Replace ILayoutComponent with BlazorLayoutComponent.

* In component tag helper discovery, consider private members too

* Reduce the work in component parameter discovery by not inspecting the BlazorComponent base class (or System.Object)
2018-05-01 10:08:01 +01:00
Daniel Roth ec1b220b7d Fixup project templates to handle project names with dashes (#692)
Fixes #291
2018-04-30 21:27:22 -07:00
Ryan Nowak f661021324 Add [Parameter] for component parameters
This change introduces ParameterAttribute to specify a bindable
component parameter. As of the 0.3 release of Blazor we plan to make
[Parameter] required to make a property bindable by callers.

This also applies to parameters when their value is set by the
infrastructure, such as `Body` for layouts, and route paramters.

The rationale behind this change is that we think there is a need to
separate the definition of properties from their suitability for a
caller to set them through markup. We plan to introduce more features in
this area in the future such as marking parameters as required. This is
first step, and we think that this approach will scale nicely as we add
more functionaly.

The 0.3 release seems like the right time to change this behavior since
we're also introducing `ref` for captures in this release.
2018-04-30 13:35:08 -07:00
Steve Sanderson 4033560734 Support 'ref' syntax for capturing references to elements and components (#685) 2018-04-27 17:41:21 +01:00
Ryan Nowak ed06d7b12e Rough cut at async events 2018-04-26 13:31:28 -07:00
Steve Sanderson 60dcc6e568 In Razor compilation, trim leading and trailing whitespace nodes (#586)
* In Razor compilation, trim leading and trailing whitespace

* Update all unit tests to account for whitespace trimming

* Recognize that TagHelperIntermediateNode produces output too

* Skip TrimWhitespacePass during first phase of two-phase compile

* Skip TrimWhitespacePass during design-time builds

* Update baselines after rebase
2018-04-24 21:44:30 +01:00
Ryan Nowak c152ed9e2a Update baseline linendings 2018-04-23 21:48:13 -07:00
Ryan Nowak f5dc6df13d Fix #597 Allow multiple tokens in attributes
The issue here is that we were missing tests for the design time code
path. We have tests that the bind-... cases work at runtime but were
missing coverage for the editor.

I took the most relevant set of the tests for running bind-... code and
added them to the tests for codegen.
2018-04-19 16:14:10 -04:00
Ryan Nowak 195bf63c6c Fix line endings on OSX
This change makes our baseline system use CRLF for line endings end to
end. This isn't needed for correctness, but it helps with quality of
life when developing on OSX.

This will avoid churn to the baseline files related to line endings when
updating.

This might only take effect if you nuke the TestFiles directory and
check it out again.
2018-04-18 15:04:35 -04:00
Steve Sanderson edf84cb80a Update link in compiler error message 2018-04-13 17:02:07 +01:00
Steve Sanderson ce3c79e051 Reject script tags in components, but allow overriding. Fixes #552 (#553) 2018-04-12 16:28:31 +01:00
Ryan Nowak 8485e2ea10 Add support for Action event handlers
This change adds `Action` to the set of types that have an overload on
RenderTreeBuilder. Additionally, we special case `Action` in the runtime
because passing the event args via DynamicInvoke() would throw.

Finally, reverted some of the clutter introduced by the first pass of
the event handler feature.
2018-04-11 07:36:05 -07:00
Steve Sanderson 2478c164b5 Update Mono to 078d5147836 (#543)
* Update Mono to 078d5147836

* Update driver.c to match updated Mono version

* Rebuild Mono binaries

* Update dependency resolution baseline to match updated Mono BCL

* Add E2E test for new RuntimeInformation values
2018-04-11 14:00:03 +01:00
Ryan Nowak 15ba614e6f Remove old workaround @onclick and @bind
This change removes support for the old syntax used for event handlers
and two-way binding.

See the relevant issues for details on the new features and
improvements:

bind https://github.com/aspnet/Blazor/issues/409
event handlers https://github.com/aspnet/Blazor/issues/503

Along with this change we've removed a few additional things Blazor
could do that aren't part of Razor's usual syntax.

----

The features that was used to make something like:
```
<button @onclick(...) />
```

is an expression that's embedded in a an element's attribute. This
feature might be useful in the future if we want to support 'splatting'
arbitrary attributes into a tag, but the runtime support for this isn't
accessible outside the Blazor core.

----

The features that implement:
```
<button onclick=@{ } />
```

have been removed in favor of a better design for lambdas, method group
conversions and other things for event handler attributes.

use `<button onclick=@(x => ...} />` instead.

We think is a better approach in general, because we want the app
developer to write and see the parameter list.

----

Both syntactic features that have been removed have dedicated error
messages in the compiler. If you're porting old code it should help you
figure out what to do.
2018-04-10 16:54:15 -07:00
Ryan Nowak df13669362 Improvements for delegate types (#516)
* Improve support for more types of event handlers

Improves support for for other types of event handlers with eventargs
types derived from UIEventArgs. Additionally fleshes out the set of
event handler types.

This change improves support for using more specific event handler types
like:

```
<button onclick="@Clicked" />

@functions {
    public void Clicked(UIMouseEventArgs e) { ... }
}
```

And:
```
builder.AddAttribute(12, "onkeypressed", KeyPressed);

...

void KeyPressed(UIKeyboardEventArgs e) { ... }

```

In particular what got better is:
- overload resolution for the AddAttribute method
- performance of different cases for AddAttribute

-----

The runtime now treats delegates as one of three types:
- arbitrary delegate: not attached to DOM events, not tracked by
renderer
- UIEventHandler: can attach to DOM events, tracked by renderer, first
class in IHandleEvents
- UIEventHandler-like: can attach to DOM events, tracked by renderer,
requires some special runtime support.

The set of overloads on AddAttribute has been tuned with a few specific
cases in mind.

Lambda expressions in an attribute will be inferred as UIEventHandler
unless the compiler does something more specific. So for instance,
passing a lambda as an attribute value for a component, where the
component doesn't define a matching attribute, will always be inferred
as UIEventHandler.

We now support method-group to delegate conversion for methods that
accept a derived UIEventArgs type. This means you can use a signature
like `void KeyPressed(UIKeyboardEventArgs e)` without any compiler
magic, and this will work in the runtime as long as the event type
produced by the runtime matches.

We also allow user-defined UIEventArgs-derived types. There's a pattern
for this and it requires defining an extension method and delegate type.

The method-group to delegate conversion part required some doing. It
doesn't play well with generics (Action<T> where T : UIEventArgs)
doesn't work at all. Adding more actual overloads (as opposed to
extensions) would cause lambda cases we want to work to be ambiguous.

----

The performance win here is to remove the need for a 'wrapper' delegate
created by the event handler tag helper code. This wrapper is now
created by the runtime, but only *after* we have checked the frame for
changes. This requires more heavy lifting in the runtime, but has the
advantage of producing no-op diffs as often as possible.

You will still get some inefficient behavior if your component uses a
capturing lambda in an event handler, so don't do that.

* Add selenium logs to test output

* Minor feedback

* WIP
2018-04-09 13:21:12 -07:00
Ryan Nowak c3366bc956 Add event handlers as tag helpers
This change adds support for mapping DOM event handlers as tag helpers
that function in a bi-modal way.

This is a new first-class feature for DOM events, and replaces a few
workarounds like using `@onclick(...)` or `click=@{ ... }`. I haven't
removed those things yet, this is a first pass to get the new support
in, we'll remove those things when we're totally satisfied.

When used with a string like `<button onclick="foo" />` the result is
a simple HTML attribute .

But when used with an implicit expression like
`<button onclick="@Foo" />` or
`<button onclick="@(x => Clicked = true)" />` a C# function is bound to
the click event from the DOM.
2018-04-06 13:00:04 -07:00
flash2048 3369208c28 Fix mistake in array index 2018-04-06 11:41:47 +01:00
Steve Sanderson fef5a52183 Reference static content from referenced assemblies. Implements #340 2018-04-06 09:50:10 +01:00
Steve Sanderson d3dc294d5e Revert live reloading implementation. See PR for explanation. 2018-04-04 18:10:12 +01:00
Ryan Nowak 707e781e5d Add Razor baseline test infrastructure
Ports somee infrastructure and converts Razor code generation tests to use
it. This makes it much easier to make cross cutting changes to code
generation and see the effect.

Use build /p:GenerateBaselines=true to update all of the generated code
in place or when adding new tests. Generally if tests are failing, the
easiest thing to do is to update the baselines and do a git diff to see
what the deltas are.

The changes to the tests here are to use the new baseline infrastructure
and to rename classes/methods to result in shorter file paths.
2018-04-04 08:05:59 -07:00
Ryan Nowak 0c942ccc76 Fix for #453
This fix adds missing line mappings for the Blazor runtime code
generation. We had the correct line mappings for design time code, but
they were missing in this case for runtime code - where they are used
for error messages and debugging (not yet supported).

Once this fix is is in the error window and output log will report the
file/line/column of the original source in .cshtml.

It looks like jumping to the code from the error window is currently not
working correctly in VS. It works from the output window.

I'm going to follow up on the VS issue in the Razor repo, since the fix
won't come from the Blazor side.
2018-04-03 15:34:41 -07:00
Ryan Nowak d097190824 Add support for conditional attributes
Adds conditional attributes for HTML elements.

This means that an attribute with a 'false' .NET bool value or a null
.NET value of another type will not be rendered in the HTML.
2018-04-03 14:06:48 -07:00
flash2048 ff5e6a78c3 Fix some tests with DateTime in German culture 2018-04-03 15:09:26 +01:00
Steve Sanderson 2c268a4eca Live reloading (currently enabled only for command-line builds - will
add VS support next)
2018-04-02 15:33:00 +01:00
Steve Sanderson b52912a460 Handle ternary expressions inside interpolated attributes. Fixes #446 2018-03-30 11:38:10 +01:00
Ryan Nowak 5b658c80a1 Replace @bind with bind-...
This change introduces a 'tag helper' that replaces @bind with custom
code generation that accomplishes roughly the same thing.

This feature lights up by dynamically generating tag helpers that are
visible to tooling and affect the code generation based on:
- pattern recognition of component properties
- attributes that create definitions for elements
- a 'fallback' case for elements

'bind' also supports format strings (currently only for DateTime) via
a separate attribute.

This change introduces the basic framework for bind and tooling support.
We know that we'll have to do more work to define the set of default
'bind' cases for the DOM and to flesh out the conversion/formatting
infrastructure.

This change gets us far enough to replace all of the cases we currently
have tests for :) with the new features. The old @bind technique still
works for now.

Examples:

@* bind an input element to an expression *@
<input bind="@SelectedDate" format="mm/dd/yyyy" />
@functions {
    public DateTime SelectedDate { get; set; }
}

@* bind an arbitrary expression to an arbitrary set of attributes *@
<div bind-myvalue-myevent="@SomeExpression">...</div>

@* write a component that supports bind *@

@* in Counter.cshtml *@
<div>...html omitted for brevity...</div>
@functions {
    public int Value { get; set; } = 1;
    public Action<int> ValueChanged { get; set; }
}

@* in another file *@
<Counter bind-Value="@CurrentValue" />
@functions {
    public int CurrentValue { get; set; }
}
2018-03-29 22:04:24 -07:00
flash2048 b53d39e16f Corrects spelling of some comments, method name and remove an excess flag 2018-03-29 14:20:04 +01:00
Ryan Nowak 808f741cdd Fix for #314 - streamline lambda component args
This change removes the magic 'auto-lambda' feature that has some
unconvincing UX.

Also working around a razor bug where explicit expressions are lowered
incorrectly. This should make it possible to write code like:

<Foo Bar="@(e => { OnChanged(e); })" />
2018-03-21 13:59:35 -07:00
Steve Sanderson cf237707f9 Log info message if IL linking is disabled. Fixes #302 2018-03-20 22:28:43 +00:00
Steve Sanderson c58df0b739 Temporary workaround for #219 ("data-" attributes don't compile) 2018-03-19 18:09:53 +00:00
Steve Sanderson 6b73892e78 Avoid culture assumption in SupportsTwoWayBindingForDateValuesWithFormatString test. Fixes #268 2018-03-15 13:25:52 +00:00
Steve Sanderson 0e9d52fe11 Support components and static content in external NuGet packages (#247)
* On build, drop <BlazorPackageContentFiles> items into dist\_content\(PackageName)\

* Add <script> and <link> tags to generated index.html

* Add testapp coverage of external content package. Still need to add E2E tests that uses it.

* Add missing unit test update

* Add example of packaging an entire Blazor component including CSS and images

* Add E2E test for component from NuGet package
2018-03-14 11:23:43 +00:00
Ryan Nowak 9549dccc54 Add @page directive
Adds the @page directive and support for specifying routes in components
at compile time.

For now the route is required and must begin with a leading /.
2018-03-14 11:23:43 +00:00
Javier Calvarro Nelson 700c2203c6 [Fixes #66] Adds linking support to the build
* Switch the tasks used to generate the blazor output to be MSBuild based.
* Package the optimized mono runtime and the BCL inside a nuget package.
* Add opt-in support for linking the application on build.
* Make the whole build process incremental.
2018-03-14 11:23:43 +00:00
Ryan Nowak ffadeecb45 Remove old <c:Foo> syntax 2018-03-14 11:23:42 +00:00
Ryan Nowak d175b4d38a Konvert to KoreBuild 2018-03-14 11:23:42 +00:00
Ryan Nowak 0c162e8c5a Allow non-property attributes to be set
This removes a limitation that prevented callers from passing
attributes to a component that aren't backed by properties.

The majority of the complication here is required to deal with the more
sophisticated way that HTML attributes are represented in the Razor IR.
2018-03-14 11:23:42 +00:00
Ryan Nowak c05657c7f2 Add support general delegate types
This builds upon existing support for UIEventHandler-typed component
properties and applies the same principle to any delegate type.

We try to help by generating the LSH of the lambda `=>` allowing you to
write `OnClick="Foo()"` rather than `OnClick="(e) => Foo()"`. You can of
course use @ as an escape.

The only rough edge here is that if the parameter names aren't memorable
for the delgate type, it's not super helpful.
2018-03-14 11:23:42 +00:00
Ryan Nowak 601e7914f7 Implement components as tag helpers
Implements Component code generation and tooling support end to end
udditionally adds some default `@addTagHelper` directives to make
programming in Blazor a little nicer.

Components are discovered as Tag Helpers using Razor's extensibility
during the build/IDE process. This drives the code generation during
build and lights up a bunch of editor features.

Add
2018-03-14 11:23:41 +00:00
Ryan Nowak 211561a6a6 Implement 2-phase compilation
This is a working (all tests passing) implementation of the two-phase
compilation system we will need for component discovery.

This builds on top of work we've doing in Razor, including the Razor
SDK, MSBuild tasks, and CLI/server.

This currently *does* discovery components during the build process, but
it doesn't use that data for anything yet.

It works like this:
1. Generate class declarations (structure only, no method bodies)
2. Compile a 'temp' assembly using the .cs files and output of 1.
3. Do component discovery using the 'temp' assembly
4. Generate class definitions (including method bodies)
5. Compile the 'real' assembly using the .cs files and output of 4.
2018-03-14 11:23:40 +00:00
Ryan Nowak 6182e8448d Get rid of RazorCompiler
Adds a little more use of Razor extensibility.

Razor is a plugin model, so we can't be the 'first mover' for initiating
compilation in the build tools and IDE.

Reorganizes tests and fills out more reusable test infrastructure for
Razor-driven testing.

Adds tests for declaration-only configuration.
2018-03-14 11:23:40 +00:00
Ryan Nowak a053155ab4 Implement layout and implements with directives
- Remove haxxxx
- Add proper directives with tooling support
2018-03-14 11:23:40 +00:00
Steve Sanderson 61e07eb615 Support @bind for enum and DateTime values (with optional format string) 2018-03-05 02:02:26 +00:00