diff --git a/components/prism-http.js b/components/prism-http.js index 306bcd8c5d..6c3e20106f 100644 --- a/components/prism-http.js +++ b/components/prism-http.js @@ -1,50 +1,79 @@ -Prism.languages.http = { - 'request-line': { - pattern: /^(?:POST|GET|PUT|DELETE|OPTIONS|PATCH|TRACE|CONNECT)\s(?:https?:\/\/|\/)\S+\sHTTP\/[0-9.]+/m, - inside: { - // HTTP Verb - property: /^(?:POST|GET|PUT|DELETE|OPTIONS|PATCH|TRACE|CONNECT)\b/, - // Path or query argument - 'attr-name': /:\w+/ - } - }, - 'response-status': { - pattern: /^HTTP\/1.[01] \d+.*/m, - inside: { - // Status, e.g. 200 OK - property: { - pattern: /(^HTTP\/1.[01] )\d+.*/i, - lookbehind: true - } +(function (Prism) { + Prism.languages.http = { + 'request-line': { + pattern: /^(?:POST|GET|PUT|DELETE|OPTIONS|PATCH|TRACE|CONNECT)\s(?:https?:\/\/|\/)\S+\sHTTP\/[0-9.]+/m, + inside: { + // HTTP Verb + 'property': /^(?:POST|GET|PUT|DELETE|OPTIONS|PATCH|TRACE|CONNECT)\b/, + // Path or query argument + 'attr-name': /:\w+/ + } + }, + 'response-status': { + pattern: /^HTTP\/1.[01] \d+.*/m, + inside: { + // Status, e.g. 200 OK + 'property': { + pattern: /(^HTTP\/1.[01] )\d+.*/i, + lookbehind: true + } + } + }, + // HTTP header name + 'header-name': { + pattern: /^[\w-]+:(?=.)/m, + alias: 'keyword' } - }, - // HTTP header name - 'header-name': { - pattern: /^[\w-]+:(?=.)/m, - alias: 'keyword' - } -}; + }; -// Create a mapping of Content-Type headers to language definitions -var httpLanguages = { - 'application/json': Prism.languages.javascript, - 'application/xml': Prism.languages.markup, - 'text/xml': Prism.languages.markup, - 'text/html': Prism.languages.markup -}; + // Create a mapping of Content-Type headers to language definitions + var langs = Prism.languages; + var httpLanguages = { + 'application/javascript': langs.javascript, + 'application/json': langs.json || langs.javascript, + 'application/xml': langs.xml, + 'text/xml': langs.xml, + 'text/html': langs.html, + 'text/css': langs.css + }; -// Insert each content type parser that has its associated language -// currently loaded. -for (var contentType in httpLanguages) { - if (httpLanguages[contentType]) { - var options = {}; - options[contentType] = { - pattern: RegExp('(content-type:\\s*' + contentType + '[\\w\\W]*?)(?:\\r?\\n|\\r){2}[\\w\\W]*', 'i'), - lookbehind: true, - inside: { - rest: httpLanguages[contentType] - } - }; + // Declare which types can also be suffixes + var suffixTypes = { + 'application/json': true, + 'application/xml': true + }; + + /** + * Returns a pattern for the given content type which matches it and any type which has it as a suffix. + * + * @param {string} contentType + * @returns {string} + */ + function getSuffixPattern(contentType) { + var suffix = contentType.replace(/^[a-z]+\//, ''); + var suffixPattern = '\\w+/(?:[\\w.-]+\\+)+' + suffix + '(?![+\\w.-])'; + return '(?:' + contentType + '|' + suffixPattern + ')'; + } + + // Insert each content type parser that has its associated language + // currently loaded. + var options; + for (var contentType in httpLanguages) { + if (httpLanguages[contentType]) { + options = options || {}; + + var pattern = suffixTypes[contentType] ? getSuffixPattern(contentType) : contentType; + options[contentType] = { + pattern: RegExp('(content-type:\\s*' + pattern + '[\\s\\S]*?)(?:\\r?\\n|\\r){2}[\\s\\S]*', 'i'), + lookbehind: true, + inside: { + rest: httpLanguages[contentType] + } + }; + } + } + if (options) { Prism.languages.insertBefore('http', 'header-name', options); } -} + +}(Prism)); diff --git a/components/prism-http.min.js b/components/prism-http.min.js index 8b2c7eed07..0e4efcc26a 100644 --- a/components/prism-http.min.js +++ b/components/prism-http.min.js @@ -1 +1 @@ -Prism.languages.http={"request-line":{pattern:/^(?:POST|GET|PUT|DELETE|OPTIONS|PATCH|TRACE|CONNECT)\s(?:https?:\/\/|\/)\S+\sHTTP\/[0-9.]+/m,inside:{property:/^(?:POST|GET|PUT|DELETE|OPTIONS|PATCH|TRACE|CONNECT)\b/,"attr-name":/:\w+/}},"response-status":{pattern:/^HTTP\/1.[01] \d+.*/m,inside:{property:{pattern:/(^HTTP\/1.[01] )\d+.*/i,lookbehind:!0}}},"header-name":{pattern:/^[\w-]+:(?=.)/m,alias:"keyword"}};var httpLanguages={"application/json":Prism.languages.javascript,"application/xml":Prism.languages.markup,"text/xml":Prism.languages.markup,"text/html":Prism.languages.markup};for(var contentType in httpLanguages)if(httpLanguages[contentType]){var options={};options[contentType]={pattern:RegExp("(content-type:\\s*"+contentType+"[\\w\\W]*?)(?:\\r?\\n|\\r){2}[\\w\\W]*","i"),lookbehind:!0,inside:{rest:httpLanguages[contentType]}},Prism.languages.insertBefore("http","header-name",options)} \ No newline at end of file +!function(t){function a(t){var a=t.replace(/^[a-z]+\//,""),e="\\w+/(?:[\\w.-]+\\+)+"+a+"(?![+\\w.-])";return"(?:"+t+"|"+e+")"}t.languages.http={"request-line":{pattern:/^(?:POST|GET|PUT|DELETE|OPTIONS|PATCH|TRACE|CONNECT)\s(?:https?:\/\/|\/)\S+\sHTTP\/[0-9.]+/m,inside:{property:/^(?:POST|GET|PUT|DELETE|OPTIONS|PATCH|TRACE|CONNECT)\b/,"attr-name":/:\w+/}},"response-status":{pattern:/^HTTP\/1.[01] \d+.*/m,inside:{property:{pattern:/(^HTTP\/1.[01] )\d+.*/i,lookbehind:!0}}},"header-name":{pattern:/^[\w-]+:(?=.)/m,alias:"keyword"}};var e,n=t.languages,r={"application/javascript":n.javascript,"application/json":n.json||n.javascript,"application/xml":n.xml,"text/xml":n.xml,"text/html":n.html,"text/css":n.css},i={"application/json":!0,"application/xml":!0};for(var s in r)if(r[s]){e=e||{};var p=i[s]?a(s):s;e[s]={pattern:RegExp("(content-type:\\s*"+p+"[\\s\\S]*?)(?:\\r?\\n|\\r){2}[\\s\\S]*","i"),lookbehind:!0,inside:{rest:r[s]}}}e&&t.languages.insertBefore("http","header-name",e)}(Prism); \ No newline at end of file diff --git a/tests/languages/css+http/css_inclusion.test b/tests/languages/css+http/css_inclusion.test new file mode 100644 index 0000000000..f805d6d4db --- /dev/null +++ b/tests/languages/css+http/css_inclusion.test @@ -0,0 +1,25 @@ +Content-type: text/css + +a.link:hover { + color: red; +} + +---------------------------------------------------- + +[ + ["header-name", "Content-type:"], + " text/css", + ["text/css", [ + ["selector", "a.link:hover"], + ["punctuation", "{"], + ["property", "color"], + ["punctuation", ":"], + " red", + ["punctuation", ";"], + ["punctuation", "}"] + ]] +] + +---------------------------------------------------- + +Checks for JavaScript content in HTTP. \ No newline at end of file diff --git a/tests/languages/javascript+http/javascript_inclusion.test b/tests/languages/javascript+http/javascript_inclusion.test index 825fea49d5..f7977c5545 100644 --- a/tests/languages/javascript+http/javascript_inclusion.test +++ b/tests/languages/javascript+http/javascript_inclusion.test @@ -1,21 +1,21 @@ -Content-type: application/json +Content-type: application/javascript -{"foo":"bar"} +var a = true; ---------------------------------------------------- [ ["header-name", "Content-type:"], - " application/json", - ["application/json", [ - ["punctuation", "{"], - ["string", "\"foo\""], - ["punctuation", ":"], - ["string", "\"bar\""], - ["punctuation", "}"] + " application/javascript", + ["application/javascript", [ + ["keyword", "var"], + " a ", + ["operator", "="], + ["boolean", "true"], + ["punctuation", ";"] ]] ] ---------------------------------------------------- -Checks for JSON content in HTTP. \ No newline at end of file +Checks for JavaScript content in HTTP. \ No newline at end of file diff --git a/tests/languages/json+http/json-suffix_inclusion.test b/tests/languages/json+http/json-suffix_inclusion.test new file mode 100644 index 0000000000..b24009783c --- /dev/null +++ b/tests/languages/json+http/json-suffix_inclusion.test @@ -0,0 +1,21 @@ +Content-type: application/x.foo+bar+json + +{"foo":"bar"} + +---------------------------------------------------- + +[ + ["header-name", "Content-type:"], + " application/x.foo+bar+json", + ["application/json", [ + ["punctuation", "{"], + ["property", "\"foo\""], + ["operator", ":"], + ["string", "\"bar\""], + ["punctuation", "}"] + ]] +] + +---------------------------------------------------- + +Checks for content with JSON suffix in HTTP. \ No newline at end of file diff --git a/tests/languages/json+http/json_inclusion.test b/tests/languages/json+http/json_inclusion.test new file mode 100644 index 0000000000..2282637f57 --- /dev/null +++ b/tests/languages/json+http/json_inclusion.test @@ -0,0 +1,21 @@ +Content-type: application/json + +{"foo":"bar"} + +---------------------------------------------------- + +[ + ["header-name", "Content-type:"], + " application/json", + ["application/json", [ + ["punctuation", "{"], + ["property", "\"foo\""], + ["operator", ":"], + ["string", "\"bar\""], + ["punctuation", "}"] + ]] +] + +---------------------------------------------------- + +Checks for JSON content in HTTP. \ No newline at end of file diff --git a/tests/languages/markup+http/html_inclusion.test b/tests/languages/markup+http/html_inclusion.test new file mode 100644 index 0000000000..b5f067c77d --- /dev/null +++ b/tests/languages/markup+http/html_inclusion.test @@ -0,0 +1,30 @@ +Content-type: text/html + + + +---------------------------------------------------- + +[ + ["header-name", "Content-type:"], + " text/html", + ["text/html", [ + ["tag", [ + ["tag", [ + ["punctuation", "<"], + "b" + ]], + ["punctuation", ">"] + ]], + ["tag", [ + ["tag", [ + ["punctuation", ""] + ]] + ]] +] + +---------------------------------------------------- + +Checks for HTML content in HTTP. \ No newline at end of file diff --git a/tests/languages/markup+http/xml-suffix_inclusion.test b/tests/languages/markup+http/xml-suffix_inclusion.test new file mode 100644 index 0000000000..589b9ed21b --- /dev/null +++ b/tests/languages/markup+http/xml-suffix_inclusion.test @@ -0,0 +1,30 @@ +Content-type: text/x.anything+something-else+xml + + + +---------------------------------------------------- + +[ + ["header-name", "Content-type:"], + " text/x.anything+something-else+xml", + ["application/xml", [ + ["tag", [ + ["tag", [ + ["punctuation", "<"], + "foo" + ]], + ["punctuation", ">"] + ]], + ["tag", [ + ["tag", [ + ["punctuation", ""] + ]] + ]] +] + +---------------------------------------------------- + +Checks for content with XML suffix in HTTP. \ No newline at end of file diff --git a/tests/languages/markup+http/markup_inclusion.test b/tests/languages/markup+http/xml_inclusion.test similarity index 68% rename from tests/languages/markup+http/markup_inclusion.test rename to tests/languages/markup+http/xml_inclusion.test index 77457709bc..89372ea680 100644 --- a/tests/languages/markup+http/markup_inclusion.test +++ b/tests/languages/markup+http/xml_inclusion.test @@ -16,12 +16,12 @@ Content-type: application/xml ["punctuation", ">"] ]], ["tag", [ - ["tag", [ - ["punctuation", ""] - ]] + ["tag", [ + ["punctuation", ""] + ]] ]] ]