cnf
2025-05-10 386fa0eca75ddc88165f9b73038f2a2239e1072e
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
  MIT License http://www.opensource.org/licenses/mit-license.php
  Author Tobias Koppers @sokra
  Modified by Evan You @yyx990803
*/
 
import listToStyles from './listToStyles'
 
var hasDocument = typeof document !== 'undefined'
 
if (typeof DEBUG !== 'undefined' && DEBUG) {
    if (!hasDocument) {
        throw new Error(
            'vue-style-loader cannot be used in a non-browser environment. ' +
            "Use { target: 'node' } in your Webpack config to indicate a server-rendering environment."
        )
    }
}
 
/*
type StyleObject = {
  id: number;
  parts: Array<StyleObjectPart>
}
 
type StyleObjectPart = {
  css: string;
  media: string;
  sourceMap: ?string
}
*/
 
var stylesInDom = {
    /*
      [id: number]: {
        id: number,
        refs: number,
        parts: Array<(obj?: StyleObjectPart) => void>
      }
    */
}
 
var head = hasDocument && (document.head || document.getElementsByTagName('head')[0])
var singletonElement = null
var singletonCounter = 0
var isProduction = false
var noop = function() {}
var options = null
var ssrIdKey = 'data-vue-ssr-id'
 
// Force single-tag solution on IE6-9, which has a hard limit on the # of <style>
// tags it will allow on a page
var isOldIE = typeof navigator !== 'undefined' && /msie [6-9]\b/.test(navigator.userAgent.toLowerCase())
 
export default function addStylesClient(parentId, list, _isProduction, _options) {
    isProduction = _isProduction
 
    options = _options || {}
 
    var styles = listToStyles(parentId, list)
    addStylesToDom(styles)
 
    return function update(newList) {
        var mayRemove = []
        for (var i = 0; i < styles.length; i++) {
            var item = styles[i]
            var domStyle = stylesInDom[item.id]
            domStyle.refs--
            mayRemove.push(domStyle)
        }
        if (newList) {
            styles = listToStyles(parentId, newList)
            addStylesToDom(styles)
        } else {
            styles = []
        }
        for (var i = 0; i < mayRemove.length; i++) {
            var domStyle = mayRemove[i]
            if (domStyle.refs === 0) {
                for (var j = 0; j < domStyle.parts.length; j++) {
                    domStyle.parts[j]()
                }
                delete stylesInDom[domStyle.id]
            }
        }
    }
}
 
function addStylesToDom(styles /* Array<StyleObject> */ ) {
    for (var i = 0; i < styles.length; i++) {
        var item = styles[i]
        var domStyle = stylesInDom[item.id]
        if (domStyle) {
            domStyle.refs++
            for (var j = 0; j < domStyle.parts.length; j++) {
                domStyle.parts[j](item.parts[j])
            }
            for (; j < item.parts.length; j++) {
                domStyle.parts.push(addStyle(item.parts[j]))
            }
            if (domStyle.parts.length > item.parts.length) {
                domStyle.parts.length = item.parts.length
            }
        } else {
            var parts = []
            for (var j = 0; j < item.parts.length; j++) {
                parts.push(addStyle(item.parts[j]))
            }
            stylesInDom[item.id] = {
                id: item.id,
                refs: 1,
                parts: parts
            }
        }
    }
}
 
function createStyleElement() {
    var styleElement = document.createElement('style')
    styleElement.type = 'text/css'
    head.appendChild(styleElement)
    return styleElement
}
 
function addStyle(obj /* StyleObjectPart */ ) {
    var update, remove
    var styleElement = document.querySelector('style[' + ssrIdKey + '~="' + obj.id + '"]')
 
    if (styleElement) {
        if (isProduction) {
            // has SSR styles and in production mode.
            // simply do nothing.
            return noop
        } else {
            // has SSR styles but in dev mode.
            // for some reason Chrome can't handle source map in server-rendered
            // style tags - source maps in <style> only works if the style tag is
            // created and inserted dynamically. So we remove the server rendered
            // styles and inject new ones.
            styleElement.parentNode.removeChild(styleElement)
        }
    }
 
    if (isOldIE) {
        // use singleton mode for IE9.
        var styleIndex = singletonCounter++
        styleElement = singletonElement || (singletonElement = createStyleElement())
        update = applyToSingletonTag.bind(null, styleElement, styleIndex, false)
        remove = applyToSingletonTag.bind(null, styleElement, styleIndex, true)
    } else {
        // use multi-style-tag mode in all other cases
        styleElement = createStyleElement()
        update = applyToTag.bind(null, styleElement)
        remove = function() {
            styleElement.parentNode.removeChild(styleElement)
        }
    }
 
    update(obj)
 
    return function updateStyle(newObj /* StyleObjectPart */ ) {
        if (newObj) {
            if (newObj.css === obj.css &&
                newObj.media === obj.media &&
                newObj.sourceMap === obj.sourceMap) {
                return
            }
            update(obj = newObj)
        } else {
            remove()
        }
    }
}
 
var replaceText = (function() {
    var textStore = []
 
    return function(index, replacement) {
        textStore[index] = replacement
        return textStore.filter(Boolean).join('\n')
    }
})()
 
function applyToSingletonTag(styleElement, index, remove, obj) {
    var css = remove ? '' : processCss(obj.css)
 
    if (styleElement.styleSheet) {
        styleElement.styleSheet.cssText = replaceText(index, css)
    } else {
        var cssNode = document.createTextNode(css)
        var childNodes = styleElement.childNodes
        if (childNodes[index]) styleElement.removeChild(childNodes[index])
        if (childNodes.length) {
            styleElement.insertBefore(cssNode, childNodes[index])
        } else {
            styleElement.appendChild(cssNode)
        }
    }
}
 
function applyToTag(styleElement, obj) {
    var css = processCss(obj.css)
    var media = obj.media
    var sourceMap = obj.sourceMap
 
    if (media) {
        styleElement.setAttribute('media', media)
    }
    if (options.ssrId) {
        styleElement.setAttribute(ssrIdKey, obj.id)
    }
 
    if (sourceMap) {
        // https://developer.chrome.com/devtools/docs/javascript-debugging
        // this makes source maps inside style tags work properly in Chrome
        css += '\n/*# sourceURL=' + sourceMap.sources[0] + ' */'
        // http://stackoverflow.com/a/26603875
        css += '\n/*# sourceMappingURL=data:application/json;base64,' + btoa(unescape(encodeURIComponent(JSON.stringify(
            sourceMap)))) + ' */'
    }
 
    if (styleElement.styleSheet) {
        styleElement.styleSheet.cssText = css
    } else {
        while (styleElement.firstChild) {
            styleElement.removeChild(styleElement.firstChild)
        }
        styleElement.appendChild(document.createTextNode(css))
    }
}
//fixed by xxxxxx
var UPX_RE = /%\?([+-]?\d+(\.\d+)?)\?%/g
var BODY_RE = /\.\?%PAGE\?%/g
var BODY_SCOPED_RE = /\?%PAGE\?%\[data-v-[a-z0-9]{8}\]/g
var PAGE_SCOPED_RE = /uni-page-body\[data-v-[a-z0-9]{8}\]/g
var VAR_STATUS_BAR_HEIGHT = /var\(--status-bar-height\)/gi
var VAR_WINDOW_TOP = /var\(--window-top\)/gi
var VAR_WINDOW_BOTTOM = /var\(--window-bottom\)/gi
var VAR_WINDOW_LEFT = /var\(--window-left\)/gi
var VAR_WINDOW_RIGHT = /var\(--window-right\)/gi
 
function processCss(css) {
    var page = getPage()
    if (typeof uni !== 'undefined' && !uni.canIUse('css.var')) { //不支持 css 变量
        var offset = getWindowOffset()
        css = css.replace(VAR_STATUS_BAR_HEIGHT, '0px')
            .replace(VAR_WINDOW_TOP, offset.top + 'px')
            .replace(VAR_WINDOW_BOTTOM, offset.bottom + 'px')
            .replace(VAR_WINDOW_LEFT, '0px')
            .replace(VAR_WINDOW_RIGHT, '0px')
    }
    return css
        .replace(BODY_SCOPED_RE, page)
        .replace(BODY_RE, '')
        .replace(PAGE_SCOPED_RE, 'body.' + page + ' uni-page-body')
        .replace(/\{[\s\S]+?\}|@media.+?\{/g, function (css) {
      if(typeof uni === 'undefined'){
        return css
      }
            return css.replace(UPX_RE, function (a, b) {
                return uni.upx2px(b) + 'px'
            })
        })
}
 
function getPage() {
    var app = typeof getApp === 'function' && getApp()
    return app && app.$route && app.$route.meta && app.$route.meta.name || ''
}
 
function getWindowOffset() {
    var app = typeof getApp === 'function' && getApp()
    if (app && app.$route && app.$route.meta && app.$route.meta.name) {
        return {
            top: app.$route.meta.windowTop,
            // TODO 可配置 TabBar 高度
            bottom: app.$route.meta.isTabBar ? 50 : 0
        }
    }
    return {
        top: 0,
        bottom: 0
    }
}