MCPcopy Create free account
hub / github.com/OpenReservation/OpenReservation / translate

Function translate

OpenReservation/wwwroot/Scripts/respond.js:116–170  ·  view source on GitHub ↗
(styles, href, media)

Source from the content-addressed store, hash-verified

114
115 //find media blocks in css text, convert to style blocks
116 translate = function (styles, href, media) {
117 var qs = styles.match(/@media[^\{]+\{([^\{\}]*\{[^\}\{]*\})+/gi),
118 ql = qs && qs.length || 0,
119 //try to get CSS path
120 href = href.substring(0, href.lastIndexOf("/")),
121 repUrls = function (css) {
122 return css.replace(/(url\()['"]?([^\/\)'"][^:\)'"]+)['"]?(\))/g, "$1" + href + "$2$3");
123 },
124 useMedia = !ql && media,
125 //vars used in loop
126 i = 0,
127 j, fullq, thisq, eachq, eql;
128
129 //if path exists, tack on trailing slash
130 if (href.length) { href += "/"; }
131
132 //if no internal queries exist, but media attr does, use that
133 //note: this currently lacks support for situations where a media attr is specified on a link AND
134 //its associated stylesheet has internal CSS media queries.
135 //In those cases, the media attribute will currently be ignored.
136 if (useMedia) {
137 ql = 1;
138 }
139
140 for (; i < ql; i++) {
141 j = 0;
142
143 //media attr
144 if (useMedia) {
145 fullq = media;
146 rules.push(repUrls(styles));
147 }
148 //parse for styles
149 else {
150 fullq = qs[i].match(/@media *([^\{]+)\{([\S\s]+?)$/) && RegExp.$1;
151 rules.push(RegExp.$2 && repUrls(RegExp.$2));
152 }
153
154 eachq = fullq.split(",");
155 eql = eachq.length;
156
157 for (; j < eql; j++) {
158 thisq = eachq[j];
159 mediastyles.push({
160 media: thisq.split("(")[0].match(/(only\s+)?([a-zA-Z]+)\s?/) && RegExp.$2 || "all",
161 rules: rules.length - 1,
162 hasquery: thisq.indexOf("(") > -1,
163 minw: thisq.match(/\(min\-width:[\s]*([\s]*[0-9\.]+)(px|em)[\s]*\)/) && parseFloat(RegExp.$1) + (RegExp.$2 || ""),
164 maxw: thisq.match(/\(max\-width:[\s]*([\s]*[0-9\.]+)(px|em)[\s]*\)/) && parseFloat(RegExp.$1) + (RegExp.$2 || "")
165 });
166 }
167 }
168
169 applyMedia();
170 },
171
172 lastCall,
173

Callers 2

ripCSSFunction · 0.85
makeRequestsFunction · 0.85

Calls 2

repUrlsFunction · 0.85
applyMediaFunction · 0.85

Tested by

no test coverage detected