diff --git a/src/interfaces.ts b/src/interfaces.ts index 9773d330d394..657ebea7e1ee 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -47,6 +47,7 @@ export interface CompileOptions { amd?: { id?: string; }; + bind?: boolean; outputFilename?: string; cssOutputFilename?: string; diff --git a/src/parse/index.ts b/src/parse/index.ts index 995a38f675ed..2f67086370a9 100644 --- a/src/parse/index.ts +++ b/src/parse/index.ts @@ -22,6 +22,7 @@ class ParseError extends CompileError { interface ParserOptions { filename?: string; + bind?: boolean; } type ParserState = (parser: Parser) => (ParserState | void); @@ -29,6 +30,7 @@ type ParserState = (parser: Parser) => (ParserState | void); export class Parser { readonly template: string; readonly filename?: string; + readonly bind: boolean; index: number; stack: Array; @@ -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 = []; diff --git a/src/parse/state/tag.ts b/src/parse/state/tag.ts index 1e39a5105974..043825daa717 100644 --- a/src/parse/state/tag.ts +++ b/src/parse/state/tag.ts @@ -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(); } diff --git a/test/parser/index.js b/test/parser/index.js index 1ce8165009a4..9cf528ac7a49 100644 --- a/test/parser/index.js +++ b/test/parser/index.js @@ -1,5 +1,6 @@ import assert from 'assert'; import fs from 'fs'; +import path from 'path'; import { svelte } from '../helpers.js'; describe('parse', () => { @@ -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') diff --git a/test/parser/samples/binding-disabled/input.html b/test/parser/samples/binding-disabled/input.html new file mode 100644 index 000000000000..6a7bf8566cbb --- /dev/null +++ b/test/parser/samples/binding-disabled/input.html @@ -0,0 +1 @@ + diff --git a/test/parser/samples/binding-disabled/options.json b/test/parser/samples/binding-disabled/options.json new file mode 100644 index 000000000000..5ece9fc983e8 --- /dev/null +++ b/test/parser/samples/binding-disabled/options.json @@ -0,0 +1,3 @@ +{ + "bind": false +} diff --git a/test/parser/samples/binding-disabled/output.json b/test/parser/samples/binding-disabled/output.json new file mode 100644 index 000000000000..0a513140d7e9 --- /dev/null +++ b/test/parser/samples/binding-disabled/output.json @@ -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 +} diff --git a/test/runtime/samples/binding-disabled/_config.js b/test/runtime/samples/binding-disabled/_config.js new file mode 100644 index 000000000000..ce173f80afb7 --- /dev/null +++ b/test/runtime/samples/binding-disabled/_config.js @@ -0,0 +1,20 @@ +export default { + data: { + name: 'world' + }, + compileOptions: { + bind: false + }, + html: `\n

hello world

`, + 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, `\n

hello world

` ); + } +}; diff --git a/test/runtime/samples/binding-disabled/main.html b/test/runtime/samples/binding-disabled/main.html new file mode 100644 index 000000000000..806f31fbdd50 --- /dev/null +++ b/test/runtime/samples/binding-disabled/main.html @@ -0,0 +1,2 @@ + +

hello {{name}}