Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP: Improved content highlighting #1598

Merged
merged 2 commits into from
Dec 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 74 additions & 45 deletions components/prism-http.js
Original file line number Diff line number Diff line change
@@ -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));
2 changes: 1 addition & 1 deletion components/prism-http.min.js

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

25 changes: 25 additions & 0 deletions tests/languages/css+http/css_inclusion.test
Original file line number Diff line number Diff line change
@@ -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.
20 changes: 10 additions & 10 deletions tests/languages/javascript+http/javascript_inclusion.test
Original file line number Diff line number Diff line change
@@ -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.
Checks for JavaScript content in HTTP.
21 changes: 21 additions & 0 deletions tests/languages/json+http/json-suffix_inclusion.test
Original file line number Diff line number Diff line change
@@ -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.
21 changes: 21 additions & 0 deletions tests/languages/json+http/json_inclusion.test
Original file line number Diff line number Diff line change
@@ -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.
30 changes: 30 additions & 0 deletions tests/languages/markup+http/html_inclusion.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Content-type: text/html

<b></b>

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

[
["header-name", "Content-type:"],
" text/html",
["text/html", [
["tag", [
["tag", [
["punctuation", "<"],
"b"
]],
["punctuation", ">"]
]],
["tag", [
["tag", [
["punctuation", "</"],
"b"
]],
["punctuation", ">"]
]]
]]
]

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

Checks for HTML content in HTTP.
30 changes: 30 additions & 0 deletions tests/languages/markup+http/xml-suffix_inclusion.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Content-type: text/x.anything+something-else+xml

<foo></foo>

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

[
["header-name", "Content-type:"],
" text/x.anything+something-else+xml",
["application/xml", [
["tag", [
["tag", [
["punctuation", "<"],
"foo"
]],
["punctuation", ">"]
]],
["tag", [
["tag", [
["punctuation", "</"],
"foo"
]],
["punctuation", ">"]
]]
]]
]

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

Checks for content with XML suffix in HTTP.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ Content-type: application/xml
["punctuation", ">"]
]],
["tag", [
["tag", [
["punctuation", "</"],
"foo"
]],
["punctuation", ">"]
]]
["tag", [
["punctuation", "</"],
"foo"
]],
["punctuation", ">"]
]]
]]
]

Expand Down