-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
115 lines (109 loc) · 2.54 KB
/
index.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
"use strict";
/**
* Properties to prefix.
*/
var postcss = require("postcss"),
list = require("postcss/lib/list"),
props = [
// text
"hyphens",
"line-break",
"text-align-last",
"text-emphasis",
"text-emphasis-color",
"text-emphasis-style",
"word-break",
// writing modes
"writing-mode",
"text-orientation",
"text-combine-upright",
// text to speech
"cue",
"cue-before",
"cue-after",
"pause",
"rest",
"speak",
"speak-as",
"voice-family"
],
types = [
"opentype",
"woff"
],
rxTypes = new RegExp("format\\(['\"]?(" + types.join("|") + ")['\"]?\\)");
module.exports = postcss.plugin("postcss-epub", function(opts) {
opts = opts || {};
var
/**
* Sets all other options to true.
* @type {boolean}
*/
isStrict = opts.strict,
/**
* Set if we are to do strict font format checking and strip any non-epub prefixes.
* @type {boolean}
*/
isStrip = isStrict || opts.strip,
/**
* Set if we are to only check that the font defaults are correct.
* @type {boolean}
*/
isFonts = isStrict || opts.fonts;
/**
* PostCSS plugin to prefix ePub3 properties.
* @param {Object} css
*/
return function(css) {
css.walkDecls(function(decl) {
if (decl.value) {
if (props.indexOf(decl.prop) >= 0) {
decl.parent.insertBefore(decl, decl.clone({
prop: "-epub-" + decl.prop
}));
} else if (isStrip && decl.prop[0] === "-" && !/^-epub-/.test(decl.prop)) {
decl.parent.remove(decl);
}
}
});
if (isFonts) {
css.walkAtRules(function(rule) {
if (rule.name === "font-face") {
var hasWeight = false,
hasStyle = false;
rule.walkDecls(function(decl) {
switch (decl.prop) {
case "src":
var i,
fonts = list.comma(decl.value),
usable = [];
for (i = 0; i < fonts.length; i++) {
if (!/url\(/.test(fonts[i]) || rxTypes.test(fonts[i])) {
usable.push(fonts[i]);
}
}
if (usable.length) {
decl.value = usable.join(",");
} else {
throw decl.error("No valid font format, must have one of: " + types.join(", "), {plugin: "postcss-epub"});
}
break;
case "font-weight":
hasWeight = true;
break;
case "font-style":
hasStyle = true;
break;
}
});
if (!hasWeight) {
rule.append({prop: "font-weight", value: "normal"});
}
if (!hasStyle) {
rule.append({prop: "font-style", value: "normal"});
}
}
});
}
};
});