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

saner escaping/unescaping of @ and # sigils #750

Merged
merged 4 commits into from
Aug 5, 2017
Merged
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
4 changes: 2 additions & 2 deletions src/generators/dom/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ export default class Block {
${properties}
};
}
`.replace(/(\\\\)?#(\w*)/g, (match, escaped, name) => {
return escaped ? match.slice(2) : this.alias(name);
`.replace(/(#+)(\w*)/g, (match: string, sigil: string, name: string) => {
return sigil === '#' ? this.alias(name) : sigil.slice(1) + name;
});
}
}
11 changes: 5 additions & 6 deletions src/generators/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export default function dom(

const textContent = stringify(options.dev ?
`${css}\n/*# sourceMappingURL=${cssMap.toUrl()} */` :
css);
css, { onlyEscapeAtSymbol: true });

builder.addBlock(deindent`
function @add_css () {
Expand Down Expand Up @@ -281,9 +281,8 @@ export default function dom(

let result = builder
.toString()
.replace(/(\\\\)?([@#])(\w*)/g, (match: string, escaped: string, sigil: string, name: string) => {
if (escaped) return match.slice(2);
if (sigil !== '@') return match;
.replace(/(@+)(\w*)/g, (match: string, sigil: string, name: string) => {
if (sigil !== '@') return sigil.slice(1) + name;

if (name in shared) {
if (options.dev && `${name}Dev` in shared) name = `${name}Dev`;
Expand All @@ -302,13 +301,13 @@ export default function dom(
});

result =
`import { ${names.join(', ')} } from ${stringify(sharedPath)};\n\n` +
`import { ${names.join(', ')} } from ${JSON.stringify(sharedPath)};\n\n` +
result;
}

else if (format === 'cjs') {
const SHARED = '__shared';
let requires = `var ${SHARED} = require( ${stringify(sharedPath)} );`;
let requires = `var ${SHARED} = require( ${JSON.stringify(sharedPath)} );`;
used.forEach(name => {
const alias = generator.alias(name);
requires += `\nvar ${alias} = ${SHARED}.${name};`;
Expand Down
16 changes: 7 additions & 9 deletions src/generators/server-side-rendering/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import preprocess from './preprocess';
import visit from './visit';
import { removeNode, removeObjectKey } from '../../utils/removeNode';
import { Parsed, Node, CompileOptions } from '../../interfaces';
import { stringify } from '../../utils/stringify';

export class SsrGenerator extends Generator {
bindings: string[];
Expand Down Expand Up @@ -93,7 +94,7 @@ export default function ssr(

var ${name} = {};

${name}.filename = ${JSON.stringify(options.filename)};
${name}.filename = ${stringify(options.filename)};

${name}.data = function () {
return ${templateProperties.data ? `@template.data()` : `{}`};
Expand Down Expand Up @@ -133,8 +134,8 @@ export default function ssr(
deindent`
components.push({
filename: ${name}.filename,
css: ${JSON.stringify(css)},
map: ${JSON.stringify(cssMap)}
css: ${stringify(css)},
map: ${stringify(cssMap.toString())}
});
`}

Expand Down Expand Up @@ -169,7 +170,7 @@ export default function ssr(

var escaped = {
'"': '"',
"'": ''',
"'": '&##39;',
'&': '&',
'<': '&lt;',
'>': '&gt;'
Expand All @@ -178,11 +179,8 @@ export default function ssr(
function __escape ( html ) {
return String( html ).replace( /["'&<>]/g, match => escaped[ match ] );
}
`.replace(/(\\)?([@#])(\w*)/g, (match: string, escaped: string, sigil: string, name: string) => {
if (escaped) return match.slice(1);
if (sigil !== '@') return match;

return generator.alias(name);
`.replace(/(@+|#+)(\w*)/g, (match: string, sigil: string, name: string) => {
return sigil === '@' ? generator.alias(name) : sigil.slice(1) + name;
});

return generator.generate(result, options, { name, format });
Expand Down
3 changes: 2 additions & 1 deletion src/generators/server-side-rendering/visitors/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Block from '../Block';
import { Node } from '../../../interfaces';
import getObject from '../../../utils/getObject';
import getTailSnippet from '../../../utils/getTailSnippet';
import { stringify } from '../../../utils/stringify';

export default function visitComponent(
generator: SsrGenerator,
Expand Down Expand Up @@ -41,7 +42,7 @@ export default function visitComponent(
} else if (attribute.value.length === 1) {
const chunk = attribute.value[0];
if (chunk.type === 'Text') {
value = isNaN(chunk.data) ? JSON.stringify(chunk.data) : chunk.data;
value = isNaN(chunk.data) ? stringify(chunk.data) : chunk.data;
} else {
const { snippet } = block.contextualise(chunk.expression);
value = snippet;
Expand Down
10 changes: 6 additions & 4 deletions src/utils/stringify.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export function stringify(data: string) {
return JSON.stringify(escape(data));
export function stringify(data: string, options = {}) {
return JSON.stringify(escape(data, options));
}

export function escape(data: string) {
return data.replace(/([^\\@#])?([@#])/g, '$1\\$2');
export function escape(data: string, { onlyEscapeAtSymbol = false } = {}) {
return data.replace(onlyEscapeAtSymbol ? /(@+)/g : /(@+|#+)/g, (match: string) => {
return match + match[0];
});
}
1 change: 1 addition & 0 deletions test/runtime/samples/component-static-at-symbol/Email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a href='mailto:{{address}}'>email</a>
3 changes: 3 additions & 0 deletions test/runtime/samples/component-static-at-symbol/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
html: `<a href='mailto:[email protected]'>email</a>`
};
9 changes: 9 additions & 0 deletions test/runtime/samples/component-static-at-symbol/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Email address='[email protected]'/>

<script>
import Email from './Email.html';

export default {
components: { Email },
};
</script>