GithubHelp home page GithubHelp logo

marcoroth / phlexing Goto Github PK

View Code? Open in Web Editor NEW
77.0 3.0 9.0 1.6 MB

Simple ERB to Phlex converter

Home Page: https://phlexing.fun

Ruby 84.81% JavaScript 4.80% CSS 0.04% HTML 10.20% Shell 0.15%
erb html phlex ruby hotwire rails

phlexing's Issues

Methods called within the ERB template are not generated in the component output

ERB Input:

<% if some_condition? %>
  <%= "Text" %>
<% end %>

Output:

class Component < Phlex::HTML
  def template
    if some_condition?
      text "Text"
    end
  end
end

Expected output:

class Component < Phlex::HTML
  def template
    if some_condition?
      text "Text"
    end
  end

  private 
  
  # TODO: Implement the `some_condition?` method
  def some_condition?
    super
  end
end

ERB interpolation within `<style>` tag doesn't work

ERB Input:

<style type="text/css">
<%= Rouge::Themes::Monokai.render(scope: 'pre') %>
</style>

Output:

style(type: "text/css") do
  "<erb loud> Rouge::Themes::Monokai.render(scope: 'pre') </erb>"
end

Expected output:

style(type: "text/css") do
  Rouge::Themes::Monokai.render(scope: 'pre')
end

Methods that take a block shouldn't be called with `plain`

ERB Input:

<%= something do %>
  Content
<% end %>

Output:

plain something do
  plain " Content"
end

Expected output:

something do
  plain " Content"
end

@adamlogic experienced this in the GoRails Phlex episode: https://youtu.be/l4bQSfqZZfQ&t=1267

I'm not sure if this is actually solvable. Since the ERB is using <%= we expect to output the return value of the method call. If the ERB is just <% the output works as expected. Like:

<% something do %>
  Content
<% end %>

Outputs:

something { plain " Content" }

More than one ERB string interpolation in HTML attribute

ERB Input:

<div style="<%= "background: red;" %><%= "display: block;" %>"></div>

Output:

div(style: ("background: red;" %><%=), block: %(), erb: %(), display:: %()) { %(">)
 }

Expected output:

div(style: %(#{"background: red;"}#{"display: block"}))

Copy to clipboard button copies old code, not what is in text box

Sorry, you don't have a bug template, so I'm freestyling.

Issue

When clicking the click->converter#copy button, what is copied to the clipboard is output from a previous ERB conversion, not the one that is currently in the box. This happens 100% of the time for me since it started previously.

I'm guessing it might have something to do with either not having a component name, or the text input not being valid but the output is still generated, and then it gets "stuck" or something in Turbo. Refreshing the page resolves the issue.

Potential Solution

I noticed in the source that it's copying from output-copy, a second buffer of the text in the pre#output field.

await navigator.clipboard.writeText(document.getElementById("output-copy").value)

You surely know better than me since you made it, but that seems like it might be unnecessary. The innerText of the pre element returns the literal text that is visible to the user, which would prevent any mismatch between what is seen and what is copied.

- await navigator.clipboard.writeText(document.getElementById("output-copy").value) 
+ await navigator.clipboard.writeText(document.getElementById("output").innerText) 

Enable Comments tests again

In #52 while moving the test suite over to the gem the test in comments_test.rb started to fail. I don't fully understand why, but html_press does remove the HTML comments while minifing in the gem context, it didn't do that in the context of the Rails app.

Since the html_press is deprecated we should anyway be looking for an alternative.

Uppercase tag names

ERB Input:

<HTML>
  <HEAD></HEAD>
  <BODY></BODY>
</HTML>

Output:

body

Expected output:

html do
  head
  body
end

Automatically detect Rails helpers

Using Phlexing::Converter.idents we should be able to figure out if the HTML is depending on any Rails helper within the template. If that's the case we could automatically include the right module, delegate the call to the helpers object Phlex provides or we could rewrite the Ruby code that we output

Using Rails tag helpers alongside other nodes

ERB Input:

<div><%= tag.div('Hello world!') %>Text</div>

Output:

div do
  text(tag.div("Hello world!"))
  text "Text"
end

Expected output:

div do
  tag.div("Hello world!")
  text "Text"
end

or ideally:

div do
  div { "Hello world!" }
  text "Text"
end

`form_with` short-circuits output

ERB Input:

<%= form_with(model: @test) do |form| %>
   <h3>Test</h3>
<% end %>

Output:

class Test < Phlex::HTML
  def view_template
  end
end

Expected output:

class Test < Phlex::HTML
  def view_template
     form_with(model: @test) do |form|
    h3 { "Test" }
  end
end

It seems the form_wth line is the issue.

Unnecessary whitespace for some tags

I'm converting some Tailwind UI components with Phlexing and noticed that I get a lot of unnecessary whitespace rows in my code. I've narrowed it down a bit and it seems to happen only for certain tags. Here's an example:

<div>
  <label>Test</label>
</div>

converts into:

div do
  whitespace
  label { "Test" }
end

If you change label to div or h1 it does not add the whitespace.

I've narrowed the cause down to the call to HtmlPress here, which leaves a space between the tags:

HtmlPress.press(converted_erb)

irb(main):001:0" html = <<~HTML.strip
irb(main):002:0"   <div>
irb(main):003:0"     <label>Test</label>
irb(main):004:0"  </div>
irb(main):005:0> HTML
=> "<div>\n  <label>Test</label>\n</div>"
irb(main):006:0> HtmlPress.press(html)
=> "<div> <label>Test</label></div>"
         ^ extra space here
irb(main):007:0" html = <<~HTML.strip
irb(main):008:0"   <div>
irb(main):009:0"     <h1>Test</h1>
irb(main):010:0"   </div>
irb(main):011:0> HTML
=> "<div>\n  <h1>Test</h1>\n</div>"
irb(main):012:0> HtmlPress.press(html)
=> "<div><h1>Test</h1></div>"
         ^ no space here

It seems that HtmlPress is unmaintained and marked deprecated by the owner (stereobooster/html_press#21), so I don't really expect them to fix this or accept a fix.

Not sure what a good approach would be here, what are your thoughts? Happy to work on some kind of fix 👍

Using Rails tag helpers with blocks

ERB Input:

<%= tag.p do %>
  <div>Text</div>
<% end %>

Output:

text tag.p do
  div { "Text" }
end

Expected output:

tag.p do
  div { "Text" }
end

or ideally:

p do
  div { "Text" }
end

ERB in attributes always outputs parens

Input:

<div class="<%= classes_helper %>" id="<%= many? ? "posts" : "post" %>"></div>

Output:

div(class: (classes_helper), id: (many? ? "posts" : "post"))

Expected output:

div(class: classes_helper, id: (many? ? "posts" : "post"))

Stray `text` calls on method calls that render or capture for `Phlex::DeferredRender

Is your feature request related to a problem? Please describe.
Phlexing can't tell when to leave out the text method call on methods that render

image

Expected:

render SomeComponent.new do |c|
  c.section { "hello world" }
end

Not sure what the solution might be. Phlexing obviously doesn't know if c.section is rendering, capturing for Phlex::DeferredRender or returning a String.

Support case-sensitive HTML elements

Even while it's encouraged to use Kebab-case for HTML-tags there are some use-cases where we have case-sensitive HTML elements, like in SVG (see #139). This would also allow to cleanup the special-handling we have for handling SVG elements in:

subchild.name = SVG_ELEMENTS[subchild.name] if SVG_ELEMENTS.key?(subchild.name)

Since we are using Nokogiri::HTML4::DocumentFragment for parsing the source it automatically casts all tag names to lowercase. While Nokogiri::HTML5::DocumentFragment would support case-sensitive tags, it doesn't support other things like properly detecting boolean attributes (see #119 and #120).

ERB Input:

<customElement>
 <anotherElement/>
</customElement>

Output:

class CustomelementComponent < Phlex::HTML
  register_element :anotherelement
  register_element :customelement

  def template
    customelement { anotherelement }
  end
end

Expected output:

class CustomelementComponent < Phlex::HTML
  register_element :anotherElement
  register_element :customElement

  def template
    customElement { anotherElement }
  end
end

Rails `t` and `translate` helpers

Case 1

ERB Input:

<%= t(".some.string") %> Text

Output:

text t(".some.string")

text " Text"

Expected output:

t(".some.string")

text " Text"

Case 2

ERB Input:

<%= translate(".some.string") %> Text

Output:

text translate(".some.string")

text " Text"

Expected output:

translate(".some.string")

text " Text"

Interpolating ERB in HTML attributes

ERB Input:

<div class="<%= classes_helper %>">Text</div>

Output:

div(class: "<erb interpolated=", true: "") { ' classes_helper ">Text' }

Expected output:

div(class: classes_helper) { "Text" }

Transform Phlex code output through RuboCop or SyntaxTree

Let's say we have this:

<%= tag.div class: something? ? "class-1" : "class-2" do %>
  <%= content_tag :span, something.text %>
<% end %>

Which would generate this through phlexing today:

class SomethingComponent < Phlex::HTML
  include Phlex::Rails::Helpers::ContentTag
  include Phlex::Rails::Helpers::Tag

  attr_accessor :something

  def initialize(something:)
    @something = something
  end

  def template
    tag.div class: something? ? "class-1" : "class-2" do
      content_tag :span, something.text
    end
  end

  private

  def something?(*args, **kwargs)
    # TODO: Implement me
  end
end

But since this is a regular Ruby class now we could write any RuboCop rule (or maybe even a SyntaxTree mutation visitor) which statically analyses the code and auto-fixes it (we could even make the rules toggleable from the UI)

For example some rules could be:

  • rewrite tag.div as div { ... }
  • rewrite content_tag :span to span { ... }
  • rewrite all references of something inside def template to @something and remove the attr_accessor for it
  • rewrite class: something? ? "class-1" : "class-2" to **classes(something?: { then: "class-1", else: "class-2" })
  • ...

We could even release these rules standalone as rubocop-phlex which people could use in their project independent of phlexing

Style tags gets minified/transformed

ERB Input:

<style>
  body { background: black }
</style>

Output:

style { "body{background:#000}" }

Expected output:

style { "body { background: #000 }" }

or

style { "body { background: black }" }

SVG tags are a special case

ERB Input:

<svg>
  <feSpecularLighting>
    <fePointLight/>
  </feSpecularLighting>    
</svg>

Output:

svg { fespecularlighting { fepointlight } }

Expected output:

# `el` is just a random name
svg { |el| el.feSpecularLighting { el.fePointLight } }
# or
svg do |el|
  el.feSpecularLighting do 
    el.fePointLight
  end
end

There are two problems:

  • Phlex uses yielded svg to build svg tags
  • SVG tag methods uses camelCase pattern, downcase method names results on NoMethodError

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.