Skip to content

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

@ianks

Description

@ianks

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions