From e7bf5952d0729b37e677168b6e8fbd1ce58ed1a2 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 10 Aug 2014 17:28:37 +0200 Subject: First version --- point/libs/require-css/css.min.js | 63 ++++++++++++++++ point/libs/require-css/normalize.js | 142 ++++++++++++++++++++++++++++++++++++ 2 files changed, 205 insertions(+) create mode 100644 point/libs/require-css/css.min.js create mode 100644 point/libs/require-css/normalize.js (limited to 'point/libs/require-css') diff --git a/point/libs/require-css/css.min.js b/point/libs/require-css/css.min.js new file mode 100644 index 0000000..1be578e --- /dev/null +++ b/point/libs/require-css/css.min.js @@ -0,0 +1,63 @@ +define(function () { + if (typeof window == "undefined")return{load: function (n, r, load) { + load() + }}; + var head = document.getElementsByTagName("head")[0]; + var engine = window.navigator.userAgent.match(/Trident\/([^ ;]*)|AppleWebKit\/([^ ;]*)|Opera\/([^ ;]*)|rv\:([^ ;]*)(.*?)Gecko\/([^ ;]*)|MSIE\s([^ ;]*)/) || 0; + var useImportLoad = false; + var useOnload = true; + if (engine[1] || engine[7])useImportLoad = parseInt(engine[1]) < 6 || parseInt(engine[7]) <= 9; else if (engine[2])useOnload = false; else if (engine[4])useImportLoad = parseInt(engine[4]) < 18; + var cssAPI = {}; + cssAPI.pluginBuilder = "./css-builder"; + var curStyle; + var createStyle = function () { + curStyle = document.createElement("style"); + head.appendChild(curStyle) + }; + var importLoad = function (url, callback) { + createStyle(); + var curSheet = curStyle.styleSheet || curStyle.sheet; + if (curSheet && curSheet.addImport) { + curSheet.addImport(url); + curStyle.onload = callback + } else { + curStyle.textContent = '@import "' + url + '";'; + var loadInterval = setInterval(function () { + try { + curStyle.sheet.cssRules; + clearInterval(loadInterval); + callback() + } catch (e) { + } + }, 10) + } + }; + var linkLoad = function (url, callback) { + var link = document.createElement("link"); + link.type = "text/css"; + link.rel = "stylesheet"; + if (useOnload)link.onload = function () { + link.onload = function () { + }; + setTimeout(callback, 7) + }; else var loadInterval = setInterval(function () { + for (var i = 0; i < document.styleSheets.length; i++) { + var sheet = document.styleSheets[i]; + if (sheet.href == link.href) { + clearInterval(loadInterval); + return callback() + } + } + }, 10); + link.href = url; + head.appendChild(link) + }; + cssAPI.normalize = function (name, normalize) { + if (name.substr(name.length - 4, 4) == ".css")name = name.substr(0, name.length - 4); + return normalize(name) + }; + cssAPI.load = function (cssId, req, load, config) { + (useImportLoad ? importLoad : linkLoad)(req.toUrl(cssId + ".css"), load) + }; + return cssAPI +}); \ No newline at end of file diff --git a/point/libs/require-css/normalize.js b/point/libs/require-css/normalize.js new file mode 100644 index 0000000..ee82b2c --- /dev/null +++ b/point/libs/require-css/normalize.js @@ -0,0 +1,142 @@ +//>>excludeStart('excludeRequireCss', pragmas.excludeRequireCss) +/* + * css.normalize.js + * + * CSS Normalization + * + * CSS paths are normalized based on an optional basePath and the RequireJS config + * + * Usage: + * normalize(css, fromBasePath, toBasePath); + * + * css: the stylesheet content to normalize + * fromBasePath: the absolute base path of the css relative to any root (but without ../ backtracking) + * toBasePath: the absolute new base path of the css relative to the same root + * + * Absolute dependencies are left untouched. + * + * Urls in the CSS are picked up by regular expressions. + * These will catch all statements of the form: + * + * url(*) + * url('*') + * url("*") + * + * @import '*' + * @import "*" + * + * (and so also @import url(*) variations) + * + * For urls needing normalization + * + */ + +define(function () { + + // regular expression for removing double slashes + // eg http://www.example.com//my///url/here -> http://www.example.com/my/url/here + var slashes = /([^:])\/+/g + var removeDoubleSlashes = function (uri) { + return uri.replace(slashes, '$1/'); + } + + // given a relative URI, and two absolute base URIs, convert it from one base to another + var protocolRegEx = /[^\:\/]*:\/\/([^\/])*/; + var absUrlRegEx = /^(\/|data:)/; + + function convertURIBase(uri, fromBase, toBase) { + if (uri.match(absUrlRegEx) || uri.match(protocolRegEx)) + return uri; + uri = removeDoubleSlashes(uri); + // if toBase specifies a protocol path, ensure this is the same protocol as fromBase, if not + // use absolute path at fromBase + var toBaseProtocol = toBase.match(protocolRegEx); + var fromBaseProtocol = fromBase.match(protocolRegEx); + if (fromBaseProtocol && (!toBaseProtocol || toBaseProtocol[1] != fromBaseProtocol[1] || toBaseProtocol[2] != fromBaseProtocol[2])) + return absoluteURI(uri, fromBase); + + else { + return relativeURI(absoluteURI(uri, fromBase), toBase); + } + }; + + // given a relative URI, calculate the absolute URI + function absoluteURI(uri, base) { + if (uri.substr(0, 2) == './') + uri = uri.substr(2); + + // absolute urls are left in tact + if (uri.match(absUrlRegEx) || uri.match(protocolRegEx)) + return uri; + + var baseParts = base.split('/'); + var uriParts = uri.split('/'); + + baseParts.pop(); + + while (curPart = uriParts.shift()) + if (curPart == '..') + baseParts.pop(); + else + baseParts.push(curPart); + + return baseParts.join('/'); + }; + + + // given an absolute URI, calculate the relative URI + function relativeURI(uri, base) { + + // reduce base and uri strings to just their difference string + var baseParts = base.split('/'); + baseParts.pop(); + base = baseParts.join('/') + '/'; + i = 0; + while (base.substr(i, 1) == uri.substr(i, 1)) + i++; + while (base.substr(i, 1) != '/') + i--; + base = base.substr(i + 1); + uri = uri.substr(i + 1); + + // each base folder difference is thus a backtrack + baseParts = base.split('/'); + var uriParts = uri.split('/'); + out = ''; + while (baseParts.shift()) + out += '../'; + + // finally add uri parts + while (curPart = uriParts.shift()) + out += curPart + '/'; + + return out.substr(0, out.length - 1); + }; + + var normalizeCSS = function (source, fromBase, toBase) { + + fromBase = removeDoubleSlashes(fromBase); + toBase = removeDoubleSlashes(toBase); + + var urlRegEx = /@import\s*("([^"]*)"|'([^']*)')|url\s*\(\s*(\s*"([^"]*)"|'([^']*)'|[^\)]*\s*)\s*\)/ig; + var result, url, source; + + while (result = urlRegEx.exec(source)) { + url = result[3] || result[2] || result[5] || result[6] || result[4]; + var newUrl; + newUrl = convertURIBase(url, fromBase, toBase); + var quoteLen = result[5] || result[6] ? 1 : 0; + source = source.substr(0, urlRegEx.lastIndex - url.length - quoteLen - 1) + newUrl + source.substr(urlRegEx.lastIndex - quoteLen - 1); + urlRegEx.lastIndex = urlRegEx.lastIndex + (newUrl.length - url.length); + } + + return source; + }; + + normalizeCSS.convertURIBase = convertURIBase; + normalizeCSS.absoluteURI = absoluteURI; + normalizeCSS.relativeURI = relativeURI; + + return normalizeCSS; +}); +//>>excludeEnd('excludeRequireCss') -- cgit v1.2.3