diff --git a/lib/liveview.js b/lib/liveview.js index 8764ec4..b023286 100644 --- a/lib/liveview.js +++ b/lib/liveview.js @@ -3,8 +3,10 @@ const liveViewFormForPlaceholder = '
'; const encodeFormForExpressions = (text) => { const liveViewFormExpressions = []; const textWithPlaceholders = text.replace(/(<%=?[^%>]*=\s+form_for[\s\S]*?%>|)/gm, (match) => { + if (match.match(/->\s*%>$/gm)) { + return match; + } liveViewFormExpressions.push(match); - return liveViewFormForPlaceholder; }); diff --git a/lib/parser.js b/lib/parser.js index 1367fcd..b043526 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -3,7 +3,12 @@ const expressionTypeMatcher = require('./expression_type_matcher'); const { encodeFormForExpressions } = require('./liveview'); function parse(text, parsers, options) { - const { liveViewFormExpressions, textWithPlaceholders } = encodeFormForExpressions(text); + let liveViewFormExpressions = []; + let textWithPlaceholders = text; + + if (options.filepath.endsWith('.leex') && text.includes('
')) { + ({ liveViewFormExpressions, textWithPlaceholders } = encodeFormForExpressions(text)); + } return { tokens: tokenizeHTML(textWithPlaceholders, /<%[\s\S]*?%>/gm, expressionTypeMatcher), diff --git a/lib/printers.js b/lib/printers.js index eb09f70..a1f7b76 100644 --- a/lib/printers.js +++ b/lib/printers.js @@ -22,7 +22,7 @@ function embed(path, _print, textToDoc, options) { const callback = decodeExpressions(expressionMap); return mapDoc(htmlDoc, (doc) => { let newDoc = doc; - if (doc === liveViewFormForPlaceholder) { + if (doc === liveViewFormForPlaceholder && liveViewFormExpressions.length) { newDoc = liveViewFormExpressions.shift(); } diff --git a/tests/expressions/__snapshots__/jsfmt.spec.js.snap b/tests/expressions/__snapshots__/jsfmt.spec.js.snap index 5c10a70..2eae9ab 100644 --- a/tests/expressions/__snapshots__/jsfmt.spec.js.snap +++ b/tests/expressions/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,33 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`form_for.html.eex 1`] = ` +====================================options===================================== +parsers: ["eex"] +printWidth: 80 + | printWidth +=====================================input====================================== +
+ <%= f = form_for a, fn -> %> + <%= text_input f, :name %> + <% end %> +
+
+
+
+ +=====================================output===================================== +
+ <%= f = form_for a, fn -> %> + <%= text_input f, :name %> + <% end %> +
+
+
+
+ +================================================================================ +`; + exports[`liveview_form_for.html.leex 1`] = ` ====================================options===================================== parsers: ["eex"] @@ -23,6 +51,10 @@ printWidth: 80 + + <%= f = form_for a, fn -> %> + <%= text_input f, :name %> + <% end %> =====================================output===================================== @@ -43,6 +75,10 @@ printWidth: 80 + + <%= f = form_for a, fn -> %> + <%= text_input f, :name %> + <% end %> ================================================================================ diff --git a/tests/expressions/form_for.html.eex b/tests/expressions/form_for.html.eex new file mode 100644 index 0000000..7b28070 --- /dev/null +++ b/tests/expressions/form_for.html.eex @@ -0,0 +1,8 @@ +
+ <%= f = form_for a, fn -> %> + <%= text_input f, :name %> + <% end %> +
+
+
+
diff --git a/tests/expressions/liveview_form_for.html.leex b/tests/expressions/liveview_form_for.html.leex index 1a9e3a6..d11d929 100644 --- a/tests/expressions/liveview_form_for.html.leex +++ b/tests/expressions/liveview_form_for.html.leex @@ -15,4 +15,8 @@ + + <%= f = form_for a, fn -> %> + <%= text_input f, :name %> + <% end %>