question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Accessing this.element throws exception for single root element with extra whitespace

See original GitHub issue

With the following template:

<div class="container">
  <h1>My App</h1>
</div>

Trying to access this.element in the didInsertElement Lifecycle Hooks throws an error:

Uncaught Error: The 'element' property can only be accessed on components that contain a single root element in their template. Try using 'bounds' instead to access the first and last nodes.

I found out that this is because of the newline at the end of the file, which is treated as a separate node. this.bounds.lastNodecontains a separate node with the content "↵" Even adding a singe whitespace character at the end of the div node makes this.element access fail.

This behavior seems kind of unintuitive, since according to my first impression of this, this should be considered to be a single root element. Would you say this is expected behavior, or is it recommended to always use this.bounds instead?


To reproduce: Add a single space immediately after the end of this template i.e. after </nav> https://github.com/glimmerjs/glimmer.js/blob/master/packages/%40glimmer/component/test/browser/element-test.ts#L50 Once you re-run the tests, the 'elements are supported' test fails.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rwjbluecommented, Jul 14, 2019

In general,gimme.js will follow suit with Ember and remove this.element all together. The main blocker for this is adding proper element modifier support.

0reactions
rjschiecommented, Jul 14, 2019

I agree, this is very unintuitive (and annoying tbh), and difficult to find the problem. Would we lose anything by always having this.element return the same thing as this.bounds.firstNode? That should solve this problem in a performant way, not change functionality, and be a more intuitive issue to solve for those who perhaps meant to grab a different node.

I guess another option would be updating the template compiler to strip surrounding whitespace? Or does the bounds assert check happen before it’s compiled? I’m not very familiar with how template compilation works, and where it fits into the pipeline.

Read more comments on GitHub >

github_iconTop Results From Across the Web

XDocument throws away SIGNIFICANT whitespace inside tags
Some observations: single line with just spaces in value var xml = "<xml><text> </text></xml>"; will also fai, it seems only white spaces in ......
Read more >
SwiftSoup/Element.swift at master - GitHub
SwiftSoup: Pure Swift HTML Parser, with best of DOM, CSS, and jquery (Supports Linux, iOS, Mac, tvOS, watchOS) - SwiftSoup/Element.swift at ...
Read more >
ProjectRootElement Class (Microsoft.Build.Construction)
Comments and white space cannot be edited through this model at present. Each project root element is associated with exactly one project collection....
Read more >
The Element Class - Cafe con Leche
throws IllegalNameException ;. For example, this code fragment creates four Element objects using the various constructors:.
Read more >
Elements in the DOM - Document - HTML Spec - WhatWG
Returns the URL of the Document from which the user navigated to this one, unless it was blocked or there was no such...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found