pull/1056/merge
Efthymis Sarmpanis 8 years ago committed by GitHub
commit dbfb59ab3f

@ -109,6 +109,12 @@ export function compile(source: string, _options: CompileOptions) {
const options = normalizeOptions(_options); const options = normalizeOptions(_options);
let parsed: Parsed; let parsed: Parsed;
if (options.bind === false) {
options.onwarn({
message: `You have disabled two-way binding for your components! It is highly recommended to enable it!`
})
}
try { try {
parsed = parse(source, options); parsed = parse(source, options);
} catch (err) { } catch (err) {

@ -47,6 +47,7 @@ export interface CompileOptions {
amd?: { amd?: {
id?: string; id?: string;
}; };
bind?: boolean;
outputFilename?: string; outputFilename?: string;
cssOutputFilename?: string; cssOutputFilename?: string;

@ -22,6 +22,7 @@ class ParseError extends CompileError {
interface ParserOptions { interface ParserOptions {
filename?: string; filename?: string;
bind?: boolean;
} }
type ParserState = (parser: Parser) => (ParserState | void); type ParserState = (parser: Parser) => (ParserState | void);
@ -29,6 +30,7 @@ type ParserState = (parser: Parser) => (ParserState | void);
export class Parser { export class Parser {
readonly template: string; readonly template: string;
readonly filename?: string; readonly filename?: string;
readonly bind: boolean;
index: number; index: number;
stack: Array<Node>; stack: Array<Node>;
@ -45,6 +47,7 @@ export class Parser {
this.template = template.replace(/\s+$/, ''); this.template = template.replace(/\s+$/, '');
this.filename = options.filename; this.filename = options.filename;
this.bind = options.bind !== false;
this.index = 0; this.index = 0;
this.stack = []; this.stack = [];

@ -182,7 +182,9 @@ export default function tag(parser: Parser) {
let attribute; let attribute;
while ((attribute = readAttribute(parser, uniqueNames))) { while ((attribute = readAttribute(parser, uniqueNames))) {
element.attributes.push(attribute); if (attribute.type !== 'Binding' || parser.bind) {
element.attributes.push(attribute);
}
parser.allowWhitespace(); parser.allowWhitespace();
} }

@ -1,5 +1,6 @@
import assert from 'assert'; import assert from 'assert';
import fs from 'fs'; import fs from 'fs';
import path from 'path';
import { svelte } from '../helpers.js'; import { svelte } from '../helpers.js';
describe('parse', () => { describe('parse', () => {
@ -20,8 +21,13 @@ describe('parse', () => {
.readFileSync(`test/parser/samples/${dir}/input.html`, 'utf-8') .readFileSync(`test/parser/samples/${dir}/input.html`, 'utf-8')
.replace(/\s+$/, ''); .replace(/\s+$/, '');
const optionsPath = `test/parser/samples/${dir}/options.json`;
const options = fs.existsSync(optionsPath) ?
JSON.parse(fs.readFileSync(optionsPath, 'utf-8')) :
{};
try { try {
const actual = svelte.parse(input); const actual = svelte.parse(input, options);
fs.writeFileSync( fs.writeFileSync(
`test/parser/samples/${dir}/_actual.json`, `test/parser/samples/${dir}/_actual.json`,
JSON.stringify(actual, null, '\t') JSON.stringify(actual, null, '\t')

@ -0,0 +1,21 @@
{
"hash": 1937205193,
"html": {
"start": 0,
"end": 25,
"type": "Fragment",
"children": [
{
"start": 0,
"end": 25,
"type": "Element",
"name": "input",
"attributes": [
],
"children": []
}
]
},
"css": null,
"js": null
}

@ -0,0 +1,20 @@
export default {
data: {
name: 'world'
},
compileOptions: {
bind: false
},
html: `<input>\n<p>hello world</p>`,
test ( assert, component, target, window ) {
const input = target.querySelector( 'input' );
assert.equal( input.value, '' );
const event = new window.Event( 'input' );
input.value = 'everybody';
input.dispatchEvent( event );
assert.equal( target.innerHTML, `<input>\n<p>hello world</p>` );
}
};

@ -0,0 +1,2 @@
<input bind:value='name'>
<p>hello {{name}}</p>
Loading…
Cancel
Save