feat(html): add support for HTML fragments (#2093)

master
Lucas Azzola 2017-06-11 13:10:34 +10:00 committed by Christopher Chedeau
parent b4f0ff346e
commit fdb7467de7
11 changed files with 138 additions and 37 deletions

View File

@ -6,7 +6,8 @@ function parse(text) {
// Inline the require to avoid loading all the JS if we don't use it
const parse5 = require("parse5");
try {
const ast = parse5.parse(text, {
const isFragment = !/^\s*<(!doctype|html|head|body)/i.test(text);
const ast = (isFragment ? parse5.parseFragment : parse5.parse)(text, {
treeAdapter: parse5.treeAdapters.htmlparser2,
locationInfo: true
});

View File

@ -1,5 +1,6 @@
"use strict";
const util = require("./util");
const docBuilders = require("./doc-builders");
const concat = docBuilders.concat;
const join = docBuilders.join;
@ -62,15 +63,24 @@ function genericPrint(path, options, print) {
children.push(childPath.call(print));
}, "children");
const hasNewline = util.hasNewlineInRange(
options.originalText,
util.locStart(n),
util.locEnd(n)
);
return group(
concat([
hasNewline ? hardline : "",
"<",
n.name,
printAttributes(n.attribs),
n.children.length ? ">" : selfClose,
indent(concat(children)),
n.name.toLowerCase() === "html"
? concat(children)
: indent(concat(children)),
n.children.length ? concat([softline, "</", n.name, ">"]) : ""
])
);

View File

@ -17,13 +17,27 @@ exports[`hello-world.html 1`] = `
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body><!-- A comment --><h1>Hello World</h1></body>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<!-- A comment -->
<h1>Hello World</h1>
</body>
</html>
`;
exports[`html-fragment.html 1`] = `
<a href="#">Link</a>
<textarea>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<a href="#">Link</a><textarea />
`;

View File

@ -0,0 +1,3 @@
<a href="#">Link</a>
<textarea>

View File

@ -15,14 +15,19 @@ exports[`html-with-css-style.html 1`] = `
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<!DOCTYPE html>
<html lang="en">
<head>
<style>
blink {
display: none;
}
</style>
</head>
<body></body>
<head>
<style>
blink {
display: none;
}
</style>
</head>
<body>
</body>
</html>
`;

View File

@ -14,13 +14,18 @@ exports[`html-with-js-script.html 1`] = `
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript">
hello("world");
</script>
</head>
<body></body>
<html lang="en">
<head>
<script type="text/javascript">
hello("world");
</script>
</head>
<body>
</body>
</html>
`;

View File

@ -23,16 +23,21 @@ exports[`html-with-ts-script.html 1`] = `
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<!DOCTYPE html>
<html lang="en">
<head>
<script lang="ts">
type X = { [K in keyof Y]: Partial<K> };
class Foo<T> {
constructor(private foo: keyof Apple) {}
}
</script>
</head>
<body></body>
<html lang="en">
<head>
<script lang="ts">
type X = { [K in keyof Y]: Partial<K> };
class Foo<T> {
constructor(private foo: keyof Apple) {}
}
</script>
</head>
<body>
</body>
</html>
`;

View File

@ -0,0 +1,41 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`vue-component.vue 1`] = `
<template >
<h1 >{{greeting}} world</h1 >
</template >
<script>
module . exports =
{data : function () {return {
greeting: "Hello"
}}
}
</script>
<style scoped >
p { font-size : 2em ; text-align : center ; }
</style >
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<template>
<h1>{{greeting}} world</h1>
</template>
<script>
module.exports = {
data: function() {
return {
greeting: "Hello"
};
}
};
</script>
<style scoped>
p {
font-size: 2em;
text-align: center;
}
</style>
`;

View File

@ -0,0 +1 @@
run_spec(__dirname, { parser: "parse5" });

View File

@ -0,0 +1,16 @@
<template >
<h1 >{{greeting}} world</h1 >
</template >
<script>
module . exports =
{data : function () {return {
greeting: "Hello"
}}
}
</script>
<style scoped >
p { font-size : 2em ; text-align : center ; }
</style >

View File

@ -14,12 +14,12 @@ const ALL_PARSERS = process.env["ALL_PARSERS"]
function run_spec(dirname, options, additionalParsers) {
fs.readdirSync(dirname).forEach(filename => {
const extension = extname(filename);
const path = dirname + "/" + filename;
if (
/^\.([jt]sx?|css|graphql|html)$/.test(extension) &&
extname(filename) !== ".snap" &&
fs.lstatSync(path).isFile() &&
filename !== "jsfmt.spec.js"
) {
const path = dirname + "/" + filename;
let rangeStart = 0;
let rangeEnd = Infinity;
const source = read(path)