-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.js
189 lines (150 loc) · 5.79 KB
/
format.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
'use strict';
/*
*
* smartFormat
*
* Provides content formatting functions
*
* Public variables:
*
* - BG - Background css class names
*
* Public functions:
*
* - setEPUBVersion - called to set _epubVersion to the version of the publication being evaluated
*
* - createMetaTag - creates a package document meta/link tag compliant with the specified version of EPUB
*
* - elementValueEscape - basic escaping for package meta element content
*
* - attributeValueEscape - basic escaping for package meta/link attribute content
*
* - convertUTCDateToString - converts a datetime value (YYYY-MM-DDTHH:MM:SSZ) into a human-readable string
*
* - formatIdentifier - formats as urn scheme to human-readable string
*
* - toTitleCase - converts a string to title case - capitalizes each word
*
* - setFieldToError - highlights warnings/errors and sets aria-invalid
*
* - setFieldToPass - highlights passes and unsets aria-invalid
*
*/
var smartFormat = (function() {
var _epubVersion = 3;
var _BG = {
"ERR": 'err',
"FAIL": 'err',
"NA": 'na',
"OBSOLETE": 'obsolete',
"PASS": 'pass',
"WARN": 'warn'
};
return {
BG: _BG,
/* stores the epub version for use by the other formatting functions */
setEPUBVersion: function(newVersion) {
_epubVersion = newVersion;
},
/* creates a meta/link link tag with the specified property */
createMetaTag: function(options) {
options = typeof(options) === 'object' ? options : {};
options.type = options.type ? options.type : 'meta';
options.property = options.property ? options.property : '';
options.value = options.value ? options.value : '';
options.id = options.id ? options.id : '';
options.refines = options.refines ? options.refines : '';
if (options.value.trim() != '') {
if (_epubVersion == 3) {
if (options.type == 'meta') {
return '<meta property="' + options.property + '"' + (options.id ? ' id="' + options.id + '"' : '') + (options.refines ? ' refines="#' + options.refines + '"' : '') + '>' + this.elementValueEscape(options.value) + '</meta>\n';
}
else {
return '<link rel="' + options.property + '" href="' + this.attributeValueEscape(options.value) + '"' + (options.id ? ' id="' + options.id + '"' : '') + (options.refines ? ' refines="#' + options.refines + '"' : '') + '/>\n';
}
}
else {
return '<meta name="' + options.property + '" content="' + this.attributeValueEscape(options.value) + '"/>\n';
}
}
return '';
},
/* escapes a value so it can be safely used in an xml element */
elementValueEscape: function(value) {
return value.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
},
/* escapes a value so it can be safely used in an xml attribute */
attributeValueEscape: function(value) {
return value.replace(/&/g,'&').replace(/"/g,'"').replace(/</g,'<').replace(/>/g,'>').replace(/\r?\n/g, ' ');
},
/* convers a utc date (dcterms:modified) to a human-readable string */
convertUTCDateToString: function (utcDate) {
var date_options = { year: "numeric", month: "short", day: "numeric", hour: "2-digit", minute: "2-digit" };
return (utcDate == '') ? '' : new Date(utcDate).toLocaleDateString("en",date_options);
},
/* reformts URNs - adds ISBN in front for urn:isbn, otherwise strips urn:xxx: leaving just the identifier */
formatIdentifier: function (identifier) {
if (identifier.match(/urn:isbn:/i)) {
identifier = 'ISBN ' + identifier.replace('urn:isbn:','');
}
else {
identifier = identifier.replace(/urn:[a-z0-9]+:/i, '');
}
return identifier;
},
/* converts the provided string to title case */
toTitleCase: function(str) {
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
},
/* formats the background color and aria-invalid status for errors */
setFieldToError: function(options) {
options = typeof(options) === 'object' ? options : {};
if (!options.hasOwnProperty('id') || !options.id) {
return;
}
options.is_warning = options.hasOwnProperty('is_warning') ? options.is_warning : false;
options.highlight_parent = options.hasOwnProperty('highlight_parent') ? options.highlight_parent : false;
var field = document.getElementById(options.id);
field.setAttribute('aria-invalid', (options.is_warning ? false : true));
if (options.highlight_parent) {
field.parentNode.classList.remove(_BG.PASS,_BG.WARN,_BG.ERR);
field.parentNode.classList.add(options.is_warning ? _BG.WARN : _BG.ERR);
}
else {
field.classList.remove(_BG.PASS,_BG.WARN,_BG.ERR);
field.classList.add(options.is_warning ? _BG.WARN : _BG.ERR);
}
},
/* sets the background color and aria-invalid status for passes */
setFieldToPass: function(options) {
options = typeof(options) === 'object' ? options : {};
if (!options.hasOwnProperty('id') || !options.id) {
return;
}
options.highlight_parent = options.hasOwnProperty('highlight_parent') ? options.highlight_parent : false;
var field = document.getElementById(options.id);
field.setAttribute('aria-invalid', false);
if (options.highlight_parent) {
field.parentNode.classList.remove(_BG.PASS,_BG.WARN,_BG.ERR);
}
else {
field.classList.remove(_BG.PASS,_BG.WARN,_BG.ERR);
}
},
copyToClipboard: function(elemid) {
var meta = document.getElementById(elemid);
if (window.clipboardData && window.clipboardData.setData) {
return window.clipboardData.setData("Text", meta.value);
}
else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
meta.select();
try {
return document.execCommand("copy");
}
catch (ex) {
return false;
}
}
}
}
})();