merge master -> gh-7

pull/525/head
Rich-Harris 8 years ago
commit dee8694e3b

@ -5,11 +5,10 @@ import isReference from '../../utils/isReference.js';
import { walk } from 'estree-walker'; import { walk } from 'estree-walker';
import deindent from '../../utils/deindent.js'; import deindent from '../../utils/deindent.js';
import CodeBuilder from '../../utils/CodeBuilder.js'; import CodeBuilder from '../../utils/CodeBuilder.js';
import toSource from '../../utils/toSource.js';
import visit from './visit.js'; import visit from './visit.js';
import { nameMap, sharedMap } from './sharedNames.js';
import Generator from '../Generator.js'; import Generator from '../Generator.js';
import preprocess from './preprocess.js'; import preprocess from './preprocess.js';
import * as shared from '../../shared/index.js';
class DomGenerator extends Generator { class DomGenerator extends Generator {
constructor ( parsed, source, name, options ) { constructor ( parsed, source, name, options ) {
@ -26,7 +25,7 @@ class DomGenerator extends Generator {
} }
helper ( name ) { helper ( name ) {
if ( this.options.dev && `${name}Dev` in shared ) { if ( this.options.dev && sharedMap.has( `${name}Dev` ) ) {
name = `${name}Dev`; name = `${name}Dev`;
} }
@ -276,8 +275,7 @@ export default function dom ( parsed, source, options ) {
); );
} else { } else {
generator.uses.forEach( key => { generator.uses.forEach( key => {
const value = shared[ key ]; // eslint-disable-line import/namespace const str = sharedMap.get( key );
const str = toSource( value );
const code = new MagicString( str ); const code = new MagicString( str );
const fn = parseExpressionAt( str, 0 ); const fn = parseExpressionAt( str, 0 );
@ -288,11 +286,12 @@ export default function dom ( parsed, source, options ) {
if ( node._scope ) scope = node._scope; if ( node._scope ) scope = node._scope;
if ( node.type === 'Identifier' && isReference( node, parent ) && !scope.has( node.name ) ) { if ( node.type === 'Identifier' && isReference( node, parent ) && !scope.has( node.name ) ) {
if ( node.name in shared ) { if ( nameMap.has( node.name ) ) {
// this helper function depends on another one // this helper function depends on another one
generator.uses.add( node.name ); const dependency = nameMap.get( node.name );
generator.uses.add( dependency );
const alias = generator.alias( node.name ); const alias = generator.alias( dependency );
if ( alias !== node.name ) code.overwrite( node.start, node.end, alias ); if ( alias !== node.name ) code.overwrite( node.start, node.end, alias );
} }
} }

@ -0,0 +1,12 @@
import * as shared from '../../shared/index.js';
export const nameMap = new Map();
export const sharedMap = new Map();
Object.keys(shared).forEach( key => {
const value = shared[ key ]; // eslint-disable-line import/namespace
if ( typeof value === 'function' ) {
nameMap.set( value.name, key );
}
sharedMap.set( key, value.toString() );
});

@ -30,7 +30,7 @@ export default function visitBinding ( generator, block, state, node, attribute
// <select> special case // <select> special case
if ( node.name === 'select' ) { if ( node.name === 'select' ) {
if ( !isMultipleSelect ) { if ( !isMultipleSelect ) {
setter = `var selectedOption = ${state.parentNode}.selectedOptions[0] || ${state.parentNode}.options[0];\n${setter}`; setter = `var selectedOption = ${state.parentNode}.querySelector(':checked') || ${state.parentNode}.options[0];\n${setter}`;
} }
const value = block.getUniqueName( 'value' ); const value = block.getUniqueName( 'value' );
@ -160,7 +160,7 @@ function getBindingEventName ( node, attribute ) {
function getBindingValue ( generator, block, state, node, attribute, isMultipleSelect, bindingGroup, type ) { function getBindingValue ( generator, block, state, node, attribute, isMultipleSelect, bindingGroup, type ) {
// <select multiple bind:value='selected> // <select multiple bind:value='selected>
if ( isMultipleSelect ) { if ( isMultipleSelect ) {
return `[].map.call( ${state.parentNode}.selectedOptions, function ( option ) { return option.__value; })`; return `[].map.call( ${state.parentNode}.querySelectorAll(':checked'), function ( option ) { return option.__value; })`;
} }
// <select bind:value='selected> // <select bind:value='selected>

@ -1,6 +1,4 @@
export default { export default {
skip: true, // selectedOptions doesn't work in JSDOM???
html: ` html: `
<p>selected: b</p> <p>selected: b</p>

@ -1,9 +1,9 @@
<p>selected: {{selected}}</p> <p>selected: {{selected}}</p>
<select bind:value='selected'> <select bind:value='selected'>
<option value="a">a</option> <option>a</option>
<option value="b">b</option> <option>b</option>
<option value="c">c</option> <option>c</option>
</select> </select>
<p>selected: {{selected}}</p> <p>selected: {{selected}}</p>

@ -1,12 +1,12 @@
export default { export default {
skip: true, // selectedOptions doesn't work in JSDOM??? skip: true, // JSDOM
data: { data: {
selected: [ 'two', 'three' ] selected: [ 'two', 'three' ]
}, },
html: ` html: `
<select> <select multiple>
<option>one</option> <option>one</option>
<option>two</option> <option>two</option>
<option>three</option> <option>three</option>
@ -26,7 +26,7 @@ export default {
assert.deepEqual( component.get( 'selected' ), [ 'three' ] ); assert.deepEqual( component.get( 'selected' ), [ 'three' ] );
assert.htmlEqual( target.innerHTML, ` assert.htmlEqual( target.innerHTML, `
<select> <select multiple>
<option>one</option> <option>one</option>
<option>two</option> <option>two</option>
<option>three</option> <option>three</option>
@ -40,7 +40,7 @@ export default {
assert.deepEqual( component.get( 'selected' ), [ 'one', 'three' ] ); assert.deepEqual( component.get( 'selected' ), [ 'one', 'three' ] );
assert.htmlEqual( target.innerHTML, ` assert.htmlEqual( target.innerHTML, `
<select> <select multiple>
<option>one</option> <option>one</option>
<option>two</option> <option>two</option>
<option>three</option> <option>three</option>
@ -56,7 +56,7 @@ export default {
assert.ok( !options[2].selected ); assert.ok( !options[2].selected );
assert.htmlEqual( target.innerHTML, ` assert.htmlEqual( target.innerHTML, `
<select> <select multiple>
<option>one</option> <option>one</option>
<option>two</option> <option>two</option>
<option>three</option> <option>three</option>

@ -1,6 +1,4 @@
export default { export default {
skip: true, // selectedOptions doesn't work in JSDOM???
html: ` html: `
<p>selected: one</p> <p>selected: one</p>
@ -13,6 +11,10 @@ export default {
<p>selected: one</p> <p>selected: one</p>
`, `,
data: {
selected: 'one'
},
test ( assert, component, target, window ) { test ( assert, component, target, window ) {
const select = target.querySelector( 'select' ); const select = target.querySelector( 'select' );
const options = [ ...target.querySelectorAll( 'option' ) ]; const options = [ ...target.querySelectorAll( 'option' ) ];

@ -1,6 +1,4 @@
export default { export default {
skip: true, // JSDOM
data: { data: {
options: [ { id: 'a' }, { id: 'b' }, { id: 'c' } ], options: [ { id: 'a' }, { id: 'b' }, { id: 'c' } ],
selected: 'b' selected: 'b'

@ -1,8 +1,6 @@
const items = [ {}, {} ]; const items = [ {}, {} ];
export default { export default {
skip: true, // JSDOM quirks
'skip-ssr': true, 'skip-ssr': true,
data: { data: {

Loading…
Cancel
Save