Skip to content

Commit

Permalink
Merge pull request #708 from kethinov/0.6.19
Browse files Browse the repository at this point in the history
0.6.19
  • Loading branch information
kethinov authored Nov 16, 2024
2 parents 98bada9 + 7546e6a commit 7a4d536
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

- Put your changes here...

## 0.6.19

- Made client-side Teddy parser a bit more tolerant of bad markup.

## 0.6.18

- Fixed a bug which caused client-side Teddy to rename yet more form elements accidentally.
Expand Down
25 changes: 20 additions & 5 deletions cheerioPolyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ function parseTeddyDOMFromString (html) {
const selfClosingTags = new Set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'])
const root = document.createElement('body')
const dom = [root]
const openTags = [] // stack to track open tags
const tagAndCommentRegex = /<\/?([a-zA-Z0-9]+)([^>]*)>|<!--([\s\S]*?)-->/g
const attrRegex = /([a-zA-Z0-9-:._]+)(?:=(["'])(.*?)\2)?/g
const attrRegex = /([a-zA-Z0-9-:._]+)(?:=(["'])(.*?)\2|([^>\s]+))?/g
let lastIndex = 0
let match

Expand All @@ -97,8 +98,19 @@ function parseTeddyDOMFromString (html) {
// handle tags
const [fullMatch, tagName, attrString] = match
const isClosingTag = fullMatch.startsWith('</')
if (isClosingTag) dom.pop() // pop the list if it's a closing tag
else {
if (isClosingTag) {
if (selfClosingTags.has(tagName.toLowerCase())) {
// convert incorrect closing tag for self-closing tag to self-closing tag
const element = document.createElement(tagName)
dom[dom.length - 1].appendChild(element)
} else {
// check if the closing tag matches the most recent open tag
if (openTags.length > 0 && openTags[openTags.length - 1] === tagName.toLowerCase()) {
openTags.pop()
dom.pop()
}
}
} else {
// create a new element
const element = document.createElement(tagName)

Expand All @@ -107,7 +119,7 @@ function parseTeddyDOMFromString (html) {
const attrMap = new Map()
while ((attrMatch = attrRegex.exec(attrString)) !== null) {
const attrName = attrMatch[1]
const attrValue = attrMatch[3]
const attrValue = attrMatch[3] || attrMatch[4] || ''

// handle duplicate attributes for special tags
if (attrMap.has(attrName)) {
Expand All @@ -134,7 +146,10 @@ function parseTeddyDOMFromString (html) {
dom[dom.length - 1].appendChild(element)

// push the new element to the dom if it's not self-closing
if (!selfClosingTags.has(tagName.toLowerCase()) && !fullMatch.endsWith('/>')) dom.push(element)
if (!selfClosingTags.has(tagName.toLowerCase()) && !fullMatch.endsWith('/>')) {
dom.push(element)
openTags.push(tagName.toLowerCase())
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"url": "https://github.com/rooseveltframework/teddy/graphs/contributors"
}
],
"version": "0.6.18",
"version": "0.6.19",
"files": [
"dist"
],
Expand Down
5 changes: 5 additions & 0 deletions test/templates/conditionals/oneLineValueVarsNoQuotes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{!
should evaluate one line if "if-something.something={something}" as false and remove attributes
!}

<option value='{something}' if-something.something={something} true='selected'>{something}</option>
6 changes: 6 additions & 0 deletions test/templates/misc/badTag.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{!
should render html with a bad tag correctly
!}

<p>hello</br>world1</p>
<p>hello</li>world2</p>
12 changes: 12 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ export default [
run: async (teddy, template, model, assert, expected) => assert(teddy.render(template, model), expected),
expected: '<option value="Some content">Some content</option>'
},
{
message: 'should evaluate one line if "if-something.something={something}" as false and remove attributes (conditionals/oneLineValueVarsNoQuotes.html)',
template: 'conditionals/oneLineValueVarsNoQuotes',
run: async (teddy, template, model, assert, expected) => assert(teddy.render(template, model), expected),
expected: '<option value="Some content">Some content</option>'
},
{
message: 'should evaluate <option> elements with the middle one selected (conditionals/oneLineValueVarsLooped.html)',
template: 'conditionals/oneLineValueVarsLooped',
Expand Down Expand Up @@ -1568,6 +1574,12 @@ export default [
run: async (teddy, template, model, assert, expected) => assert(teddy.render(template, model), expected),
expected: '<p>0</p>'
},
{
message: 'should render html with a bad tag correctly (misc/badTag.html)',
template: 'misc/badTag',
run: async (teddy, template, model, assert, expected) => assert(teddy.render(template, model), expected),
expected: '<p>hello<br>world1</p><p>helloworld2</p>'
},
{
message: 'should not render Teddy code in server-side comments in loops (misc/serverSideCommentsWithTeddyCode.html)',
template: 'misc/serverSideCommentsWithTeddyCode',
Expand Down

0 comments on commit 7a4d536

Please sign in to comment.