Browse Source

remove preserveWhitespace config option

dev
Evan You 9 years ago
parent
commit
34bcc02b77
  1. 1
      flow/compiler.js
  2. 4
      src/compiler/parser/index.js
  3. 8
      src/core/config.js
  4. 2
      src/entries/web-runtime-with-compiler.js
  5. 5
      src/platforms/web/compiler/index.js
  6. 26
      test/unit/features/component/component-slot.spec.js
  7. 26
      test/unit/features/directives/if.spec.js
  8. 28
      test/unit/features/directives/model-radio.spec.js
  9. 14
      test/unit/features/directives/once.spec.js
  10. 19
      test/unit/features/global-api/config.spec.js
  11. 5
      test/unit/index.js

1
flow/compiler.js

@ -12,7 +12,6 @@ declare type CompilerOptions = {
transforms?: Array<Function>, // a list of transforms on parsed AST before codegen
// runtime user-configurable
preserveWhitespace?: boolean, // whether to keep whitespaces between elements
delimiters?: [string, string] // template delimiters
}

4
src/compiler/parser/index.js

@ -202,9 +202,7 @@ export function parse (
text = currentParent.tag === 'pre' || text.trim()
? decodeHTMLCached(text)
// only preserve whitespace if its not right after a starting tag
: options.preserveWhitespace && currentParent.children.length
? ' '
: ''
: currentParent.children.length ? ' ' : ''
if (text) {
let expression
if (!inPre && text !== ' ' && (expression = parseText(text, delimiters))) {

8
src/core/config.js

@ -3,9 +3,9 @@
import { no } from 'shared/util'
export type Config = {
preserveWhitespace: boolean,
optionMergeStrategies: { [key: string]: Function },
silent: boolean,
errorHandler: ?Function,
isReservedTag: (x?: string) => boolean,
isUnknownElement: (x?: string) => boolean,
mustUseProp: (x?: string) => boolean,
@ -17,12 +17,6 @@ export type Config = {
}
const config: Config = {
/**
* Preserve whitespaces between elements.
*/
preserveWhitespace: true,
/**
* Option merge strategies (used in core/util/options)
*/

2
src/entries/web-runtime-with-compiler.js

@ -1,7 +1,6 @@
/* @flow */
import Vue from './web-runtime'
import config from 'core/config'
import { warn, cached } from 'core/util/index'
import { query } from 'web/util/index'
import { compileToFunctions } from 'web/compiler/index'
@ -39,7 +38,6 @@ Vue.prototype.$mount = function (
}
if (template) {
const { render, staticRenderFns } = compileToFunctions(template, {
preserveWhitespace: config.preserveWhitespace,
delimiters: options.delimiters,
warn
}, this)

5
src/platforms/web/compiler/index.js

@ -8,13 +8,11 @@ import modules from './modules/index'
import directives from './directives/index'
import { isIE, isReservedTag, isUnaryTag, mustUseProp, getTagNamespace } from '../util/index'
const cache1: { [key: string]: CompiledFunctionResult } = Object.create(null)
const cache2: { [key: string]: CompiledFunctionResult } = Object.create(null)
const cache: { [key: string]: CompiledFunctionResult } = Object.create(null)
export const baseOptions: CompilerOptions = {
isIE,
expectHTML: true,
preserveWhitespace: true,
modules,
staticKeys: genStaticKeys(modules),
directives,
@ -57,7 +55,6 @@ export function compileToFunctions (
}
}
}
const cache = options && options.preserveWhitespace === false ? cache1 : cache2
const key = options && options.delimiters
? String(options.delimiters) + template
: template

26
test/unit/features/component/component-slot.spec.js

@ -61,9 +61,9 @@ describe('Component slot', () => {
`,
parentContent: '<p slot="b">slot b</p>'
})
expect(child.$el.childNodes.length).toBe(2)
expect(child.$el.firstChild.textContent).toBe('fallback a')
expect(child.$el.lastChild.textContent).toBe('slot b')
expect(child.$el.children.length).toBe(2)
expect(child.$el.children[0].textContent).toBe('fallback a')
expect(child.$el.children[1].textContent).toBe('slot b')
})
it('fallback content with mixed named/unamed slots', () => {
@ -76,9 +76,9 @@ describe('Component slot', () => {
`,
parentContent: '<p slot="b">slot b</p>'
})
expect(child.$el.childNodes.length).toBe(2)
expect(child.$el.firstChild.textContent).toBe('fallback a')
expect(child.$el.lastChild.textContent).toBe('slot b')
expect(child.$el.children.length).toBe(2)
expect(child.$el.children[0].textContent).toBe('fallback a')
expect(child.$el.children[1].textContent).toBe('slot b')
})
it('selector matching multiple elements', () => {
@ -100,7 +100,7 @@ describe('Component slot', () => {
`,
parentContent: '<div>foo</div><p slot="a">1</p><p slot="b">2</p>'
})
expect(child.$el.innerHTML).toBe('<p>1</p><div>foo</div><p>2</p>')
expect(child.$el.innerHTML).toBe('<p>1</p> <div>foo</div> <p>2</p>')
})
it('name should only match children', function () {
@ -108,8 +108,8 @@ describe('Component slot', () => {
childTemplate: `
<div>
<slot name="a"><p>fallback a</p></slot>
<slot name="b">fallback b</slot>
<slot name="c">fallback c</slot>
<slot name="b"><p>fallback b</p></slot>
<slot name="c"><p>fallback c</p></slot>
</div>
`,
parentContent: `
@ -118,10 +118,10 @@ describe('Component slot', () => {
'<span><p slot="c">nested c</p></span>
`
})
expect(child.$el.childNodes.length).toBe(3)
expect(child.$el.firstChild.textContent).toBe('fallback a')
expect(child.$el.childNodes[1].textContent).toBe('select b')
expect(child.$el.lastChild.textContent).toBe('fallback c')
expect(child.$el.children.length).toBe(3)
expect(child.$el.children[0].textContent).toBe('fallback a')
expect(child.$el.children[1].textContent).toBe('select b')
expect(child.$el.children[2].textContent).toBe('fallback c')
})
it('should accept expressions in slot attribute and slot names', () => {

26
test/unit/features/directives/if.spec.js

@ -60,31 +60,31 @@ describe('Directive v-if', () => {
`,
data: { foo: true }
}).$mount()
expect(vm.$el.innerHTML).toBe('<span>hello</span>')
expect(vm.$el.innerHTML.trim()).toBe('<span>hello</span>')
vm.foo = false
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('<span>bye</span>')
expect(vm.$el.innerHTML.trim()).toBe('<span>bye</span>')
vm.foo = {}
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>hello</span>')
expect(vm.$el.innerHTML.trim()).toBe('<span>hello</span>')
vm.foo = 0
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>bye</span>')
expect(vm.$el.innerHTML.trim()).toBe('<span>bye</span>')
vm.foo = []
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>hello</span>')
expect(vm.$el.innerHTML.trim()).toBe('<span>hello</span>')
vm.foo = null
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>bye</span>')
expect(vm.$el.innerHTML.trim()).toBe('<span>bye</span>')
vm.foo = '0'
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>hello</span>')
expect(vm.$el.innerHTML.trim()).toBe('<span>hello</span>')
vm.foo = undefined
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>bye</span>')
expect(vm.$el.innerHTML.trim()).toBe('<span>bye</span>')
vm.foo = 1
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>hello</span>')
expect(vm.$el.innerHTML.trim()).toBe('<span>hello</span>')
}).then(done)
})
@ -132,16 +132,16 @@ describe('Directive v-if', () => {
]
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<span>hello</span><span>bye</span><span>hello</span>')
expect(vm.$el.innerHTML.trim()).toBe('<span>hello</span><span>bye</span><span>hello</span>')
vm.list[0].value = false
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('<span>bye</span><span>bye</span><span>hello</span>')
expect(vm.$el.innerHTML.trim()).toBe('<span>bye</span><span>bye</span><span>hello</span>')
vm.list.push({ value: true })
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>bye</span><span>bye</span><span>hello</span><span>hello</span>')
expect(vm.$el.innerHTML.trim()).toBe('<span>bye</span><span>bye</span><span>hello</span><span>hello</span>')
vm.list.splice(1, 2)
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>bye</span><span>hello</span>')
expect(vm.$el.innerHTML.trim()).toBe('<span>bye</span><span>hello</span>')
}).then(done)
})
})

28
test/unit/features/directives/model-radio.spec.js

@ -14,15 +14,15 @@ describe('Directive v-model radio', () => {
`
}).$mount()
document.body.appendChild(vm.$el)
expect(vm.$el.childNodes[0].checked).toBe(true)
expect(vm.$el.childNodes[1].checked).toBe(false)
expect(vm.$el.children[0].checked).toBe(true)
expect(vm.$el.children[1].checked).toBe(false)
vm.test = '2'
waitForUpdate(() => {
expect(vm.$el.childNodes[0].checked).toBe(false)
expect(vm.$el.childNodes[1].checked).toBe(true)
vm.$el.childNodes[0].click()
expect(vm.$el.childNodes[0].checked).toBe(true)
expect(vm.$el.childNodes[1].checked).toBe(false)
expect(vm.$el.children[0].checked).toBe(false)
expect(vm.$el.children[1].checked).toBe(true)
vm.$el.children[0].click()
expect(vm.$el.children[0].checked).toBe(true)
expect(vm.$el.children[1].checked).toBe(false)
expect(vm.test).toBe('1')
}).then(() => {
document.body.removeChild(vm.$el)
@ -42,15 +42,15 @@ describe('Directive v-model radio', () => {
`
}).$mount()
document.body.appendChild(vm.$el)
expect(vm.$el.childNodes[0].checked).toBe(true)
expect(vm.$el.childNodes[1].checked).toBe(false)
expect(vm.$el.children[0].checked).toBe(true)
expect(vm.$el.children[1].checked).toBe(false)
vm.test = 2
waitForUpdate(() => {
expect(vm.$el.childNodes[0].checked).toBe(false)
expect(vm.$el.childNodes[1].checked).toBe(true)
vm.$el.childNodes[0].click()
expect(vm.$el.childNodes[0].checked).toBe(true)
expect(vm.$el.childNodes[1].checked).toBe(false)
expect(vm.$el.children[0].checked).toBe(false)
expect(vm.$el.children[1].checked).toBe(true)
vm.$el.children[0].click()
expect(vm.$el.children[0].checked).toBe(true)
expect(vm.$el.children[1].checked).toBe(false)
expect(vm.test).toBe(1)
}).then(() => {
document.body.removeChild(vm.$el)

14
test/unit/features/directives/once.spec.js

@ -29,11 +29,11 @@ describe('Directive v-once', () => {
}).$mount()
expect(vm.$children.length).toBe(1)
expect(vm.$el.innerHTML)
.toBe('<span>hello</span><div>hello</div>')
.toBe('<span>hello</span> <div>hello</div>')
vm.a = 'world'
waitForUpdate(() => {
expect(vm.$el.innerHTML)
.toBe('<span>hello</span><div>hello</div>')
.toBe('<span>hello</span> <div>hello</div>')
}).then(done)
})
@ -53,11 +53,11 @@ describe('Directive v-once', () => {
}).$mount()
expect(vm.$children.length).toBe(1)
expect(vm.$el.innerHTML)
.toBe('<span>hello</span><div>hello</div>')
.toBe('<span>hello</span> <div>hello</div>')
vm.a = 'world'
waitForUpdate(() => {
expect(vm.$el.innerHTML)
.toBe('<span>world</span><div>hello</div>')
.toBe('<span>world</span> <div>hello</div>')
}).then(done)
})
@ -80,15 +80,15 @@ describe('Directive v-once', () => {
}
}).$mount()
expect(vm.$el.innerHTML)
.toBe('<span>hello</span><div>hello</div><span>?</span>')
.toBe('<span>hello</span> <div>hello</div> <span>?</span>')
vm.a = 'world'
waitForUpdate(() => {
expect(vm.$el.innerHTML)
.toBe('<span>hello</span><div>world</div><span>?</span>')
.toBe('<span>hello</span> <div>world</div> <span>?</span>')
vm.suffix = '!'
}).then(() => {
expect(vm.$el.innerHTML)
.toBe('<span>hello</span><div>world</div><span>!</span>')
.toBe('<span>hello</span> <div>world</div> <span>!</span>')
}).then(done)
})
})

19
test/unit/features/global-api/config.spec.js

@ -1,25 +1,6 @@
import Vue from 'vue'
describe('Global config', () => {
describe('preserveWhitespace', () => {
it('should preserve whitepspaces when set to true', () => {
// this option is set to false during unit tests.
Vue.config.preserveWhitespace = true
const vm = new Vue({
template: '<div><span>hi</span> <span>ha</span></div>'
}).$mount()
expect(vm.$el.innerHTML).toBe('<span>hi</span> <span>ha</span>')
Vue.config.preserveWhitespace = false
})
it('should remove whitespaces when set to false', () => {
const vm = new Vue({
template: '<div><span>hi</span> <span>ha</span></div>'
}).$mount()
expect(vm.$el.innerHTML).toBe('<span>hi</span><span>ha</span>')
})
})
describe('silent', () => {
it('should be false by default', () => {
Vue.util.warn('foo')

5
test/unit/index.js

@ -1,8 +1,3 @@
import Vue from 'vue'
// ignore whitespace in tests
Vue.config.preserveWhitespace = false
// import all helpers
const helpersContext = require.context('../helpers', true)
helpersContext.keys().forEach(helpersContext)

Loading…
Cancel
Save