-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
20 changed files
with
481 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require "erb_parser" | ||
require "deface" | ||
|
||
module Phlexing | ||
class ErbTransformer | ||
def self.transform(html) | ||
transformed = html.to_s | ||
def self.transform(source) | ||
transformed = source.to_s | ||
transformed = transform_template_tags(transformed) | ||
transformed = transform_erb_tags(transformed) | ||
transformed = transform_remove_newlines(transformed) | ||
transformed = transform_template_tag(transformed) | ||
transformed = transform_whitespace(transformed) | ||
|
||
ErbParser.transform_xml(transformed) | ||
transformed | ||
rescue StandardError | ||
html | ||
source | ||
end | ||
|
||
def self.transform_remove_newlines(html) | ||
html.tr("\n", "").tr("\r", "") | ||
def self.transform_remove_newlines(source) | ||
source.tr("\n", "").tr("\r", "") | ||
end | ||
|
||
def self.transform_template_tag(html) | ||
html.gsub("<template", "<template-tag").gsub("</template", "</template-tag") | ||
def self.transform_template_tags(source) | ||
source.gsub("<template", "<template-tag").gsub("</template", "</template-tag") | ||
end | ||
|
||
def self.transform_erb_tags(source) | ||
Deface::Parser.convert(source).to_html | ||
end | ||
|
||
def self.transform_whitespace(source) | ||
source.strip | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.