diff --git a/src/parser/html/MarkupHandler.js b/src/parser/html/MarkupHandler.js index 649c140..b2d70a0 100644 --- a/src/parser/html/MarkupHandler.js +++ b/src/parser/html/MarkupHandler.js @@ -38,6 +38,7 @@ const PLUGINS = { list: require('../plugins/ListPlugin'), repeat: require('../plugins/ListPlugin'), test: require('../plugins/TestPlugin'), + unwrap: require('../plugins/UnwrapPlugin'), attribute: require('../plugins/AttributePlugin'), use: require('../plugins/UsePlugin'), resource: require('../plugins/ResourcePlugin'), diff --git a/src/parser/plugins/UnwrapPlugin.js b/src/parser/plugins/UnwrapPlugin.js new file mode 100644 index 0000000..ae821f6 --- /dev/null +++ b/src/parser/plugins/UnwrapPlugin.js @@ -0,0 +1,64 @@ +/* + * Copyright 2018 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ + +const Plugin = require('../html/Plugin'); +const VariableBinding = require('../commands/VariableBinding'); +const Conditional = require('../commands/Conditional'); +const BooleanConstant = require('../htl/nodes/BooleanConstant'); +const StringConstant = require('../htl/nodes/StringConstant'); + +module.exports = class UnwrapPlugin extends Plugin { + + beforeElement(stream/* , tagName */) { + const ctx = this.pluginContext; + this.variableName = this._signature.getVariableName(null); + this._useGlobalBinding = this.variableName != null; + if (this.variableName == null) { + this.variableName = ctx.generateVariable('unwrapCondition'); + } + + this.unwrapTest = new Conditional.Start(this.variableName, true); + + if (this._useGlobalBinding) { + stream.write(new VariableBinding.Global(this.variableName, this.expression.root)); + } else { + stream.write(new VariableBinding.Start(this.variableName, this.testRoot())); + } + } + + beforeTagOpen(stream) { + stream.write(this.unwrapTest); + } + + afterTagOpen(stream) { + stream.write(Conditional.END); + } + + beforeTagClose(stream) { + stream.write(this.unwrapTest); + } + + afterTagClose(stream) { + stream.write(Conditional.END); + } + + afterElement(stream) { + if (!this._useGlobalBinding) { + stream.write(VariableBinding.END); + } + } + + testRoot() { + let testNode = this.expression.root instanceof StringConstant && this.expression.root.text.length == 0; + return testNode ? BooleanConstant.TRUE : this.expression.root; + } +}; diff --git a/test/specs/unwrap_spec.js b/test/specs/unwrap_spec.js new file mode 100644 index 0000000..56c1e7c --- /dev/null +++ b/test/specs/unwrap_spec.js @@ -0,0 +1,18 @@ +/* + * Copyright 2018 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +module.exports = { + properties: { + unwrap: true, + url: 'url', + title: 'yes' + }, +}; diff --git a/test/specs/unwrap_spec.txt b/test/specs/unwrap_spec.txt new file mode 100644 index 0000000..3a7a224 --- /dev/null +++ b/test/specs/unwrap_spec.txt @@ -0,0 +1,28 @@ +# +### unwrap with value and variable +# + + ${properties.title} + +=== + + yes + +# +### unwrap with value no variable +# + + ${properties.title} + +=== + + yes + +# +### unwrap +# +${properties.title} +=== +yes +# +###