From 54eda39cb8991dada269166a76387bfc3294dfb8 Mon Sep 17 00:00:00 2001 From: "Nikolei, Stefan" Date: Thu, 24 May 2018 15:01:53 +0200 Subject: [PATCH] Update Anglesharp --- Blazor.sln | 14 --- build/repo.targets | 10 +- .../Core/IndexHtmlWriter.cs | 26 ++-- .../Microsoft.AspNetCore.Blazor.Build.csproj | 7 +- .../targets/RazorCompilation.targets | 8 +- .../ComponentDocumentRewritePass.cs | 43 ++++--- ....AspNetCore.Blazor.Razor.Extensions.csproj | 11 +- src/anglesharp/AngleSharpBuilder/.gitignore | 1 - .../AngleSharpBuilder.csproj | 23 ---- src/anglesharp/AngleSharpBuilder/Program.cs | 117 ------------------ ...rosoft.AspNetCore.Blazor.Build.Test.csproj | 3 +- ...rosoft.VisualStudio.BlazorExtension.csproj | 6 +- .../Properties/AssemblyInfo.cs | 15 ++- .../source.extension.vsixmanifest | 2 +- 14 files changed, 60 insertions(+), 226 deletions(-) delete mode 100644 src/anglesharp/AngleSharpBuilder/.gitignore delete mode 100644 src/anglesharp/AngleSharpBuilder/AngleSharpBuilder.csproj delete mode 100644 src/anglesharp/AngleSharpBuilder/Program.cs diff --git a/Blazor.sln b/Blazor.sln index 68b8a2f64f..19a1655711 100644 --- a/Blazor.sln +++ b/Blazor.sln @@ -50,10 +50,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "testapps", "testapps", "{4A EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BasicTestApp", "test\testapps\BasicTestApp\BasicTestApp.csproj", "{2838CB6F-D2C7-4C0A-A994-C72E56F16984}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "anglesharp", "anglesharp", "{4E3BD19A-6159-4548-A88F-700443E6D934}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AngleSharpBuilder", "src\anglesharp\AngleSharpBuilder\AngleSharpBuilder.csproj", "{36706AC2-C851-4038-B161-9C1E44B668C8}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Blazor.Razor.Extensions", "src\Microsoft.AspNetCore.Blazor.Razor.Extensions\Microsoft.AspNetCore.Blazor.Razor.Extensions.csproj", "{D652A019-B765-4922-B7B8-3AB1C58338D7}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Blazor.Browser.Test", "test\Microsoft.AspNetCore.Blazor.Browser.Test\Microsoft.AspNetCore.Blazor.Browser.Test.csproj", "{EC2A38BF-6E77-4A8E-A731-15929544F29C}" @@ -227,14 +223,6 @@ Global {2838CB6F-D2C7-4C0A-A994-C72E56F16984}.Release|Any CPU.Build.0 = Release|Any CPU {2838CB6F-D2C7-4C0A-A994-C72E56F16984}.ReleaseNoVSIX|Any CPU.ActiveCfg = Release|Any CPU {2838CB6F-D2C7-4C0A-A994-C72E56F16984}.ReleaseNoVSIX|Any CPU.Build.0 = Release|Any CPU - {36706AC2-C851-4038-B161-9C1E44B668C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {36706AC2-C851-4038-B161-9C1E44B668C8}.Debug|Any CPU.Build.0 = Debug|Any CPU - {36706AC2-C851-4038-B161-9C1E44B668C8}.DebugNoVSIX|Any CPU.ActiveCfg = Debug|Any CPU - {36706AC2-C851-4038-B161-9C1E44B668C8}.DebugNoVSIX|Any CPU.Build.0 = Debug|Any CPU - {36706AC2-C851-4038-B161-9C1E44B668C8}.Release|Any CPU.ActiveCfg = Release|Any CPU - {36706AC2-C851-4038-B161-9C1E44B668C8}.Release|Any CPU.Build.0 = Release|Any CPU - {36706AC2-C851-4038-B161-9C1E44B668C8}.ReleaseNoVSIX|Any CPU.ActiveCfg = Release|Any CPU - {36706AC2-C851-4038-B161-9C1E44B668C8}.ReleaseNoVSIX|Any CPU.Build.0 = Release|Any CPU {D652A019-B765-4922-B7B8-3AB1C58338D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {D652A019-B765-4922-B7B8-3AB1C58338D7}.Debug|Any CPU.Build.0 = Debug|Any CPU {D652A019-B765-4922-B7B8-3AB1C58338D7}.DebugNoVSIX|Any CPU.ActiveCfg = Debug|Any CPU @@ -374,8 +362,6 @@ Global {8FD8636E-AFA5-434D-8857-06D02686741A} = {ADA3AE29-F6DE-49F6-8C7C-B321508CAE8E} {4AE0D35B-D97A-44D0-8392-C9240377DCCE} = {ADA3AE29-F6DE-49F6-8C7C-B321508CAE8E} {2838CB6F-D2C7-4C0A-A994-C72E56F16984} = {4AE0D35B-D97A-44D0-8392-C9240377DCCE} - {4E3BD19A-6159-4548-A88F-700443E6D934} = {B867E038-B3CE-43E3-9292-61568C46CDEB} - {36706AC2-C851-4038-B161-9C1E44B668C8} = {4E3BD19A-6159-4548-A88F-700443E6D934} {D652A019-B765-4922-B7B8-3AB1C58338D7} = {B867E038-B3CE-43E3-9292-61568C46CDEB} {EC2A38BF-6E77-4A8E-A731-15929544F29C} = {ADA3AE29-F6DE-49F6-8C7C-B321508CAE8E} {29CD3FC6-49E3-4756-B5DF-E03B46E5CD45} = {4AE0D35B-D97A-44D0-8392-C9240377DCCE} diff --git a/build/repo.targets b/build/repo.targets index e6a41f7b0a..9577314e63 100644 --- a/build/repo.targets +++ b/build/repo.targets @@ -5,13 +5,5 @@ Configuration=$(Configuration)NoVSIX - - - - - - - + diff --git a/src/Microsoft.AspNetCore.Blazor.Build/Core/IndexHtmlWriter.cs b/src/Microsoft.AspNetCore.Blazor.Build/Core/IndexHtmlWriter.cs index b401e6ce60..e20eee54a0 100644 --- a/src/Microsoft.AspNetCore.Blazor.Build/Core/IndexHtmlWriter.cs +++ b/src/Microsoft.AspNetCore.Blazor.Build/Core/IndexHtmlWriter.cs @@ -1,15 +1,16 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using AngleSharp; +using AngleSharp.Extensions; +using AngleSharp.Html; +using AngleSharp.Parser.Html; +using Mono.Cecil; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; -using AngleSharp; -using AngleSharp.Html; -using AngleSharp.Parser.Html; -using Mono.Cecil; namespace Microsoft.AspNetCore.Blazor.Build { @@ -109,15 +110,14 @@ namespace Microsoft.AspNetCore.Blazor.Build // Search for a tag of the form , and replace // it with a fully-configured Blazor boot script tag - var tokenizer = new HtmlTokenizer( - new TextSource(htmlTemplate), - HtmlEntityService.Resolver); + var textSource = new TextSource(htmlTemplate); var currentRangeStartPos = 0; var isInBlazorBootTag = false; var resumeOnNextToken = false; - while (true) + var result = string.Empty; + + foreach (var token in textSource.Tokenize(HtmlEntityService.Resolver)) { - var token = tokenizer.Get(); var tokenCharIndex = token.Position.Position - 1; if (resumeOnNextToken) { @@ -181,9 +181,13 @@ namespace Microsoft.AspNetCore.Blazor.Build { resultBuilder.Append(htmlTemplate, currentRangeStartPos, remainingLength); } - return resultBuilder.ToString(); + result = resultBuilder.ToString(); + break; } } + + + return result; } private static void AppendReferenceTags(StringBuilder resultBuilder, IEnumerable urls, string format) diff --git a/src/Microsoft.AspNetCore.Blazor.Build/Microsoft.AspNetCore.Blazor.Build.csproj b/src/Microsoft.AspNetCore.Blazor.Build/Microsoft.AspNetCore.Blazor.Build.csproj index 0a2a52bd3d..ee810d9981 100644 --- a/src/Microsoft.AspNetCore.Blazor.Build/Microsoft.AspNetCore.Blazor.Build.csproj +++ b/src/Microsoft.AspNetCore.Blazor.Build/Microsoft.AspNetCore.Blazor.Build.csproj @@ -45,7 +45,7 @@ $(RepositoryUrl) $(PackageArtifactCategory) $(PackageSigningCertName) - true + true false true @@ -67,6 +67,7 @@ + @@ -77,8 +78,4 @@ - - - - diff --git a/src/Microsoft.AspNetCore.Blazor.Build/targets/RazorCompilation.targets b/src/Microsoft.AspNetCore.Blazor.Build/targets/RazorCompilation.targets index 8113f28477..ac330d09a0 100644 --- a/src/Microsoft.AspNetCore.Blazor.Build/targets/RazorCompilation.targets +++ b/src/Microsoft.AspNetCore.Blazor.Build/targets/RazorCompilation.targets @@ -1,4 +1,4 @@ - + @@ -18,7 +18,7 @@ - <_BlazorAngleSharpAssemblyPath>$(MSBuildThisFileDirectory)../tools/Microsoft.AspNetCore.Blazor.AngleSharp.dll + <_AngleSharpAssemblyPath>$(MSBuildThisFileDirectory)../tools/AngleSharp.dll <_BlazorExtensionAssemblyPath>$(MSBuildThisFileDirectory)../tools/Microsoft.AspNetCore.Blazor.Razor.Extensions.dll @@ -55,8 +55,8 @@ - Microsoft.AspNetCore.Blazor.AngleSharp - $(_BlazorAngleSharpAssemblyPath) + AngleSharp + $(_AngleSharpAssemblyPath) Microsoft.AspNetCore.Blazor.Razor.Extensions diff --git a/src/Microsoft.AspNetCore.Blazor.Razor.Extensions/ComponentDocumentRewritePass.cs b/src/Microsoft.AspNetCore.Blazor.Razor.Extensions/ComponentDocumentRewritePass.cs index 86025cfc15..fd40cf241f 100644 --- a/src/Microsoft.AspNetCore.Blazor.Razor.Extensions/ComponentDocumentRewritePass.cs +++ b/src/Microsoft.AspNetCore.Blazor.Razor.Extensions/ComponentDocumentRewritePass.cs @@ -1,16 +1,17 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using AngleSharp; +using AngleSharp.Extensions; +using AngleSharp.Html; +using AngleSharp.Parser.Html; +using Microsoft.AspNetCore.Razor.Language; +using Microsoft.AspNetCore.Razor.Language.Intermediate; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; -using AngleSharp; -using AngleSharp.Html; -using AngleSharp.Parser.Html; -using Microsoft.AspNetCore.Razor.Language; -using Microsoft.AspNetCore.Razor.Language.Intermediate; namespace Microsoft.AspNetCore.Blazor.Razor { @@ -80,7 +81,7 @@ namespace Microsoft.AspNetCore.Blazor.Razor RewriteChildren(_source, node); } } - + public override void VisitHtmlAttribute(HtmlAttributeIntermediateNode node) { // Don't rewrite inside of attributes @@ -123,19 +124,23 @@ namespace Microsoft.AspNetCore.Blazor.Razor // the attributes from the parsed HTML and the CSharpAttribute value. var parser = new HtmlParser(source); var attributes = new List(); - + for (var i = 0; i < children.Length; i++) { if (children[i] is HtmlContentIntermediateNode htmlNode) { parser.Push(htmlNode); - - while (true) + var tokens = parser.Get(); + foreach (var token in tokens) { // We have to call this before get. Anglesharp doesn't return the start position // of tokens. var start = parser.GetCurrentLocation(); - var token = parser.Get(); + + // We have to set the Location explicitly otherwise we would need to include + // the token in every call to the parser. + parser.SetLocation(token); + var end = parser.GetCurrentLocation(); if (token.Type == HtmlTokenType.EndOfFile) @@ -349,7 +354,7 @@ namespace Microsoft.AspNetCore.Blazor.Razor // Tracks the offsets between the start of _content and then original source document. private List<(int offset, int sourceOffset)> _offsets; - private HtmlTokenizer _tokenizer; + private TextSource _textSource; private int _position; private string _content; @@ -382,7 +387,7 @@ namespace Microsoft.AspNetCore.Blazor.Razor _content = builder.ToString(); _offsets = offsets; - _tokenizer = new HtmlTokenizer(new TextSource(_content), HtmlEntityService.Resolver); + _textSource = new TextSource(_content); _position = 0; } @@ -391,23 +396,25 @@ namespace Microsoft.AspNetCore.Blazor.Razor return _position >= _content.Length ? string.Empty : _content.Substring(_position); } - public HtmlToken Get() + public IEnumerable Get() { - if (_tokenizer == null) + if (_textSource == null) { throw new InvalidOperationException("You need to call Push first."); } - var token = _tokenizer.Get(); + var tokens = _textSource.Tokenize(HtmlEntityService.Resolver); + return tokens; + } + public void SetLocation(HtmlToken token) + { // The tokenizer will advance to the end when you have an unclosed tag. // We don't want this, we want to resume before the unclosed tag. if (token.Type != HtmlTokenType.EndOfFile) { - _position = _tokenizer.Position; + _position = _textSource.Index; } - - return token; } public SourceLocation GetCurrentLocation(int offset = 0) diff --git a/src/Microsoft.AspNetCore.Blazor.Razor.Extensions/Microsoft.AspNetCore.Blazor.Razor.Extensions.csproj b/src/Microsoft.AspNetCore.Blazor.Razor.Extensions/Microsoft.AspNetCore.Blazor.Razor.Extensions.csproj index 37cb51d9ad..787baad869 100644 --- a/src/Microsoft.AspNetCore.Blazor.Razor.Extensions/Microsoft.AspNetCore.Blazor.Razor.Extensions.csproj +++ b/src/Microsoft.AspNetCore.Blazor.Razor.Extensions/Microsoft.AspNetCore.Blazor.Razor.Extensions.csproj @@ -10,20 +10,11 @@ + - - - AngleSharpBuilder - False - TargetFramework=netcoreapp2.0 - false - - - - True diff --git a/src/anglesharp/AngleSharpBuilder/.gitignore b/src/anglesharp/AngleSharpBuilder/.gitignore deleted file mode 100644 index 849ddff3b7..0000000000 --- a/src/anglesharp/AngleSharpBuilder/.gitignore +++ /dev/null @@ -1 +0,0 @@ -dist/ diff --git a/src/anglesharp/AngleSharpBuilder/AngleSharpBuilder.csproj b/src/anglesharp/AngleSharpBuilder/AngleSharpBuilder.csproj deleted file mode 100644 index 43ee734717..0000000000 --- a/src/anglesharp/AngleSharpBuilder/AngleSharpBuilder.csproj +++ /dev/null @@ -1,23 +0,0 @@ - - - - netcoreapp2.0 - Exe - false - false - - - - - - - - - - - - diff --git a/src/anglesharp/AngleSharpBuilder/Program.cs b/src/anglesharp/AngleSharpBuilder/Program.cs deleted file mode 100644 index 474d7a2237..0000000000 --- a/src/anglesharp/AngleSharpBuilder/Program.cs +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using AngleSharp.Parser.Html; -using Mono.Cecil; -using System; -using System.IO; -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Threading; - -namespace AngleSharpBuilder -{ - /* - * AngleSharp's HtmlTokenizer is perfect for the RazorCompiler use case of splitting up - * incomplete HTML strings into partial elements, attributes, etc. Unfortunately, - * AngleSharp does not expose HtmlTokenizer publicly. - * - * For now, we work around this by building a custom version of AngleSharp.dll that - * specifies [InternalsVisibleTo("Microsoft.AspNetCore.Blazor.Build")]. Longer term we can ask - * AngleSharp to expose HtmlTokenizer as a public API, and if that's not viable, possibly - * replace AngleSharp with a different library for HTML tokenization. - * - * Similarly, we have build-process reasons for needing the assembly not to be strong - * named and be called Microsoft.AspNetCore.Blazor.AngleSharp. These requirements will - * not be permanent but it enables progress in the short term. - */ - - public static class Program - { - public static void Main() - { - // Prevent concurrent execution of this program. Since this is called by MSBuild - // make sure to wait for other instances to exit, we need the file to exist when - // this program exits. - using (var mutex = new Mutex(true, "AngleSharpBuilder", out var created)) - { - if (created) - { - try - { - var outputDir = Path.Combine(Directory.GetCurrentDirectory(), "dist"); - var inputAssembly = Assembly.GetAssembly(typeof(HtmlParser)); - WriteModifiedAssembly(inputAssembly, outputDir); - } - finally - { - mutex.ReleaseMutex(); - } - } - else - { - Console.WriteLine("Waiting for AngleSharpBuilder to exit..."); - mutex.WaitOne(TimeSpan.FromSeconds(10)); - } - } - } - - private static void WriteModifiedAssembly(Assembly assembly, string outputDir) - { - Directory.CreateDirectory(outputDir); - - var assemblyLocation = assembly.Location; - var moduleDefinition = ModuleDefinition.ReadModule(assemblyLocation); - - AddInternalsVisibleTo(moduleDefinition, "Microsoft.AspNetCore.Blazor.Build"); - AddInternalsVisibleTo(moduleDefinition, "Microsoft.AspNetCore.Blazor.Razor.Extensions"); - RemoveStrongName(moduleDefinition); - SetAssemblyName(moduleDefinition, "Microsoft.AspNetCore.Blazor.AngleSharp"); - - // Try to minimize the chance of a parallel build reading the assembly in an - // incomplete state by writing it to a temp location then moving the result. - // There's still a race condition here, but hopefully this is enough to stop - // it from surfacing in practice. - - var tempFilePath = Path.GetTempFileName(); - moduleDefinition.Write(tempFilePath); - - var outputPath = Path.Combine(outputDir, $"{moduleDefinition.Name}.dll"); - try - { - File.Delete(outputPath); - File.Move(tempFilePath, outputPath); - } - finally - { - File.Delete(tempFilePath); // In case it didn't get moved above - } - } - - private static void SetAssemblyName(ModuleDefinition moduleDefinition, string name) - { - moduleDefinition.Name = name; - moduleDefinition.Assembly.Name.Name = name; - } - - private static void RemoveStrongName(ModuleDefinition moduleDefinition) - { - var assemblyName = moduleDefinition.Assembly.Name; - assemblyName.HasPublicKey = false; - assemblyName.PublicKey = new byte[0]; - moduleDefinition.Attributes &= ~ModuleAttributes.StrongNameSigned; - } - - private static void AddInternalsVisibleTo(ModuleDefinition moduleDefinition, string internalVisibleToArg) - { - var internalsVisibleToCtor = moduleDefinition.ImportReference( - typeof(InternalsVisibleToAttribute).GetConstructor(new[] { typeof(string) })); - - var customAttribute = new CustomAttribute(internalsVisibleToCtor); - customAttribute.ConstructorArguments.Add( - new CustomAttributeArgument(moduleDefinition.TypeSystem.String, internalVisibleToArg)); - - moduleDefinition.Assembly.CustomAttributes.Add(customAttribute); - } - } -} diff --git a/test/Microsoft.AspNetCore.Blazor.Build.Test/Microsoft.AspNetCore.Blazor.Build.Test.csproj b/test/Microsoft.AspNetCore.Blazor.Build.Test/Microsoft.AspNetCore.Blazor.Build.Test.csproj index dd64e86671..031cc5f4b5 100644 --- a/test/Microsoft.AspNetCore.Blazor.Build.Test/Microsoft.AspNetCore.Blazor.Build.Test.csproj +++ b/test/Microsoft.AspNetCore.Blazor.Build.Test/Microsoft.AspNetCore.Blazor.Build.Test.csproj @@ -19,6 +19,7 @@ + @@ -40,9 +41,7 @@ - - diff --git a/tooling/Microsoft.VisualStudio.BlazorExtension/Microsoft.VisualStudio.BlazorExtension.csproj b/tooling/Microsoft.VisualStudio.BlazorExtension/Microsoft.VisualStudio.BlazorExtension.csproj index 6e847b0236..a2e1397374 100644 --- a/tooling/Microsoft.VisualStudio.BlazorExtension/Microsoft.VisualStudio.BlazorExtension.csproj +++ b/tooling/Microsoft.VisualStudio.BlazorExtension/Microsoft.VisualStudio.BlazorExtension.csproj @@ -1,4 +1,4 @@ - + 15.0 @@ -156,8 +156,8 @@ This is built as a P2P by Microsoft.VisualStudio.LanguageServices.Blazor. This is required, adding the P2P to this project will cause a NU1201 error that doesn't have a workaround. --> - - Microsoft.AspNetCore.Blazor.AngleSharp.dll + + AngleSharp.dll true PreserveNewest false diff --git a/tooling/Microsoft.VisualStudio.BlazorExtension/Properties/AssemblyInfo.cs b/tooling/Microsoft.VisualStudio.BlazorExtension/Properties/AssemblyInfo.cs index 62755d1c9b..f71bc39e4b 100644 --- a/tooling/Microsoft.VisualStudio.BlazorExtension/Properties/AssemblyInfo.cs +++ b/tooling/Microsoft.VisualStudio.BlazorExtension/Properties/AssemblyInfo.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.VisualStudio.Shell; @@ -8,13 +8,6 @@ using Microsoft.VisualStudio.Shell; // // The versions here need to match what the build is producing. If you change the version numbers // for the Blazor assemblies, this needs to change as well. -[assembly: ProvideBindingRedirection( - AssemblyName = "Microsoft.AspNetCore.Blazor.AngleSharp", - GenerateCodeBase = true, - PublicKeyToken = "", - OldVersionLowerBound = "0.0.0.0", - OldVersionUpperBound = "0.9.9.0", - NewVersion = "0.9.9.0")] [assembly: ProvideBindingRedirection( AssemblyName = "Microsoft.AspNetCore.Blazor.Razor.Extensions", GenerateCodeBase = true, @@ -29,3 +22,9 @@ using Microsoft.VisualStudio.Shell; OldVersionLowerBound = "0.0.0.0", OldVersionUpperBound = "0.4.0.0", NewVersion = "0.4.0.0")] +[assembly: ProvideBindingRedirection( + AssemblyName = "AngleSharp", + PublicKeyToken = "", + OldVersionLowerBound = "0.0.0.0", + OldVersionUpperBound = "0.9.9.0", + NewVersion = "0.9.9.0")] \ No newline at end of file diff --git a/tooling/Microsoft.VisualStudio.BlazorExtension/source.extension.vsixmanifest b/tooling/Microsoft.VisualStudio.BlazorExtension/source.extension.vsixmanifest index c05cc01b1d..405418df2e 100644 --- a/tooling/Microsoft.VisualStudio.BlazorExtension/source.extension.vsixmanifest +++ b/tooling/Microsoft.VisualStudio.BlazorExtension/source.extension.vsixmanifest @@ -20,7 +20,7 @@ - +