Skip to content

Commit e42ed68

Browse files
authored
Split escapecss into two platforms (#9475)
* Split escapecss into to platforms This will reduce core size slightly * Update change note
1 parent 8445641 commit e42ed68

File tree

3 files changed

+107
-88
lines changed

3 files changed

+107
-88
lines changed

core-server/utils/escapecss.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*\
2+
title: $:/core-server/modules/utils/escapecss.js
3+
type: application/javascript
4+
module-type: utils-node
5+
6+
Provides CSS.escape() functionality.
7+
8+
\*/
9+
10+
"use strict";
11+
12+
exports.escapeCSS = (function() {
13+
// see also https://drafts.csswg.org/cssom/#serialize-an-identifier
14+
15+
/* eslint-disable */
16+
/*! https://mths.be/cssescape v1.5.1 by @mathias | MIT license */
17+
return function(value) {
18+
if (arguments.length == 0) {
19+
throw new TypeError('`CSS.escape` requires an argument.');
20+
}
21+
var string = String(value);
22+
var length = string.length;
23+
var index = -1;
24+
var codeUnit;
25+
var result = '';
26+
var firstCodeUnit = string.charCodeAt(0);
27+
while (++index < length) {
28+
codeUnit = string.charCodeAt(index);
29+
// Note: there’s no need to special-case astral symbols, surrogate
30+
// pairs, or lone surrogates.
31+
32+
// If the character is NULL (U+0000), then the REPLACEMENT CHARACTER
33+
// (U+FFFD).
34+
if (codeUnit == 0x0000) {
35+
result += '\uFFFD';
36+
continue;
37+
}
38+
39+
if (
40+
// If the character is in the range [\1-\1F] (U+0001 to U+001F) or is
41+
// U+007F, […]
42+
(codeUnit >= 0x0001 && codeUnit <= 0x001F) || codeUnit == 0x007F ||
43+
// If the character is the first character and is in the range [0-9]
44+
// (U+0030 to U+0039), […]
45+
(index == 0 && codeUnit >= 0x0030 && codeUnit <= 0x0039) ||
46+
// If the character is the second character and is in the range [0-9]
47+
// (U+0030 to U+0039) and the first character is a `-` (U+002D), […]
48+
(
49+
index == 1 &&
50+
codeUnit >= 0x0030 && codeUnit <= 0x0039 &&
51+
firstCodeUnit == 0x002D
52+
)
53+
) {
54+
// https://drafts.csswg.org/cssom/#escape-a-character-as-code-point
55+
result += '\\' + codeUnit.toString(16) + ' ';
56+
continue;
57+
}
58+
59+
if (
60+
// If the character is the first character and is a `-` (U+002D), and
61+
// there is no second character, […]
62+
index == 0 &&
63+
length == 1 &&
64+
codeUnit == 0x002D
65+
) {
66+
result += '\\' + string.charAt(index);
67+
continue;
68+
}
69+
70+
// If the character is not handled by one of the above rules and is
71+
// greater than or equal to U+0080, is `-` (U+002D) or `_` (U+005F), or
72+
// is in one of the ranges [0-9] (U+0030 to U+0039), [A-Z] (U+0041 to
73+
// U+005A), or [a-z] (U+0061 to U+007A), […]
74+
if (
75+
codeUnit >= 0x0080 ||
76+
codeUnit == 0x002D ||
77+
codeUnit == 0x005F ||
78+
codeUnit >= 0x0030 && codeUnit <= 0x0039 ||
79+
codeUnit >= 0x0041 && codeUnit <= 0x005A ||
80+
codeUnit >= 0x0061 && codeUnit <= 0x007A
81+
) {
82+
// the character itself
83+
result += string.charAt(index);
84+
continue;
85+
}
86+
87+
// Otherwise, the escaped character.
88+
// https://drafts.csswg.org/cssom/#escape-a-character
89+
result += '\\' + string.charAt(index);
90+
91+
}
92+
return result;
93+
};
94+
/* eslint-enable */
95+
})();

core/modules/utils/escapecss.js

Lines changed: 2 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,14 @@
11
/*\
22
title: $:/core/modules/utils/escapecss.js
33
type: application/javascript
4-
module-type: utils
4+
module-type: utils-browser
55
66
Provides CSS.escape() functionality.
77
88
\*/
99

1010
"use strict";
1111

12-
// TODO -- resolve this construction
1312
exports.escapeCSS = (function() {
14-
// use browser's native CSS.escape() function if available
15-
if ($tw.browser && window.CSS && window.CSS.escape) {
16-
return window.CSS.escape;
17-
}
18-
19-
// otherwise, a utility method is provided
20-
// see also https://drafts.csswg.org/cssom/#serialize-an-identifier
21-
22-
/*! https://mths.be/cssescape v1.5.1 by @mathias | MIT license */
23-
return function(value) {
24-
if (arguments.length == 0) {
25-
throw new TypeError('`CSS.escape` requires an argument.');
26-
}
27-
var string = String(value);
28-
var length = string.length;
29-
var index = -1;
30-
var codeUnit;
31-
var result = '';
32-
var firstCodeUnit = string.charCodeAt(0);
33-
while (++index < length) {
34-
codeUnit = string.charCodeAt(index);
35-
// Note: there’s no need to special-case astral symbols, surrogate
36-
// pairs, or lone surrogates.
37-
38-
// If the character is NULL (U+0000), then the REPLACEMENT CHARACTER
39-
// (U+FFFD).
40-
if (codeUnit == 0x0000) {
41-
result += '\uFFFD';
42-
continue;
43-
}
44-
45-
if (
46-
// If the character is in the range [\1-\1F] (U+0001 to U+001F) or is
47-
// U+007F, […]
48-
(codeUnit >= 0x0001 && codeUnit <= 0x001F) || codeUnit == 0x007F ||
49-
// If the character is the first character and is in the range [0-9]
50-
// (U+0030 to U+0039), […]
51-
(index == 0 && codeUnit >= 0x0030 && codeUnit <= 0x0039) ||
52-
// If the character is the second character and is in the range [0-9]
53-
// (U+0030 to U+0039) and the first character is a `-` (U+002D), […]
54-
(
55-
index == 1 &&
56-
codeUnit >= 0x0030 && codeUnit <= 0x0039 &&
57-
firstCodeUnit == 0x002D
58-
)
59-
) {
60-
// https://drafts.csswg.org/cssom/#escape-a-character-as-code-point
61-
result += '\\' + codeUnit.toString(16) + ' ';
62-
continue;
63-
}
64-
65-
if (
66-
// If the character is the first character and is a `-` (U+002D), and
67-
// there is no second character, […]
68-
index == 0 &&
69-
length == 1 &&
70-
codeUnit == 0x002D
71-
) {
72-
result += '\\' + string.charAt(index);
73-
continue;
74-
}
75-
76-
// If the character is not handled by one of the above rules and is
77-
// greater than or equal to U+0080, is `-` (U+002D) or `_` (U+005F), or
78-
// is in one of the ranges [0-9] (U+0030 to U+0039), [A-Z] (U+0041 to
79-
// U+005A), or [a-z] (U+0061 to U+007A), […]
80-
if (
81-
codeUnit >= 0x0080 ||
82-
codeUnit == 0x002D ||
83-
codeUnit == 0x005F ||
84-
codeUnit >= 0x0030 && codeUnit <= 0x0039 ||
85-
codeUnit >= 0x0041 && codeUnit <= 0x005A ||
86-
codeUnit >= 0x0061 && codeUnit <= 0x007A
87-
) {
88-
// the character itself
89-
result += string.charAt(index);
90-
continue;
91-
}
92-
93-
// Otherwise, the escaped character.
94-
// https://drafts.csswg.org/cssom/#escape-a-character
95-
result += '\\' + string.charAt(index);
96-
97-
}
98-
return result;
99-
};
13+
return window.CSS.escape;
10014
})();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
title: $:/changenotes/5.4.0/#9475
2+
description: Split escapecss.js into two platforms
3+
release: 5.4.0
4+
tags: $:/tags/ChangeNote
5+
change-type: performance
6+
change-category: internal
7+
github-links: https://github.com/TiddlyWiki/TiddlyWiki5/pull/9475
8+
github-contributors: Leilei332
9+
10+
Split [[$:/core/modules/utils/escapecss.js]] for two platforms: one for the browser, the other for Node.js. The `CSS.escape` polyfill is moved to `core-server`.

0 commit comments

Comments
 (0)