-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Interpolating ERB in HTML attributes #48
Labels
Comments
marcoroth
added a commit
that referenced
this issue
Jan 22, 2023
This pull request removes the `erb_parser` dependency and adds `deface` for turning HTML+ERB into HTML with `<erb>` tags. Here's a summary of what changed: * `erb_parser` used to transform the input from `<div><%= method %></div>` to `<div><erb loud> method </erb></div>` in `Phlexing::ErbTransformer`. This was replaced with the `Deface::Parser` * `erb_parser` used to extract the Ruby code from ERB templates in `Phlexing::RubyAnalyzer`. This was replaces with our `Phlexing::Parser` class. * most `html` variables were renamed to `source` * a bunch of new tests for, mostly notably for `Phlexing::Minifier`, `Phlexing::Parser` and `Phlexing::ErbTransformer` The most important reason for switching from `erb_parser` to `deface` is the way how `deface` transforms the HTML+ERB source into HTML. ### Example **Input:** ```html+erb <div class="<%= something? "class-1" : "class-2" %>"><%= some_method %></div> ``` **`erb_parser` transform output:** ```html <div class=\"<erb interpolated=\"true\"> something? "class-1" : "class-2" </erb>\"><erb interpolated=\"true\"> some_method </erb></div> ``` Which parsed in Nokogiri ends up with this invalid HTML, because `<erb>` tags (or any tags for that matter) aren't allowed in attributes: ```ruby #(Document:0x5f104 { name = "document", children = [ #(Element:0x5f230 { name = "div", attributes = [ #(Attr:0x5f35c { name = "class", value = "" })] }) ] }) ``` **`deface` transform output:** Deface handles this by prefxing the attribute name with `data-erb-` and escapes the value. With that we are able to detect which HTML attributes not to be interpolated, so we can process the value of the attribute manually: ```html <div data-erb-class='<%= something? \"class-1\" : \"class-2\" %>'><erb loud> some_method </erb></div> ``` Parsing this with Nokogiri gives us the thing we are looking for: ```ruby #(Document:0x1eb504 { name = "document", children = [ #(Element:0x1eb658 { name = "div", attributes = [ #(Attr:0x1eb7ac { name = "data-erb-class", value = "<%= something? \"class-1\" : \"class-2\" %>" })], children = [ #(Element:0x1eba2c { name = "erb", children = [ #(Text " some_method ")] })] }) ] }) ``` ### TL;DR: This allows us to solve issues like #48.
Thanks to #60, it at least generates more correct output now: div(data_erb_class: "<%= classes_helper %>") { "Text" } |
marcoroth
added a commit
that referenced
this issue
Jan 23, 2023
This pull request adds support for ERB interpolation in HTML attributes. This allows use-cases like: **ERB Input:** ```erb <div class="<%= classes_helper %>">Text</div> ``` **Output:** ```ruby div(class: classes_helper) { "Text" } ``` Fixes #48
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ERB Input:
Output:
Expected output:
The text was updated successfully, but these errors were encountered: