Merge branch '3.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / bbcode / highlighter / XmlHighlighter.class.php
CommitLineData
dcc2332d
MW
1<?php
2namespace wcf\system\bbcode\highlighter;
dcc2332d
MW
3use wcf\system\Regex;
4use wcf\util\StringStack;
5use wcf\util\StringUtil;
6
7/**
8 * Highlights syntax of xml sourcecode.
9 *
10 * @author Tim Duesterhus
c839bd49 11 * @copyright 2001-2018 WoltLab GmbH
dcc2332d 12 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
e71525e4 13 * @package WoltLabSuite\Core\System\Bbcode\Highlighter
dcc2332d
MW
14 */
15class XmlHighlighter extends Highlighter {
16 /**
0fcfe5f6 17 * @inheritDoc
dcc2332d
MW
18 */
19 protected $allowsNewslinesInQuotes = true;
20
21 /**
0fcfe5f6 22 * @inheritDoc
dcc2332d 23 */
058cbd6a 24 protected $quotes = ['"'];
dcc2332d
MW
25
26 /**
0fcfe5f6 27 * @inheritDoc
dcc2332d 28 */
058cbd6a 29 protected $singleLineComment = [];
dcc2332d
MW
30
31 /**
0fcfe5f6 32 * @inheritDoc
dcc2332d 33 */
058cbd6a 34 protected $commentStart = ["<!--"];
dcc2332d
MW
35
36 /**
0fcfe5f6 37 * @inheritDoc
dcc2332d 38 */
058cbd6a 39 protected $commentEnd = ["-->"];
dcc2332d
MW
40
41 /**
0fcfe5f6 42 * @inheritDoc
dcc2332d 43 */
058cbd6a 44 protected $separators = ["<", ">"];
dcc2332d
MW
45
46 /**
0fcfe5f6 47 * @inheritDoc
dcc2332d 48 */
058cbd6a 49 protected $operators = [];
dcc2332d
MW
50
51 const XML_ATTRIBUTE_NAME = '[a-z0-9](?:(?:(?<!-)-)?[a-z0-9])*';
52
53 /**
0fcfe5f6 54 * @inheritDoc
dcc2332d
MW
55 */
56 protected function highlightKeywords($string) {
57 $string = parent::highlightKeywords($string);
58 // find tags
59 $regex = new Regex('&lt;(?:/|\!|\?)?[a-z0-9]+(?:\s+'.self::XML_ATTRIBUTE_NAME.'(?:=[^\s/\?&]+)?)*(?:\s+/|\?)?&gt;', Regex::CASE_INSENSITIVE);
a0c6927a 60 $string = $regex->replace($string, function ($matches) {
dcc2332d
MW
61 // highlight attributes
62 $tag = Regex::compile(XmlHighlighter::XML_ATTRIBUTE_NAME.'(?:=[^\s/\?&]+)?(?=\s|&)', Regex::CASE_INSENSITIVE)->replace($matches[0], '<span class="hlKeywords2">\\0</span>');
63
64 // highlight tag
65 return '<span class="hlKeywords1">'.$tag.'</span>';
a0c6927a 66 });
dcc2332d
MW
67
68 return $string;
69 }
70
71 /**
0fcfe5f6 72 * @inheritDoc
dcc2332d
MW
73 */
74 protected function cacheQuotes($string) {
ecf558a1
TD
75 $string = parent::cacheQuotes($string);
76
dcc2332d 77 // highlight CDATA-Tags as quotes
a0c6927a 78 $string = Regex::compile('<!\[CDATA\[.*?\]\]>', Regex::DOT_ALL)->replace($string, function (array $matches) {
dcc2332d 79 return StringStack::pushToStringStack('<span class="hlQuotes">'.StringUtil::encodeHTML($matches[0]).'</span>', 'highlighterQuotes');
a0c6927a 80 });
dcc2332d 81
dcc2332d
MW
82 return $string;
83 }
84
ecf558a1 85 /**
0fcfe5f6 86 * @inheritDoc
ecf558a1
TD
87 */
88 protected function highlightQuotes($string) {
89 return StringStack::reinsertStrings(parent::highlightQuotes($string), 'highlighterQuotes');
90 }
91
dcc2332d 92 /**
0fcfe5f6 93 * @inheritDoc
dcc2332d
MW
94 */
95 protected function highlightNumbers($string) {
96 // do not highlight numbers
97 return $string;
98 }
99}