Is it intentional that Textual DOMNode IDs aren't required to be unique? #1044
-
I suspect most folk arriving at Textual from building web apps will be expecting that it's intended that IDs are supposed to be globally unique. However, they aren't: from textual.app import App
from textual.widgets import Static
class IDTest(App[int]):
BINDINGS = [("q","return","Return the result")]
def compose(self):
for _ in range(30):
yield Static("The only good bug is a dead bug!", id="quote")
def action_return(self):
self.exit(len(self.query("#quote")))
print(f"I found this many quotes: {IDTest().run()}") To some degree the stopwatch tutorial requires that this is the case too. Given that this is likely not what someone would expect (non-unique IDs), if it's an intentional design choice for Textual (and I can see how it can be useful), might it be a good idea for us to highlight this in the documentation somewhere so folk using Textual know for sure? All of the above is written while keeping in mind that unique in a web document is a SHOULD, but not a MUST, of course; I think what I'm thinking here is that we may want to make it clear if IDs in Textual SHOULD be unique or not, or MUST be unique or not. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I'm taking my lead from the browser, which doesn't enforce uniqueness of IDs. Although we do deviate where appropriate, so it is a good discussion to have. My gut feeling is that enforcing IDs as globally unique may not improve the user experience. I can think of no reason why Textual would need unique IDs, so I'd be reluctant to force that on the the dev. And its actually quite difficult to coordinate, when you have a "page" assembled from multiple components. So I suspect uniqueness should be a SHOULD, and not a MUST. A compromise might be we enforce uniqueness within a single component's children. Since a component could manage that quite easily, and a duplicate ID has a high chance of being an error. |
Beta Was this translation helpful? Give feedback.
I'm taking my lead from the browser, which doesn't enforce uniqueness of IDs. Although we do deviate where appropriate, so it is a good discussion to have.
My gut feeling is that enforcing IDs as globally unique may not improve the user experience. I can think of no reason why Textual would need unique IDs, so I'd be reluctant to force that on the the dev. And its actually quite difficult to coordinate, when you have a "page" assembled from multiple components.
So I suspect uniqueness should be a SHOULD, and not a MUST.
A compromise might be we enforce uniqueness within a single component's children. Since a component could manage that quite easily, and a duplicate ID has a high chance of…