Skip to content

Commit

Permalink
add m-html support
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Mar 2, 2017
1 parent 9e4944e commit 4c78c11
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
14 changes: 14 additions & 0 deletions dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,11 @@
addEventListeners(node, vnode, instance);
}

// Check if innerHTML was changed, don't diff children if so
if (vnode.props.dom && vnode.props.dom.innerHTML) {
return node;
}

// Diff Children
var currentChildNode = node.firstChild;
// Optimization:
Expand Down Expand Up @@ -1495,6 +1500,15 @@
}
};

specialDirectives[Moon.config.prefix + "html"] = {
beforeGenerate: function (value, vnode) {
if (!vnode.props.dom) {
vnode.props.dom = {};
}
vnode.props.dom.innerHTML = '"' + compileTemplate(value, true) + '"';
}
};

specialDirectives[Moon.config.prefix + "text"] = {
beforeGenerate: function (value, vnode) {
vnode.children = [value];
Expand Down
2 changes: 1 addition & 1 deletion dist/moon.min.js

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/directives/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ specialDirectives[Moon.config.prefix + "pre"] = {
}
}

specialDirectives[Moon.config.prefix + "html"] = {
beforeGenerate: function(value, vnode) {
if(!vnode.props.dom) {
vnode.props.dom = {};
}
vnode.props.dom.innerHTML = `"${compileTemplate(value, true)}"`;
}
}

specialDirectives[Moon.config.prefix + "text"] = {
beforeGenerate: function(value, vnode) {
vnode.children = [value];
Expand Down
5 changes: 5 additions & 0 deletions src/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,11 @@ var diff = function(node, vnode, parent, instance) {
addEventListeners(node, vnode, instance);
}

// Check if innerHTML was changed, don't diff children if so
if(vnode.props.dom && vnode.props.dom.innerHTML) {
return node;
}

// Diff Children
var currentChildNode = node.firstChild;
// Optimization:
Expand Down
16 changes: 16 additions & 0 deletions test/js/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,22 @@ describe("Directive", function() {
});
});

describe('HTML Directive', function() {
createTestElement("html", '<span m-html="{{html}}" id="html-directive-span"></span>');
var htmlApp = new Moon({
el: "#html",
data: {
html: "<strong>Hello Moon!</strong>"
}
});
it('should fill DOM with a value', function() {
expect(document.getElementById("html-directive-span").innerHTML).to.equal("<strong>Hello Moon!</strong>");
});
it('should not be present at runtime', function() {
expect(document.getElementById('html-directive-span').getAttribute("m-html")).to.be['null'];
});
});

describe('Text Directive', function() {
createTestElement("text", '<span m-text="{{msg}}" id="text-directive-span"></span>');
var textApp = new Moon({
Expand Down

0 comments on commit 4c78c11

Please sign in to comment.