1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
|
Description: add node-domutils package as patch
Author: Felix Boehm <me@feedic.com>
Origin: https://github.com/fb55/domutils/
Bug-Debian: https://bugs.debian.org/761439
Last-Update: 2014-09-21
---
This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
--- /dev/null
+++ b/domutils/LICENSE
@@ -0,0 +1,11 @@
+Copyright (c) Felix Böhm
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+
+Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS,
+EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--- /dev/null
+++ b/domutils/index.js
@@ -0,0 +1,14 @@
+var DomUtils = module.exports;
+
+[
+ require("./lib/stringify"),
+ require("./lib/traversal"),
+ require("./lib/manipulation"),
+ require("./lib/querying"),
+ require("./lib/legacy"),
+ require("./lib/helpers")
+].forEach(function(ext){
+ Object.keys(ext).forEach(function(key){
+ DomUtils[key] = ext[key].bind(DomUtils);
+ });
+});
--- /dev/null
+++ b/domutils/lib/helpers.js
@@ -0,0 +1,141 @@
+// removeSubsets
+// Given an array of nodes, remove any member that is contained by another.
+exports.removeSubsets = function(nodes) {
+ var idx = nodes.length, node, ancestor, replace;
+
+ // Check if each node (or one of its ancestors) is already contained in the
+ // array.
+ while (--idx > -1) {
+ node = ancestor = nodes[idx];
+
+ // Temporarily remove the node under consideration
+ nodes[idx] = null;
+ replace = true;
+
+ while (ancestor) {
+ if (nodes.indexOf(ancestor) > -1) {
+ replace = false;
+ nodes.splice(idx, 1);
+ break;
+ }
+ ancestor = ancestor.parent;
+ }
+
+ // If the node has been found to be unique, re-insert it.
+ if (replace) {
+ nodes[idx] = node;
+ }
+ }
+
+ return nodes;
+};
+
+// Source: http://dom.spec.whatwg.org/#dom-node-comparedocumentposition
+var POSITION = {
+ DISCONNECTED: 1,
+ PRECEDING: 2,
+ FOLLOWING: 4,
+ CONTAINS: 8,
+ CONTAINED_BY: 16
+};
+
+// Compare the position of one node against another node in any other document.
+// The return value is a bitmask with the following values:
+//
+// document order:
+// > There is an ordering, document order, defined on all the nodes in the
+// > document corresponding to the order in which the first character of the
+// > XML representation of each node occurs in the XML representation of the
+// > document after expansion of general entities. Thus, the document element
+// > node will be the first node. Element nodes occur before their children.
+// > Thus, document order orders element nodes in order of the occurrence of
+// > their start-tag in the XML (after expansion of entities). The attribute
+// > nodes of an element occur after the element and before its children. The
+// > relative order of attribute nodes is implementation-dependent./
+// Source:
+// http://www.w3.org/TR/DOM-Level-3-Core/glossary.html#dt-document-order
+//
+// @argument {Node} nodaA The first node to use in the comparison
+// @argument {Node} nodeB The second node to use in the comparison
+//
+// @return {Number} A bitmask describing the input nodes' relative position.
+// See http://dom.spec.whatwg.org/#dom-node-comparedocumentposition for
+// a description of these values.
+var comparePos = exports.compareDocumentPosition = function(nodeA, nodeB) {
+ var aParents = [];
+ var bParents = [];
+ var current, sharedParent, siblings, aSibling, bSibling, idx;
+
+ if (nodeA === nodeB) {
+ return 0;
+ }
+
+ current = nodeA;
+ while (current) {
+ aParents.unshift(current);
+ current = current.parent;
+ }
+ current = nodeB;
+ while (current) {
+ bParents.unshift(current);
+ current = current.parent;
+ }
+
+ idx = 0;
+ while (aParents[idx] === bParents[idx]) {
+ idx++;
+ }
+
+ if (idx === 0) {
+ return POSITION.DISCONNECTED;
+ }
+
+ sharedParent = aParents[idx - 1];
+ siblings = sharedParent.children;
+ aSibling = aParents[idx];
+ bSibling = bParents[idx];
+
+ if (siblings.indexOf(aSibling) > siblings.indexOf(bSibling)) {
+ if (sharedParent === nodeB) {
+ return POSITION.FOLLOWING | POSITION.CONTAINED_BY;
+ }
+ return POSITION.FOLLOWING;
+ } else {
+ if (sharedParent === nodeA) {
+ return POSITION.PRECEDING | POSITION.CONTAINS;
+ }
+ return POSITION.PRECEDING;
+ }
+};
+
+// Sort an array of nodes based on their relative position in the document and
+// remove any duplicate nodes. If the array contains nodes that do not belong
+// to the same document, sort order is unspecified.
+//
+// @argument {Array} nodes Array of DOM nodes
+//
+// @returns {Array} collection of unique nodes, sorted in document order
+exports.uniqueSort = function(nodes) {
+ var idx = nodes.length, node, position;
+
+ nodes = nodes.slice();
+
+ while (--idx > -1) {
+ node = nodes[idx];
+ position = nodes.indexOf(node);
+ if (position > -1 && position < idx) {
+ nodes.splice(idx, 1);
+ }
+ }
+ nodes.sort(function(a, b) {
+ var relative = comparePos(a, b);
+ if (relative & POSITION.PRECEDING) {
+ return -1;
+ } else if (relative & POSITION.FOLLOWING) {
+ return 1;
+ }
+ return 0;
+ });
+
+ return nodes;
+};
--- /dev/null
+++ b/domutils/lib/legacy.js
@@ -0,0 +1,87 @@
+var ElementType = require("domelementtype");
+var isTag = exports.isTag = ElementType.isTag;
+
+exports.testElement = function(options, element){
+ for(var key in options){
+ if(!options.hasOwnProperty(key));
+ else if(key === "tag_name"){
+ if(!isTag(element) || !options.tag_name(element.name)){
+ return false;
+ }
+ } else if(key === "tag_type"){
+ if(!options.tag_type(element.type)) return false;
+ } else if(key === "tag_contains"){
+ if(isTag(element) || !options.tag_contains(element.data)){
+ return false;
+ }
+ } else if(!element.attribs || !options[key](element.attribs[key])){
+ return false;
+ }
+ }
+ return true;
+};
+
+var Checks = {
+ tag_name: function(name){
+ if(typeof name === "function"){
+ return function(elem){ return isTag(elem) && name(elem.name); };
+ } else if(name === "*"){
+ return isTag;
+ } else {
+ return function(elem){ return isTag(elem) && elem.name === name; };
+ }
+ },
+ tag_type: function(type){
+ if(typeof type === "function"){
+ return function(elem){ return type(elem.type); };
+ } else {
+ return function(elem){ return elem.type === type; };
+ }
+ },
+ tag_contains: function(data){
+ if(typeof data === "function"){
+ return function(elem){ return !isTag(elem) && data(elem.data); };
+ } else {
+ return function(elem){ return !isTag(elem) && elem.data === data; };
+ }
+ }
+};
+
+function getAttribCheck(attrib, value){
+ if(typeof value === "function"){
+ return function(elem){ return elem.attribs && value(elem.attribs[attrib]); };
+ } else {
+ return function(elem){ return elem.attribs && elem.attribs[attrib] === value; };
+ }
+}
+
+function combineFuncs(a, b){
+ return function(elem){
+ return a(elem) || b(elem);
+ };
+}
+
+exports.getElements = function(options, element, recurse, limit){
+ var funcs = Object.keys(options).map(function(key){
+ var value = options[key];
+ return key in Checks ? Checks[key](value) : getAttribCheck(key, value);
+ });
+
+ return funcs.length === 0 ? [] : this.filter(
+ funcs.reduce(combineFuncs),
+ element, recurse, limit
+ );
+};
+
+exports.getElementById = function(id, element, recurse){
+ if(!Array.isArray(element)) element = [element];
+ return this.findOne(getAttribCheck("id", id), element, recurse !== false);
+};
+
+exports.getElementsByTagName = function(name, element, recurse, limit){
+ return this.filter(Checks.tag_name(name), element, recurse, limit);
+};
+
+exports.getElementsByTagType = function(type, element, recurse, limit){
+ return this.filter(Checks.tag_type(type), element, recurse, limit);
+};
--- /dev/null
+++ b/domutils/lib/manipulation.js
@@ -0,0 +1,77 @@
+exports.removeElement = function(elem){
+ if(elem.prev) elem.prev.next = elem.next;
+ if(elem.next) elem.next.prev = elem.prev;
+
+ if(elem.parent){
+ var childs = elem.parent.children;
+ childs.splice(childs.lastIndexOf(elem), 1);
+ }
+};
+
+exports.replaceElement = function(elem, replacement){
+ var prev = replacement.prev = elem.prev;
+ if(prev){
+ prev.next = replacement;
+ }
+
+ var next = replacement.next = elem.next;
+ if(next){
+ next.prev = replacement;
+ }
+
+ var parent = replacement.parent = elem.parent;
+ if(parent){
+ var childs = parent.children;
+ childs[childs.lastIndexOf(elem)] = replacement;
+ }
+};
+
+exports.appendChild = function(elem, child){
+ child.parent = elem;
+
+ if(elem.children.push(child) !== 1){
+ var sibling = elem.children[elem.children.length - 2];
+ sibling.next = child;
+ child.prev = sibling;
+ child.next = null;
+ }
+};
+
+exports.append = function(elem, next){
+ var parent = elem.parent,
+ currNext = elem.next;
+
+ next.next = currNext;
+ next.prev = elem;
+ elem.next = next;
+ next.parent = parent;
+
+ if(currNext){
+ currNext.prev = next;
+ if(parent){
+ var childs = parent.children;
+ childs.splice(childs.lastIndexOf(currNext), 0, next);
+ }
+ } else if(parent){
+ parent.children.push(next);
+ }
+};
+
+exports.prepend = function(elem, prev){
+ var parent = elem.parent;
+ if(parent){
+ var childs = parent.children;
+ childs.splice(childs.lastIndexOf(elem), 0, prev);
+ }
+
+ if(elem.prev){
+ elem.prev.next = prev;
+ }
+
+ prev.parent = parent;
+ prev.prev = elem.prev;
+ prev.next = elem;
+ elem.prev = prev;
+};
+
+
--- /dev/null
+++ b/domutils/lib/querying.js
@@ -0,0 +1,94 @@
+var isTag = require("domelementtype").isTag;
+
+module.exports = {
+ filter: filter,
+ find: find,
+ findOneChild: findOneChild,
+ findOne: findOne,
+ existsOne: existsOne,
+ findAll: findAll
+};
+
+function filter(test, element, recurse, limit){
+ if(!Array.isArray(element)) element = [element];
+
+ if(typeof limit !== "number" || !isFinite(limit)){
+ limit = Infinity;
+ }
+ return find(test, element, recurse !== false, limit);
+}
+
+function find(test, elems, recurse, limit){
+ var result = [], childs;
+
+ for(var i = 0, j = elems.length; i < j; i++){
+ if(test(elems[i])){
+ result.push(elems[i]);
+ if(--limit <= 0) break;
+ }
+
+ childs = elems[i].children;
+ if(recurse && childs && childs.length > 0){
+ childs = find(test, childs, recurse, limit);
+ result = result.concat(childs);
+ limit -= childs.length;
+ if(limit <= 0) break;
+ }
+ }
+
+ return result;
+}
+
+function findOneChild(test, elems){
+ for(var i = 0, l = elems.length; i < l; i++){
+ if(test(elems[i])) return elems[i];
+ }
+
+ return null;
+}
+
+function findOne(test, elems){
+ var elem = null;
+
+ for(var i = 0, l = elems.length; i < l && !elem; i++){
+ if(!isTag(elems[i])){
+ continue;
+ } else if(test(elems[i])){
+ elem = elems[i];
+ } else if(elems[i].children.length > 0){
+ elem = findOne(test, elems[i].children);
+ }
+ }
+
+ return elem;
+}
+
+function existsOne(test, elems){
+ for(var i = 0, l = elems.length; i < l; i++){
+ if(
+ isTag(elems[i]) && (
+ test(elems[i]) || (
+ elems[i].children.length > 0 &&
+ existsOne(test, elems[i].children)
+ )
+ )
+ ){
+ return true;
+ }
+ }
+
+ return false;
+}
+
+function findAll(test, elems){
+ var result = [];
+ for(var i = 0, j = elems.length; i < j; i++){
+ if(!isTag(elems[i])) continue;
+ if(test(elems[i])) result.push(elems[i]);
+
+ if(elems[i].children.length > 0){
+ result = result.concat(findAll(test, elems[i].children));
+ }
+ }
+ return result;
+}
--- /dev/null
+++ b/domutils/lib/stringify.js
@@ -0,0 +1,22 @@
+var ElementType = require("domelementtype"),
+ getOuterHTML = require("dom-serializer"),
+ isTag = ElementType.isTag;
+
+module.exports = {
+ getInnerHTML: getInnerHTML,
+ getOuterHTML: getOuterHTML,
+ getText: getText
+};
+
+function getInnerHTML(elem, opts){
+ return elem.children ? elem.children.map(function(elem){
+ return getOuterHTML(elem, opts);
+ }).join("") : "";
+}
+
+function getText(elem){
+ if(Array.isArray(elem)) return elem.map(getText).join("");
+ if(isTag(elem) || elem.type === ElementType.CDATA) return getText(elem.children);
+ if(elem.type === ElementType.Text) return elem.data;
+ return "";
+}
--- /dev/null
+++ b/domutils/lib/traversal.js
@@ -0,0 +1,24 @@
+var getChildren = exports.getChildren = function(elem){
+ return elem.children;
+};
+
+var getParent = exports.getParent = function(elem){
+ return elem.parent;
+};
+
+exports.getSiblings = function(elem){
+ var parent = getParent(elem);
+ return parent ? getChildren(parent) : [elem];
+};
+
+exports.getAttributeValue = function(elem, name){
+ return elem.attribs && elem.attribs[name];
+};
+
+exports.hasAttrib = function(elem, name){
+ return hasOwnProperty.call(elem.attribs, name);
+};
+
+exports.getName = function(elem){
+ return elem.name;
+};
--- /dev/null
+++ b/domutils/package.json
@@ -0,0 +1,46 @@
+{
+ "name": "domutils",
+ "version": "1.5.0",
+ "description": "utilities for working with htmlparser2's dom",
+ "main": "index.js",
+ "directories": {
+ "test": "tests"
+ },
+ "scripts": {
+ "test": "mocha test/tests/**.js && jshint index.js test/**/*.js lib/*.js"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/FB55/domutils.git"
+ },
+ "keywords": [
+ "dom",
+ "htmlparser2"
+ ],
+ "dependencies": {
+ "dom-serializer": "0",
+ "domelementtype": "1"
+ },
+ "devDependencies": {
+ "htmlparser2": "~3.3.0",
+ "domhandler": "2",
+ "jshint": "~2.3.0",
+ "mocha": "~1.15.1"
+ },
+ "author": "Felix Boehm <me@feedic.com>",
+ "jshintConfig": {
+ "proto": true,
+ "unused": true,
+ "eqnull": true,
+ "undef": true,
+ "quotmark": "double",
+ "eqeqeq": true,
+ "trailing": true,
+ "node": true,
+ "globals": {
+ "describe": true,
+ "it": true,
+ "beforeEach": true
+ }
+ }
+}
--- /dev/null
+++ b/domutils/readme.md
@@ -0,0 +1 @@
+utilities for working with htmlparser2's dom
--- /dev/null
+++ b/domutils/test/fixture.js
@@ -0,0 +1,6 @@
+var makeDom = require("./utils").makeDom;
+var markup = Array(21).join(
+ "<?xml><tag1 id='asdf'> <script>text</script> <!-- comment --> <tag2> text </tag1>"
+);
+
+module.exports = makeDom(markup);
--- /dev/null
+++ b/domutils/test/tests/helpers.js
@@ -0,0 +1,89 @@
+var makeDom = require("../utils").makeDom;
+var helpers = require("../..");
+var assert = require("assert");
+
+describe("helpers", function() {
+ describe("removeSubsets", function() {
+ var removeSubsets = helpers.removeSubsets;
+ var dom = makeDom("<div><p><span></span></p><p></p></div>")[0];
+
+ it("removes identical trees", function() {
+ var matches = removeSubsets([dom, dom]);
+ assert.equal(matches.length, 1);
+ });
+
+ it("Removes subsets found first", function() {
+ var matches = removeSubsets([dom, dom.children[0].children[0]]);
+ assert.equal(matches.length, 1);
+ });
+
+ it("Removes subsets found last", function() {
+ var matches = removeSubsets([dom.children[0], dom]);
+ assert.equal(matches.length, 1);
+ });
+
+ it("Does not remove unique trees", function() {
+ var matches = removeSubsets([dom.children[0], dom.children[1]]);
+ assert.equal(matches.length, 2);
+ });
+ });
+
+ describe("compareDocumentPosition", function() {
+ var compareDocumentPosition = helpers.compareDocumentPosition;
+ var markup = "<div><p><span></span></p><a></a></div>";
+ var dom = makeDom(markup)[0];
+ var p = dom.children[0];
+ var span = p.children[0];
+ var a = dom.children[1];
+
+ it("reports when the first node occurs before the second indirectly", function() {
+ assert.equal(compareDocumentPosition(span, a), 2);
+ });
+
+ it("reports when the first node contains the second", function() {
+ assert.equal(compareDocumentPosition(p, span), 10);
+ });
+
+ it("reports when the first node occurs after the second indirectly", function() {
+ assert.equal(compareDocumentPosition(a, span), 4);
+ });
+
+ it("reports when the first node is contained by the second", function() {
+ assert.equal(compareDocumentPosition(span, p), 20);
+ });
+
+ it("reports when the nodes belong to separate documents", function() {
+ var other = makeDom(markup)[0].children[0].children[0];
+
+ assert.equal(compareDocumentPosition(span, other), 1);
+ });
+
+ it("reports when the nodes are identical", function() {
+ assert.equal(compareDocumentPosition(span, span), 0);
+ });
+ });
+
+ describe("uniqueSort", function() {
+ var uniqueSort = helpers.uniqueSort;
+ var dom, p, span, a;
+
+ beforeEach(function() {
+ dom = makeDom("<div><p><span></span></p><a></a></div>")[0];
+ p = dom.children[0];
+ span = p.children[0];
+ a = dom.children[1];
+ });
+
+ it("leaves unique elements untouched", function() {
+ assert.deepEqual(uniqueSort([p, a]), [p, a]);
+ });
+
+ it("removes duplicate elements", function() {
+ assert.deepEqual(uniqueSort([p, a, p]), [p, a]);
+ });
+
+ it("sorts nodes in document order", function() {
+ assert.deepEqual(uniqueSort([a, dom, span, p]), [dom, p, span, a]);
+ });
+ });
+});
--- /dev/null
+++ b/domutils/test/tests/legacy.js
@@ -0,0 +1,119 @@
+var DomUtils = require("../..");
+var fixture = require("../fixture");
+var assert = require("assert");
+
+// Set up expected structures
+var expected = {
+ idAsdf: fixture[1],
+ tag2: [],
+ typeScript: []
+};
+for (var idx = 0; idx < 20; ++idx) {
+ expected.tag2.push(fixture[idx*2 + 1].children[5]);
+ expected.typeScript.push(fixture[idx*2 + 1].children[1]);
+}
+
+describe("legacy", function() {
+ describe("getElements", function() {
+ var getElements = DomUtils.getElements;
+ it("returns the node with the specified ID", function() {
+ assert.deepEqual(
+ getElements({ id: "asdf" }, fixture, true, 1),
+ [expected.idAsdf]
+ );
+ });
+ it("returns empty array for unknown IDs", function() {
+ assert.deepEqual(getElements({ id: "asdfs" }, fixture, true), []);
+ });
+ it("returns the nodes with the specified tag name", function() {
+ assert.deepEqual(
+ getElements({ tag_name:"tag2" }, fixture, true),
+ expected.tag2
+ );
+ });
+ it("returns empty array for unknown tag names", function() {
+ assert.deepEqual(
+ getElements({ tag_name : "asdfs" }, fixture, true),
+ []
+ );
+ });
+ it("returns the nodes with the specified tag type", function() {
+ assert.deepEqual(
+ getElements({ tag_type: "script" }, fixture, true),
+ expected.typeScript
+ );
+ });
+ it("returns empty array for unknown tag types", function() {
+ assert.deepEqual(
+ getElements({ tag_type: "video" }, fixture, true),
+ []
+ );
+ });
+ });
+
+ describe("getElementById", function() {
+ var getElementById = DomUtils.getElementById;
+ it("returns the specified node", function() {
+ assert.equal(
+ expected.idAsdf,
+ getElementById("asdf", fixture, true)
+ );
+ });
+ it("returns `null` for unknown IDs", function() {
+ assert.equal(null, getElementById("asdfs", fixture, true));
+ });
+ });
+
+ describe("getElementsByTagName", function() {
+ var getElementsByTagName = DomUtils.getElementsByTagName;
+ it("returns the specified nodes", function() {
+ assert.deepEqual(
+ getElementsByTagName("tag2", fixture, true),
+ expected.tag2
+ );
+ });
+ it("returns empty array for unknown tag names", function() {
+ assert.deepEqual(
+ getElementsByTagName("tag23", fixture, true),
+ []
+ );
+ });
+ });
+
+ describe("getElementsByTagType", function() {
+ var getElementsByTagType = DomUtils.getElementsByTagType;
+ it("returns the specified nodes", function() {
+ assert.deepEqual(
+ getElementsByTagType("script", fixture, true),
+ expected.typeScript
+ );
+ });
+ it("returns empty array for unknown tag types", function() {
+ assert.deepEqual(
+ getElementsByTagType("video", fixture, true),
+ []
+ );
+ });
+ });
+
+ describe("getOuterHTML", function() {
+ var getOuterHTML = DomUtils.getOuterHTML;
+ it("Correctly renders the outer HTML", function() {
+ assert.equal(
+ getOuterHTML(fixture[1]),
+ "<tag1 id=\"asdf\"> <script>text</script> <!-- comment --> <tag2> text </tag2></tag1>"
+ );
+ });
+ });
+
+ describe("getInnerHTML", function() {
+ var getInnerHTML = DomUtils.getInnerHTML;
+ it("Correctly renders the inner HTML", function() {
+ assert.equal(
+ getInnerHTML(fixture[1]),
+ " <script>text</script> <!-- comment --> <tag2> text </tag2>"
+ );
+ });
+ });
+
+});
--- /dev/null
+++ b/domutils/test/utils.js
@@ -0,0 +1,9 @@
+var htmlparser = require("htmlparser2");
+
+exports.makeDom = function(markup) {
+ var handler = new htmlparser.DomHandler(),
+ parser = new htmlparser.Parser(handler);
+ parser.write(markup);
+ parser.done();
+ return handler.dom;
+};
|