Skip to content

Commit 991a60a

Browse files
committed
remove ignore comment
1 parent 3f80766 commit 991a60a

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

__tests__/purgecssDefault.test.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,21 @@ describe('purge methods with files and default extractor', () => {
204204
})
205205

206206
describe('ignore comment', () => {
207-
it('ignores h1', () => {
207+
let purgecssResult
208+
beforeAll(() => {
208209
const purgecss = new Purgecss({
209210
content: [`${root}ignore_comment/ignore_comment.html`],
210211
css: [`${root}ignore_comment/ignore_comment.css`]
211212
})
212-
const result = purgecss.purge()[0].css
213-
expect(result.includes('h1')).toBe(true)
213+
purgecssResult = purgecss.purge()[0].css
214+
})
215+
it('ignores h1', () => {
216+
expect(purgecssResult.includes('h1')).toBe(true)
217+
})
218+
219+
it('removes the comment', () => {
220+
console.log(purgecssResult)
221+
expect(purgecssResult.includes('/* purgecss ignore */')).toBe(false)
214222
})
215223
})
216224

@@ -232,6 +240,12 @@ describe('purge methods with files and default extractor', () => {
232240
it('removes h4', () => {
233241
expect(purgecssResult.includes('h4')).toBe(false)
234242
})
243+
244+
it('removes the comments', () => {
245+
console.log(purgecssResult)
246+
expect(purgecssResult.includes('/* purgecss start ignore */')).toBe(false)
247+
expect(purgecssResult.includes('/* purgecss end ignore */')).toBe(false)
248+
})
235249
})
236250

237251
describe('font-face', () => {
@@ -289,7 +303,7 @@ describe('purge methods with files and default extractor', () => {
289303
})
290304

291305
it('conserves empty attributes', () => {
292-
expect(purgecssResult.includes('input[value=""]')).toBe(true);
306+
expect(purgecssResult.includes('input[value=""]')).toBe(true)
293307
})
294308
})
295309

src/index.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ class Purgecss {
8989
// Get selectors from content files
9090
const { content, extractors, css } = this.options
9191

92-
const fileFormatContents = ((content.filter(o => typeof o === 'string'): Array<any>): Array<
93-
string
94-
>)
95-
const rawFormatContents = ((content.filter(o => typeof o === 'object'): Array<any>): Array<
96-
RawContent
97-
>)
92+
const fileFormatContents = ((content.filter(
93+
o => typeof o === 'string'
94+
): Array<any>): Array<string>)
95+
const rawFormatContents = ((content.filter(
96+
o => typeof o === 'object'
97+
): Array<any>): Array<RawContent>)
9898

9999
const cssFileSelectors = this.extractFileSelector(fileFormatContents, extractors)
100100
const cssRawSelectors = this.extractRawSelector(rawFormatContents, extractors)
@@ -244,9 +244,10 @@ class Purgecss {
244244
extractSelectors(content: string, extractor: Object | Function): Set<string> {
245245
let selectors = new Set()
246246

247-
const arraySelector = typeof extractor.extract === 'undefined'
248-
? extractor(content)
249-
: extractor.extract(content)
247+
const arraySelector =
248+
typeof extractor.extract === 'undefined'
249+
? extractor(content)
250+
: extractor.extract(content)
250251
if (arraySelector === null) {
251252
throw new Error(ERROR_EXTRACTER_FAILED)
252253
}
@@ -271,8 +272,15 @@ class Purgecss {
271272
return this.evaluateAtRule(node)
272273
}
273274
if (node.type === 'comment') {
274-
if (this.isIgnoreAnnotation(node, 'start')) this.ignore = true
275-
else if (this.isIgnoreAnnotation(node, 'end')) this.ignore = false
275+
if (this.isIgnoreAnnotation(node, 'start')) {
276+
this.ignore = true
277+
// remove ignore annotation
278+
node.remove()
279+
} else if (this.isIgnoreAnnotation(node, 'end')) {
280+
this.ignore = false
281+
// remove ignore annotation
282+
node.remove()
283+
}
276284
}
277285
})
278286
}
@@ -284,7 +292,15 @@ class Purgecss {
284292
*/
285293
evaluateRule(node: Object, selectors: Set<string>) {
286294
const annotation = node.prev()
287-
if (this.isIgnoreAnnotation(annotation, 'next') || this.ignore === true) return
295+
if (this.ignore) return
296+
if (
297+
typeof annotation !== 'undefined' &&
298+
annotation.type === 'comment' &&
299+
this.isIgnoreAnnotation(annotation, 'next')
300+
) {
301+
annotation.remove()
302+
return
303+
}
288304

289305
let keepSelector = true
290306
node.selector = selectorParser(selectorsParsed => {

0 commit comments

Comments
 (0)