Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dist/cli/htmlhint.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/core/core.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions dist/core/htmlparser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/cli/htmlhint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ function hintFile(filepath: string, ruleset?: Ruleset) {
// ignore
}

return HTMLHint.verify(content, ruleset)
return HTMLHint.verify(content, ruleset, filepath)
}

// hint stdin
Expand Down
8 changes: 6 additions & 2 deletions src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,36 @@
this.rules[rule.id] = rule
}

public verify(html: string, ruleset: Ruleset = this.defaultRuleset) {
public verify(
html: string,
ruleset: Ruleset = this.defaultRuleset,
filepath: string = ''
) {
Comment thread
commodis marked this conversation as resolved.
if (Object.keys(ruleset).length === 0) {
ruleset = this.defaultRuleset
}

// parse inline ruleset
html = html.replace(
/^\s*<!--\s*htmlhint\s+([^\r\n]+?)\s*-->/i,
(all, strRuleset: string) => {
// For example:
// all is '<!-- htmlhint alt-require:true-->'
// strRuleset is 'alt-require:true'
strRuleset.replace(
/(?:^|,)\s*([^:,]+)\s*(?:\:\s*([^,\s]+))?/g,
(all, ruleId: string, value: string | undefined) => {
// For example:
// all is 'alt-require:true'
// ruleId is 'alt-require'
// value is 'true'

ruleset[ruleId] =
value !== undefined && value.length > 0 ? JSON.parse(value) : true

return ''
}
)

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on
library input
may run slow on strings starting with '<!--htmlhint ' and with many repetitions of ' '.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of ' '.

return ''
}
Expand All @@ -74,7 +78,7 @@
}
}

parser.parse(html)
parser.parse(html, filepath)

return reporter.messages
}
Expand Down
4 changes: 3 additions & 1 deletion src/core/htmlparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
long: boolean
close: string
lastEvent?: Partial<Block>
filepath?: string
}

export type Listener = (event: Block) => void
Expand Down Expand Up @@ -49,7 +50,7 @@
return obj
}

public parse(html: string): void {
public parse(html: string, filepath: string = ''): void {
const mapCdataTags = this._mapCdataTags

const regTag =
Expand Down Expand Up @@ -78,6 +79,7 @@
pos: 0,
line: 1,
col: 1,
filepath: filepath,
Comment thread
commodis marked this conversation as resolved.
})

// Do not ignore validation inside <script type="ng/template"> template
Expand Down Expand Up @@ -114,7 +116,7 @@
line++
lastLineIndex = pos + regLine.lastIndex
}
}

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on
library input
may run slow on strings starting with '</' and with many repetitions of '</!'.
This
regular expression
that depends on
library input
may run slow on strings starting with '<!--' and with many repetitions of '<!--a'.
This
regular expression
that depends on
library input
may run slow on strings starting with '<!' and with many repetitions of '<!='.
This
regular expression
that depends on
library input
may run slow on strings starting with '<-' and with many repetitions of ' '.
This
regular expression
that depends on
library input
may run slow on strings starting with '<-' and with many repetitions of ' '.
This
regular expression
that depends on
library input
may run slow on strings starting with '<- ' and with many repetitions of '<- '.
This
regular expression
that depends on
library input
may run slow on strings starting with '<- !' and with many repetitions of '=<- ! '.
This
regular expression
that depends on
library input
may run slow on strings starting with '<- !="' and with many repetitions of '" <- !="'.
This
regular expression
that depends on
library input
may run slow on strings starting with '<- !='' and with many repetitions of '' <- !=''.
This
regular expression
that depends on
library input
may run slow on strings starting with '</' and with many repetitions of '</!'.
This
regular expression
that depends on
library input
may run slow on strings starting with '<!--' and with many repetitions of '<!--a'.
This
regular expression
that depends on
library input
may run slow on strings starting with '<!' and with many repetitions of '<!='.
This
regular expression
that depends on
library input
may run slow on strings starting with '<-' and with many repetitions of ' '.
This
regular expression
that depends on
library input
may run slow on strings starting with '<-' and with many repetitions of ' '.
This
regular expression
that depends on
library input
may run slow on strings starting with '<- ' and with many repetitions of '<- '.
This
regular expression
that depends on
library input
may run slow on strings starting with '<- !' and with many repetitions of '=<- ! '.
This
regular expression
that depends on
library input
may run slow on strings starting with '<- !="' and with many repetitions of '" <- !="'.
This
regular expression
that depends on
library input
may run slow on strings starting with '<- !='' and with many repetitions of '' <- !=''.

while ((match = regTag.exec(html))) {
matchIndex = match.index
Expand Down
28 changes: 28 additions & 0 deletions test/htmlparser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,3 +655,31 @@ describe('HTMLParser: Case parse', () => {
parser.parse('<img src="aaa" alt= />')
})
})

describe('HTMLParser: Filepath', () => {
it('should include filepath in start event when provided to parse', (done) => {
const parser = new HTMLParser()
const arrEvents = []
getAllEvents(parser, arrEvents, () => {
// The start event must be the first event (index 0)
expect(arrEvents[0]).toEvent('start', {
filepath: '/some/example.txt',
})
done()
})
parser.parse('', '/some/example.txt')
})

it('should include empty string filepath in start event when not provided', (done) => {
const parser = new HTMLParser()
const arrEvents = []
getAllEvents(parser, arrEvents, () => {
// The start event must be the first event (index 0)
expect(arrEvents[0]).toEvent('start', {
filepath: '',
})
done()
})
parser.parse('')
})
})
Loading