Skip to content

Commit d7bd884

Browse files
author
José Valim
committed
Move JavaScript code out of onclick
The main motivation for this is to provide support for Content Security Policy, which recommends disabling all inline scripts in a page. We took the opportunity to also add support for data-confirm in `link/2`.
1 parent 58bb28c commit d7bd884

File tree

8 files changed

+106
-3
lines changed

8 files changed

+106
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/_build
22
/cover
3+
/node_modules
34
/deps
45
/doc
56
erl_crash.dump

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ application.
88

99
See the [docs](https://hexdocs.pm/phoenix_html/) for more information.
1010

11+
### Building phoenix_html.js
12+
13+
```bash
14+
$ npm install
15+
$ npm install -g brunch
16+
$ brunch watch
17+
```
18+
1119
## License
1220

1321
Copyright (c) 2014 Chris McCord

brunch-config.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
exports.config = {
2+
sourceMaps: false,
3+
production: true,
4+
5+
modules: {
6+
wrapper: false,
7+
definition: false
8+
},
9+
10+
files: {
11+
javascripts: {
12+
joinTo: 'phoenix_html.js'
13+
},
14+
},
15+
16+
paths: {
17+
// Which directories to watch
18+
watched: ["web/static", "test/static"],
19+
20+
// Where to compile files to
21+
public: "priv/static"
22+
},
23+
24+
plugins: {
25+
babel: {
26+
// Do not use ES6 compiler in vendor code
27+
ignore: [/^(web\/static\/vendor)/],
28+
loose: "all"
29+
}
30+
}
31+
};

lib/phoenix_html/link.ex

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ defmodule Phoenix.HTML.Link do
3737
is not `:get`
3838
3939
All other options are forwarded to the underlying `<a>` tag.
40+
41+
## Data attributes
42+
43+
The following data attributes are supported/generated:
44+
45+
* `data-submit=parent` - used when the `:method` is not
46+
`:get`, this module attribute says the underlying link
47+
should submit the parent node whenever there is a click
48+
49+
* `data-confirm` - shows a confirmation prompt before
50+
submitting the parent when `:method` is not `:get`
51+
52+
## JavaScript dependency
53+
54+
In order to support the data attributes above, `Phoenix.HTML`
55+
relies on JavaScript. You can either load the ES5 version from
56+
`priv/static/phoenix_html.js` or depend on the one at
57+
`web/static/js/phoenix_html.js` written in ES6 directly from
58+
your build tool.
4059
"""
4160
def link(text, opts)
4261

@@ -61,7 +80,7 @@ defmodule Phoenix.HTML.Link do
6180
else
6281
{form, opts} = form_options(opts, method, "link")
6382
form_tag(to, form) do
64-
content_tag(:a, text, [href: "#", onclick: "this.parentNode.submit(); return false;"] ++ opts)
83+
content_tag(:a, text, [href: "#", data: [submit: "parent"]] ++ opts)
6584
end
6685
end
6786
end

mix.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule PhoenixHtml.Mixfile do
22
use Mix.Project
33

4-
@version "1.4.0"
4+
@version "2.0.0-dev"
55

66
def project do
77
[app: :phoenix_html,
@@ -20,7 +20,7 @@ defmodule PhoenixHtml.Mixfile do
2020
end
2121

2222
defp deps do
23-
[{:plug, ">= 0.12.2 and < 2.0.0"},
23+
[{:plug, ">= 0.13 and < 2.0.0"},
2424

2525
# Docs dependencies
2626
{:earmark, "~> 0.1", only: :docs},

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"repository": {
3+
},
4+
"dependencies": {
5+
"brunch": "^1.8.1",
6+
"babel-brunch": "^5.1.1",
7+
"javascript-brunch": ">= 1.0 < 1.8",
8+
"uglify-js-brunch": ">= 1.0 < 1.8"
9+
}
10+
}

priv/static/phoenix_html.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Although ^=parent is not technically correct,
2+
// we need to use it in order to get IE8 support.
3+
'use strict';
4+
5+
var elements = document.querySelectorAll('[data-submit^=parent]');
6+
var len = elements.length;
7+
8+
for (var i = 0; i < len; ++i) {
9+
elements[i].addEventListener('click', function () {
10+
var message = this.getAttribute("data-confirm");
11+
if (message === null || confirm(message)) {
12+
this.parentNode.submit();
13+
};
14+
event.preventDefault();
15+
return false;
16+
}, false);
17+
}
18+
19+
;

web/static/js/phoenix_html.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Although ^=parent is not technically correct,
2+
// we need to use it in order to get IE8 support.
3+
var elements = document.querySelectorAll('[data-submit^=parent]')
4+
var len = elements.length
5+
6+
for (var i=0; i<len; ++i) {
7+
elements[i].addEventListener('click', function(){
8+
var message = this.getAttribute("data-confirm")
9+
if(message === null || confirm(message)){
10+
this.parentNode.submit()
11+
};
12+
event.preventDefault()
13+
return false
14+
}, false)
15+
}

0 commit comments

Comments
 (0)