Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide way to get document position information for start and end tags #201

Open
ianks opened this issue Nov 29, 2023 · 1 comment
Open

Comments

@ianks
Copy link

ianks commented Nov 29, 2023

Background

At Shopify, we are working on a small library to optimize our current HTML rewriting implementation and wish to use lol-html. We want to define a number of rewrite steps and execute them in a single lol-html pass. By doing this, we can avoid unnecessary traversals the current implementation suffers from.

We're wrapping lol-html in a Ruby gem, and looks something like:

Rewriter
  .new
  .inject_if_selector_missing(
    missing_selector: "link[rel='canonical']",
    inject_at: Location.once("head", Position.end_of_selector),
    injected_string: %(<link rel="canonical" href="#{canonical_url}">),
  )
  .inject_if_selector_missing(
    missing_selector: "meta[property='og:image'][content]",
    inject_at: Location.once("head", Position.end_of_selector),
    injected_string: social_sharing_image_tags,
  )          
  .replace("<placeholder/>", "<something else/>")
  .rewrite(html_response_body)

In order to execute this in one pass, we needed access to the StartTag::raw and EndTag::raw method in order to track the position where we will later make the string injections / replacements.

Questions

  1. Is using StartTag::raw and EndTag::raw a reasonable way to go about this, order does lol-html provide another way to do something similar?
  2. Would lol-html be open to providing a pub method to expose the positions offset of StartTag and EndTag, or something similar to allow for single-pass rewriting?

cc: @gmalette

Thanks!

@JoshuaWise
Copy link

I also need this feature. Related issue: cloudflare/html-rewriter-wasm#5

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

No branches or pull requests

2 participants