Skip to content

Add support for dangerouslySetInnerHTML #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add support for dangerouslySetInnerHTML
  • Loading branch information
ithinkihaveacat committed Jan 17, 2019
commit ac746b5cec54d439f793ce7188dea154e8138aa6
28 changes: 19 additions & 9 deletions src/vhtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,36 @@ export default function h(name, attrs) {
// return name(attrs, stack.reverse());
}

let innerHTML = null;

let s = `<${name}`;
if (attrs) for (let i in attrs) {
if (attrs[i]!==false && attrs[i]!=null) {
s += ` ${DOMAttributeNames[i] ? DOMAttributeNames[i] : esc(i)}="${esc(attrs[i])}"`;
if (i.toLowerCase() === 'dangerouslysetinnerhtml') {
innerHTML = attrs[i].__html ? attrs[i].__html : '';
} else {
s += ` ${DOMAttributeNames[i] ? DOMAttributeNames[i] : esc(i)}="${esc(attrs[i])}"`;
}
}
}

if (emptyTags.indexOf(name) === -1) {
s += '>';

while (stack.length) {
let child = stack.pop();
if (child) {
if (child.pop) {
for (let i=child.length; i--; ) stack.push(child[i]);
}
else {
s += sanitized[child]===true ? child : esc(child);
if (innerHTML === null) {
while (stack.length) {
let child = stack.pop();
if (child) {
if (child.pop) {
for (let i=child.length; i--; ) stack.push(child[i]);
}
else {
s += sanitized[child]===true ? child : esc(child);
}
}
}
} else {
s += innerHTML;
}

s += `</${name}>`;
Expand Down
16 changes: 16 additions & 0 deletions test/vhtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,20 @@ describe('vhtml', () => {
'<div class="my-class" for="id"></div>'
);
});

it('should support dangerouslySetInnerHTML', () => {
expect(
<div dangerouslySetInnerHTML={{__html: '<foo>&amp;</foo>'}}></div>
).to.equal(
'<div><foo>&amp;</foo></div>'
);
});

it('should support dangerouslySetInnerHTML (without __html)', () => {
expect(
<div dangerouslySetInnerHTML>foo</div>
).to.equal(
'<div></div>'
);
});
});
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy