From c1928a8432663430653d0acd8d6e8127e5b4c8ce Mon Sep 17 00:00:00 2001 From: Rob Brackett Date: Thu, 18 Dec 2014 16:39:18 -0500 Subject: [PATCH 1/3] Starter tests for addressing #3. --- package.json | 26 ++++++++++++++++++++++++++ test/parser.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 package.json create mode 100644 test/parser.js diff --git a/package.json b/package.json new file mode 100644 index 0000000..5f36632 --- /dev/null +++ b/package.json @@ -0,0 +1,26 @@ +{ + "name": "element-query", + "version": "0.0.1", + "description": "JS prollyfill for \"element queries\"", + "main": "index.js", + "directories": { + "test": "test" + }, + "dependencies": {}, + "devDependencies": { + "mocha": "^2.0.1" + }, + "scripts": { + "test": "mocha" + }, + "repository": { + "type": "git", + "url": "https://github.com/Mr0grog/element-query.git" + }, + "author": "Rob Brackett", + "license": "MIT", + "bugs": { + "url": "https://github.com/Mr0grog/element-query/issues" + }, + "homepage": "https://github.com/Mr0grog/element-query" +} diff --git a/test/parser.js b/test/parser.js new file mode 100644 index 0000000..3949738 --- /dev/null +++ b/test/parser.js @@ -0,0 +1,31 @@ +var assert = require("assert"); + +// For now, we need an `elementQuery` object in the global space for the +// parser to attach to. This could probably be done better in the future. +global.elementQuery = { + classNameForRules: function(rules) { + var name = "query"; + for (var i = 0, len = rules.length; i < len; i++) { + name += "_" + rules[i].property + "_" + rules[i].value + rules[i].units; + } + return name; + } +} +require("../lib/parser.js"); + +describe("Parser", function() { + + it("can detect a simple element query", function() { + var result = elementQuery.parser.parseStyleText( + ".test-element:media(max-available-width: 30em) { background: purple; }" + ); + assert.equal(result.queries.length, 1, "Should have found 1 query."); + assert.equal(result.queries[0].selector, ".test-element", "The selector for the query should be '.test-element'."); + + assert.equal(result.queries[0].rules.length, 1, "There should be one rule in the query."); + assert.equal(result.queries[0].rules[0].property, "max-available-width", "The query should be for 'max-available-width'."); + assert.equal(result.queries[0].rules[0].value, 30, "The query's value should be 30."); + assert.equal(result.queries[0].rules[0].units, "em", "The query's units should be 'em'"); + }); + +}); From 979719120228689f2f1dacf98dca14bf2034697b Mon Sep 17 00:00:00 2001 From: Rob Brackett Date: Thu, 18 Dec 2014 16:52:14 -0500 Subject: [PATCH 2/3] A few more parser tests --- test/parser.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/test/parser.js b/test/parser.js index 3949738..79dd716 100644 --- a/test/parser.js +++ b/test/parser.js @@ -28,4 +28,59 @@ describe("Parser", function() { assert.equal(result.queries[0].rules[0].units, "em", "The query's units should be 'em'"); }); + it("skips @rules", function() { + var result = elementQuery.parser.parseStyleText( + "@import url(\"test2.css\");\ + .test-element { background: purple; }" + ); + assert.equal(result.queries.length, 0, "Should not have found any queries."); + }); + + it("skips @rules like @font-face that have a block of properties", function() { + var result = elementQuery.parser.parseStyleText( + "@font-face {\ + font-family: \"font of all knowledge\";\ + src: local(\"font of all knowledge\"), local(fontofallknowledge), url(fontofallknowledge.woff);\ + font-weight: 400;\ + font-style: normal;\ + }" + ); + assert.equal(result.queries.length, 0, "Should not have found any queries."); + }); + + it("finds element queries inside media queries", function() { + var result = elementQuery.parser.parseStyleText( + "@media all and (-webkit-min-device-pixel-ratio: 5) {\ + .test-element {\ + background: red;\ + }\ + .test-element:media(max-available-width: 30em) {\ + background: yellow;\ + }\ + }" + ); + assert.equal(result.queries.length, 1, "Should have found 1 query."); + console.log(result); + }); + + it("finds element queries inside nested media queries", function() { + var result = elementQuery.parser.parseStyleText( + "@media all and (-webkit-min-device-pixel-ratio: 5) {\ + .test-element {\ + background: red;\ + }\ + .test-element:media(max-available-width: 30em) {\ + background: yellow;\ + }\ + /* UHOH, nested queries! */\ + @media all and (-webkit-min-device-pixel-ratio: 6) {\ + .test-element:media(max-available-width: 40em) {\ + background: rainbow;\ + }\ + }\ + }" + ); + assert.equal(result.queries.length, 2, "Should have found 2 queries."); + }); + }); From da9ee174e81f2e1018c1c5cefddcb0f0f15d542b Mon Sep 17 00:00:00 2001 From: Rob Brackett Date: Thu, 18 Dec 2014 16:53:07 -0500 Subject: [PATCH 3/3] Ignore node_modules directory --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules