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
- 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?
- 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!
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:
In order to execute this in one pass, we needed access to the
StartTag::rawandEndTag::rawmethod in order to track the position where we will later make the string injections / replacements.Questions
StartTag::rawandEndTag::rawa reasonable way to go about this, order doeslol-htmlprovide another way to do something similar?lol-htmlbe open to providing a pub method to expose the positions offset ofStartTagandEndTag, or something similar to allow for single-pass rewriting?cc: @gmalette
Thanks!