Searching for Patterns in HTML using ReSharper 6
In ReSharper 5, we added a feature called Structural Search and Replace (SSR). You could think of it as a cross between Regular Expressions and Templating, with the added benefit that you don’t need to know RegEx to use it. The basic idea behind it is to allow you to extend ReSharper’s code analysis by defining custom patterns, and optionally providing quick-fixes (for replacing these with other patterns).
In ReSharper 6.1, we extended SSR with support for other languages, specifically HTML and ASPX markup.
Let’s take a look at how this can help us with web applications we work on.
Searching for specific tags
Often you want to search for tags in your markup, for instance a div that is using a specific class. With ReSharper, we could search for the usages of the CSS class and filter it out, or as in the case we’re covering, use SSR. To do so:
1. Click on ReSharper | Find | Search with Pattern.
2. Select HTML from the drop-down list and enter the following pattern below:
and click Find. If any match is find, it will be displayed in the Find Results window:
Looking at the results, we can see that it not only finds patterns that match the exact class=”corner” pattern, but also anything that uses the corner class. The reason for that is that we have the option Match similar constructs selected. Deactivating that would result in displaying only exact pattern matches:
It is important to know that with the previous search, a div tag of the format <div class=”corner” id”=”submenu”> would appear in the results. That is because we have Ignore unmatched elements selected. If we’d like the pattern to include tags that have other attributes not explicitly defined, we need to check this option.
Using CSS selectors to search for patterns
If you’re doing any kind of web development, you’ve no doubt used or at least heard of CSS selectors, which probably many learned to master when they worked with jQuery (including myself). Well that same power of CSS selectors is also available in ReSharper for searching for tags.
1. Enter the following pattern in the Find dialog
The values enclosed in $ signs are placeholders required for Structural Search and Replace, and the next step is to define them.
2. Instead of manually setting placeholder properties, we can use the newer feature of ReSharper that allows us to extract them from the pattern:
3. We now have to define the different meanings of each placeholder. This is where the CSS selector comes into play. For the tag attribute, we’re going to define it as a tag with a selector:
The attribute placeholder can also have certain conditions such as matching a regular expression or indicating the number of attributes we want to have as a minimum and maximum:
In our case, we’re going to leave the defaults. Once complete, we can click Find once again to get the results:
Of course, now that we have CSS selectors, we can take it further. For instance, to search for all p inside a div that has a class corner we can do:
Any CSS selector expression is valid.
Apart from tags and attributes, we can also have attribute value and content placeholders, and in the case of the latter, we can also use CSS selectors to define the contents.
More to come
In this first part on HTML patterns, we’ve seen how to search for patterns in HTML using both markup patterns as well as CSS selectors. However, it doesn’t stop there. We can perform replacements as well as save patterns so as to offer custom code analysis and quick-fixes. However, as the post is already long enough, we’ll save that for the second part.
Subscribe to Blog updates
Thanks, we've got you!
Eager, Lazy and Explicit Loading with Entity Framework Core
Entity Framework Core (EF Core) supports a number of ways to load related data. There’s eager loading, lazy loading, and explicit loading. Each of these approaches have their own advantages and drawbacks. In this post, let’s have a quick look at each of these ways to load data for navigational prope…
OSS Power-Ups: bUnit – Webinar Recording
The recording of our webinar, OSS Power-Ups: bUnit, with Egil Hansen and Steven Giesel, is available. This was the twelfth episode of our OSS Power-Ups series, where we put a spotlight on open-source .NET projects. Subscribe to our community newsletter to receive notifications about future webi…
Accelerating Your Testing Workflow with Unit Test Creation and Navigation
Unit tests play an important role in our daily development workflow. They help us ensure our codebase's correctness when writing new functionality or performing refactorings to improve readability and maintainability. In the process, we often create new test files that accompany the p…
Introducing Predictive Debugging: A Game-Changing Look into the Future
With the introduction of debugging tools, software developers were empowered to interactively investigate the control flow of software programs to find bugs in live environments. At JetBrains, we've always strived to improve the art of debugging. Besides the more standard things you expect from a de…