From 7464938df423352b0322d2f02efa1383062d774c Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Thu, 20 Feb 2020 08:23:50 -0500 Subject: [PATCH 1/3] add(php-template) Explicit language to detect PHP templates (vs xml) - Adds a separate language to detect PHP templates to avoid overloading `xml` XML never should have built-in PHP support in the first place as there are any number of templating languages out there, and they all can't be built into XML. This rectifies that situation. --- CHANGES.md | 2 +- VERSION_10_BREAKING.md | 5 +++++ src/languages/php-template.js | 21 +++++++++++++++++++++ src/languages/php.js | 7 ++++++- src/languages/xml.js | 13 ------------- test/detect/php-template/default.txt | 8 ++++++++ 6 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 src/languages/php-template.js create mode 100644 test/detect/php-template/default.txt diff --git a/CHANGES.md b/CHANGES.md index 3bb58c9569..8c1cf896ec 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,7 +2,7 @@ New languages: -- none. +- (php-template) Explicit language to detect PHP templates (vs xml) [Josh Goebel][] New themes: diff --git a/VERSION_10_BREAKING.md b/VERSION_10_BREAKING.md index 0d67efda1f..11a0ac99a6 100644 --- a/VERSION_10_BREAKING.md +++ b/VERSION_10_BREAKING.md @@ -1,13 +1,16 @@ ## Version 10 Breaking Changes Incompatibilities: + - chore(parser): compressed version 9.x language definitions no longer supported (rebuild them minified) [Josh Goebel][] - `nohightlight` and `no-highlight` are the only "ignore me" css classes now (`plain` and `text` no longer count) (to get the old behavior you can customize `noHighlightRe`) - a grammar with a top-level `self` reference will now always throw an error (previously in safe mode this would be silently ignored) +- PHP templates are now detected as `php-template`, not `xml` Renamed Language Files: + - chore(parser): rename `nimrod.js` to `nim.js` [Josh Goebel][] - chore(parser): rename `cs.js` to `csharp.js` [Josh Goebel][] - chore(parser): rename `tex.js` to `latex.js` [Josh Goebel][] @@ -19,11 +22,13 @@ Renamed Language Files: (https://github.com/highlightjs/highlight.js/issues/2146) Legacy Browser Issues: + - **We're now using ES2015 features in the codebase. Internet Explorer 11 is no longer supported.** - In general legacy browsers are no longer supported. - chore(parser): remove `load` listener in favor of only the newer `DOMContentLoaded` [Josh Goebel][] Removed styles: + - chore(styles): darkula.css (use darcula.css instead) [Josh Goebel][] [Josh Goebel]: https://github.com/yyyc514 diff --git a/src/languages/php-template.js b/src/languages/php-template.js new file mode 100644 index 0000000000..8e28d0419f --- /dev/null +++ b/src/languages/php-template.js @@ -0,0 +1,21 @@ +/* +Language: PHP Template +Requires: xml.js, php.js +Author: Josh Goebel +Website: https://www.php.net +Category: common +*/ + +export default function(hljs) { + return { + name: "PHP template", + subLanguage: 'xml', + contains: [ + { + begin: /<\?(php|=)?/, + end: /\?>/, + subLanguage: 'php', + } + ] + }; +} diff --git a/src/languages/php.js b/src/languages/php.js index 99e0c0d436..7de2acd073 100644 --- a/src/languages/php.js +++ b/src/languages/php.js @@ -11,7 +11,12 @@ export default function(hljs) { begin: '\\$+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' }; var PREPROCESSOR = { - className: 'meta', begin: /<\?(php|=)?|\?>/ + className: 'meta', + variants: [ + { begin: /<\?php/, relevance: 10 }, // boost for obvious PHP + { begin: /<\?=/ }, + { begin: /\?>/ } // end php tag + ] }; var STRING = { className: 'string', diff --git a/src/languages/xml.js b/src/languages/xml.js index e3146e3082..509c4dbf45 100644 --- a/src/languages/xml.js +++ b/src/languages/xml.js @@ -96,19 +96,6 @@ export default function(hljs) { className: 'meta', begin: /<\?xml/, end: /\?>/, relevance: 10 }, - { - begin: /<\?(php)?/, end: /\?>/, - subLanguage: 'php', - contains: [ - // We don't want the php closing tag ?> to close the PHP block when - // inside any of the following blocks: - {begin: '/\\*', end: '\\*/', skip: true}, - {begin: 'b"', end: '"', skip: true}, - {begin: 'b\'', end: '\'', skip: true}, - hljs.inherit(hljs.APOS_STRING_MODE, {illegal: null, className: null, contains: null, skip: true}), - hljs.inherit(hljs.QUOTE_STRING_MODE, {illegal: null, className: null, contains: null, skip: true}) - ] - }, { className: 'tag', /* diff --git a/test/detect/php-template/default.txt b/test/detect/php-template/default.txt new file mode 100644 index 0000000000..d01ad4a791 --- /dev/null +++ b/test/detect/php-template/default.txt @@ -0,0 +1,8 @@ + + + + + From 42f3c301c276beaf03cce5e1688db47e2de59548 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Thu, 20 Feb 2020 08:28:07 -0500 Subject: [PATCH 2/3] prevent detecting incorret php end tag --- src/languages/php-template.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/languages/php-template.js b/src/languages/php-template.js index 8e28d0419f..dfebbf6be5 100644 --- a/src/languages/php-template.js +++ b/src/languages/php-template.js @@ -15,6 +15,15 @@ export default function(hljs) { begin: /<\?(php|=)?/, end: /\?>/, subLanguage: 'php', + contains: [ + // We don't want the php closing tag ?> to close the PHP block when + // inside any of the following blocks: + {begin: '/\\*', end: '\\*/', skip: true}, + {begin: 'b"', end: '"', skip: true}, + {begin: 'b\'', end: '\'', skip: true}, + hljs.inherit(hljs.APOS_STRING_MODE, {illegal: null, className: null, contains: null, skip: true}), + hljs.inherit(hljs.QUOTE_STRING_MODE, {illegal: null, className: null, contains: null, skip: true}) + ] } ] }; From 1eaa697a51e7351b35dfbfedea4bd7290dbafa19 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Sat, 22 Feb 2020 15:48:18 -0500 Subject: [PATCH 3/3] fix failing test --- src/languages/php.js | 2 +- test/fixtures/expect/sublanguages.txt | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/languages/php.js b/src/languages/php.js index 7de2acd073..e5c65981e8 100644 --- a/src/languages/php.js +++ b/src/languages/php.js @@ -14,7 +14,7 @@ export default function(hljs) { className: 'meta', variants: [ { begin: /<\?php/, relevance: 10 }, // boost for obvious PHP - { begin: /<\?=/ }, + { begin: /<\?[=]?/ }, { begin: /\?>/ } // end php tag ] }; diff --git a/test/fixtures/expect/sublanguages.txt b/test/fixtures/expect/sublanguages.txt index 2a89d6ae2d..9c5be2499d 100644 --- a/test/fixtures/expect/sublanguages.txt +++ b/test/fixtures/expect/sublanguages.txt @@ -1,4 +1,5 @@ -<? echo 'php'; /* ?> */ ?> +<? echo 'php'; /* ?> */ ?> <body> <script>document.write('Legacy code');</script> </body> +