oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.49k stars 2.79k forks source link

add `once` to HTMLRewriter #13406

Closed huseeiin closed 1 month ago

huseeiin commented 3 months ago

What is the problem this feature would solve?

much like cheerio first() selector:

const $ = cheerio(await res.text());

const activity = $("div").first();

What is the feature you are proposing to solve the problem?

get only the first element and ignore the rest

new HTMLRewriter().once("div", { element() {} });

What alternatives have you considered?

i tried div:first-of-type it didn't work

huseeiin commented 1 month ago

i feel like this is stupid now. i will close. mods can reopen if they wish to

Jarred-Sumner commented 1 month ago

why stupid?

huseeiin commented 1 month ago

why stupid?

because what i wanted also wasn't supported in another HTMLRewriter implementation, https://www.npmjs.com/package/htmlrewriter, so i didn't think bun should go out of its way to change the spec.

huseeiin commented 1 month ago

div:first-of-type works fine unless you want the div to also have a class like .activity-descp:first-of-type better reproduction, htmlrewriter and cheerio:

import { load } from "cheerio";

const res = await fetch(
  "https://www.twstalker.com/jarredsumner/status/1844638476853301535"
);

const html = await res.text();

new HTMLRewriter()
  .on(".activity-descp:first-of-type", {
    element(e) {
      // it logs nothing
      console.log(e);
    },
  })
  .transform(html);

const $ = load(html);

// it works
console.log($(".activity-descp").text());