Skip to content

Commit

Permalink
test: add html attributes test
Browse files Browse the repository at this point in the history
  • Loading branch information
woai3c committed Jan 1, 2023
1 parent 8966613 commit 01c4418
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
7 changes: 5 additions & 2 deletions examples/HTMLParser.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
console.log(parse('<html><body><div>test!</div><\/body><\/html>'))
console.log(parse(`
<html>
<body >
<div>test!</div>
<body id=" body " data-index="1" >
<div class="lightblue test">test!</div>
<\/body>
<\/html>
`))
console.log(parse(`
<div class="lightblue test" id=" div " data-index="1">test!</div>
`))
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion src/HTMLParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export default class Parser {
}

this.index++
ele.attributes[attr] = value
ele.attributes[attr] = value.trim()
this.sliceHTML()
}

Expand Down
26 changes: 24 additions & 2 deletions tests/HTMLParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,30 @@ describe('HTMLParser test', () => {
expect(htmlTemplate).toBe(JSON.stringify(html, null, 4))
})

// TODO
const htmlTemplate3 = `{
"tagName": "div",
"attributes": {
"class": "lightblue test",
"id": "div",
"data-index": "1"
},
"children": [
{
"nodeValue": "test!",
"nodeType": 3
}
],
"nodeType": 1
}`

test('parse html attributes', () => {

const parser = new HTMLParser()
const parseResult = JSON.stringify(
parser.parse('<div class="lightblue test" id=" div " data-index="1">test!</div>'),
null,
4,
)

expect(htmlTemplate3).toBe(parseResult)
})
})

0 comments on commit 01c4418

Please sign in to comment.