From ada3f81bd3a69301eab3bfb5b75852e78589abdb Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Wed, 3 Feb 2016 16:20:20 -0700 Subject: [PATCH] Fixes #181 - Marko v3: Scriptlets --- compiler/Builder.js | 6 +++ compiler/HtmlJsParser.js | 5 ++ compiler/Parser.js | 7 +++ compiler/ast/Scriptlet.js | 26 ++++++++++ .../parser/autotest/scriptlet/expected.json | 49 +++++++++++++++++++ .../parser/autotest/scriptlet/template.marko | 8 +++ .../autotest-pending/scriptlet/template.marko | 6 --- .../scriptlet/expected.html | 0 .../render/autotest/scriptlet/template.marko | 11 +++++ .../scriptlet/test.js | 0 10 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 compiler/ast/Scriptlet.js create mode 100644 test/fixtures/parser/autotest/scriptlet/expected.json create mode 100644 test/fixtures/parser/autotest/scriptlet/template.marko delete mode 100644 test/fixtures/render/autotest-pending/scriptlet/template.marko rename test/fixtures/render/{autotest-pending => autotest}/scriptlet/expected.html (100%) create mode 100644 test/fixtures/render/autotest/scriptlet/template.marko rename test/fixtures/render/{autotest-pending => autotest}/scriptlet/test.js (100%) diff --git a/compiler/Builder.js b/compiler/Builder.js index 6440ea3cd..2362fdfbe 100644 --- a/compiler/Builder.js +++ b/compiler/Builder.js @@ -42,6 +42,8 @@ var Property = require('./ast/Property'); var VariableDeclarator = require('./ast/VariableDeclarator'); var ThisExpression = require('./ast/ThisExpression'); var Expression = require('./ast/Expression'); +var Scriptlet = require('./ast/Scriptlet'); + var parseExpression = require('./util/parseExpression'); var parseJavaScriptArgs = require('./util/parseJavaScriptArgs'); var removeEscapeFunctions = require('./util/removeEscapeFunctions'); @@ -367,6 +369,10 @@ class Builder { return new Return({argument}); } + scriptlet(code) { + return new Scriptlet({code}); + } + selfInvokingFunction(params, args, body) { if (arguments.length === 1) { body = arguments[0]; diff --git a/compiler/HtmlJsParser.js b/compiler/HtmlJsParser.js index 20f9ad9a5..3d333b07d 100644 --- a/compiler/HtmlJsParser.js +++ b/compiler/HtmlJsParser.js @@ -67,6 +67,11 @@ class HtmlJsParser { handlers.handleComment(event.value); }, + onScriptlet(event) { + // <% (code) %> + handlers.handleScriptlet(event.value); + }, + onError(event) { handlers.handleError(event); } diff --git a/compiler/Parser.js b/compiler/Parser.js index 21a764371..d24f8f4f6 100644 --- a/compiler/Parser.js +++ b/compiler/Parser.js @@ -184,6 +184,13 @@ class Parser { this.parentNode.appendChild(text); } + handleScriptlet(code) { + this.prevTextNode = null; + var builder = this.context.builder; + var scriptlet = builder.scriptlet(code); + this.parentNode.appendChild(scriptlet); + } + handleError(event) { this.context.addError({ message: event.message, diff --git a/compiler/ast/Scriptlet.js b/compiler/ast/Scriptlet.js new file mode 100644 index 000000000..d190f271f --- /dev/null +++ b/compiler/ast/Scriptlet.js @@ -0,0 +1,26 @@ +'use strict'; + +var Node = require('./Node'); +var adjustIndent = require('../util/adjustIndent'); + +class Scriptlet extends Node { + constructor(def) { + super('Scriptlet'); + this.code = def.code; + } + + generateCode(codegen) { + var code = this.code; + + if (!code) { + return; + } + + code = adjustIndent(code, codegen.currentIndent); + + codegen.write(code); + codegen.write('\n'); + } +} + +module.exports = Scriptlet; \ No newline at end of file diff --git a/test/fixtures/parser/autotest/scriptlet/expected.json b/test/fixtures/parser/autotest/scriptlet/expected.json new file mode 100644 index 000000000..eedacc728 --- /dev/null +++ b/test/fixtures/parser/autotest/scriptlet/expected.json @@ -0,0 +1,49 @@ +{ + "type": "TemplateRoot", + "body": [ + { + "type": "Scriptlet", + "code": " if (true) { " + }, + { + "type": "Text", + "argument": { + "type": "Literal", + "value": "\n HELLO\n" + } + }, + { + "type": "Scriptlet", + "code": " } " + }, + { + "type": "Text", + "argument": { + "type": "Literal", + "value": "\n" + } + }, + { + "type": "Scriptlet", + "code": " if (false) { " + }, + { + "type": "Text", + "argument": { + "type": "Literal", + "value": "\n WORLD\n" + } + }, + { + "type": "Scriptlet", + "code": " } " + }, + { + "type": "Text", + "argument": { + "type": "Literal", + "value": "\n" + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/parser/autotest/scriptlet/template.marko b/test/fixtures/parser/autotest/scriptlet/template.marko new file mode 100644 index 000000000..10d11423c --- /dev/null +++ b/test/fixtures/parser/autotest/scriptlet/template.marko @@ -0,0 +1,8 @@ +--- +<% if (true) { %> + HELLO +<% } %> +<% if (false) { %> + WORLD +<% } %> +--- \ No newline at end of file diff --git a/test/fixtures/render/autotest-pending/scriptlet/template.marko b/test/fixtures/render/autotest-pending/scriptlet/template.marko deleted file mode 100644 index 7382b8a3b..000000000 --- a/test/fixtures/render/autotest-pending/scriptlet/template.marko +++ /dev/null @@ -1,6 +0,0 @@ -{% if (true) { %} - HELLO -{% } %} -{% if (false) { %} - WORLD -{% } %} \ No newline at end of file diff --git a/test/fixtures/render/autotest-pending/scriptlet/expected.html b/test/fixtures/render/autotest/scriptlet/expected.html similarity index 100% rename from test/fixtures/render/autotest-pending/scriptlet/expected.html rename to test/fixtures/render/autotest/scriptlet/expected.html diff --git a/test/fixtures/render/autotest/scriptlet/template.marko b/test/fixtures/render/autotest/scriptlet/template.marko new file mode 100644 index 000000000..030c27fc2 --- /dev/null +++ b/test/fixtures/render/autotest/scriptlet/template.marko @@ -0,0 +1,11 @@ + +--- +<% a=10 %> +${a} +<% if (true) { %> + HELLO +<% } %> +<% if (false) { %> + WORLD +<% } %> +--- \ No newline at end of file diff --git a/test/fixtures/render/autotest-pending/scriptlet/test.js b/test/fixtures/render/autotest/scriptlet/test.js similarity index 100% rename from test/fixtures/render/autotest-pending/scriptlet/test.js rename to test/fixtures/render/autotest/scriptlet/test.js