Skip to content

Commit

Permalink
Fixes prettier#5
Browse files Browse the repository at this point in the history
  • Loading branch information
rart committed Dec 16, 2019
1 parent 369ec70 commit f230921
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ const endTagPattern = `((\\/)${tagNamePattern}\\s*>)`;

const commentPattern = "(!--)([\\s\\S\\n]*?)-->";
const declPattern = "((\\?xml)(-model)?)(.+)\\?>";
const docTypePattern = "(!(doctype|DOCTYPE))";

const tagPattern = `<(${cDataPattern}|${startTagPattern}|${endTagPattern}|${commentPattern}|${declPattern})([^<]*)`;
const tagPattern = `<(${cDataPattern}|${startTagPattern}|${endTagPattern}|${commentPattern}|${declPattern}|${docTypePattern})([^<]*)`;

class XMLNode {
constructor(tagname, opts) {
Expand Down Expand Up @@ -72,6 +73,16 @@ const parse = (text, _parsers, _opts) => {
locEnd: tag.index + tag[0].trim().length
})
);
} else if ((tag[21]||'').toLowerCase() === 'doctype') {
node.children.push(
new XMLNode(`!doctype`, {
parent: node,
value: tag[0].trim(),
attrs: tag[22].trim().replace(/>$/, ''),
locStart: tag.index,
locEnd: tag.index + tag[0].trim().length
})
);
} else if (tag[4] === "]]>") {
node.children.push(
new XMLNode("!cdata", {
Expand Down
2 changes: 1 addition & 1 deletion src/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const genericPrint = (path, opts, print) => {
return concat([join(hardline, path.map(print, "children")), hardline]);
}

if (tagname === "!comment") {
if (tagname === "!comment" || tagname === '!doctype') {
return group(concat([value]));
}

Expand Down
3 changes: 3 additions & 0 deletions test/doctype.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ describe("doctype", () => {
const content = here(`
<?xml version="1.0" encoding="UTF-8" ?>
<?xml-model href="model.rnc" type="application/relax-ng-compact-sync-syntax" ?>
<!DOCTYPE foo PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://docbook.org/xml/4.5/docbookx.dtd">
<!DOCTYPE foo PUBLIC "-//OASIS//DTD DITA Task//EN" "foo.dtd">
<foo />
`);

Expand Down

0 comments on commit f230921

Please sign in to comment.