This repository has been archived by the owner. It is now read-only.
forked from pragdave/codex
/
shBrushXml.js
executable file
·70 lines (59 loc) · 2.32 KB
/
shBrushXml.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
dp.sh.Brushes.Xml = function()
{
this.CssClass = 'dp-xml';
this.Style = '.dp-xml .cdata { color: #ff1493; }' +
'.dp-xml .tag, .dp-xml .tag-name { color: #069; font-weight: bold; }' +
'.dp-xml .attribute { color: red; }' +
'.dp-xml .attribute-value { color: blue; }';
}
dp.sh.Brushes.Xml.prototype = new dp.sh.Highlighter();
dp.sh.Brushes.Xml.Aliases = ['xml', 'xhtml', 'xslt', 'html', 'xhtml'];
dp.sh.Brushes.Xml.prototype.ProcessRegexList = function()
{
function push(array, value)
{
array[array.length] = value;
}
/* If only there was a way to get index of a group within a match, the whole XML
could be matched with the expression looking something like that:
(<!\[CDATA\[\s*.*\s*\]\]>)
| (<!--\s*.*\s*?-->)
| (<)*(\w+)*\s*(\w+)\s*=\s*(".*?"|'.*?'|\w+)(/*>)*
| (</?)(.*?)(/?>)
*/
var index = 0;
var match = null;
var regex = null;
// Match CDATA in the following format <![ ... [ ... ]]>
// (\<|<)\!\[[\w\s]*?\[(.|\s)*?\]\](\>|>)
this.GetMatches(new RegExp('(\<|<)\\!\\[[\\w\\s]*?\\[(.|\\s)*?\\]\\](\>|>)', 'gm'), 'cdata');
// Match comments
// (\<|<)!--\s*.*\s*?--(\>|>)
this.GetMatches(new RegExp('(\<|<)!--\\s*.*\\s*?--(\>|>)', 'gm'), 'comments');
// Match attributes and their values
// (:|\w+)\s*=\s*(".*?"|\'.*?\'|\w+)*
regex = new RegExp('([:\\w-\.]+)\\s*=\\s*(".*?"|\'.*?\'|\\w+)*|(\\w+)', 'gm'); // Thanks to Tomi Blinnikka of Yahoo! for fixing namespaces in attributes
while((match = regex.exec(this.code)) != null)
{
if(match[1] == null)
{
continue;
}
push(this.matches, new dp.sh.Match(match[1], match.index, 'attribute'));
// if xml is invalid and attribute has no property value, ignore it
if(match[2] != undefined)
{
push(this.matches, new dp.sh.Match(match[2], match.index + match[0].indexOf(match[2]), 'attribute-value'));
}
}
// Match opening and closing tag brackets
// (\<|<)/*\?*(?!\!)|/*\?*(\>|>)
this.GetMatches(new RegExp('(\<|<)/*\\?*(?!\\!)|/*\\?*(\>|>)', 'gm'), 'tag');
// Match tag names
// (\<|<)/*\?*\s*(\w+)
regex = new RegExp('(?:\<|<)/*\\?*\\s*([:\\w-\.]+)', 'gm');
while((match = regex.exec(this.code)) != null)
{
push(this.matches, new dp.sh.Match(match[1], match.index + match[0].indexOf(match[1]), 'tag-name'));
}
}