|
@@ -19,6 +19,7 @@ export default class AutoHeightWebView extends PureComponent {
|
19
|
19
|
source: WebView.propTypes.source,
|
20
|
20
|
onHeightUpdated: PropTypes.func,
|
21
|
21
|
customScript: PropTypes.string,
|
|
22
|
+ customStyle: PropTypes.string,
|
22
|
23
|
enableAnimation: PropTypes.bool,
|
23
|
24
|
// if set to true may cause some layout issues (smaller font size)
|
24
|
25
|
scalesPageToFit: PropTypes.bool,
|
|
@@ -48,7 +49,13 @@ export default class AutoHeightWebView extends PureComponent {
|
48
|
49
|
if (this.props.enableAnimation) {
|
49
|
50
|
this.opacityAnimatedValue = new Animated.Value(0);
|
50
|
51
|
}
|
51
|
|
- const initialScript = props.files ? this.appendFilesToHead(props.files, props.hasIframe ? IframeBaseScript : BaseScript) : props.hasIframe ? IframeBaseScript : BaseScript;
|
|
52
|
+ let initialScript = props.hasIframe ? IframeBaseScript : BaseScript;
|
|
53
|
+ initialScript = props.files
|
|
54
|
+ ? this.appendFilesToHead(props.files, BaseScript)
|
|
55
|
+ : BaseScript;
|
|
56
|
+ initialScript = props.customStyle
|
|
57
|
+ ? this.appendStylesToHead(props.customStyle, initialScript)
|
|
58
|
+ : initialScript;
|
52
|
59
|
this.state = {
|
53
|
60
|
height: 0,
|
54
|
61
|
script: initialScript
|
|
@@ -58,8 +65,11 @@ export default class AutoHeightWebView extends PureComponent {
|
58
|
65
|
componentWillReceiveProps(nextProps) {
|
59
|
66
|
let currentScript = nextProps.hasIframe ? IframeBaseScript : BaseScript;
|
60
|
67
|
if (nextProps.files) {
|
61
|
|
- currentScript = this.appendFilesToHead(nextProps.files, nextProps.hasIframe ? IframeBaseScript : BaseScript);
|
|
68
|
+ currentScript = this.appendFilesToHead(nextProps.files, currentScript);
|
62
|
69
|
}
|
|
70
|
+ currentScript = nextProps.customStyle
|
|
71
|
+ ? this.appendStylesToHead(nextProps.customStyle, currentScript)
|
|
72
|
+ : currentScript;
|
63
|
73
|
this.setState({ script: currentScript });
|
64
|
74
|
}
|
65
|
75
|
|
|
@@ -67,17 +77,29 @@ export default class AutoHeightWebView extends PureComponent {
|
67
|
77
|
if (!files) {
|
68
|
78
|
return script;
|
69
|
79
|
}
|
70
|
|
- for (let file of files) {
|
71
|
|
- script =
|
72
|
|
- `
|
73
|
|
- var link = document.createElement('link');
|
74
|
|
- link.rel = '` + file.rel + `';
|
75
|
|
- link.type = '` + file.type + `';
|
76
|
|
- link.href = '` + file.href + `';
|
77
|
|
- document.head.appendChild(link);
|
78
|
|
- `+ script;
|
79
|
|
- }
|
|
80
|
+ return files.reduceRight((file, combinedScript) => `
|
|
81
|
+ var link = document.createElement('link');
|
|
82
|
+ link.rel = '${file.rel}';
|
|
83
|
+ link.type = '${file.type}';
|
|
84
|
+ link.href = '${file.href}';
|
|
85
|
+ document.head.appendChild(link);
|
|
86
|
+ ${combinedScript}
|
|
87
|
+ `, script)
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ appendStylesToHead(styles, script) {
|
|
91
|
+ if (!styles) {
|
80
|
92
|
return script;
|
|
93
|
+ }
|
|
94
|
+ // Escape any single quotes or newlines in the CSS with .replace()
|
|
95
|
+ const escaped = styles.replace(/\'/g, "\\'").replace(/\n/g, '\\n')
|
|
96
|
+ return `
|
|
97
|
+ var styleElement = document.createElement('style');
|
|
98
|
+ var styleText = document.createTextNode('${escaped}');
|
|
99
|
+ styleElement.appendChild(styleText);
|
|
100
|
+ document.head.appendChild(styleElement);
|
|
101
|
+ ${script}
|
|
102
|
+ `
|
81
|
103
|
}
|
82
|
104
|
|
83
|
105
|
onHeightUpdated(height) {
|