Make doctype be case insensitive (#419)
* Make doctype be case insensitive Fixes #413 * Make doctype completely case insensitive * Make check for style scoping doctype override be case insensitive * Check for doctype in the right place
This commit is contained in:
parent
0ef0c99b10
commit
0b16b2ddd0
6 changed files with 26 additions and 8 deletions
|
@ -9,7 +9,7 @@ export default function (_opts: { filename: string; fileID: string }): Transform
|
||||||
html: {
|
html: {
|
||||||
Element: {
|
Element: {
|
||||||
enter(node, parent, _key, index) {
|
enter(node, parent, _key, index) {
|
||||||
if (node.name === '!doctype') {
|
if (node.name.toLowerCase() === '!doctype') {
|
||||||
hasDoctype = true;
|
hasDoctype = true;
|
||||||
}
|
}
|
||||||
if (node.name === 'html' && !hasDoctype) {
|
if (node.name === 'html' && !hasDoctype) {
|
||||||
|
|
|
@ -28,8 +28,7 @@ export const NEVER_SCOPED_TAGS = new Set<string>([
|
||||||
'noscript',
|
'noscript',
|
||||||
'script',
|
'script',
|
||||||
'style',
|
'style',
|
||||||
'title',
|
'title'
|
||||||
'!doctype',
|
|
||||||
]);
|
]);
|
||||||
/**
|
/**
|
||||||
* Scope Rules
|
* Scope Rules
|
||||||
|
|
|
@ -4,13 +4,12 @@ import type { TemplateNode } from '@astrojs/parser';
|
||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
import { createRequire } from 'module';
|
import { createRequire } from 'module';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { fileURLToPath } from 'url';
|
|
||||||
import autoprefixer from 'autoprefixer';
|
import autoprefixer from 'autoprefixer';
|
||||||
import postcss, { Plugin } from 'postcss';
|
import postcss, { Plugin } from 'postcss';
|
||||||
import postcssKeyframes from 'postcss-icss-keyframes';
|
import postcssKeyframes from 'postcss-icss-keyframes';
|
||||||
import findUp from 'find-up';
|
import findUp from 'find-up';
|
||||||
import sass from 'sass';
|
import sass from 'sass';
|
||||||
import { debug, error, LogOptions } from '../../logger.js';
|
import { error, LogOptions } from '../../logger.js';
|
||||||
import astroScopedStyles, { NEVER_SCOPED_TAGS } from './postcss-scoped-styles/index.js';
|
import astroScopedStyles, { NEVER_SCOPED_TAGS } from './postcss-scoped-styles/index.js';
|
||||||
import slash from 'slash';
|
import slash from 'slash';
|
||||||
|
|
||||||
|
@ -222,7 +221,9 @@ export default function transformStyles({ compileOptions, filename, fileID }: Tr
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. add scoped HTML classes
|
// 2. add scoped HTML classes
|
||||||
if (NEVER_SCOPED_TAGS.has(node.name)) return; // only continue if this is NOT a <script> tag, etc.
|
if (NEVER_SCOPED_TAGS.has(node.name) || node.name.toLowerCase() === '!doctype') {
|
||||||
|
return; // only continue if this is NOT a <script> tag, etc.
|
||||||
|
}
|
||||||
// Note: currently we _do_ scope web components/custom elements. This seems correct?
|
// Note: currently we _do_ scope web components/custom elements. This seems correct?
|
||||||
|
|
||||||
injectScopedClassAttribute(node, scopedClass);
|
injectScopedClassAttribute(node, scopedClass);
|
||||||
|
|
|
@ -7,8 +7,8 @@ const voidTags = new Set(['area', 'base', 'br', 'col', 'command', 'embed', 'hr',
|
||||||
|
|
||||||
/** Generator for primary h() function */
|
/** Generator for primary h() function */
|
||||||
function* _h(tag: string, attrs: HProps, children: Array<HChild>) {
|
function* _h(tag: string, attrs: HProps, children: Array<HChild>) {
|
||||||
if (tag === '!doctype') {
|
if (tag.toLowerCase() === '!doctype') {
|
||||||
yield '<!doctype ';
|
yield `<${tag} `;
|
||||||
if (attrs) {
|
if (attrs) {
|
||||||
yield Object.keys(attrs).join(' ');
|
yield Object.keys(attrs).join(' ');
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,4 +56,13 @@ DType.skip('Preserves user provided doctype', async () => {
|
||||||
assert.ok(html.startsWith('<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'), 'Doctype included was preserved');
|
assert.ok(html.startsWith('<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'), 'Doctype included was preserved');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
DType('User provided doctype is case insensitive', async () => {
|
||||||
|
const result = await runtime.load('/capital');
|
||||||
|
if (result.error) throw new Error(result.error);
|
||||||
|
|
||||||
|
const html = result.contents.toString('utf-8');
|
||||||
|
assert.ok(html.startsWith('<!DOCTYPE html>'), 'Doctype left alone');
|
||||||
|
assert.not.ok(html.includes('</!DOCTYPE>'), 'There should not be a closing tag');
|
||||||
|
});
|
||||||
|
|
||||||
DType.run();
|
DType.run();
|
||||||
|
|
9
packages/astro/test/fixtures/astro-doctype/src/pages/capital.astro
vendored
Normal file
9
packages/astro/test/fixtures/astro-doctype/src/pages/capital.astro
vendored
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
---
|
||||||
|
let title = 'My Site';
|
||||||
|
---
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head><title>{title}</title></head>
|
||||||
|
<body><h1>Hello world</h1></body>
|
||||||
|
</html>
|
Loading…
Add table
Reference in a new issue