Skip to content

TagSpaces is an offline, open source, document manager with tagging support

Integrating of requirecss library

uggrock1d893080d652
diff --git a/data/libs/requirecss/css.js b/data/libs/requirecss/css.js
new file mode 100644
index 000000000..88afd8c87
--- /dev/null
+++ b/data/libs/requirecss/css.js
@@ -0,0 +1,156 @@
+/*
+ * css! loader plugin
+ * Allows for loading stylesheets with the 'css!' syntax.
+ *
+ * External stylesheets supported.
+ *
+ * '!' suffix skips load checking
+ *
+ */
+define(['./normalize'], function(normalize) {
+ if (typeof window == 'undefined')
+ return { load: function(n, r, load){ load() } };
+
+ var head = document.getElementsByTagName('head')[0];
+
+
+ /* XHR code - copied from RequireJS text plugin */
+ var progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
+ var get = function(url, callback, errback) {
+
+ var xhr, i, progId;
+ if (typeof XMLHttpRequest !== 'undefined')
+ xhr = new XMLHttpRequest();
+ else if (typeof ActiveXObject !== 'undefined')
+ for (i = 0; i < 3; i += 1) {
+ progId = progIds[i];
+ try {
+ xhr = new ActiveXObject(progId);
+ }
+ catch (e) {}
+
+ if (xhr) {
+ progIds = [progId]; // so faster next time
+ break;
+ }
+ }
+
+ xhr.open('GET', url, requirejs.inlineRequire ? false : true);
+
+ xhr.onreadystatechange = function (evt) {
+ var status, err;
+ //Do not explicitly handle errors, those should be
+ //visible via console output in the browser.
+ if (xhr.readyState === 4) {
+ status = xhr.status;
+ if (status > 399 && status < 600) {
+ //An http 4xx or 5xx error. Signal an error.
+ err = new Error(url + ' HTTP status: ' + status);
+ err.xhr = xhr;
+ errback(err);
+ }
+ else
+ callback(xhr.responseText);
+ }
+ };
+
+ xhr.send(null);
+ }
+
+ //main api object
+ var cssAPI = {};
+
+ cssAPI.pluginBuilder = './css-builder';
+
+ //<style> tag creation
+ var stylesheet = document.createElement('style');
+ stylesheet.type = 'text/css';
+ head.appendChild(stylesheet);
+
+ if (stylesheet.styleSheet)
+ cssAPI.inject = function(css) {
+ stylesheet.styleSheet.cssText += css;
+ }
+ else
+ cssAPI.inject = function(css) {
+ stylesheet.appendChild(document.createTextNode(css));
+ }
+
+ cssAPI.inspect = function() {
+ if (stylesheet.styleSheet)
+ return stylesheet.styleSheet.cssText;
+ else if (stylesheet.innerHTML)
+ return stylesheet.innerHTML;
+ }
+
+ var instantCallbacks = {};
+ cssAPI.normalize = function(name, normalize) {
+ var instantCallback;
+ if (name.substr(name.length - 1, 1) == '!')
+ instantCallback = true;
+ if (instantCallback)
+ name = name.substr(0, name.length - 1);
+ if (name.substr(name.length - 4, 4) == '.css')
+ name = name.substr(0, name.length - 4);
+
+ name = normalize(name);
+
+ if (instantCallback)
+ instantCallbacks[name] = instantCallback;
+
+ return name;
+ }
+
+ cssAPI.load = function(cssId, req, load, config, parse) {
+ var instantCallback = instantCallbacks[cssId];
+ if (instantCallback)
+ delete instantCallbacks[cssId];
+
+ var fileUrl = cssId;
+
+ if (fileUrl.substr(fileUrl.length - 4, 4) != '.css' && !parse)
+ fileUrl += '.css';
+
+ fileUrl = req.toUrl(fileUrl);
+
+ //external url -> add as a <link> tag to load. onload support not reliable so not provided
+ if (fileUrl.substr(0, 7) == 'http://' || fileUrl.substr(0, 8) == 'https://') {
+ if (parse)
+ throw 'Cannot preprocess external css.';
+ var link = document.createElement('link');
+ link.type = 'text/css';
+ link.rel = 'stylesheet';
+ link.href = fileUrl;
+ head.appendChild(link);
+
+ //only instant callback due to onload not being reliable
+ load(cssAPI);
+ }
+ //internal url -> download and inject into <style> tag
+ else {
+ get(fileUrl, function(css) {
+ if (parse)
+ css = parse(css);
+
+ var pathname = window.location.pathname.split('/');
+ pathname.pop();
+ pathname = pathname.join('/') + '/';
+
+ //make file url absolute
+ if (fileUrl.substr(0, 1) != '/')
+ fileUrl = '/' + normalize.convertURIBase(fileUrl, pathname, '/');
+
+ css = normalize(css, fileUrl, pathname);
+
+ cssAPI.inject(css);
+
+ if (!instantCallback)
+ load(cssAPI);
+ });
+ if (instantCallback)
+ load(cssAPI);
+ }
+ }
+
+ return cssAPI;
+});
diff --git a/data/libs/requirecss/normalize.js b/data/libs/requirecss/normalize.js
new file mode 100644
index 000000000..a66e72396
--- /dev/null
+++ b/data/libs/requirecss/normalize.js
@@ -0,0 +1,119 @@
+/*
+ * 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(['require', 'module'], function(require, module) {
+
+ function convertURIBase(uri, fromBase, toBase) {
+ // absolute urls are left in tact
+ if (uri.match(/^\/|([^\:\/]*:)/))
+ return uri;
+ 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);
+
+ 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) {
+
+ var urlRegEx = /(url\(\s*"(.*)"\s*\))|(url\(\s*'(.*)'\s*\))|(url\(\s*(.*)\s*\))/g;
+ var result, url, source;
+
+ while (result = urlRegEx.exec(source)) {
+ url = result[2] || result[4] || result[6];
+ var newUrl = convertURIBase(url, fromBase, toBase);
+ var quoteLen = result[2] || result[4] ? 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);
+ }
+
+ var importRegEx = /(@import\s*'(.*)')|(@import\s*"(.*)")/g;
+
+ while (result = importRegEx.exec(source)) {
+ url = result[2] || result[4];
+ var newUrl = convertURIBase(url, fromBase, toBase);
+ source = source.substr(0, importRegEx.lastIndex - url.length - 1) + newUrl + source.substr(importRegEx.lastIndex - 1);
+ importRegEx.lastIndex = importRegEx.lastIndex + (newUrl.length - url.length);
+ }
+
+ return source;
+ };
+
+ normalizeCSS.convertURIBase = convertURIBase;
+
+ return normalizeCSS;
+});
diff --git a/data/libs/requirejs/css.js b/data/libs/requirejs/css.js
deleted file mode 100644
index fbf5fb6ba..000000000
--- a/data/libs/requirejs/css.js
+++ /dev/null
@@ -1,144 +0,0 @@
-/**
- * @license RequireCSS 0.3.1 Copyright (c) 2011, VIISON All Rights Reserved.
- * Available via the MIT or new BSD license.
- * see: http://github.com/VIISON/RequireCSS for details
- */
-
-/*jslint forin: true */
-/*global document: true, setTimeout: true, define: true */
-
-(function () {
- "use strict";
-
- var doc = document,
- head = doc.head || doc.getElementsByTagName('head')[0],
- // Eliminate browsers that admit to not support the link load event (e.g. Firefox < 9)
- nativeLoad = doc.createElement('link').onload === null ? undefined : false,
- a = doc.createElement('a');
-
- function createLink(url) {
- var link = doc.createElement('link');
-
- link.rel = "stylesheet";
- link.type = "text/css";
- link.href = url;
-
- return link;
- }
-
- function styleSheetLoaded(url) {
- var i;
-
- // Get absolute url by assigning to a link and reading it back below
- a.href = url;
-
- for (i in doc.styleSheets) {
- if (doc.styleSheets[i].href === a.href) {
- return true;
- }
- }
-
- return false;
- }
-
- /**
- * Load using the browsers built-in load event on link tags
- */
- function loadLink(url, load) {
- var link = createLink(url);
-
- link.onload = function () {
- load();
- };
-
- head.appendChild(link);
- };
-
- /**
- * Insert a script tag and use it's onload & onerror to know when
- * the CSS is loaded, this will unfortunately also fire on other
- * errors (file not found, network problems)
- */
- function loadScript(url, load) {
- var link = createLink(url),
- script = doc.createElement('script');
-
- head.appendChild(link);
-
- script.onload = script.onerror = function () {
- head.removeChild(script);
-
- // In Safari the stylesheet might not yet be applied, when
- // the script is loaded so we poll document.styleSheets for it
- var checkLoaded = function () {
- if (styleSheetLoaded(url)) {
- load();
-
- return;
- }
-
- setTimeout(checkLoaded, 25);
- };
- checkLoaded();
- };
- script.src = url;
-
- head.appendChild(script);
- };
-
- function loadSwitch(url, load) {
- if (nativeLoad) {
- loadLink(url, load);
- } else {
- loadScript(url, load);
- }
- };
-
- define(function () {
- var css;
-
- css = {
- version: '0.3.1',
-
- load: function (name, req, load) { //, config (not used)
- // convert name to actual url
- var url = req.toUrl(
- // Append default extension
- name.search(/\.(css|less|scss)$/i) === -1 ? name + '.css' : name
- );
-
- // Test if the browser supports the link load event,
- // in case we don't know yet (mostly WebKit)
- if (nativeLoad === undefined) {
- // Create a link element with a data url,
- // it would fire a load event immediately
- var link = createLink('data:text/css,');
-
- link.onload = function () {
- // Native link load event works
- nativeLoad = true;
- };
-
- head.appendChild(link);
-
- // Schedule function in event loop, this will
- // execute after a potential execution of the link onload
- setTimeout(function () {
- head.removeChild(link);
-
- if (nativeLoad !== true) {
- // Native link load event is broken
- nativeLoad = false;
- }
-
- loadSwitch(url, load);
- }, 0);
- } else {
- loadSwitch(url, load);
- }
- }
- };
-
- return css;
- });
-}());
\ No newline at end of file