Skip to content

Commit

Permalink
fix(engine): implement support for data-sly-unwrap
Browse files Browse the repository at this point in the history
fixes #55 

Thanks @znikolovski
  • Loading branch information
znikolovski authored and tripodsan committed May 7, 2019
1 parent 0ccc218 commit 2956097
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/parser/html/MarkupHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
64 changes: 64 additions & 0 deletions src/parser/plugins/UnwrapPlugin.js
Original file line number Diff line number Diff line change
@@ -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;
}
};
18 changes: 18 additions & 0 deletions test/specs/unwrap_spec.js
Original file line number Diff line number Diff line change
@@ -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'
},
};
28 changes: 28 additions & 0 deletions test/specs/unwrap_spec.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#
### unwrap with value and variable
#
<a href="${properties.url}" data-sly-unwrap.unwrapTest="${properties.unwrap}">
<span>${properties.title}</span>
</a>
===

<span>yes</span>

#
### unwrap with value no variable
#
<a href="${properties.url}" data-sly-unwrap="${properties.unwrap}">
<span>${properties.title}</span>
</a>
===

<span>yes</span>

#
### unwrap
#
<a href="${properties.url}" data-sly-unwrap><span>${properties.title}</span></a>
===
<span>yes</span>
#
###

0 comments on commit 2956097

Please sign in to comment.