Skip to content

Remove the generated title when no-title is added after render - #402

Open
kwy404 wants to merge 1 commit into
github:mainfrom
kwy404:fix-no-title-added-later
Open

kwy404 wants to merge 1 commit into
github:mainfrom
kwy404:fix-no-title-added-later

Conversation

@kwy404

@kwy404 kwy404 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

The README says adding no-title removes the title attribute, but that only works if no-title is there before the first render. If it is added later, the title from the earlier render stays:

const el = document.createElement('relative-time')
el.datetime = new Date().toISOString()
document.body.append(el)
// later
el.noTitle = true
// before: title="Nov 10, 2026, 1:29 AM GMT+4" is still there
// after: no title attribute

The leftover title also goes stale: if datetime changes while no-title is set, the element keeps the title of the old date.

update() only skipped setting the title when noTitle was true, it never removed one that was already there. The fix removes the title attribute in update() when no-title is present. This is inside the !this.#customTitle branch, so a custom title is still kept, as before. Removing no-title sets the generated title again, same as before.

One behavior change to note: an element rendered with both no-title and a title that is exactly the generated title (for example the same string rendered by the server) now has that title removed, because it cannot be told apart from a generated one. A different custom title is not touched.

After this change, an element that gets no-title later fires relative-time-updated the same way as one that had no-title from the start: on each update, with newTitle set to the computed title and oldTitle empty. Removing the title itself does not fire an event, because the computed title did not change.

Tests: added tests for adding no-title after render (fails before the change, passes after), keeping a custom title when no-title is added, and getting the title back when no-title is removed. I ran the full suite (web-test-runner, Chromium) and npm run lint (tsc --noEmit and eslint). My machine's locale is not English, so I ran the suite with the browser context locale set to en-US (as in CI): 667 passed on this branch, 664 on main. With the unmodified config the same 133 locale-dependent tests fail on both main and this branch.

update() only skipped setting the title while no-title was present, so a
title written by an earlier render stayed on the element. Adding no-title
later (or setting noTitle = true) left the title in place, and it went
stale when datetime changed. The README says no-title removes the title.

Remove the title attribute in update() when no-title is present. A custom
title is still kept, since that branch only runs for generated titles.
@kwy404
kwy404 requested a review from a team as a code owner November 9, 2026 22:20
Copilot AI auto review requested due to automatic review settings November 9, 2026 22:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants