Skip to content

Commit

Permalink
Unescape namespace in all node types.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseppstein committed Mar 29, 2018
1 parent e20cfbc commit 3c52465
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
9 changes: 9 additions & 0 deletions src/__tests__/namespaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,12 @@ test('namespace with qualified id selector', 'ns|h1#foo', (t, tree) => {
test('namespace with qualified class selector', 'ns|h1.foo', (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[0].namespace, 'ns');
});

test('ns alias for namespace', 'f\\oo|h1.foo', (t, tree) => {
let tag = tree.nodes[0].nodes[0];
t.deepEqual(tag.namespace, 'foo');
t.deepEqual(tag.ns, 'foo');
tag.ns = "bar";
t.deepEqual(tag.namespace, 'bar');
t.deepEqual(tag.ns, 'bar');
});
6 changes: 4 additions & 2 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ function unescapeProp (node, prop) {
}
if (value.indexOf("\\") !== -1) {
ensureObject(node, 'raws');
node[prop] = unesc(value);
if (node.raws[prop] === undefined) {
node.raws[prop] = value;
}
node[prop] = unesc(value);
}
return node;
}
Expand Down Expand Up @@ -761,7 +761,8 @@ export default class Parser {
source,
sourceIndex,
};
node = new Tag(unescapeProp(tagOpts, "value"));
unescapeProp(tagOpts, "value");
node = new Tag(tagOpts);
}
this.newNode(node, namespace);
// Ensure that the namespace is used only once
Expand Down Expand Up @@ -885,6 +886,7 @@ export default class Parser {
namespace = true;
}
node.namespace = namespace;
unescapeProp(node, "namespace");
}
if (this.spaces) {
node.spaces.before = this.spaces;
Expand Down
16 changes: 0 additions & 16 deletions src/selectors/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,22 +273,6 @@ export default class Attribute extends Namespace {
}
}

get namespace () {
return this._namespace;
}

set namespace (v) {
if (v === "*") {
// Don't escape this special value for the namespace.
if (this._constructed) {
delete this.raws.namespace;
}
} else {
this._handleEscapes("namespace", v);
}
this._namespace = v;
}

get attribute () {
return this._attribute;
}
Expand Down
21 changes: 16 additions & 5 deletions src/selectors/namespace.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
import cssesc from 'cssesc';
import {ensureObject} from '../util';
import Node from './node';

export default class Namespace extends Node {
get namespace () {
return this._namespace;
}
set namespace (namespace) {
if (namespace === true || namespace === "*" || namespace === "&") {
this._namespace = namespace;
if (this.raws) {
delete this.raws.namespace;
}
return;
}

let escaped = cssesc(namespace, {isIdentifier: true});
this._namespace = namespace;
if (this.raws) {
if (escaped !== namespace) {
ensureObject(this, "raws");
this.raws.namespace = escaped;
} else if (this.raws) {
delete this.raws.namespace;
}
}
get ns () {
return this._namespace;
}
set ns (namespace) {
this._namespace = namespace;
if (this.raws) {
delete this.raws.namespace;
}
this.namespace = namespace;
}

get namespaceString () {
Expand Down

0 comments on commit 3c52465

Please sign in to comment.