Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from jarrodldavis/bugfix/template-tags
Browse files Browse the repository at this point in the history
Add support for <template> tags
  • Loading branch information
Ffloriel committed Jan 26, 2019
2 parents 7c02448 + 0f47fee commit b50cfeb
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 24 deletions.
34 changes: 34 additions & 0 deletions __tests__/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,37 @@ export const TEST_1_TAG = ["html", "head", "title", "body", "div", "a", "input"]
export const TEST_1_CLASS = ["test-container", "test-footer", "a-link"]

export const TEST_1_ID = ["a-link", "blo"]

export const TEST_2_CONTENT = `
<template>
<div id="app">
<div class="test-container">Well</div>
<div class="test-footer" id="an-id"></div>
<a href="#" id="a-link" class="a-link"></a>
<input id="blo" type="text" disabled/>
</div>
</template>
<script>
export default {
name: 'its-just-a-test'
}
</script>
<style>
.test-container {
color: darkgray;
text-align: center;
}
.test-footer {
font-weight: bold;
}
</style>
`

export const TEST_2_TAG = ["div", "a", "input"]

export const TEST_2_CLASS = ["test-container", "test-footer", "a-link"]

export const TEST_2_ID = ["a-link", "blo"]
82 changes: 58 additions & 24 deletions __tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,67 @@
import purgehtml from "./../index.js"
import { TEST_1_CONTENT, TEST_1_TAG, TEST_1_CLASS, TEST_1_ID } from "./data"
import { TEST_2_CONTENT, TEST_2_TAG, TEST_2_CLASS, TEST_2_ID } from "./data"

describe("purgehtml", () => {
it("finds tag selectors", () => {
const received = purgehtml.extract(TEST_1_CONTENT)
for (let item of TEST_1_TAG) {
expect(received.includes(item)).toBe(true)
}
})
describe("from a normal html document", () => {
it("finds tag selectors", () => {
const received = purgehtml.extract(TEST_1_CONTENT)
for (let item of TEST_1_TAG) {
expect(received.includes(item)).toBe(true)
}
})

it("finds classes selectors", () => {
const received = purgehtml.extract(TEST_1_CONTENT)
for (let item of TEST_1_CLASS) {
expect(received.includes(item)).toBe(true)
}
})
it("finds classes selectors", () => {
const received = purgehtml.extract(TEST_1_CONTENT)
for (let item of TEST_1_CLASS) {
expect(received.includes(item)).toBe(true)
}
})

it("finds id selectors", () => {
const received = purgehtml.extract(TEST_1_CONTENT)
for (let item of TEST_1_ID) {
expect(received.includes(item)).toBe(true)
}
})
it("finds id selectors", () => {
const received = purgehtml.extract(TEST_1_CONTENT)
for (let item of TEST_1_ID) {
expect(received.includes(item)).toBe(true)
}
})

it("finds all selectors", () => {
const received = purgehtml.extract(TEST_1_CONTENT)
const selectors = [...TEST_1_TAG, ...TEST_1_CLASS, ...TEST_1_ID]
for (let item of selectors) {
expect(received.includes(item)).toBe(true)
}
})
});

describe("from a template tag", () => {
it("finds tag selectors", () => {
const received = purgehtml.extract(TEST_2_CONTENT)
for (let item of TEST_2_TAG) {
expect(received.includes(item)).toBe(true)
}
})

it("finds classes selectors", () => {
const received = purgehtml.extract(TEST_2_CONTENT)
for (let item of TEST_2_CLASS) {
expect(received.includes(item)).toBe(true)
}
})

it("finds id selectors", () => {
const received = purgehtml.extract(TEST_2_CONTENT)
for (let item of TEST_2_ID) {
expect(received.includes(item)).toBe(true)
}
})

it("finds all selectors", () => {
const received = purgehtml.extract(TEST_1_CONTENT)
const selectors = [...TEST_1_TAG, ...TEST_1_CLASS, ...TEST_1_ID]
for (let item of selectors) {
expect(received.includes(item)).toBe(true)
}
it("finds all selectors", () => {
const received = purgehtml.extract(TEST_2_CONTENT)
const selectors = [...TEST_2_TAG, ...TEST_2_CLASS, ...TEST_2_ID]
for (let item of selectors) {
expect(received.includes(item)).toBe(true)
}
})
})
})
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ const getSelectorsInNodes = node => {
...ids,
...getSelectorsInNodes(childNode)
]
} else if (childNode.type === "root") {
selectors = [
...selectors,
...getSelectorsInNodes(childNode)
]
}
}
return selectors
Expand Down
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
testEnvironment: "node"
}

0 comments on commit b50cfeb

Please sign in to comment.