Skip to content
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

Separate stringify from entry #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
50 changes: 50 additions & 0 deletions src/stringify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import emptyTags from './empty-tags';

// escape an attribute
let esc = str => String(str).replace(/[&<>"']/g, s=>`&${map[s]};`);
let map = {'&':'amp','<':'lt','>':'gt','"':'quot',"'":'apos'};
let DOMAttributeNames = {
className: 'class',
htmlFor: 'for'
};

let sanitized = {};

export default function stringify(name, attrs, stack) {
// Sortof component support!
if (typeof name==='function') {
attrs.children = stack.reverse();
return String(name(attrs));
}

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 (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 {
let resolved = String(child);
s += sanitized[resolved]===true ? resolved : esc(resolved);
}
}
}

s += `</${name}>`;
} else {
s += '>';
}

sanitized[s] = true;
return s;
}
49 changes: 2 additions & 47 deletions src/vhtml.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
import emptyTags from './empty-tags';

// escape an attribute
let esc = str => String(str).replace(/[&<>"']/g, s=>`&${map[s]};`);
let map = {'&':'amp','<':'lt','>':'gt','"':'quot',"'":'apos'};
let DOMAttributeNames = {
className: 'class',
htmlFor: 'for'
};

let sanitized = {};
import stringify from './stringify';

/** Hyperscript reviver that constructs a sanitized HTML string. */
export default function h(name, attrs) {
Expand All @@ -17,40 +7,5 @@ export default function h(name, attrs) {
stack.push(arguments[i]);
}

// Sortof component support!
if (typeof name==='function') {
(attrs || (attrs = {})).children = stack.reverse();
return name(attrs);
// return name(attrs, stack.reverse());
}

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 (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);
}
}
}

s += `</${name}>`;
} else {
s += '>';
}

sanitized[s] = true;
return s;
return stringify(name, attrs || {}, stack);
}