Skip to content

Commit

Permalink
Prevent two-way binding, by not including it to the element attribute…
Browse files Browse the repository at this point in the history
…s, based on options

Closes sveltejs#54
  • Loading branch information
esarbanis committed Dec 27, 2017
1 parent 37f8f8a commit a6ba31b
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface CompileOptions {
amd?: {
id?: string;
};
bind?: boolean;

outputFilename?: string;
cssOutputFilename?: string;
Expand Down
3 changes: 3 additions & 0 deletions src/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ class ParseError extends CompileError {

interface ParserOptions {
filename?: string;
bind?: boolean;
}

type ParserState = (parser: Parser) => (ParserState | void);

export class Parser {
readonly template: string;
readonly filename?: string;
readonly bind: boolean;

index: number;
stack: Array<Node>;
Expand All @@ -45,6 +47,7 @@ export class Parser {

this.template = template.replace(/\s+$/, '');
this.filename = options.filename;
this.bind = options.bind !== false;

this.index = 0;
this.stack = [];
Expand Down
4 changes: 3 additions & 1 deletion src/parse/state/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ export default function tag(parser: Parser) {

let attribute;
while ((attribute = readAttribute(parser, uniqueNames))) {
element.attributes.push(attribute);
if (attribute.type !== 'Binding' || parser.bind) {
element.attributes.push(attribute);
}
parser.allowWhitespace();
}

Expand Down
8 changes: 7 additions & 1 deletion test/parser/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'assert';
import fs from 'fs';
import path from 'path';
import { svelte } from '../helpers.js';

describe('parse', () => {
Expand All @@ -20,8 +21,13 @@ describe('parse', () => {
.readFileSync(`test/parser/samples/${dir}/input.html`, 'utf-8')
.replace(/\s+$/, '');

const optionsPath = `test/parser/samples/${dir}/options.json`;
const options = fs.existsSync(optionsPath) ?
JSON.parse(fs.readFileSync(optionsPath, 'utf-8')) :
{};

try {
const actual = svelte.parse(input);
const actual = svelte.parse(input, options);
fs.writeFileSync(
`test/parser/samples/${dir}/_actual.json`,
JSON.stringify(actual, null, '\t')
Expand Down
1 change: 1 addition & 0 deletions test/parser/samples/binding-disabled/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<input bind:value='name'>
3 changes: 3 additions & 0 deletions test/parser/samples/binding-disabled/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"bind": false
}
21 changes: 21 additions & 0 deletions test/parser/samples/binding-disabled/output.json
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 20 additions & 0 deletions test/runtime/samples/binding-disabled/_config.js
Original file line number Diff line number Diff line change
@@ -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>` );
}
};
2 changes: 2 additions & 0 deletions test/runtime/samples/binding-disabled/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<input bind:value='name'>
<p>hello {{name}}</p>

0 comments on commit a6ba31b

Please sign in to comment.