diff --git a/lib/nodes/Quoted.js b/lib/nodes/Quoted.js index dbee524..f2a42c2 100644 --- a/lib/nodes/Quoted.js +++ b/lib/nodes/Quoted.js @@ -18,8 +18,13 @@ class Quoted extends Node { constructor(options) { super(options); this.type = 'quoted'; - this.contents = unquote(options.value); - [this.quote] = options.value; + /** + * When cloning the node via {@link Node.clone()} there are no constructor params + */ + if (options && options.value) { + this.contents = unquote(options.value); + [this.quote] = options.value; + } } static fromTokens(tokens, parser) { diff --git a/test/quoted.test.js b/test/quoted.test.js new file mode 100644 index 0000000..1dc810a --- /dev/null +++ b/test/quoted.test.js @@ -0,0 +1,57 @@ +/* + Copyright © 2018 Andrew Powell + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of this Source Code Form. +*/ +const test = require('ava'); + +const { nodeToString, parse } = require('../lib'); + +const { snapshot, throws } = require('./fixtures/quoted'); + +for (const fixture of snapshot) { + test(fixture, (t) => { + const root = parse(fixture); + const nodes = root.nodes.map((node) => { + delete node.parent; // eslint-disable-line no-param-reassign + return node; + }); + const string = nodeToString(root); + + t.is(string, fixture); + t.is(fixture, root.toString()); + t.snapshot(root.first.toString()); + t.snapshot(string); + t.snapshot(nodes); + + root.clone(); + }); + + test(`${fixture} be should cloned`, (t) => { + const root = parse(fixture); + const nodes = root.nodes.map((node) => { + delete node.parent; // eslint-disable-line no-param-reassign + return node; + }); + const string = nodeToString(root); + + const cloned = root.clone(); + + t.is(string, fixture); + t.is(fixture, root.toString()); + t.snapshot(cloned.first.toString()); + t.snapshot(string); + t.snapshot(nodes); + }); +} + +for (const fixture of throws) { + test(fixture, (t) => { + t.throws(() => parse(fixture)); + }); +} diff --git a/test/snapshots/quoted.test.js.md b/test/snapshots/quoted.test.js.md new file mode 100644 index 0000000..4b3f561 --- /dev/null +++ b/test/snapshots/quoted.test.js.md @@ -0,0 +1,1445 @@ +# Snapshot report for `test/quoted.test.js` + +The actual snapshot is saved in `quoted.test.js.snap`. + +Generated by [AVA](https://avajs.dev). + +## "string" + +> Snapshot 1 + + '"string"' + +> Snapshot 2 + + ' "string" ' + +> Snapshot 3 + + [ + Quoted { + contents: 'string', + quote: '"', + raws: { + after: '', + before: ' ', + }, + source: { + end: { + column: 2, + line: 1, + offset: 1, + }, + input: Input { + css: ' "string" ', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 2, + line: 1, + offset: 1, + }, + }, + type: 'quoted', + value: '"string"', + [Symbol(isClean)]: false, + }, + ] + +## "" + +> Snapshot 1 + + '""' + +> Snapshot 2 + + '""' + +> Snapshot 3 + + [ + Quoted { + contents: '', + quote: '"', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '""', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '""', + [Symbol(isClean)]: false, + }, + ] + +## "string" + +> Snapshot 1 + + '"string"' + +> Snapshot 2 + + '"string"' + +> Snapshot 3 + + [ + Quoted { + contents: 'string', + quote: '"', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '"string"', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '"string"', + [Symbol(isClean)]: false, + }, + ] + +## "word'word" + +> Snapshot 1 + + '"word\'word"' + +> Snapshot 2 + + '"word\'word"' + +> Snapshot 3 + + [ + Quoted { + contents: 'word\'word', + quote: '"', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '"word\'word"', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '"word\'word"', + [Symbol(isClean)]: false, + }, + ] + +## "word\"word" + +> Snapshot 1 + + '"word\\"word"' + +> Snapshot 2 + + '"word\\"word"' + +> Snapshot 3 + + [ + Quoted { + contents: 'word"word', + quote: '"', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '"word\\"word"', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '"word\\"word"', + [Symbol(isClean)]: false, + }, + ] + +## '' + +> Snapshot 1 + + '\'\'' + +> Snapshot 2 + + '\'\'' + +> Snapshot 3 + + [ + Quoted { + contents: '', + quote: '\'', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '\'\'', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '\'\'', + [Symbol(isClean)]: false, + }, + ] + +## 'word"word' + +> Snapshot 1 + + '\'word"word\'' + +> Snapshot 2 + + '\'word"word\'' + +> Snapshot 3 + + [ + Quoted { + contents: 'word"word', + quote: '\'', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '\'word"word\'', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '\'word"word\'', + [Symbol(isClean)]: false, + }, + ] + +## 'word\'word' + +> Snapshot 1 + + '\'word\\\'word\'' + +> Snapshot 2 + + '\'word\\\'word\'' + +> Snapshot 3 + + [ + Quoted { + contents: 'word\'word', + quote: '\'', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '\'word\\\'word\'', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '\'word\\\'word\'', + [Symbol(isClean)]: false, + }, + ] + +## word1"string"word2 + +> Snapshot 1 + + 'word1' + +> Snapshot 2 + + 'word1"string"word2' + +> Snapshot 3 + + [ + Word { + isColor: false, + isHex: false, + isUrl: false, + isVariable: false, + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: 'word1"string"word2', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'word', + value: 'word1', + [Symbol(isClean)]: false, + }, + Quoted { + contents: 'string', + quote: '"', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 6, + line: 1, + offset: 5, + }, + input: Input { + css: 'word1"string"word2', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 6, + line: 1, + offset: 5, + }, + }, + type: 'quoted', + value: '"string"', + [Symbol(isClean)]: false, + }, + Word { + isColor: false, + isHex: false, + isUrl: false, + isVariable: false, + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 14, + line: 1, + offset: 13, + }, + input: Input { + css: 'word1"string"word2', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 14, + line: 1, + offset: 13, + }, + }, + type: 'word', + value: 'word2', + [Symbol(isClean)]: false, + }, + ] + +## should clone "string" + +> Snapshot 1 + + '"string"' + +> Snapshot 2 + + ' "string" ' + +> Snapshot 3 + + [ + Quoted { + contents: 'string', + quote: '"', + raws: { + after: '', + before: ' ', + }, + source: { + end: { + column: 2, + line: 1, + offset: 1, + }, + input: Input { + css: ' "string" ', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 2, + line: 1, + offset: 1, + }, + }, + type: 'quoted', + value: '"string"', + [Symbol(isClean)]: false, + }, + ] + +## should clone "" + +> Snapshot 1 + + '""' + +> Snapshot 2 + + '""' + +> Snapshot 3 + + [ + Quoted { + contents: '', + quote: '"', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '""', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '""', + [Symbol(isClean)]: false, + }, + ] + +## should clone "string" + +> Snapshot 1 + + '"string"' + +> Snapshot 2 + + '"string"' + +> Snapshot 3 + + [ + Quoted { + contents: 'string', + quote: '"', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '"string"', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '"string"', + [Symbol(isClean)]: false, + }, + ] + +## should clone "word'word" + +> Snapshot 1 + + '"word\'word"' + +> Snapshot 2 + + '"word\'word"' + +> Snapshot 3 + + [ + Quoted { + contents: 'word\'word', + quote: '"', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '"word\'word"', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '"word\'word"', + [Symbol(isClean)]: false, + }, + ] + +## should clone "word\"word" + +> Snapshot 1 + + '"word\\"word"' + +> Snapshot 2 + + '"word\\"word"' + +> Snapshot 3 + + [ + Quoted { + contents: 'word"word', + quote: '"', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '"word\\"word"', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '"word\\"word"', + [Symbol(isClean)]: false, + }, + ] + +## should clone '' + +> Snapshot 1 + + '\'\'' + +> Snapshot 2 + + '\'\'' + +> Snapshot 3 + + [ + Quoted { + contents: '', + quote: '\'', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '\'\'', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '\'\'', + [Symbol(isClean)]: false, + }, + ] + +## should clone 'word"word' + +> Snapshot 1 + + '\'word"word\'' + +> Snapshot 2 + + '\'word"word\'' + +> Snapshot 3 + + [ + Quoted { + contents: 'word"word', + quote: '\'', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '\'word"word\'', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '\'word"word\'', + [Symbol(isClean)]: false, + }, + ] + +## should clone 'word\'word' + +> Snapshot 1 + + '\'word\\\'word\'' + +> Snapshot 2 + + '\'word\\\'word\'' + +> Snapshot 3 + + [ + Quoted { + contents: 'word\'word', + quote: '\'', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '\'word\\\'word\'', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '\'word\\\'word\'', + [Symbol(isClean)]: false, + }, + ] + +## should clone word1"string"word2 + +> Snapshot 1 + + 'word1' + +> Snapshot 2 + + 'word1"string"word2' + +> Snapshot 3 + + [ + Word { + isColor: false, + isHex: false, + isUrl: false, + isVariable: false, + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: 'word1"string"word2', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'word', + value: 'word1', + [Symbol(isClean)]: false, + }, + Quoted { + contents: 'string', + quote: '"', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 6, + line: 1, + offset: 5, + }, + input: Input { + css: 'word1"string"word2', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 6, + line: 1, + offset: 5, + }, + }, + type: 'quoted', + value: '"string"', + [Symbol(isClean)]: false, + }, + Word { + isColor: false, + isHex: false, + isUrl: false, + isVariable: false, + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 14, + line: 1, + offset: 13, + }, + input: Input { + css: 'word1"string"word2', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 14, + line: 1, + offset: 13, + }, + }, + type: 'word', + value: 'word2', + [Symbol(isClean)]: false, + }, + ] + +## "string" be should cloned + +> Snapshot 1 + + '"string"' + +> Snapshot 2 + + ' "string" ' + +> Snapshot 3 + + [ + Quoted { + contents: 'string', + quote: '"', + raws: { + after: '', + before: ' ', + }, + source: { + end: { + column: 2, + line: 1, + offset: 1, + }, + input: Input { + css: ' "string" ', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 2, + line: 1, + offset: 1, + }, + }, + type: 'quoted', + value: '"string"', + [Symbol(isClean)]: false, + }, + ] + +## "" be should cloned + +> Snapshot 1 + + '""' + +> Snapshot 2 + + '""' + +> Snapshot 3 + + [ + Quoted { + contents: '', + quote: '"', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '""', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '""', + [Symbol(isClean)]: false, + }, + ] + +## "string" be should cloned + +> Snapshot 1 + + '"string"' + +> Snapshot 2 + + '"string"' + +> Snapshot 3 + + [ + Quoted { + contents: 'string', + quote: '"', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '"string"', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '"string"', + [Symbol(isClean)]: false, + }, + ] + +## "word'word" be should cloned + +> Snapshot 1 + + '"word\'word"' + +> Snapshot 2 + + '"word\'word"' + +> Snapshot 3 + + [ + Quoted { + contents: 'word\'word', + quote: '"', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '"word\'word"', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '"word\'word"', + [Symbol(isClean)]: false, + }, + ] + +## "word\"word" be should cloned + +> Snapshot 1 + + '"word\\"word"' + +> Snapshot 2 + + '"word\\"word"' + +> Snapshot 3 + + [ + Quoted { + contents: 'word"word', + quote: '"', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '"word\\"word"', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '"word\\"word"', + [Symbol(isClean)]: false, + }, + ] + +## '' be should cloned + +> Snapshot 1 + + '\'\'' + +> Snapshot 2 + + '\'\'' + +> Snapshot 3 + + [ + Quoted { + contents: '', + quote: '\'', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '\'\'', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '\'\'', + [Symbol(isClean)]: false, + }, + ] + +## 'word"word' be should cloned + +> Snapshot 1 + + '\'word"word\'' + +> Snapshot 2 + + '\'word"word\'' + +> Snapshot 3 + + [ + Quoted { + contents: 'word"word', + quote: '\'', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '\'word"word\'', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '\'word"word\'', + [Symbol(isClean)]: false, + }, + ] + +## 'word\'word' be should cloned + +> Snapshot 1 + + '\'word\\\'word\'' + +> Snapshot 2 + + '\'word\\\'word\'' + +> Snapshot 3 + + [ + Quoted { + contents: 'word\'word', + quote: '\'', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: '\'word\\\'word\'', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'quoted', + value: '\'word\\\'word\'', + [Symbol(isClean)]: false, + }, + ] + +## word1"string"word2 be should cloned + +> Snapshot 1 + + 'word1' + +> Snapshot 2 + + 'word1"string"word2' + +> Snapshot 3 + + [ + Word { + isColor: false, + isHex: false, + isUrl: false, + isVariable: false, + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 1, + line: 1, + offset: 0, + }, + input: Input { + css: 'word1"string"word2', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 1, + line: 1, + offset: 0, + }, + }, + type: 'word', + value: 'word1', + [Symbol(isClean)]: false, + }, + Quoted { + contents: 'string', + quote: '"', + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 6, + line: 1, + offset: 5, + }, + input: Input { + css: 'word1"string"word2', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 6, + line: 1, + offset: 5, + }, + }, + type: 'quoted', + value: '"string"', + [Symbol(isClean)]: false, + }, + Word { + isColor: false, + isHex: false, + isUrl: false, + isVariable: false, + raws: { + after: '', + before: '', + }, + source: { + end: { + column: 14, + line: 1, + offset: 13, + }, + input: Input { + css: 'word1"string"word2', + hasBOM: false, + id: '', + [Symbol(fromOffset cache)]: [ + 0, + ], + }, + start: { + column: 14, + line: 1, + offset: 13, + }, + }, + type: 'word', + value: 'word2', + [Symbol(isClean)]: false, + }, + ] diff --git a/test/snapshots/quoted.test.js.snap b/test/snapshots/quoted.test.js.snap new file mode 100644 index 0000000..0fa3324 Binary files /dev/null and b/test/snapshots/quoted.test.js.snap differ