Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser fails on tags with one space inside a <pre> tag #4758

Closed
blackjid opened this issue Jan 20, 2017 · 2 comments · Fixed by #4760
Closed

Parser fails on tags with one space inside a <pre> tag #4758

blackjid opened this issue Jan 20, 2017 · 2 comments · Fixed by #4760
Labels

Comments

@blackjid
Copy link

Vue.js version

2.1.10

Reproduction Link

https://jsfiddle.net/blackjid/br2h9pko/1/

Steps to reproduce

Have and element with just one space inside a <pre> tag element.

<div id="app">
  <pre>
    <span> </span>
  </pre>
</div>

and initialize vue in a parent element

new Vue({
  el: '#app'
});

What is Expected?

Vue initializes correctly.

What is actually happening?

An exception is thrown.

vue.js:7105 Uncaught TypeError: Cannot read property 'text' of undefined
    at Object.chars (vue.js:7105)
    at parseHTML (vue.js:6401)
    at parse (vue.js:6923)
    at compile$1 (vue.js:8003)
    at compile$$1 (vue.js:8425)
    at compileToFunctions (vue.js:8458)
    at Vue$3.$mount (vue.js:8536)
    at Vue$3.Vue._init (vue.js:3379)
    at new Vue$3 (vue.js:3427)

What did I try

  • Adding a v-pre attribute to the <pre> tag or to a wrapper element. This didn't work either
  • Changing the space with a &nbsp;. This worked, but you don't always control the code that you are bootstrapping vue on.
@blackjid
Copy link
Author

A slightly modified test like this one triggers the error.

it('preserve whitespace in <pre> tag', function () {
const options = extend({}, baseOptions)
const ast = parse('<pre><code> \n<span>hi</span>\n </code></pre>', options)
const code = ast.children[0]
expect(code.children[0].type).toBe(3)
expect(code.children[0].text).toBe(' \n')
expect(code.children[2].type).toBe(3)
expect(code.children[2].text).toBe('\n ')
})

  it('preserve whitespace in <pre> tag', function () {
    const options = extend({}, baseOptions)
    const ast = parse('<pre><code>  \n<span> </span>\n  </code></pre>', options)
    const code = ast.children[0]
    expect(code.children[0].type).toBe(3)
    expect(code.children[0].text).toBe('  \n')
    expect(code.children[2].type).toBe(3)
    expect(code.children[2].text).toBe('\n  ')
  })

I also pinpoint this line, that has a comment that describes the problem.

text = inPre || text.trim()
? decodeHTMLCached(text)
// only preserve whitespace if its not right after a starting tag
: preserveWhitespace && children.length ? ' ' : '';

@blackjid
Copy link
Author

Hey! thanks!, that was fast!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants