Are custom HTML tags in Angular 2 truly safe? Exploring the safety of custom tags compared to custom attributes

Here we delve into the realm of Angular 2 selectors, comparing Custom tags versus Custom attributes in relation to SEO and browser rendering.

Upon embarking on my Angular 2 journey and altering the selector to '[my-component]' (attribute selector) instead of 'my-component' (tag selector), I encountered a dilemma concerning HTML validation. While it may not adhere strictly to standards, it was an acceptable compromise for the sake of usability.

However, a conflicting viewpoint emerged from the Angular team advocating for tag selectors due to performance considerations, prompting me to reassess my approach.

The W3C's disapproval of non-standard HTML usage raised concerns beyond mere validation errors - particularly regarding the impact on SEO. How would search engines interpret sites with <custom-make-believe-tags> compared to standard HTML tags? Would it affect rankings and visibility?

  1. Another crucial aspect is how browsers handle CSS styling within custom components:

Building a SPA site with Angular 2 introduced complexities when reconciling the component tree with conventional HTML structure. Two options arose, exemplified by the <video-listing-component>: embedding existing HTML or styling directly within the custom tag. Both methods achieved the desired layout visually, but behavioural nuances still persisted.

The puzzling behaviour exhibited by custom tags, such as occupying zero space in the DOM while maintaining proper rendering, prompted further contemplation. Are these outcomes merely coincidental or indicative of stable rendering across various browsers?

Whether styling should be applied directly to custom components or kept separate remains a point of contention, raising questions about best practices and potential pitfalls.

In light of these uncertainties, the need for clarity and consistency in handling custom tags and CSS properties becomes apparent to ensure long-term stability and usability across different platforms and browsers.

Answer №1

The problem lies within your HTML validator. Having a - in the element name is necessary for custom elements to be recognized, as it follows valid HTML5 standards. While Angular doesn't mandate the use of - in element names, it is considered good practice.

You can refer to resources like https://www.w3.org/TR/custom-elements/#registering-custom-elements (look for x-foo) or https://w3c.github.io/webcomponents/spec/custom/#custom-elements-custom-tag-example for further clarification on this rule. Although not explicitly stated, this requirement is crucial in frameworks like Polymer which relies on correctly defined custom elements, unlike Angular where it may not be as significant. A noteworthy distinction is that without the -, querying the element would result in aHTMLUnknownElement, whereas having it will yield anHTMLElement.

For more insights, you can explore my past inquiry regarding Angular's naming convention at Why does Angular not need a dash in component name

On a related note, custom tags typically do not take up visible space in the DOM by default; they are essentially 0px by 0px. Despite this, their content still displays on the browser seamlessly. It's intriguing how browsers handle this rendering behind the scenes

I might require clarity on this query. When Angular processes the template, it dynamically incorporates the content. Hence, what you see rendered in the browser is indeed present in the DOM structure with actual dimensions.

Moreover, search engine crawlers have the capability to navigate JavaScript-generated pages. In cases where this isn't feasible, server-side rendered pages offer static HTML content to crawlers, ensuring complete access to the view.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Struggling with CSS issues in ASP.NET 4.6

For the past few months, I've been diligently working on a project that suddenly hit a snag today. Upon inspecting my website, I noticed that all my button sizes appear to be uniform. I typically use Chrome's developer tools to troubleshoot my we ...

The challenge of navigating CSS specificity guidelines

In my CSS file, I have defined two styles for a table with the class "myTable". The first style sets the background color of header cells to gray, while the second style sets the background color of odd rows to blue: .myTable th { background-color: gr ...

When a new element is dynamically added to a table cell, how is the computation of "width: 100%" handled?

When utilizing Backgrid for editing values in a table, an <input type="text"> is inserted inside the <td>. Despite setting the input to max-width: 100%, it can still cause the column to expand beyond desired dimensions. To see this behavior in ...

Text within cells is not wrapping onto a new line in an HTML table

Let me show you how it currently looks: https://i.sstatic.net/OeHRg.png The maximum width of the cell is working properly, but the text is not wrapping to a new line as expected. Instead, it overflows out of the cell. Here is the CSS applied to the tabl ...

Creating a button that allows the menu to open either by clicking on the button itself or on the marker within the leaflet

I am currently using a sidebar similar to this one: . I have removed the source code from the Github repository found here: https://github.com/Turbo87/sidebar-v2/blob/master/examples/index.html. However, I am facing an issue where clicking on the marker ...

Identifying browser with appCodeName to apply specific CSS styling

My task was to detect which browser is being used and then load CSS files depending on the specific browser. I decided to accomplish this using JavaScript. However, I encountered a problem where regardless of the browser I use, it always displays "Mozilla" ...

Solving the style upload issue with NextJS and Module Federation in ReactJS

My current project utilizes React 18 and webpack 5 with the module federation plugin. I am exploring the possibility of integrating Next JS with module federation, but I am encountering an issue where the host project is not able to fetch styles from the r ...

Struggling to retrieve a route parameter using route.snapshot within the main component

How can I retrieve a token from the URL? Currently, it is returning null as a value. In my app.component.ts file: import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; @Component({ sel ...

Move the scss .map file to a separate directory within Intellij IDEA

I have a specific structure set up in my styles folder within the project directory. It includes three subfolders: css, scss, and css-maps. My goal is to automatically update a corresponding CSS file in the css folder and a .css.map file in the css-maps fo ...

"Unique central logo design that seamlessly overlaps the navigation bar on the

Hello, I am having trouble getting my navbar to have this layout: https://i.sstatic.net/clmZA.png However, I am struggling to place the image on top of the navbar. I am using Bootstrap 5 and have not added any CSS to the navbar yet. Is there a specific pr ...

Issue with arranging cards in a grid when utilizing v-for alongside bootstrap

Just starting out with vuejs and attempting to create a grid by looping through objects using bootstrap classes. However, I'm running into an issue where the cards are not forming a grid as expected when utilizing the col classes. Below is the code f ...

Enhance your Angular2 Directive

Looking to customize the angular2-clipboard npm package by extending its functionalities. Specifically, I aim to access and modify its ngOnInit() function to cater to a specific copying use case. Being new to angular2, I am uncertain about the process of ...

I'm looking for a skilled CSS spinner creator. Can anyone recommend one?

Can anyone recommend a good online application for creating CSS spinners? I've been searching and only found one on David Walsh's blog, but it only has one available. Any suggestions? ...

What is the best way to switch the direction of the arrows based on the sorting order?

Is there a way to dynamically change the direction of arrows based on sorting, similar to the example shown here? sortingPipe.ts: import { SprBitType } from '../spr-bit-type/sprBitType'; import { Pipe, PipeTransform } from '@angular/core& ...

Playing around with adjusting the size of a div that is positioned absolutely on the right side

Snippet: http://jsfiddle.net/scottbeeson/TU6Zw/ HTML Code: <div id="itemMap" class="shadow"> <div class="mapHeader">List of Items</div> <div class="mapItem">Item 1</div> <div class="mapItem">Item 2</div& ...

Transfer the background-image from one div to another

I have several div elements that act as buttons, each with a different background image set in the CSS file: #button1 { background-image: url('../images/abc.png) } #button2 { background-image: url('../images/def.png) } #button3 { background-imag ...

Using inline CSS in JavaScript does not seem to function properly

I'm having an issue with this code. I created a javascript variable in the same file and referenced it for setting the background color of the body. However, it's not working as expected. Can someone please explain why and help me achieve my desi ...

Press the div, excluding the button - Vue

I have a basic div that spans 100% of the width, containing a button inside. The issue I'm facing is that when I add a click event to the div, the entire div becomes clickable (including the button within it). What I want is for the whole div to be ...

Is it possible to utilize @ViewChild() or a similar method with a router-outlet? If yes, how can it be

There is a recurring situation I encounter where I find myself wanting to access a child component located on the opposite end of a router outlet instead of through a selector: For example: <router-outlet></router-outlet> NOT: <selector-na ...

Click Event for Basic CSS Dropdown Menu

My goal is to develop a straightforward feature where a dropdown menu appears below a specific text field once it is clicked. $(".val").children("div").on("click", function() { alert("CLICK"); }); .container { display: inline-block; } .val { d ...