Skip to content

Commit

Permalink
Markup: Improved doctype pattern (#2094)
Browse files Browse the repository at this point in the history
This changes Markup's `doctype` pattern to adhere to the XML spec more closely.
  • Loading branch information
RunDevelopment committed Oct 19, 2019
1 parent f460eaf commit 99994c5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
5 changes: 4 additions & 1 deletion components/prism-markup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
Prism.languages.markup = {
'comment': /<!--[\s\S]*?-->/,
'prolog': /<\?[\s\S]+?\?>/,
'doctype': /<!DOCTYPE[\s\S]+?>/i,
'doctype': {
pattern: /<!DOCTYPE(?:[^>"'[\]]|"[^"]*"|'[^']*')+(?:\[(?:(?!<!--)[^"'\]]|"[^"]*"|'[^']*'|<!--[\s\S]*?-->)*\]\s*)?>/i,
greedy: true
},
'cdata': /<!\[CDATA\[[\s\S]*?]]>/i,
'tag': {
pattern: /<\/?(?!\d)[^\s>\/=$<%]+(?:\s(?:\s*[^\s>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?\s*\/?>/i,
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.

5 changes: 4 additions & 1 deletion prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,10 @@ if (typeof global !== 'undefined') {
Prism.languages.markup = {
'comment': /<!--[\s\S]*?-->/,
'prolog': /<\?[\s\S]+?\?>/,
'doctype': /<!DOCTYPE[\s\S]+?>/i,
'doctype': {
pattern: /<!DOCTYPE(?:[^>"'[\]]|"[^"]*"|'[^']*')+(?:\[(?:(?!<!--)[^"'\]]|"[^"]*"|'[^']*'|<!--[\s\S]*?-->)*\]\s*)?>/i,
greedy: true
},
'cdata': /<!\[CDATA\[[\s\S]*?]]>/i,
'tag': {
pattern: /<\/?(?!\d)[^\s>\/=$<%]+(?:\s(?:\s*[^\s>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?\s*\/?>/i,
Expand Down
16 changes: 14 additions & 2 deletions tests/languages/markup/doctype_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!DOCTYPE greeting SYSTEM "hello.dtd">
<!DOCTYPE greeting [
<!ELEMENT greeting (#PCDATA)>
]>
<!DOCTYPE greeting [
<!ELEMENT greeting (#PCDATA)>
<!ELEMENT subject (#PCDATA)>
<!-- comment ]> -->
]>

----------------------------------------------------

[
["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 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]>"]
]

----------------------------------------------------

Checks for different doctypes, single-line and multi-line.
Checks for different doctypes, single-line and multi-line.

0 comments on commit 99994c5

Please sign in to comment.