This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
3,061 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,6 @@ | |
node_modules | ||
dist | ||
*.js | ||
bin/amp-css | ||
!jest.config.js | ||
bin/amp-css | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ test/* | |
*.ts | ||
tsconfig.json | ||
*.iml | ||
.idea | ||
.idea | ||
jest.config.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Contributing | ||
|
||
Feel free to submit any new features via Pull Request. | ||
Make sure the supplied tests still work and to add own test cases to cover your code. | ||
|
||
## Running tests | ||
|
||
Run tests by this command: | ||
|
||
```shell script | ||
yarn test | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module.exports = { | ||
globals: { | ||
'ts-jest': { | ||
tsConfigFile: 'tsconfig.json' | ||
} | ||
}, | ||
moduleFileExtensions: [ | ||
'ts', | ||
'js' | ||
], | ||
transform: { | ||
'^.+\\.(ts|tsx)$': './node_modules/ts-jest/preprocessor.js' | ||
}, | ||
testMatch: [ | ||
'**/test/**/*.test.ts' | ||
], | ||
testEnvironment: 'node' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export abstract class AbstractFileWorker { | ||
protected file: string; | ||
public setFile(file: string) { | ||
this.file = file; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * as assert from "assert"; | ||
import DoneCallback = jest.DoneCallback; | ||
import {CompileCssWorker} from "../../src/Worker/CompileCssWorker"; | ||
import * as path from "path"; | ||
import {AssignCssWorker} from "../../src/Worker/AssignCssWorker"; | ||
|
||
describe("AssignCssWorker", () => { | ||
it("should assign CSS into an empty amp-custom styling tag", (done: DoneCallback) => { | ||
const worker = new AssignCssWorker(); | ||
worker.setHtml('<style amp-custom></style>'); | ||
worker.setCss('MY-COOL-CSS'); | ||
worker.work() | ||
.then((modifiedHTML: string) => { | ||
assert.equal(modifiedHTML, '<style amp-custom>MY-COOL-CSS</style>'); | ||
done(); | ||
}) | ||
.catch(() => done.fail("Worker failed.")); | ||
}); | ||
|
||
it("should replace CSS in an existing amp-custom styling tag", (done: DoneCallback) => { | ||
const worker = new AssignCssWorker(); | ||
worker.setHtml('<style amp-custom>MY-OLD-CSS</style>'); | ||
worker.setCss('MY-COOL-CSS'); | ||
worker.work() | ||
.then((modifiedHTML: string) => { | ||
assert.equal(modifiedHTML, '<style amp-custom>MY-COOL-CSS</style>'); | ||
done(); | ||
}) | ||
.catch(() => done.fail("Worker failed.")); | ||
}); | ||
|
||
it("should assign CSS in multiline amp-custom tag", (done: DoneCallback) => { | ||
const worker = new AssignCssWorker(); | ||
worker.setHtml(`<style amp-custom> | ||
</style>`); | ||
worker.setCss('MY-COOL-CSS'); | ||
worker.work() | ||
.then((modifiedHTML: string) => { | ||
assert.equal(modifiedHTML, '<style amp-custom>MY-COOL-CSS</style>'); | ||
done(); | ||
}) | ||
.catch(() => done.fail("Worker failed.")); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as assert from "assert"; | ||
import DoneCallback = jest.DoneCallback; | ||
import {CompileCssWorker} from "../../src/Worker/CompileCssWorker"; | ||
import * as path from "path"; | ||
|
||
describe("CompileCssWorker", () => { | ||
it("should compile SCSS to CSS. minify = true", (done: DoneCallback) => { | ||
const worker = new CompileCssWorker(); | ||
worker.setFile(path.join(__dirname, '..', 'files', 'simple-file.scss')); | ||
worker.work() | ||
.then((compiledCss: string) => { | ||
assert.equal(compiledCss, 'body{color:red}'); | ||
done(); | ||
}) | ||
.catch(() => done.fail("Worker failed.")); | ||
}); | ||
it("should compile SCSS to CSS. minify = false", (done: DoneCallback) => { | ||
const worker = new CompileCssWorker(); | ||
worker.setFile(path.join(__dirname, '..', 'files', 'simple-file.scss')); | ||
worker.setMinify(false); | ||
worker.work() | ||
.then((compiledCss: string) => { | ||
assert.equal(compiledCss, `body { | ||
color: red; }`); | ||
done(); | ||
}) | ||
.catch((err: string) => done.fail("Worker failed: " + err)); | ||
}); | ||
it("should compile SCSS with import to CSS", (done: DoneCallback) => { | ||
const worker = new CompileCssWorker(); | ||
worker.setFile(path.join(__dirname, '..', 'files', 'advanced-file.scss')); | ||
worker.work() | ||
.then((compiledCss: string) => { | ||
assert.equal(compiledCss, 'body{color:red}'); | ||
done(); | ||
}) | ||
.catch((error: string) => done.fail("Worker failed: " + error)); | ||
}); | ||
it("should return CSS file as is", (done: DoneCallback) => { | ||
const worker = new CompileCssWorker(); | ||
worker.setFile(path.join(__dirname, '..', 'files', 'regular-css.css')); | ||
worker.work() | ||
.then((compiledCss: string) => { | ||
assert.equal(compiledCss, `body { | ||
color: blue; | ||
}`); | ||
done(); | ||
}) | ||
.catch((err: string) => done.fail("Worker failed: " + err)); | ||
}); | ||
it("should not process non-CSS and SCSS files", (done: DoneCallback) => { | ||
const worker = new CompileCssWorker(); | ||
worker.setFile(path.join(__dirname, '..', 'files', 'some-file.html')); | ||
worker.work() | ||
.then(() => done.fail('Validation succeeded even though it should not!')) | ||
.catch(() => done()); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {SanitizeCssWorker} from "../../src/Worker/SanitizeCssWorker"; | ||
import * as assert from "assert"; | ||
import DoneCallback = jest.DoneCallback; | ||
|
||
describe("SanitizeCssWorker", () => { | ||
it("should remove !important from CSS", (done: DoneCallback) => { | ||
const worker = new SanitizeCssWorker(); | ||
worker.setCss(`body{color:red !important;}`); | ||
worker.work() | ||
.then((cleanedCSS: string) => { | ||
assert.equal(cleanedCSS, 'body{color:red ;}'); | ||
done(); | ||
}) | ||
.catch(() => done.fail("Worker failed.")); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {SanitizeCssWorker} from "../../src/Worker/SanitizeCssWorker"; | ||
import * as assert from "assert"; | ||
import DoneCallback = jest.DoneCallback; | ||
import {ValidateCssWorker} from "../../src/Worker/ValidateCssWorker"; | ||
|
||
describe("ValidateCssWorker", () => { | ||
it("should validate valid CSS", (done: DoneCallback) => { | ||
const worker = new ValidateCssWorker(); | ||
worker.setCss(`body{color:red;}`); | ||
worker.work() | ||
.then(() => done()) | ||
.catch(() => done.fail("Validation failed.")); | ||
}); | ||
it("should fail on invalid CSS", (done: DoneCallback) => { | ||
const worker = new ValidateCssWorker(); | ||
worker.setCss(`body{color:red !important;}`); | ||
worker.work() | ||
.then(() => done.fail('Validation succeeded even though it should not!')) | ||
.catch(() => done()); | ||
}); | ||
it("should fail on too large CSS", (done: DoneCallback) => { | ||
const fiftyByteCss = `body { font-size: 15px; border: 1px solid black; }`; // this CSS is 50 byte | ||
let overFiftykBytesCss = ''; | ||
for (let i = 0; i < 1001; i++) { | ||
overFiftykBytesCss = overFiftykBytesCss.concat(fiftyByteCss); | ||
} | ||
|
||
const worker = new ValidateCssWorker(); | ||
worker.setCss(overFiftykBytesCss); | ||
worker.work() | ||
.then(() => done.fail('Validation succeeded even though it should not!')) | ||
.catch(() => done()); | ||
}); | ||
it("should succeed on CSS being exactly 50kb", (done: DoneCallback) => { | ||
const fiftyByteCss = `body { font-size: 15px; border: 1px solid black; }`; // this CSS is 50 byte | ||
let exactlyFiftykBytesCss = ''; | ||
for (let i = 0; i < 1000; i++) { | ||
exactlyFiftykBytesCss = exactlyFiftykBytesCss.concat(fiftyByteCss); | ||
} | ||
|
||
const worker = new ValidateCssWorker(); | ||
worker.setCss(exactlyFiftykBytesCss); | ||
worker.work() | ||
.then(() => done()) | ||
.catch(() => done.fail("Validation failed.")); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
$color: red; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@import "_advanced-file-variables"; | ||
body { | ||
color: $color; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
body { | ||
color: blue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
$color: red; | ||
body { | ||
color: $color; | ||
} |
Oops, something went wrong.