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

fix: html injection cases #114

Merged
merged 2 commits into from
Mar 17, 2022
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
2 changes: 1 addition & 1 deletion chompfile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ name = 'unit:#'
serial = true
deps = ['test/#.test.js', 'lib/**/*.js', 'dist/*']
display = 'dot'
run = 'node -C source $DEP'
run = 'node --enable-source-maps -C source $DEP'

[[task]]
name = 'test:browser'
Expand Down
1 change: 1 addition & 0 deletions src/common/source-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function detectIndent (source: string, newline: string) {
if (curIndent && (indent === undefined || curIndent.length < indent.length))
indent = curIndent[0].slice(0, -1);
}
indent = indent || '';
lines = lines.map(line => line.slice(indent!.length));
let tabSpaces = lines.map(line => line.match(/^[ \t]*/)?.[0] || '') || [];
let tabDifferenceFreqs = new Map<number, number>();
Expand Down
23 changes: 18 additions & 5 deletions src/html/analyze.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseStyled } from '../common/json.js';
import { SourceStyle } from '../common/source-style.js';
import { defaultStyle, SourceStyle } from '../common/source-style.js';
import { baseUrl, isPlain } from '../common/url.js';
import { isWs, ParsedAttribute, ParsedTag, parseHtml } from './lexer.js';
// @ts-ignore
Expand Down Expand Up @@ -63,28 +63,35 @@ export function analyzeHtml (source: string, url: URL = baseUrl): HtmlAnalysis {
esModuleShims: null,
comments: []
};
let createdInjectionPoint = false;
const tags = parseHtml(source);
for (const tag of tags) {
switch (tag.tagName) {
case '!--':
analysis.comments.push({ start: tag.start, end: tag.end, attrs: {} });
break;
case 'base':
if (!analysis.map.json) createInjectionPoint(source, analysis.map, tag, analysis);
if (!createdInjectionPoint) {
createInjectionPoint(source, analysis.map, tag, analysis);
createdInjectionPoint = true;
}
const href = getAttr(source, tag, 'href');
if (href)
analysis.base = new URL(href, url);
break;
case 'script':
const type = getAttr(source, tag, 'type');
if (type === 'importmap') {
const { json, style } = parseStyled(source.slice(tag.innerStart, tag.innerEnd), url.href + '#importmap');
const mapText = source.slice(tag.innerStart, tag.innerEnd);
const emptyMap = mapText.trim().length === 0;
const { json, style } = emptyMap ? { json: {}, style: defaultStyle } : parseStyled(mapText, url.href + '#importmap');
const { start, end } = tag;
const attrs = toHtmlAttrs(source, tag.attributes);
let lastChar = tag.innerEnd;
while (isWs(source.charCodeAt(--lastChar)));
analysis.newlineTab = detectIndent(source, lastChar + 1);
analysis.map = { json, style, start, end, attrs, newScript: false };
createdInjectionPoint = true;
}
else if (type === 'module') {
const src = getAttr(source, tag, 'src');
Expand Down Expand Up @@ -120,10 +127,16 @@ export function analyzeHtml (source: string, url: URL = baseUrl): HtmlAnalysis {
}
}
}
if (!analysis.map.json) createInjectionPoint(source, analysis.map, tag, analysis);
if (!createdInjectionPoint) {
createInjectionPoint(source, analysis.map, tag, analysis);
createdInjectionPoint = true;
}
break;
case 'link':
if (!analysis.map.json) createInjectionPoint(source, analysis.map, tag, analysis);
if (!createdInjectionPoint) {
createInjectionPoint(source, analysis.map, tag, analysis);
createdInjectionPoint = true;
}
if (getAttr(source, tag, 'rel') === 'modulepreload') {
const { start, end } = tag;
const attrs = toHtmlAttrs(source, tag.attributes);
Expand Down
22 changes: 22 additions & 0 deletions test/html/emptymap.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Generator } from '@jspm/generator';
import assert from 'assert';
import { SemverRange } from 'sver';

const generator = new Generator({
rootUrl: new URL('./local', import.meta.url),
env: ['production', 'browser']
});

const esmsPkg = await generator.traceMap.resolver.resolveLatestTarget({ name: 'es-module-shims', registry: 'npm', ranges: [new SemverRange('*')] }, false, generator.traceMap.installer.defaultProvider);
const esmsUrl = generator.traceMap.resolver.pkgToUrl(esmsPkg, generator.traceMap.installer.defaultProvider) + 'dist/es-module-shims.js';

assert.strictEqual(await generator.htmlGenerate(`<!DOCTYPE html>

<script type="importmap"></script>
`, { preload: true }), '<!DOCTYPE html>\n' +
'\n' +
'<!-- Generated by @jspm/generator - https://github.com/jspm/generator -->\n' +
`<script async src="${esmsUrl}" crossorigin="anonymous"></script>\n` +
'<script type="importmap">\n' +
'{}\n' +
'</script>\n');
49 changes: 49 additions & 0 deletions test/html/injectionpoint.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Generator } from '@jspm/generator';
import assert from 'assert';
import { SemverRange } from 'sver';

const generator = new Generator({
rootUrl: new URL('./local', import.meta.url),
env: ['production', 'browser']
});

const esmsPkg = await generator.traceMap.resolver.resolveLatestTarget({ name: 'es-module-shims', registry: 'npm', ranges: [new SemverRange('*')] }, false, generator.traceMap.installer.defaultProvider);
const esmsUrl = generator.traceMap.resolver.pkgToUrl(esmsPkg, generator.traceMap.installer.defaultProvider) + 'dist/es-module-shims.js';

assert.strictEqual(await generator.htmlGenerate(`<!DOCTYPE html>

<script type="module">
import "react";
</script>

<body>
<script type="text/plain">
Hello World
</script>
</body>`, { preload: true }), '<!DOCTYPE html>\n' +
'\n' +
'<!-- Generated by @jspm/generator - https://github.com/jspm/generator -->\n' +
`<script async src="${esmsUrl}" crossorigin="anonymous"></script>\n` +
'<script type="importmap">\n' +
'{\n' +
' "imports": {\n' +
' "react": "https://ga.jspm.io/npm:react@17.0.2/index.js"\n' +
' },\n' +
' "scopes": {\n' +
' "https://ga.jspm.io/": {\n' +
' "object-assign": "https://ga.jspm.io/npm:object-assign@4.1.1/index.js"\n' +
' }\n' +
' }\n' +
'}\n' +
'</script>\n' +
'<link rel="modulepreload" href="https://ga.jspm.io/npm:object-assign@4.1.1/index.js" />\n' +
'<link rel="modulepreload" href="https://ga.jspm.io/npm:react@17.0.2/index.js" />\n' +
'<script type="module">\n' +
' import "react";\n' +
'</script>\n' +
'\n' +
'<body>\n' +
' <script type="text/plain">\n' +
' Hello World\n' +
' </script>\n' +
'</body>');