Skip to content

Commit

Permalink
Fix double cleaning of HTML tags
Browse files Browse the repository at this point in the history
In markdown, in HTML code (or XML, SVG, etc), a sanitation mechanism that Prism
needs was in place to drop what looks like HTML tags.
We don’t need that here in refractor, because we don’t have strings of HTML.
That is, we also don’t need to sanitize again, because with that in place we
dropped *safe* tags.

Closes GH-64.
  • Loading branch information
wooorm committed Feb 28, 2023
1 parent 6f51f51 commit 9186068
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
6 changes: 1 addition & 5 deletions lang/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,11 +390,7 @@ export default function markdown(Prism) {
})
}
} else {
env.content = Prism.highlight(
textContent(env.content.value),
grammar,
codeLang
)
env.content = Prism.highlight(env.content.value, grammar, codeLang)
}
})
var tagPattern = RegExp(Prism.languages.markup.tag.pattern.source, 'gi')
Expand Down
21 changes: 15 additions & 6 deletions script/languages.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @typedef {import('babel__core').PluginObj} PluginObj
*/

import assert from 'node:assert/strict'
import fs from 'node:fs/promises'
import chalk from 'chalk'
import babel from '@babel/core'
Expand Down Expand Up @@ -47,11 +48,18 @@ async function generate(name) {
typeof info.alias === 'string' ? [info.alias] : info.alias || []
).sort(alphaSort())

/** @type {string} */
// @ts-expect-error: TS is wrong.
const doc = babel.transformSync(String(buf), {
const result = babel.transformSync(String(buf), {
plugins: [fixWrapHook]
}).code
})
assert(result, 'expected `result`')
let doc = result.code
assert(doc, 'expected `doc`')

if (id === 'markdown') {
// A useless function only used in `markdown` for us: our `value` is
// already text content.
doc = doc.replace(/textContent\(env\.content\.value\)/, 'env.content.value')
}

await fs.writeFile(
new URL('../lang/' + name + '.js', import.meta.url),
Expand Down Expand Up @@ -105,6 +113,7 @@ function fixWrapHook() {
},
// If a syntax is assigning `Prism.highlight` to `env.content`, we should
// add the result to `env.content` instead of `env.content.value`.
// This is currently only done for markdown.
AssignmentExpression: {
enter(path) {
const callee = path.get('right.callee')
Expand All @@ -113,7 +122,7 @@ function fixWrapHook() {
callee.matchesPattern('Prism.highlight') &&
path.get('left').matchesPattern('env.content')
) {
// @ts-expect-error Mutate.
// @ts-expect-error Patch custom field, handled next.
path.get('left').node.ignoreValueSuffix = true
}
}
Expand All @@ -124,7 +133,7 @@ function fixWrapHook() {
enter(path) {
if (
this.inWrapHook &&
// @ts-expect-error Mutate.
// @ts-expect-error Patched custom field.
!path.node.ignoreValueSuffix &&
path.matchesPattern('env.content')
) {
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/markdown-code/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```html
<h1>a</h1>
```

```css
* { color: red; }
```
7 changes: 7 additions & 0 deletions test/fixtures/markdown-code/output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<span class="token code"><span class="token punctuation">```</span><span class="token code-language">html</span>
<span class="token code-block language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&#x3C;</span>h1</span><span class="token punctuation">></span></span>a<span class="token tag"><span class="token tag"><span class="token punctuation">&#x3C;/</span>h1</span><span class="token punctuation">></span></span></span>
<span class="token punctuation">```</span></span>

<span class="token code"><span class="token punctuation">```</span><span class="token code-language">css</span>
<span class="token code-block language-css"><span class="token selector">*</span> <span class="token punctuation">{</span> <span class="token property">color</span><span class="token punctuation">:</span> <span class="token color">red</span><span class="token punctuation">;</span> <span class="token punctuation">}</span></span>
<span class="token punctuation">```</span></span>

0 comments on commit 9186068

Please sign in to comment.