Skip to content
This repository has been archived by the owner on Sep 14, 2021. It is now read-only.

Commit

Permalink
Merge pull request #39 from PolymerLabs/justin-work
Browse files Browse the repository at this point in the history
Upgrade to parse5 2.2
  • Loading branch information
justinfagnani authored Sep 23, 2016
2 parents b619bae + b899bf1 commit 5bc535b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 73 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ language: node_js
sudo: false
node_js:
- "6"
- "5"
- "4"
- "0.12"
- "0.10"
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Change Log

## [v2.0.0](https://github.com/PolymerLabs/dom5/tree/v2.0.0) (2016-09-23)
- Upgraded to parse5 v2.2.1
- (*breaking*) Because parse5 2.x correctly handles `<template>` elements,
the nodeWalk* and query* functions will no longer return results from within
`<template>`s. Code that relied on this must explicitly walk into template
content documents.
- (*breaking*) Removed `parse()`, `parseFragment()` and `serialize()`. Use
`parse5.parse()`, etc., instead.
- Nodes can be appended to parents with a `null` `childNodes` array.
- DocumentFragments that are appended have their `childNodes` array cleared,
rather than getting a new empty `childNodes` instance.
21 changes: 4 additions & 17 deletions dom5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,10 @@ function insertNode(

if (newNode) {
if (isDocumentFragment(newNode)) {
newNodes = newNode.childNodes || [];
newNode.childNodes = [];
if (newNode.childNodes) {
newNodes = Array.from(newNode.childNodes);
newNode.childNodes.length = 0;
}
} else {
newNodes = [newNode];
remove(newNode);
Expand Down Expand Up @@ -578,21 +580,6 @@ export function append(parent: Node, newNode: Node) {
insertNode(parent, index, newNode);
}

export function parse(text: string, options?: parse5.ParserOptions) {
const parser = new parse5.Parser(parse5.TreeAdapters.default, options);
return parser.parse(text);
}

export function parseFragment(text: string) {
const parser = new parse5.Parser();
return parser.parseFragment(text);
}

export function serialize(ast: Node) {
const serializer = new parse5.Serializer();
return serializer.serialize(ast);
}

export const predicates = {
hasClass: hasClass,
hasAttr: hasAttr,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dom5",
"version": "1.3.6",
"version": "2.0.0",
"description": "Utilities for working with parse5 ASTs",
"license": "BSD-3-Clause",
"scripts": {
Expand Down Expand Up @@ -36,6 +36,6 @@
"@types/node": "^4.0.30",
"@types/parse5": "^0.0.31",
"clone": "^1.0.2",
"parse5": "^1.4.1"
"parse5": "^2.2.1"
}
}
107 changes: 56 additions & 51 deletions test/dom5_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

var chai = require('chai');
var fs = require('fs');
var parse5 = require('parse5');

var dom5 = require('../dom5');

Expand All @@ -18,37 +19,35 @@ var assert = chai.assert;
suite('dom5', function() {

suite('Parse5 Wrapper Functions', function() {
var parse5 = require('parse5');
var docText = "<!DOCTYPE html><div id='A' qux>a1<div bar='b1' bar='b2'>b1</div>a2</div><!-- comment -->";
var fragText = '<template><span>Foo</span></template><!-- comment --><my-bar></my-bar>';
var parser = new parse5.Parser();

test('parse', function() {
var doc_expected = parser.parse(docText);
var doc_actual = dom5.parse(docText);
var doc_expected = parse5.parse(docText);
var doc_actual = parse5.parse(docText);

assert.deepEqual(doc_expected, doc_actual);
});

test('parseFragment', function() {
var frag_expected = parser.parseFragment(fragText);
var frag_actual = dom5.parseFragment(fragText);
var frag_expected = parse5.parseFragment(fragText);
var frag_actual = parse5.parseFragment(fragText);

assert.deepEqual(frag_expected, frag_actual);
});

test('serialize', function() {
var serializer = new parse5.Serializer();
// var serializer = new parse5.Serializer();

var ast = parser.parse(docText);
var expected = serializer.serialize(ast);
var actual = dom5.serialize(ast);
var ast = parse5.parse(docText);
var expected = parse5.serialize(ast);
var actual = parse5.serialize(ast);

assert.equal(expected, actual);

ast = parser.parseFragment(fragText);
expected = serializer.serialize(ast);
actual = dom5.serialize(ast);
ast = parse5.parseFragment(fragText);
expected = parse5.serialize(ast);
actual = parse5.serialize(ast);

assert.equal(expected, actual);
});
Expand All @@ -66,7 +65,7 @@ suite('dom5', function() {
var doc = null;

setup(function () {
doc = dom5.parse(docText);
doc = parse5.parse(docText);
});

suite('Node Identity', function() {
Expand Down Expand Up @@ -292,7 +291,7 @@ suite('dom5', function() {
var dom, div, span;

setup(function() {
dom = dom5.parseFragment('<div>a</div><span></span>b');
dom = parse5.parseFragment('<div>a</div><span></span>b');
div = dom.childNodes[0];
span = dom.childNodes[1];
});
Expand Down Expand Up @@ -326,17 +325,21 @@ suite('dom5', function() {
var fragment = dom5.constructors.fragment();
var span = dom5.constructors.element('span');
var text = dom5.constructors.text('foo');
fragment.childNodes.push(span);
fragment.childNodes.push(text);
// hold a reference to make sure append() clears childNodes
var fragmentChildren = fragment.childNodes;
fragmentChildren.push(span);
fragmentChildren.push(text);

dom5.append(div, fragment);

assert.equal(div.childNodes.indexOf(span), 1);
assert.equal(div.childNodes.indexOf(text), 2);
assert.equal(fragment.childNodes.length, 0);
assert.equal(fragmentChildren.length, 0);
});

test('append to node with no children', function() {
var emptyBody = dom5.parse('<head></head><body></body>');
var emptyBody = parse5.parse('<head></head><body></body>');
var body = emptyBody.childNodes[0].childNodes[1];
var span = dom5.constructors.element('span');
dom5.append(body, span);
Expand All @@ -346,42 +349,45 @@ suite('dom5', function() {
});

suite('InsertBefore', function() {
var dom, div, span, a;
var dom, div, span, text;

setup(function() {
dom = dom5.parseFragment('<div></div><span></span>a');
dom = parse5.parseFragment('<div></div><span></span>text');
div = dom.childNodes[0];
span = dom.childNodes[1];
a = dom.childNodes[2];
text = dom.childNodes[2];
});

test('ordering is correct', function() {
dom5.insertBefore(dom, span, a);
assert.equal(dom.childNodes.indexOf(a), 1);
dom5.insertBefore(dom, div, a);
assert.equal(dom.childNodes.indexOf(a), 0);
dom5.insertBefore(dom, span, text);
assert.equal(dom.childNodes.indexOf(text), 1);
var newHtml = parse5.serialize(dom);
assert.equal(newHtml, '<div></div>text<span></span>');
dom5.insertBefore(dom, div, text);
assert.equal(dom.childNodes.indexOf(text), 0);
});

test('accepts document fragments', function() {
var fragment = dom5.constructors.fragment();
var span2 = dom5.constructors.element('span');
var text = dom5.constructors.text('foo');
var text2 = dom5.constructors.text('foo');
fragment.childNodes.push(span2);
fragment.childNodes.push(text);
fragment.childNodes.push(text2);

dom5.insertBefore(dom, span, fragment);
assert.equal(dom.childNodes.indexOf(span2), 1);
assert.equal(dom.childNodes.indexOf(text), 2);
assert.equal(dom.childNodes.indexOf(text2), 2);
assert.equal(dom.childNodes.indexOf(span), 3);
assert.equal(dom.childNodes.indexOf(a), 4);
assert.equal(dom.childNodes.indexOf(text), 4);
assert.equal(fragment.childNodes.length, 0);
});

});

suite('cloneNode', function() {

test('clones a node', function() {
var dom = dom5.parseFragment('<div><span foo="bar">a</span></div>');
var dom = parse5.parseFragment('<div><span foo="bar">a</span></div>');
var div = dom.childNodes[0];
var span = div.childNodes[0];

Expand All @@ -408,7 +414,7 @@ suite('dom5', function() {
var fragText = '<div id="a" class="b c"><!-- nametag -->Hello World</div>';
var frag = null;
suiteSetup(function() {
frag = dom5.parseFragment(fragText).childNodes[0];
frag = parse5.parseFragment(fragText).childNodes[0];
});

test('hasTagName', function() {
Expand Down Expand Up @@ -526,7 +532,7 @@ suite('dom5', function() {
test('parentMatches', function() {
var fragText =
'<div class="a"><div class="b"><div class="c"></div></div></div>';
var frag = dom5.parseFragment(fragText);
var frag = parse5.parseFragment(fragText);
var fn = dom5.predicates.parentMatches(dom5.predicates.hasClass('a'));
assert.isFalse(fn(frag.childNodes[0])); // a
assert.isTrue(fn(frag.childNodes[0].childNodes[0])); // b
Expand All @@ -539,25 +545,26 @@ suite('dom5', function() {
'<!DOCTYPE html>',
'<link rel="import" href="polymer.html">',
'<dom-module id="my-el">',
'<template>',
'<div>', // TODO(justinfagnani): this used to be a template, we should test templates
'<img src="foo.jpg">',
'<a href="next-page.html">Anchor</a>',
'sample element',
'<!-- comment node -->',
'</template>',
'</div>',
'</dom-module>',
'<script>Polymer({is: "my-el"})</script>'
].join('\n');
var doc = null;

setup(function() {
doc = dom5.parse(docText);
doc = parse5.parse(docText);
});

test('nodeWalkAncestors', function() {
// doc -> dom-module -> template -> a
// doc -> dom-module -> div -> a
var anchor = doc.childNodes[1].childNodes[1].childNodes[0]
.childNodes[1].childNodes[0].childNodes[3];
.childNodes[1].childNodes[3];

assert(dom5.predicates.hasTagName('a')(anchor));
var domModule =
dom5.nodeWalkAncestors(
Expand All @@ -570,23 +577,22 @@ suite('dom5', function() {
});

test('nodeWalk', function() {
// doc -> body -> dom-module -> template -> template.content
var templateContent = doc.childNodes[1].childNodes[1].childNodes[0]
.childNodes[1].childNodes[0];
// doc -> body -> dom-module -> div
var div = doc.childNodes[1].childNodes[1].childNodes[0].childNodes[1];

var textNode = dom5.predicates.AND(
dom5.isTextNode,
dom5.predicates.hasTextValue('\nsample element\n')
);

// 'sample element' text node
var expected = templateContent.childNodes[4];
var expected = div.childNodes[4];
var actual = dom5.nodeWalk(doc, textNode);
assert.equal(expected, actual);

// <!-- comment node -->
expected = templateContent.childNodes[5];
actual = dom5.nodeWalk(templateContent, dom5.isCommentNode);
expected = div.childNodes[5];
actual = dom5.nodeWalk(div, dom5.isCommentNode);
assert.equal(expected, actual);
});

Expand All @@ -610,7 +616,7 @@ suite('dom5', function() {
);

// serialize to count for inserted <head> and <body>
var serializedDoc = dom5.serialize(doc);
var serializedDoc = parse5.serialize(doc);
// subtract one to get "gap" number
var expected = serializedDoc.split('\n').length - 1;
// add two for normalized text node "\nsample text\n"
Expand All @@ -630,14 +636,13 @@ suite('dom5', function() {
)
);

// doc -> body -> dom-module -> template -> template.content
var templateContent = doc.childNodes[1].childNodes[1].childNodes[0]
.childNodes[1].childNodes[0];
// doc -> body -> dom-module -> div
var div = doc.childNodes[1].childNodes[1].childNodes[0].childNodes[1];

// img
var expected_1 = templateContent.childNodes[1];
var expected_1 = div.childNodes[1];
// anchor
var expected_2 = templateContent.childNodes[3];
var expected_2 = div.childNodes[3];
var actual = dom5.queryAll(doc, fn);

assert.equal(actual.length, 2);
Expand All @@ -651,7 +656,7 @@ suite('dom5', function() {
var doc = null;

setup(function() {
doc = dom5.parse(docText);
doc = parse5.parse(docText);
});

test('nodeWalkAllPrior', function() {
Expand Down Expand Up @@ -774,7 +779,7 @@ suite('dom5', function() {
});

test('document can be normalized', function() {
var doc = dom5.parse('<!DOCTYPE html>');
var doc = parse5.parse('<!DOCTYPE html>');
var body = doc.childNodes[1].childNodes[1];
var div = con.element('div');
var tn1 = con.text('foo');
Expand Down

0 comments on commit 5bc535b

Please sign in to comment.