Skip to content

Commit

Permalink
Markup: Added tokens inside DOCTYPE (#2349)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed May 19, 2020
1 parent 96a0116 commit 9c7bc82
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 11 deletions.
21 changes: 19 additions & 2 deletions components/prism-markup.js
Expand Up @@ -2,8 +2,24 @@ Prism.languages.markup = {
'comment': /<!--[\s\S]*?-->/,
'prolog': /<\?[\s\S]+?\?>/,
'doctype': {
// https://www.w3.org/TR/xml/#NT-doctypedecl
pattern: /<!DOCTYPE(?:[^>"'[\]]|"[^"]*"|'[^']*')+(?:\[(?:[^<"'\]]|"[^"]*"|'[^']*'|<(?!!--)|<!--(?:[^-]|-(?!->))*-->)*\]\s*)?>/i,
greedy: true
greedy: true,
inside: {
'internal-subset': {
pattern: /(\[)[\s\S]+(?=\]>$)/,
lookbehind: true,
greedy: true,
inside: null // see below
},
'string': {
pattern: /"[^"]*"|'[^']*'/,
greedy: true
},
'punctuation': /^<!|>$|[[\]]/,
'doctype-tag': /^DOCTYPE/,
'name': /[^\s<>'"]+/
}
},
'cdata': /<!\[CDATA\[[\s\S]*?]]>/i,
'tag': {
Expand Down Expand Up @@ -50,9 +66,10 @@ Prism.languages.markup = {

Prism.languages.markup['tag'].inside['attr-value'].inside['entity'] =
Prism.languages.markup['entity'];
Prism.languages.markup['doctype'].inside['internal-subset'].inside = Prism.languages.markup;

// Plugin to make entity title show the real entity, idea by Roman Komarov
Prism.hooks.add('wrap', function(env) {
Prism.hooks.add('wrap', function (env) {

if (env.type === 'entity') {
env.attributes['title'] = env.content.replace(/&amp;/, '&');
Expand Down
2 changes: 1 addition & 1 deletion components/prism-markup.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 19 additions & 2 deletions prism.js
Expand Up @@ -738,8 +738,24 @@ Prism.languages.markup = {
'comment': /<!--[\s\S]*?-->/,
'prolog': /<\?[\s\S]+?\?>/,
'doctype': {
// https://www.w3.org/TR/xml/#NT-doctypedecl
pattern: /<!DOCTYPE(?:[^>"'[\]]|"[^"]*"|'[^']*')+(?:\[(?:[^<"'\]]|"[^"]*"|'[^']*'|<(?!!--)|<!--(?:[^-]|-(?!->))*-->)*\]\s*)?>/i,
greedy: true
greedy: true,
inside: {
'internal-subset': {
pattern: /(\[)[\s\S]+(?=\]>$)/,
lookbehind: true,
greedy: true,
inside: null // see below
},
'string': {
pattern: /"[^"]*"|'[^']*'/,
greedy: true
},
'punctuation': /^<!|>$|[[\]]/,
'doctype-tag': /^DOCTYPE/,
'name': /[^\s<>'"]+/
}
},
'cdata': /<!\[CDATA\[[\s\S]*?]]>/i,
'tag': {
Expand Down Expand Up @@ -786,9 +802,10 @@ Prism.languages.markup = {

Prism.languages.markup['tag'].inside['attr-value'].inside['entity'] =
Prism.languages.markup['entity'];
Prism.languages.markup['doctype'].inside['internal-subset'].inside = Prism.languages.markup;

// Plugin to make entity title show the real entity, idea by Roman Komarov
Prism.hooks.add('wrap', function(env) {
Prism.hooks.add('wrap', function (env) {

if (env.type === 'entity') {
env.attributes['title'] = env.content.replace(/&amp;/, '&');
Expand Down
97 changes: 91 additions & 6 deletions tests/languages/markup/doctype_feature.test
Expand Up @@ -15,12 +15,97 @@
----------------------------------------------------

[
["doctype", "<!DOCTYPE html>"],
["doctype", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">"],
["doctype", "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\r\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"],
["doctype", "<!DOCTYPE greeting SYSTEM \"hello.dtd\">"],
["doctype", "<!DOCTYPE greeting [\r\n\t<!ELEMENT greeting (#PCDATA)>\r\n]>"],
["doctype", "<!DOCTYPE greeting [\r\n\t<!ELEMENT greeting (#PCDATA)>\r\n\t<!ELEMENT subject (#PCDATA)>\r\n\t<!-- comment ]> -->\r\n]>"]
["doctype", [
["punctuation", "<!"],
["doctype-tag", "DOCTYPE"],
["name", "html"],
["punctuation", ">"]
]],
["doctype", [
["punctuation", "<!"],
["doctype-tag", "DOCTYPE"],
["name", "HTML"],
["name", "PUBLIC"],
["string", "\"-//W3C//DTD HTML 4.01//EN\""],
["punctuation", ">"]
]],
["doctype", [
["punctuation", "<!"],
["doctype-tag", "DOCTYPE"],
["name", "html"],
["name", "PUBLIC"],
["string", "\"-//W3C//DTD XHTML 1.0 Strict//EN\""],
["string", "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\""],
["punctuation", ">"]
]],
["doctype", [
["punctuation", "<!"],
["doctype-tag", "DOCTYPE"],
["name", "greeting"],
["name", "SYSTEM"],
["string", "\"hello.dtd\""],
["punctuation", ">"]
]],
["doctype", [
["punctuation", "<!"],
["doctype-tag", "DOCTYPE"],
["name", "greeting"],
["punctuation", "["],
["internal-subset", [
["tag", [
["tag", [
["punctuation", "<"],
"!ELEMENT"
]],
["attr-name", [
"greeting"
]],
["attr-name", [
"(#PCDATA)"
]],
["punctuation", ">"]
]]
]],
["punctuation", "]"],
["punctuation", ">"]
]],
["doctype", [
["punctuation", "<!"],
["doctype-tag", "DOCTYPE"],
["name", "greeting"],
["punctuation", "["],
["internal-subset", [
["tag", [
["tag", [
["punctuation", "<"],
"!ELEMENT"
]],
["attr-name", [
"greeting"
]],
["attr-name", [
"(#PCDATA)"
]],
["punctuation", ">"]
]],
["tag", [
["tag", [
["punctuation", "<"],
"!ELEMENT"
]],
["attr-name", [
"subject"
]],
["attr-name", [
"(#PCDATA)"
]],
["punctuation", ">"]
]],
["comment", "<!-- comment ]> -->"]
]],
["punctuation", "]"],
["punctuation", ">"]
]]
]

----------------------------------------------------
Expand Down

0 comments on commit 9c7bc82

Please sign in to comment.