fix(compiler): support full range of entity decoding in browser builds

BREAKING CHANGE: compiler options have been adjusted.
    - new option `decodeEntities` is added.
    - `namedCharacterReferences` option has been removed.
    - `maxCRNameLength` option has been rmeoved.
This commit is contained in:
Evan You
2020-04-08 18:51:25 -04:00
parent 8c17535a47
commit 1f6e72b110
11 changed files with 245 additions and 1809 deletions

View File

@@ -5,12 +5,10 @@ import {
TextNode,
ErrorCodes,
ElementTypes,
InterpolationNode
InterpolationNode,
AttributeNode
} from '@vue/compiler-core'
import {
parserOptionsStandard as parserOptions,
DOMNamespaces
} from '../src/parserOptionsStandard'
import { parserOptions, DOMNamespaces } from '../src/parserOptions'
describe('DOM parser', () => {
describe('Text', () => {
@@ -170,6 +168,77 @@ describe('DOM parser', () => {
content: `foo${nbsp}${nbsp}bar`
})
})
// https://html.spec.whatwg.org/multipage/parsing.html#named-character-reference-state
test('HTML entities compatibility in text', () => {
const ast = parse('&ampersand;', parserOptions)
const text = ast.children[0] as TextNode
expect(text).toStrictEqual({
type: NodeTypes.TEXT,
content: '&ersand;',
loc: {
start: { offset: 0, line: 1, column: 1 },
end: { offset: 11, line: 1, column: 12 },
source: '&ampersand;'
}
})
})
// https://html.spec.whatwg.org/multipage/parsing.html#named-character-reference-state
test('HTML entities compatibility in attribute', () => {
const ast = parse(
'<div a="&ampersand;" b="&amp;ersand;" c="&amp!"></div>',
parserOptions
)
const element = ast.children[0] as ElementNode
const text1 = (element.props[0] as AttributeNode).value
const text2 = (element.props[1] as AttributeNode).value
const text3 = (element.props[2] as AttributeNode).value
expect(text1).toStrictEqual({
type: NodeTypes.TEXT,
content: '&ampersand;',
loc: {
start: { offset: 7, line: 1, column: 8 },
end: { offset: 20, line: 1, column: 21 },
source: '"&ampersand;"'
}
})
expect(text2).toStrictEqual({
type: NodeTypes.TEXT,
content: '&ersand;',
loc: {
start: { offset: 23, line: 1, column: 24 },
end: { offset: 37, line: 1, column: 38 },
source: '"&amp;ersand;"'
}
})
expect(text3).toStrictEqual({
type: NodeTypes.TEXT,
content: '&!',
loc: {
start: { offset: 40, line: 1, column: 41 },
end: { offset: 47, line: 1, column: 48 },
source: '"&amp!"'
}
})
})
test('Some control character reference should be replaced.', () => {
const ast = parse('&#x86;', parserOptions)
const text = ast.children[0] as TextNode
expect(text).toStrictEqual({
type: NodeTypes.TEXT,
content: '†',
loc: {
start: { offset: 0, line: 1, column: 1 },
end: { offset: 6, line: 1, column: 7 },
source: '&#x86;'
}
})
})
})
describe('Interpolation', () => {