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

Add support for <template> tags #1

Merged
merged 6 commits into from Jan 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions __tests__/data.js
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
@@ -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
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
@@ -0,0 +1,3 @@
module.exports = {
testEnvironment: "node"
}