summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Matthew Phillips <matthew@matthewphillips.info> 2021-06-14 12:00:51 -0400
committerGravatar GitHub <noreply@github.com> 2021-06-14 12:00:51 -0400
commit0b16b2ddd0e3f0bf9ac74d7c801c9f09dbd40a1a (patch)
tree5562b08e7d6ca9d7f9cca763ce75e4215a9c7958
parent0ef0c99b101c2010c3b8f564b9f2d522a39826a0 (diff)
downloadastro-0b16b2ddd0e3f0bf9ac74d7c801c9f09dbd40a1a.tar.gz
astro-0b16b2ddd0e3f0bf9ac74d7c801c9f09dbd40a1a.tar.zst
astro-0b16b2ddd0e3f0bf9ac74d7c801c9f09dbd40a1a.zip
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
-rw-r--r--packages/astro/src/compiler/transform/doctype.ts2
-rw-r--r--packages/astro/src/compiler/transform/postcss-scoped-styles/index.ts3
-rw-r--r--packages/astro/src/compiler/transform/styles.ts7
-rw-r--r--packages/astro/src/internal/h.ts4
-rw-r--r--packages/astro/test/astro-doctype.test.js9
-rw-r--r--packages/astro/test/fixtures/astro-doctype/src/pages/capital.astro9
6 files changed, 26 insertions, 8 deletions
diff --git a/packages/astro/src/compiler/transform/doctype.ts b/packages/astro/src/compiler/transform/doctype.ts
index 1a0ff39e1..7647c205e 100644
--- a/packages/astro/src/compiler/transform/doctype.ts
+++ b/packages/astro/src/compiler/transform/doctype.ts
@@ -9,7 +9,7 @@ export default function (_opts: { filename: string; fileID: string }): Transform
html: {
Element: {
enter(node, parent, _key, index) {
- if (node.name === '!doctype') {
+ if (node.name.toLowerCase() === '!doctype') {
hasDoctype = true;
}
if (node.name === 'html' && !hasDoctype) {
diff --git a/packages/astro/src/compiler/transform/postcss-scoped-styles/index.ts b/packages/astro/src/compiler/transform/postcss-scoped-styles/index.ts
index 63ac9fd8d..7db630764 100644
--- a/packages/astro/src/compiler/transform/postcss-scoped-styles/index.ts
+++ b/packages/astro/src/compiler/transform/postcss-scoped-styles/index.ts
@@ -28,8 +28,7 @@ export const NEVER_SCOPED_TAGS = new Set<string>([
'noscript',
'script',
'style',
- 'title',
- '!doctype',
+ 'title'
]);
/**
* Scope Rules
diff --git a/packages/astro/src/compiler/transform/styles.ts b/packages/astro/src/compiler/transform/styles.ts
index 1e459bb7f..e1199e9a6 100644
--- a/packages/astro/src/compiler/transform/styles.ts
+++ b/packages/astro/src/compiler/transform/styles.ts
@@ -4,13 +4,12 @@ import type { TemplateNode } from '@astrojs/parser';
import crypto from 'crypto';
import { createRequire } from 'module';
import path from 'path';
-import { fileURLToPath } from 'url';
import autoprefixer from 'autoprefixer';
import postcss, { Plugin } from 'postcss';
import postcssKeyframes from 'postcss-icss-keyframes';
import findUp from 'find-up';
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 slash from 'slash';
@@ -222,7 +221,9 @@ export default function transformStyles({ compileOptions, filename, fileID }: Tr
}
// 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?
injectScopedClassAttribute(node, scopedClass);
diff --git a/packages/astro/src/internal/h.ts b/packages/astro/src/internal/h.ts
index bcfb4833f..07420a353 100644
--- a/packages/astro/src/internal/h.ts
+++ b/packages/astro/src/internal/h.ts
@@ -7,8 +7,8 @@ const voidTags = new Set(['area', 'base', 'br', 'col', 'command', 'embed', 'hr',
/** Generator for primary h() function */
function* _h(tag: string, attrs: HProps, children: Array<HChild>) {
- if (tag === '!doctype') {
- yield '<!doctype ';
+ if (tag.toLowerCase() === '!doctype') {
+ yield `<${tag} `;
if (attrs) {
yield Object.keys(attrs).join(' ');
}
diff --git a/packages/astro/test/astro-doctype.test.js b/packages/astro/test/astro-doctype.test.js
index 0db0135ae..d5b863992 100644
--- a/packages/astro/test/astro-doctype.test.js
+++ b/packages/astro/test/astro-doctype.test.js
@@ -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');
});
+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();
diff --git a/packages/astro/test/fixtures/astro-doctype/src/pages/capital.astro b/packages/astro/test/fixtures/astro-doctype/src/pages/capital.astro
new file mode 100644
index 000000000..583de2c34
--- /dev/null
+++ b/packages/astro/test/fixtures/astro-doctype/src/pages/capital.astro
@@ -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> \ No newline at end of file