Skip to content

Commit ff12eb9

Browse files
heiskrCopilotCopilotEbonsignori
authored
Upgrade cheerio from 1.0.0-rc.12 to 1.2.0 (#59784)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Evan Bonsignori <ebonsignori@github.com>
1 parent acaaf45 commit ff12eb9

39 files changed

Lines changed: 585 additions & 453 deletions

package-lock.json

Lines changed: 378 additions & 246 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@
179179
"azure-kusto-data": "^7.0.0",
180180
"bottleneck": "2.19.5",
181181
"boxen": "8.0.1",
182-
"cheerio": "^1.0.0-rc.12",
183-
"cheerio-to-text": "0.2.4",
182+
"cheerio": "^1.2.0",
184183
"classnames": "^2.5.1",
185184
"clsx": "^2.1.1",
186185
"connect-timeout": "1.9.1",
@@ -267,7 +266,6 @@
267266
"@octokit/rest": "22.0.0",
268267
"@playwright/test": "^1.56",
269268
"@types/accept-language-parser": "1.5.7",
270-
"@types/cheerio": "^0.22.35",
271269
"@types/connect-timeout": "1.9.0",
272270
"@types/cookie": "0.6.0",
273271
"@types/cookie-parser": "1.4.8",

src/assets/scripts/validate-asset-images.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import path from 'path'
1616

1717
import { program } from 'commander'
1818
import chalk from 'chalk'
19-
import cheerio from 'cheerio'
19+
import { load } from 'cheerio'
2020
import { fileTypeFromFile } from 'file-type'
2121
import walk from 'walk-sync'
2222
import isSVG from 'is-svg'
@@ -138,14 +138,16 @@ async function checkFile(filePath: string) {
138138
}
139139

140140
function checkSVGContent(content: string) {
141-
const $ = cheerio.load(content)
141+
const $ = load(content)
142142
const disallowedTagNames = new Set(['script', 'object', 'iframe', 'embed'])
143143
$('*').each((i, element) => {
144-
const { tagName } = $(element).get(0)
144+
const el = $(element).get(0)
145+
if (!el || !('tagName' in el)) return
146+
const { tagName } = el
145147
if (disallowedTagNames.has(tagName)) {
146148
throw new Error(`contains a <${tagName}> tag`)
147149
}
148-
for (const key in $(element).get(0).attribs) {
150+
for (const key in 'attribs' in el ? el.attribs : {}) {
149151
// Looks for suspicious event handlers on tags.
150152
// For example `<path oNload="alert(1)"" d="M28 0l4.59 4.59-9.76`
151153
// We don't need to do a case-sensitive regex here because cheerio

src/automated-pipelines/tests/rendering.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { readFileSync } from 'fs'
22

3-
import cheerio from 'cheerio'
3+
import { load } from 'cheerio'
44
import { describe, expect, test, vi } from 'vitest'
55

66
import { loadPages } from '@/frame/lib/page-data'
@@ -39,7 +39,7 @@ describe('autogenerated docs render', () => {
3939
return `${res.statusCode} status error on ${url}`
4040
}
4141
// Using `xmlMode: true` is marginally faster
42-
const $ = cheerio.load(res.body, { xmlMode: true })
42+
const $ = load(res.body, { xmlMode: true })
4343
const headingIDs = $('body')
4444
.find('h2, h3, h4, h5, h6')
4545
.map((_, el) => $(el).attr('id'))

src/content-render/tests/annotate.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, test } from 'vitest'
2-
import cheerio from 'cheerio'
2+
import { load } from 'cheerio'
33

44
import { renderContent } from '@/content-render/index'
55
import type { Context } from '@/types'
@@ -20,7 +20,7 @@ on:
2020
describe('annotate', () => {
2121
test('renders annotations', async () => {
2222
const res = await renderContent(example)
23-
const $ = cheerio.load(res)
23+
const $ = load(res)
2424

2525
// Check that the annotation structure is rendered correctly
2626
const annotation = $('.annotate')
@@ -76,7 +76,7 @@ echo "Hello, world!"
7676
\`\`\`
7777
`.trim()
7878
const res = await renderContent(bashExample)
79-
const $ = cheerio.load(res)
79+
const $ = load(res)
8080

8181
const headerCode = $('header pre').text()
8282
expect(headerCode).toMatch(bashExample.split('\n').slice(1, -1).join('\n'))
@@ -104,7 +104,7 @@ on:
104104
`.trim()
105105

106106
const res = await renderContent(emptyCommentExample)
107-
const $ = cheerio.load(res)
107+
const $ = load(res)
108108

109109
const headerCode = $('header pre').text()
110110
expect(headerCode).toMatch(emptyCommentExample.split('\n').slice(1, -1).join('\n'))
@@ -152,7 +152,7 @@ on: [push]
152152
} as unknown as Context
153153

154154
const res = await renderContent(autotitleExample, mockContext)
155-
const $ = cheerio.load(res)
155+
const $ = load(res)
156156

157157
const rows = $('.annotate-row')
158158
const notes = $('.annotate-note', rows)

src/content-render/tests/render-content.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import cheerio from 'cheerio'
1+
import { load } from 'cheerio'
22
import { describe, expect, test } from 'vitest'
33

44
import { renderContent } from '@/content-render/index'
@@ -32,7 +32,7 @@ describe('renderContent', () => {
3232
1. item three`)
3333

3434
const html = await renderContent(template)
35-
const $ = cheerio.load(html, { xmlMode: true })
35+
const $ = load(html, { xmlMode: true })
3636
expect($('ol').length).toBe(1)
3737
expect($('ol > li').length).toBe(3)
3838
})
@@ -43,7 +43,7 @@ describe('renderContent', () => {
4343
- <a>item</a>`)
4444

4545
const html = await renderContent(template)
46-
const $ = cheerio.load(html, { xmlMode: true })
46+
const $ = load(html, { xmlMode: true })
4747
expect($('ul p').length).toBe(0)
4848
})
4949

@@ -69,7 +69,7 @@ describe('renderContent', () => {
6969
|<kbd>g</kbd> <kbd>i</kbd> | Go to the **Issues** tab. For more information, see "[About issues](/articles/about-issues)."
7070
`)
7171
const html = await renderContent(template)
72-
const $ = cheerio.load(html, { xmlMode: true })
72+
const $ = load(html, { xmlMode: true })
7373
expect(
7474
$.html().includes('&quot;<a href="/articles/about-issues">About issues</a>.&quot;'),
7575
).toBeTruthy()
@@ -82,7 +82,7 @@ describe('renderContent', () => {
8282
| Python | \`requirements.txt\`, \`pipfile.lock\`
8383
`)
8484
const html = await renderContent(template)
85-
const $ = cheerio.load(html, { xmlMode: true })
85+
const $ = load(html, { xmlMode: true })
8686
expect(
8787
$.html().includes('<code>requirements.txt</code>, <code>pipfile.lock</code>'),
8888
).toBeTruthy()
@@ -95,7 +95,7 @@ describe('renderContent', () => {
9595
| <code>user:<em>USERNAME</em></code> | [**user:defunkt ubuntu**](https://github.com/search?q=user%3Adefunkt+ubuntu&type=Issues) matches issues with the word "ubuntu" from repositories owned by @defunkt.
9696
`)
9797
const html = await renderContent(template)
98-
const $ = cheerio.load(html, { xmlMode: true })
98+
const $ = load(html, { xmlMode: true })
9999
expect($.html().includes('<code>user:<em>USERNAME</em></code>')).toBeTruthy()
100100
})
101101

@@ -110,7 +110,7 @@ describe('renderContent', () => {
110110
1. This is another list item.
111111
`)
112112
const html = await renderContent(template)
113-
const $ = cheerio.load(html, { xmlMode: true })
113+
const $ = load(html, { xmlMode: true })
114114
expect($('ol').length).toBe(1)
115115
expect($.html().includes('<span class="hljs-meta prompt_"># </span')).toBeTruthy()
116116
expect($.html().includes('some comment here')).toBeTruthy()
@@ -131,7 +131,7 @@ describe('renderContent', () => {
131131
##### This is a level 5
132132
`)
133133
const html = await renderContent(template)
134-
const $ = cheerio.load(html, { xmlMode: true })
134+
const $ = load(html, { xmlMode: true })
135135

136136
for (const level of [1, 2, 3, 4, 5]) {
137137
expect(
@@ -147,7 +147,7 @@ const example = true
147147
\`\`\`\`
148148
`)
149149
let html = await renderContent(template)
150-
let $ = cheerio.load(html, { xmlMode: true })
150+
let $ = load(html, { xmlMode: true })
151151
expect($.html().includes('<pre><code class="hljs language-js">')).toBeTruthy()
152152
expect($.html().includes('<span class="hljs-keyword">const</span>')).toBeTruthy()
153153

@@ -157,7 +157,7 @@ const example = true
157157
\`\`\`\`
158158
`)
159159
html = await renderContent(template)
160-
$ = cheerio.load(html, { xmlMode: true })
160+
$ = load(html, { xmlMode: true })
161161
expect($.html().includes('<pre><code class="hljs language-erb">')).toBeTruthy()
162162
expect($.html().includes('<span class="hljs-variable">@articles</span>')).toBeTruthy()
163163

@@ -167,7 +167,7 @@ POST / HTTP/2
167167
\`\`\`\`
168168
`)
169169
html = await renderContent(template)
170-
$ = cheerio.load(html, { xmlMode: true })
170+
$ = load(html, { xmlMode: true })
171171
expect($.html().includes('<pre><code class="hljs language-http">')).toBeTruthy()
172172
expect($.html().includes('<span class="hljs-keyword">POST</span>')).toBeTruthy()
173173

@@ -180,7 +180,7 @@ plugins {
180180
\`\`\`\`
181181
`)
182182
html = await renderContent(template)
183-
$ = cheerio.load(html, { xmlMode: true })
183+
$ = load(html, { xmlMode: true })
184184
expect($.html().includes('<pre><code class="hljs language-groovy">')).toBeTruthy()
185185
expect(
186186
$.html().includes('<span class="hljs-string">&apos;maven-publish&apos;</span>'),
@@ -192,7 +192,7 @@ FROM alpine:3.10
192192
\`\`\`\`
193193
`)
194194
html = await renderContent(template)
195-
$ = cheerio.load(html, { xmlMode: true })
195+
$ = load(html, { xmlMode: true })
196196
expect($.html().includes('<pre><code class="hljs language-Dockerfile">')).toBeTruthy()
197197
expect($.html().includes('<span class="hljs-keyword">FROM</span>')).toBeTruthy()
198198

@@ -202,7 +202,7 @@ $resourceGroupName = "octocat-testgroup"
202202
\`\`\`\`
203203
`)
204204
html = await renderContent(template)
205-
$ = cheerio.load(html, { xmlMode: true })
205+
$ = load(html, { xmlMode: true })
206206
expect($.html().includes('<pre><code class="hljs language-Powershell">')).toBeTruthy()
207207
expect(
208208
$.html().includes('<span class="hljs-variable">&#x24;resourceGroupName</span>'),
@@ -216,7 +216,7 @@ var a = 1
216216
\`\`\`
217217
`)
218218
const html = await renderContent(template)
219-
const $ = cheerio.load(html, { xmlMode: true })
219+
const $ = load(html, { xmlMode: true })
220220
expect($.html().includes('var a = 1')).toBeTruthy()
221221
})
222222

@@ -238,7 +238,7 @@ var a = 1
238238
\`\`\`
239239
`)
240240
const html = await renderContent(template)
241-
const $ = cheerio.load(html)
241+
const $ = load(html)
242242
const el = $('button.js-btn-copy')
243243
expect(el.data('clipboard')).toBe(2967273189)
244244
// Generates a murmurhash based ID that matches a <pre>
@@ -250,7 +250,7 @@ var a = 1
250250
> This is a note with a [link](https://example.com)
251251
`)
252252
const html = await renderContent(template, { alertTitles: { NOTE: 'Note' } })
253-
const $ = cheerio.load(html)
253+
const $ = load(html)
254254
const alertEl = $('.ghd-alert')
255255
expect(alertEl.length).toBe(1)
256256
expect(alertEl.attr('data-container')).toBe('alert')

src/content-render/tests/table-accessibility-labels.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import cheerio from 'cheerio'
1+
import { load } from 'cheerio'
22
import { describe, expect, test } from 'vitest'
33

44
import { renderContent } from '@/content-render/index'
@@ -20,7 +20,7 @@ describe('table accessibility labels', () => {
2020
`)
2121

2222
const html = await renderContent(template)
23-
const $ = cheerio.load(html)
23+
const $ = load(html)
2424

2525
const table = $('table')
2626
expect(table.length).toBe(1)
@@ -42,7 +42,7 @@ describe('table accessibility labels', () => {
4242
`)
4343

4444
const html = await renderContent(template)
45-
const $ = cheerio.load(html)
45+
const $ = load(html)
4646

4747
const table = $('table')
4848
expect(table.attr('aria-labelledby')).toBe('configuration-options')
@@ -59,7 +59,7 @@ describe('table accessibility labels', () => {
5959
`)
6060

6161
const html = await renderContent(template)
62-
const $ = cheerio.load(html)
62+
const $ = load(html)
6363

6464
const table = $('table')
6565
expect(table.attr('aria-label')).toBe('Pre-labeled table')
@@ -78,7 +78,7 @@ describe('table accessibility labels', () => {
7878
`)
7979

8080
const html = await renderContent(template)
81-
const $ = cheerio.load(html)
81+
const $ = load(html)
8282

8383
const table = $('table')
8484
expect(table.find('caption').text()).toBe('Existing caption')
@@ -101,7 +101,7 @@ describe('table accessibility labels', () => {
101101
`)
102102

103103
const html = await renderContent(template)
104-
const $ = cheerio.load(html)
104+
const $ = load(html)
105105

106106
const tables = $('table')
107107
expect(tables.length).toBe(2)
@@ -123,7 +123,7 @@ Some text here.
123123
`)
124124

125125
const html = await renderContent(template)
126-
const $ = cheerio.load(html)
126+
const $ = load(html)
127127

128128
const tables = $('table')
129129
expect(tables.length).toBe(2)
@@ -145,7 +145,7 @@ Some additional context here.
145145
`)
146146

147147
const html = await renderContent(template)
148-
const $ = cheerio.load(html)
148+
const $ = load(html)
149149

150150
const table = $('table')
151151
expect(table.attr('aria-labelledby')).toBe('data-table')
@@ -165,7 +165,7 @@ Some additional context here.
165165
`)
166166

167167
const html = await renderContent(template)
168-
const $ = cheerio.load(html)
168+
const $ = load(html)
169169

170170
const tables = $('table')
171171
expect(tables.length).toBe(2)
@@ -185,7 +185,7 @@ Some additional context here.
185185
`)
186186

187187
const html = await renderContent(template)
188-
const $ = cheerio.load(html)
188+
const $ = load(html)
189189

190190
const table = $('table')
191191
expect(table.attr('aria-labelledby')).toBe('supported-github-actions-features')
@@ -201,7 +201,7 @@ Some additional context here.
201201
`)
202202

203203
const html = await renderContent(template)
204-
const $ = cheerio.load(html)
204+
const $ = load(html)
205205

206206
const table = $('table')
207207
expect(table.find('thead th').length).toBe(2)

src/content-render/unified/text-only.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import cheerio from 'cheerio'
1+
import { load } from 'cheerio'
22
import { decode } from 'html-entities'
33

44
// Given a piece of HTML return it without HTML. E.g.
@@ -13,6 +13,6 @@ export function fastTextOnly(html: string): string {
1313
const middle = html.slice(3, -4)
1414
if (!middle.includes('<')) return decode(middle.trim())
1515
}
16-
const $ = cheerio.load(html, { xmlMode: true })
16+
const $ = load(html, { xmlMode: true })
1717
return $.root().text().trim()
1818
}

src/fixtures/tests/annotations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { describe, expect, test } from 'vitest'
2-
import cheerio from 'cheerio'
2+
import type { CheerioAPI } from 'cheerio'
33

44
import { getDOM } from '@/tests/helpers/e2etest'
55

66
describe('annotations', () => {
77
test('code-snippet-with-hashbang', async () => {
8-
const $: cheerio.Root = await getDOM('/get-started/foo/code-snippet-with-hashbang')
8+
const $: CheerioAPI = await getDOM('/get-started/foo/code-snippet-with-hashbang')
99
const annotations = $('#article-contents .annotate')
1010

1111
// Check http://localhost:4000/en/get-started/foo/code-snippet-with-hashbang

src/fixtures/tests/breadcrumbs.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { describe, expect, test } from 'vitest'
22

3+
import type { Element } from 'domhandler'
4+
35
import { getDOM } from '@/tests/helpers/e2etest'
46

57
describe('breadcrumbs', () => {
@@ -68,7 +70,7 @@ describe('breadcrumbs', () => {
6870

6971
expect($breadcrumbTitles.length).toBe(0)
7072
expect($breadcrumbLinks.length).toBe(2)
71-
expect(($breadcrumbLinks[0] as cheerio.TagElement).attribs.title).toBe('Deeper secrets')
72-
expect(($breadcrumbLinks[1] as cheerio.TagElement).attribs.title).toBe('Mariana Trench')
73+
expect(($breadcrumbLinks[0] as Element).attribs.title).toBe('Deeper secrets')
74+
expect(($breadcrumbLinks[1] as Element).attribs.title).toBe('Mariana Trench')
7375
})
7476
})

0 commit comments

Comments
 (0)