Skip to content

Commit 019f6ff

Browse files
committed
feat: add support for javascript tags
1 parent be6274a commit 019f6ff

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

lib/sources/html-source.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,46 @@
11
const _ = require('lodash')
22
const HtmlParser = require('htmlparser2').Parser
33
const ParsedSource = require('./parsed-source')
4+
const JsSource = require('./js-source')
5+
6+
const DEFAULT_ATTRIBUTES = ['id', 'class', 'ng-class', 'data-ng-class']
47

58
class HtmlSource extends ParsedSource {
69
get type() {
710
return 'html'
811
}
912

13+
_findToken(selector) {
14+
return _.find(this._parsed.children, source => source.contains(selector))
15+
}
16+
1017
static get joiner() {
1118
return '\n<!-- joined by nukecss -->\n'
1219
}
1320

14-
static parse(text) {
21+
static parse(text, opts) {
1522
const tokens = new Set()
23+
const children = []
24+
const attributeKeys = _.get(opts, 'html.attributes', DEFAULT_ATTRIBUTES)
1625

1726
let error
27+
let innerText
1828
const parser = new HtmlParser({
1929
onopentag(name, attributes) {
20-
[name, attributes.id, attributes.class]
30+
const attributeValues = Array.isArray(attributeKeys) ?
31+
attributeKeys.map(key => attributes[key]) : []
32+
attributeValues.concat([name])
2133
.filter(candidate => typeof candidate === 'string')
2234
.forEach(candidate => tokens.add(candidate.toLowerCase()))
2335
},
36+
ontext(value) {
37+
innerText = value
38+
},
39+
onclosetag(name) {
40+
if (name === 'script' && innerText) {
41+
children.push(JsSource.from(innerText))
42+
}
43+
},
2444
onerror(err) {
2545
error = err
2646
},
@@ -35,7 +55,7 @@ class HtmlSource extends ParsedSource {
3555
throw new Error('No tokens found')
3656
}
3757

38-
return tokens
58+
return {tokens, children}
3959
}
4060
}
4161

lib/sources/js-source.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class JsSource extends ParsedSource {
3737
ancestry.pop()
3838
},
3939
})
40-
return tokens
40+
return {tokens}
4141
}
4242
}
4343

lib/sources/parsed-source.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ const debug = require('debug')('nukecss:parsed-source')
33
const SimpleSource = require('./simple-source')
44

55
class ParsedSource {
6-
constructor(text, tokens, opts = {}) {
6+
constructor(text, parsed, opts = {}) {
77
this._text = text
8+
this._parsed = parsed
89
this._options = opts
9-
this._tokensArray = Array.from(tokens).map(t => t.toLowerCase())
10+
this._tokensArray = Array.from(parsed.tokens).map(t => t.toLowerCase())
1011
this._tokens = new Set(this._tokensArray)
1112
}
1213

@@ -34,13 +35,22 @@ class ParsedSource {
3435
return false
3536
}
3637

38+
_findToken() {
39+
return false
40+
}
41+
3742
contains(selector) {
3843
selector = selector.toLowerCase()
3944
return Boolean(this._tokens.has(selector) ||
45+
this._findToken(selector) ||
4046
this._findWholeSelectorInTokens(selector) ||
4147
this._findSelectorPartsInTokens(selector))
4248
}
4349

50+
_joinParsed(parsedA, parsedB, tokens) {
51+
return Object.assign({}, parsedA, parsedB, {tokens})
52+
}
53+
4454
join(that) {
4555
const Class = this.constructor
4656
if (this.type !== that.type) {
@@ -53,14 +63,15 @@ class ParsedSource {
5363
const thatTokens = that._tokensArray || []
5464
const tokens = new Set(thisTokens.concat(thatTokens))
5565
const joiner = Class.joiner
56-
return new Class(`${this._text}${joiner}${that._text}`, tokens, this._options)
66+
const parsed = this._joinParsed(this._parsed, that._parsed, tokens)
67+
return new Class(`${this._text}${joiner}${that._text}`, parsed, this._options)
5768
}
5869

5970
static from(text, opts) {
6071
try {
6172
const Class = this
62-
const tokens = Class.parse(text, opts)
63-
return new Class(text, tokens, opts)
73+
const parsed = Class.parse(text, opts)
74+
return new Class(text, parsed, opts)
6475
} catch (err) {
6576
debug(err)
6677
return new SimpleSource(text, opts)

test/sources/html-source.test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ describe('sources/html-source.js', () => {
4444
<div class="several classes in-a-row">
4545
<h1>My Header</h1>
4646
<p class="lead">Examplelongtext</p>
47+
<div ng-class="{strike: deleted}" ng-repeat="value in obj">
48+
{{template value}}
49+
</div>
4750
</div>
4851
</div>
4952
<script>
@@ -66,6 +69,7 @@ describe('sources/html-source.js', () => {
6669
expect(source).to.contain('container')
6770
expect(source).to.contain('ConTainer')
6871
expect(source).to.contain('lead')
72+
expect(source).to.contain('strike')
6973
})
7074

7175
it('should find tokens as multiple classes', () => {
@@ -80,13 +84,18 @@ describe('sources/html-source.js', () => {
8084

8185
it('should not find tokens as other attribtues', () => {
8286
expect(source).to.not.contain('stylesheet')
83-
expect(source).to.not.contain('javascript')
87+
expect(source).to.not.contain('text/javascript')
8488
})
8589

8690
it('should not find tokens as text', () => {
8791
expect(source).to.not.contain('Header')
8892
expect(source).to.not.contain('examplelongtext')
8993
})
94+
95+
it('should find tokens in script', () => {
96+
expect(source).to.contain('my-javascript-class')
97+
expect(source).to.not.contain('myJsVar')
98+
})
9099
})
91100
})
92101
})

0 commit comments

Comments
 (0)