GithubHelp home page GithubHelp logo

aaronbarker / css-variables-polyfill Goto Github PK

View Code? Open in Web Editor NEW
112.0 8.0 29.0 52 KB

A basic polyfill for CSS Variables/custom-properties

License: MIT License

JavaScript 50.15% CSS 10.01% HTML 39.84%

css-variables-polyfill's Introduction

CSS Variables Polyfill

A basic polyfill for CSS Variables/custom-properties

This is an attempt at a very basic CSS variables (custom properties) polyfil. In reality this is more of a partial polyfill as it will not cover variables inside of variables, DOM scoping or anything else "fancy". Just taking variables declared anywhere in the CSS and then re-parsing the CSS for var() statements and replacing them in browsers that don't natively support CSS variables.

According to caniuse.com, of current browsers only IE, Edge and Opera Mini do not support CSS variables. This polyfill appears to work on all three really well. I don't see why this wouldn't work on older browsers as well, but I haven't been able to test it on them yet.

Changelog

1.2 - 2021-03-19

  • Update findSetters regex and add external test https://regex101.com/r/kWwUmp/3 (some day I will learn to write test cases)
  • let to var for better old browser support

1.1.2 - 2019-03-23

  • Update findSetters and replaceGetters (thanks @CodeZeno)

1.1.1 - 2018-04-30

  • Add support for HTML Imports (thanks @Pilatch)
  • Misc IE11 fixes (thanks @proteantech)

1.0.0 - 2018-03-09

  • Initial release, pulled in from codepen

Todo

  • Verify cross domain working or not (it is working from dropbox)
  • Option to wait to apply anything until all s are parsed or inject what we have and update as each returns
  • Need to test on a more complex CSS file
  • Option to save parsed file in local/session storage so there isn't a delay on additional page loads. Could only do it for links (with URLs to use as keys) and style blocks with IDs of some sort
  • Need to test more complex values like rgba(255,0,0,0.5); and something with !important
  • Try multiple links
  • Local links
  • Ajax driven site, or CSS added later the top of the stack

css-variables-polyfill's People

Contributors

aaronbarker avatar codezeno avatar coliff avatar proteantech avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

css-variables-polyfill's Issues

findSetters is matching some wrong variables

findSetters regex (--[^:)]+:[\s]*[^;}]+) is set to match multi-line characters, so, the following is being match as a variable name-value pair:

/* ----------------------------------------------------- */
@import url("https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap");

I've fixed it using this regex instead of the original one: (--[^:)\n\r]+:[\s]*[^;}]+)

Variables with "var(...)" values

Hello,

I faced a problem in a project where one was my variables was assigned as another one (e.g. --var1: var(--var2);). I managed to solve it by modifying replaceGetters like this, so it will keep iterating through the CSS as long as matches are found:

  replaceGetters: function(curCSS, varList) {
    // console.log(varList);
	let replaced = true;
	while (replaced) {
	  replaced = false;
	  for (let theVar in varList) {
		// console.log(theVar);
		// match the variable with the actual variable name
		let getterRegex = new RegExp('var\\(\\s*' + theVar + '\\s*\\)', 'g');
		// console.log(getterRegex);
		// console.log(curCSS);
		if (curCSS.search(getterRegex, varList[theVar])>=0) {	  
		  curCSS = curCSS.replace(getterRegex, varList[theVar]);
		  replaced = true;
		}

		// now check for any getters that are left that have fallbacks
		let getterRegex2 = new RegExp('var\\([^\\)]+,\\s*([^\\)]+)\\)', 'g');
		// console.log(getterRegex);
		// console.log(curCSS);
		let matches = curCSS.match(getterRegex2);
		while (matches) {
		  // console.log("matches",matches);
		  matches.forEach(function(match) {
		    // console.log(match.match(/var\(.+,\s*(.+)\)/))
			// find the fallback within the getter
			curCSS = curCSS.replace(match, match.match(/var\([^\)]+,\s*([^\)]+)\)/)[1]);
			replaced = true;
		  });
		}
		  // curCSS = curCSS.replace(getterRegex2,varList[theVar]);
	  };
	};
    // console.log(curCSS);
    return curCSS;
  },

Multiple Vars referenced in one line don't work

Something like this doesn't work:

:root {
--lh_grey: pink;
--lh_lightgrey: purple; //#f3f3f3
--timezone-button-background: linear-gradient(to bottom, var(--lh_lightgrey) 0%, var(--lh_grey) 50%, var(--lh_lightgrey) 100%); // This doesn't work.
--timezone-button-background: var(--lh_grey); // This does.
}

Add LICENSE

The lack of a license such as MIT discourages use of this polyfill in our company.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.