Debian Codegraph @

mixmark-io-domino/lib/xmlnames.js

raw symbols SCIP
1 "use strict";
2 // This grammar is from the XML and XML Namespace specs. It specifies whether
3 // a string (such as an element or attribute name) is a valid Name or QName.
4 //
5 // Name ::= NameStartChar (NameChar)*
6 // NameStartChar ::= ":" | [A-Z] | "_" | [a-z] |
7 // [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] |
8 // [#x370-#x37D] | [#x37F-#x1FFF] |
9 // [#x200C-#x200D] | [#x2070-#x218F] |
10 // [#x2C00-#x2FEF] | [#x3001-#xD7FF] |
11 // [#xF900-#xFDCF] | [#xFDF0-#xFFFD] |
12 // [#x10000-#xEFFFF]
13 //
14 // NameChar ::= NameStartChar | "-" | "." | [0-9] |
15 // #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
16 //
17 // QName ::= PrefixedName| UnprefixedName
18 // PrefixedName ::= Prefix ':' LocalPart
19 // UnprefixedName ::= LocalPart
20 // Prefix ::= NCName
21 // LocalPart ::= NCName
22 // NCName ::= Name - (Char* ':' Char*)
23 // # An XML Name, minus the ":"
24 //
25
26 exports.isValidName = isValidName;
27 exports.isValidQName = isValidQName;
28
29 // Most names will be ASCII only. Try matching against simple regexps first
30 var simplename = /^[_:A-Za-z][-.:\w]+$/;
31 var simpleqname = /^([_A-Za-z][-.\w]+|[_A-Za-z][-.\w]+:[_A-Za-z][-.\w]+)$/;
32
33 // If the regular expressions above fail, try more complex ones that work
34 // for any identifiers using codepoints from the Unicode BMP
35 var ncnamestartchars = "_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02ff\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD";
36 var ncnamechars = "-._A-Za-z0-9\u00B7\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02ff\u0300-\u037D\u037F-\u1FFF\u200C\u200D\u203f\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD";
37
38 var ncname = "[" + ncnamestartchars + "][" + ncnamechars + "]*";
39 var namestartchars = ncnamestartchars + ":";
40 var namechars = ncnamechars + ":";
41 var name = new RegExp("^[" + namestartchars + "]" + "[" + namechars + "]*$");
42 var qname = new RegExp("^(" + ncname + "|" + ncname + ":" + ncname + ")$");
43
44 // XML says that these characters are also legal:
45 // [#x10000-#xEFFFF]. So if the patterns above fail, and the
46 // target string includes surrogates, then try the following
47 // patterns that allow surrogates and then run an extra validation
48 // step to make sure that the surrogates are in valid pairs and in
49 // the right range. Note that since the characters \uf0000 to \u1f0000
50 // are not allowed, it means that the high surrogate can only go up to
51 // \uDB7f instead of \uDBFF.
52 var hassurrogates = /[\uD800-\uDB7F\uDC00-\uDFFF]/;
53 var surrogatechars = /[\uD800-\uDB7F\uDC00-\uDFFF]/g;
54 var surrogatepairs = /[\uD800-\uDB7F][\uDC00-\uDFFF]/g;
55
56 // Modify the variables above to allow surrogates
57 ncnamestartchars += "\uD800-\uDB7F\uDC00-\uDFFF";
58 ncnamechars += "\uD800-\uDB7F\uDC00-\uDFFF";
59 ncname = "[" + ncnamestartchars + "][" + ncnamechars + "]*";
60 namestartchars = ncnamestartchars + ":";
61 namechars = ncnamechars + ":";
62
63 // Build another set of regexps that include surrogates
64 var surrogatename = new RegExp("^[" + namestartchars + "]" + "[" + namechars + "]*$");
65 var surrogateqname = new RegExp("^(" + ncname + "|" + ncname + ":" + ncname + ")$");
66
67 function isValidName(s) {
68 if (simplename.test(s)) return true; // Plain ASCII
69 if (name.test(s)) return true; // Unicode BMP
70
71 // Maybe the tests above failed because s includes surrogate pairs
72 // Most likely, though, they failed for some more basic syntax problem
73 if (!hassurrogates.test(s)) return false;
74
75 // Is the string a valid name if we allow surrogates?
76 if (!surrogatename.test(s)) return false;
77
78 // Finally, are the surrogates all correctly paired up?
79 var chars = s.match(surrogatechars), pairs = s.match(surrogatepairs);
80 return pairs !== null && 2*pairs.length === chars.length;
81 }
82
83 function isValidQName(s) {
84 if (simpleqname.test(s)) return true; // Plain ASCII
85 if (qname.test(s)) return true; // Unicode BMP
86
87 if (!hassurrogates.test(s)) return false;
88 if (!surrogateqname.test(s)) return false;
89 var chars = s.match(surrogatechars), pairs = s.match(surrogatepairs);
90 return pairs !== null && 2*pairs.length === chars.length;
91 }