timeago.js icon indicating copy to clipboard operation
timeago.js copied to clipboard

Can HTML5 "data-*" custom attributes be targeted instead?

Open jschrab opened this issue 6 years ago • 2 comments
trafficstars

In dom.ts, getDateAttribute(...) fetches the custom datetime attribute...

export function getDateAttribute(node: HTMLElement): string {
  return node.getAttribute('datetime');
}

But AFAIK, HTML5 would prefer the use of data- prefixed custom attributes. I am in a development environment that returns proxy objects instead of direct DOM references. The handler for this proxy is strict and does not pass back any value to the datetime attribute. If the target attribute were (optionally) data-datetime, that would work and be more HTML5-ish.

Perhaps...?

export function getDateAttribute(node: HTMLElement): string {
  return node.getAttribute('datetime') || node.dataset.datetime;
}

...for backwards compatibility? More validation is probably necessary for the above.

jschrab avatar Oct 30 '19 17:10 jschrab

Actually, being able to define what the name of the attribute should be would be even better:

export function getDateAttribute(node: HTMLElement): string {
  return node.getAttribute(this.dateTimeAttributeName);
}

...which would still work if the attribute was data-datetime.

jschrab avatar Oct 31 '19 12:10 jschrab

When using timeago.js with React and Typescript, you're unable to specify the datetime attribute (but you are able to specify any custom data-* attributes, as suggested by @jschrab).

Current fix for me was to just use the timeago-react package instead.

nicholaschiang avatar Nov 27 '20 19:11 nicholaschiang