Giter Site home page Giter Site logo

crowdhailer / eex_html Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 5.0 23 KB

Extension to Embedded Elixir (EEx), that allows content to be safely embedded into HTML.

Home Page: https://hex.pm/packages/eex_html

License: Apache License 2.0

Elixir 100.00%
elixir-language html web

eex_html's Introduction

EExHTML

Extension to Embedded Elixir (EEx), that allows content to be safely embedded into HTML.

Hex pm Build Status License

Usage

iex> title = "EEx Rocks!"
iex> content = ~E"<h1><%= title %></h1>"
iex> "#{content}"
"<h1>EEx Rocks!</h1>"

iex> title = "<script>"
iex> content = ~E"<h1><%= title %></h1>"
iex> "#{content}"
"<h1>&lt;script&gt;</h1>"

Elixir language proposal

I would like to see this project as part of the Elixir language. The reasons for this are explained in this proposal.

eex_html's People

Contributors

crowdhailer avatar ejc123 avatar harshbalyan avatar x4lldux avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

eex_html's Issues

Library fails to compile: no function clause matching in EExHTML.sigil_E/2

After an Elixir & OTP update this library fails to compile with the following reason:

==> eex_html
Compiling 3 files (.ex)

== Compilation error in file lib/eex_html.ex ==
** (FunctionClauseError) no function clause matching in EExHTML.sigil_E/2    
    expanding macro: EExHTML.sigil_E/2
    lib/eex_html.ex:255: EExHTML.javascript_variables/1
    (elixir 1.11.3) lib/kernel/parallel_compiler.ex:314: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7

My elixir version:

└──╼ elixir -v
Erlang/OTP 23 [erts-11.1.6] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]

Elixir 1.11.3 (compiled with Erlang/OTP 23)

Opening this repo in my IDE it looks like the javascript_variables function doesn't know the ~E sigil. Maybe Elixir no longer allows using sigils in the same file that declared them? The same error also occurs on the Elixir master 0.12 development branch.

if condition needs an else clause or it will crash

If you use an if structure, you need to add an else clause, or nil will be added to the output, which will crash when the content is attempted to convert to string.

For example here, the empty else clause is required, to return an empty string instead of nil:

<%= raw(if ... do %>
  <%= ... %>
<% else %>
<% end) %>

for and other control structures need raw() to escape contents

If you use for, if, case etc. structures, you need to wrap it in raw() to prevent escaping of the contents:

<%= for i <- 1..10 do %> <span id="<%= i %>"></span><% end %>
<%= raw(for i <- 1..10 do %> <span id="<%= i %>"></span><% end) %>

results in

&lt;span id=&quot;1&quot;&gt;&lt;/span&gt; &lt;span id=&quot;2&quot;&gt;&lt;/span&gt; &lt;span id=&quot;3&quot;&gt;&lt;/span&gt; &lt;span id=&quot;4&quot;&gt;&lt;/span&gt; &lt;span id=&quot;5&quot;&gt;&lt;/span&gt; &lt;span id=&quot;6&quot;&gt;&lt;/span&gt; &lt;span id=&quot;7&quot;&gt;&lt;/span&gt; &lt;span id=&quot;8&quot;&gt;&lt;/span&gt; &lt;span id=&quot;9&quot;&gt;&lt;/span&gt; &lt;span id=&quot;10&quot;&gt;&lt;/span&gt;
<span id="1"></span> <span id="2"></span> <span id="3"></span> <span id="4"></span> <span id="5"></span> <span id="6"></span> <span id="7"></span> <span id="8"></span> <span id="9"></span> <span id="10"></span>

This is inconsistent with the behaviour of Phoenix, for example. It would be more convenient to not need to wrap the structures in raw().

Fix warnings that come from the `EEx.HTML.Safe` protocol warnings.

:0: Unknown function 'Elixir.EEx.HTML.Safe.Atom':'__impl__'/1
:0: Unknown function 'Elixir.EEx.HTML.Safe.BitString':'__impl__'/1
:0: Unknown function 'Elixir.EEx.HTML.Safe.Float':'__impl__'/1
:0: Unknown function 'Elixir.EEx.HTML.Safe.Function':'__impl__'/1
:0: Unknown function 'Elixir.EEx.HTML.Safe.Integer':'__impl__'/1
:0: Unknown function 'Elixir.EEx.HTML.Safe.List':'__impl__'/1
:0: Unknown function 'Elixir.EEx.HTML.Safe.Map':'__impl__'/1
:0: Unknown function 'Elixir.EEx.HTML.Safe.PID':'__impl__'/1
:0: Unknown function 'Elixir.EEx.HTML.Safe.Port':'__impl__'/1
:0: Unknown function 'Elixir.EEx.HTML.Safe.Reference':'__impl__'/1
:0: Unknown function 'Elixir.EEx.HTML.Safe.Tuple':'__impl__'/1

I think these warnings can be fixed by adding an implementation for each of the types.
It would be overkill because everysingle implementation would just call the implementation of String.Chars.to_string and escape the result.

Because I have an implementation for Any I think they would all be redundant an exist only to clean up these warnings.

Is there a better way to fix this?

Issue copied from CrowdHailer/raxx#133

Unexpected EEx.Engine state

I am trying to update elixir from 1.7.4 to 1.16.2.
I am running into a compilation error.

Compilation error in file lib/myapp/api/actions/support/some_action.ex ==
** (RuntimeError) unexpected EEx.Engine state: {{:., [], [{:__aliases__, [alias: false], [:EExHTML]}, :raw]}, [], [[]]}. This typically means a bug or an outdated EEx.Engine or tool
    (eex 1.16.2) lib/eex/engine.ex:218: EEx.Engine.check_state!/1
    (eex 1.16.2) lib/eex/engine.ex:181: EEx.Engine.handle_text/3
    (eex 1.16.2) lib/eex/compiler.ex:321: EEx.Compiler.generate_buffer/4
    (raxx_view 0.1.7) expanding macro: Raxx.View.__using__/1
    lib/myapp/api/actions/support/some_action:6: SomeAction (module)
    (elixir 1.16.2) expanding macro: Kernel.use/2
    lib/myapp/api/actions/support/some_action.ex:6: SomeAction (module)
    expanding macro: MyApp.API.SupportLayout.__using__/1

I have an action like

def SomeAction
  ...
    use MyApp.API.SupportLayout,
    arguments: [:transaction],
    template: Path.join(__DIR__, "transaction.html.eex")

...
end

Support Layout

defmodule MyApp.API.SupportLayout do
  use Raxx.View.Layout,
    optional: [
      page_title: "Admin",
      with_navbar: true,
      message: nil,
      message_type: "info"
    ]
end

EEx.Engine.check_state!/1 expects %{binary: _, dynamic: _, vars_count: _} which was added in Elixir v1.8.
EExHTML.Engine.init/1 doesn't set any of these params.

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.