Skip to content
Myr

Removed ElementCollection support

Before adoption: this commit came in with the history the project adopted, so no landing records it.

this will become another library

authored by visionmedia · committed by visionmedia · parent 9586ecf · Browse files · 8 files+13 added−297 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+13 added−297 removed

  • tests:+2 added−95 removed
  • other:+11 added−202 removed

8 files+13 added−297 removed

modified:lib/express/core.js+10 added-9 removed
12// Express - Core - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)3var utils = require('express/utils');4utils.mixin(require('sys'))5utils.mixin(require('express/exceptions'))6utils.mixin(require('express/collection'))7utils.mixin(require('express/event'))8utils.mixin(require('express/request'))9utils.mixin(require('express/plugin'))10utils.mixin(require('express/dsl'))34/**5 * Module dependencies.6 */78var multipart = require('multipart'),9 utils = require('express/utils'),10 events = require('events'),11 fs = require('fs')19 1213utils.mixin(require('sys'))14utils.mixin(require('express/exceptions'))15utils.mixin(require('express/collection'))16utils.mixin(require('express/event'))17utils.mixin(require('express/request'))18utils.mixin(require('express/plugin'))19utils.mixin(require('express/dsl'))2021// --- Route2223Route = Class({24
299 unmodified lines
deleted:lib/express/element-collection.js-179 removed
12// Express - ElementCollection - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)34/**5 * Module dependencies.6 */78var libxml = require('libxmljs')910// --- css2xpath1112/** version 0.1 2009-04-3013 * @author Andrea Giammarchi14 * @license Mit Style License15 * @project http://code.google.com/p/css2xpath/16 */17var css2xpath=(function(){var b=[/\[([^\]~\$\*\^\|\!]+)(=[^\]]+)?\]/g,"[@$1$2]",/\s*,\s*/g,"|",/\s*(\+|~|>)\s*/g,"$1",/([a-zA-Z0-9\_\-\*])~([a-zA-Z0-9\_\-\*])/g,"$1/following-sibling::$2",/([a-zA-Z0-9\_\-\*])\+([a-zA-Z0-9\_\-\*])/g,"$1/following-sibling::*[1]/self::$2",/([a-zA-Z0-9\_\-\*])>([a-zA-Z0-9\_\-\*])/g,"$1/$2",/\[([^=]+)=([^'|"][^\]]*)\]/g,"[$1='$2']",/(^|[^a-zA-Z0-9\_\-\*])(#|\.)([a-zA-Z0-9\_\-]+)/g,"$1*$2$3",/([\>\+\|\~\,\s])([a-zA-Z\*]+)/g,"$1//$2",/\s+\/\//g,"//",/([a-zA-Z0-9\_\-\*]+):first-child/g,"*[1]/self::$1",/([a-zA-Z0-9\_\-\*]+):last-child/g,"$1[not(following-sibling::*)]",/([a-zA-Z0-9\_\-\*]+):only-child/g,"*[last()=1]/self::$1",/([a-zA-Z0-9\_\-\*]+):empty/g,"$1[not(*) and not(normalize-space())]",/([a-zA-Z0-9\_\-\*]+):not\(([^\)]*)\)/g,function(f,e,d){return e.concat("[not(",a(d).replace(/^[^\[]+\[([^\]]*)\].*$/g,"$1"),")]")},/([a-zA-Z0-9\_\-\*]+):nth-child\(([^\)]*)\)/g,function(f,e,d){switch(d){case"n":return e;case"even":return"*[position() mod 2=0 and position()>=0]/self::"+e;case"odd":return e+"[(count(preceding-sibling::*) + 1) mod 2=1]";default:d=(d||"0").replace(/^([0-9]*)n.*?([0-9]*)$/,"$1+$2").split("+");d[1]=d[1]||"0";return"*[(position()-".concat(d[1],") mod ",d[0],"=0 and position()>=",d[1],"]/self::",e)}},/:contains\(([^\)]*)\)/g,function(e,d){return"[contains(string(.),'"+d+"')]"},/\[([a-zA-Z0-9\_\-]+)\|=([^\]]+)\]/g,"[@$1=$2 or starts-with(@$1,concat($2,'-'))]",/\[([a-zA-Z0-9\_\-]+)\*=([^\]]+)\]/g,"[contains(@$1,$2)]",/\[([a-zA-Z0-9\_\-]+)~=([^\]]+)\]/g,"[contains(concat(' ',normalize-space(@$1),' '),concat(' ',$2,' '))]",/\[([a-zA-Z0-9\_\-]+)\^=([^\]]+)\]/g,"[starts-with(@$1,$2)]",/\[([a-zA-Z0-9\_\-]+)\$=([^\]]+)\]/g,function(f,e,d){return"[substring(@".concat(e,",string-length(@",e,")-",d.length-3,")=",d,"]")},/\[([a-zA-Z0-9\_\-]+)\!=([^\]]+)\]/g,"[not(@$1) or @$1!=$2]",/#([a-zA-Z0-9\_\-]+)/g,"[@id='$1']",/\.([a-zA-Z0-9\_\-]+)/g,"[contains(concat(' ',normalize-space(@class),' '),' $1 ')]",/\]\[([^\]]+)/g," and ($1)"],c=b.length;return function a(e){var d=0;while(d<c){e=e.replace(b[d++],b[d++])}return"//"+e}})();1819// --- ElementCollection2021ElementCollection = Collection.extend({22 23 /**24 * Initialize with string of _markup_.25 *26 * @param {string} markup27 * @api private28 */29 30 init: function(markup) {31 if (typeof markup != 'string')32 return this.__super__(markup)33 if (!(/<html>/.test(markup))) 34 markup = '<html><body>' + markup + '</body></html>'35 this.document = libxml.parseHtmlString(markup)36 this.arr = [this.document.root()]37 },38 39 /**40 * Return the first element's name.41 *42 * @return {string}43 * @api public44 */45 46 name: function() {47 return this.at(0).name()48 },49 50 /**51 * Search child elements with the given _xpath_.52 *53 * @param {string} xpath54 * @return {ElementCollection}55 * @api public56 */57 58 xpath: function(xpath) {59 // TODO: refactor with flatten()60 return $(this.reduce([], function(array, e){61 $(e.find(xpath)).each(function(child){62 array.push(child)63 })64 return array65 }))66 },67 68 /**69 * Search child elements with the given css _selector_70 *71 * @param {string} selector72 * @return {ElementCollection}73 * @api public74 */75 76 search: function(selector) {77 return this.xpath(css2xpath(selector))78 },79 80 /**81 * Return collection of children.82 *83 * @return {ElementCollection}84 * @api public85 */86 87 children: function() {88 // TODO: refactor with flatten()89 return $(this.reduce([], function(array, e){90 $(e.children()).each(function(child){91 array.push(child)92 })93 return array94 }))95 },96 97 /**98 * Return collection of parents.99 *100 * @return {ElementCollection}101 * @api public102 */103 104 parents: function() {105 return this.map(function(e){106 return e.parent()107 })108 },109 110 /**111 * Return the first element's parent.112 *113 * @return {ElementCollection}114 * @api public115 */116 117 parent: function() {118 return $([this.at(0).parent()])119 },120 121 /**122 * Return the prev element.123 *124 * @return {ElementCollection}125 * @api public126 */127 128 prev: function() {129 return $([this.at(0).prevSibling()])130 },131 132 /**133 * Return the next element.134 *135 * @return {ElementCollection}136 * @api public137 */138 139 next: function() {140 return $([this.at(0).nextSibling()])141 },142 143 /**144 * Return the first element's text content.145 *146 * @return {string}147 * @api public148 */149 150 text: function() {151 return this.at(0).text()152 },153 154 /**155 * Convert collection to a string.156 *157 * @return {string}158 * @api public159 */160 161 toString: function() {162 if (this.at(0) && this.at(0).doc)163 return '[Collection <elements>]'164 return this.__super__()165 }166})167168/**169 * Add markup support to $().170 *171 * @param {object} arr172 * @return {Collection, ElementCollection}173 * @api public174 */175 176var $ = exports.$ = function(arr) {177 if (arr instanceof Collection) return arr178 return new ElementCollection(arr)179}
deleted:spec/support/libxmljsnot shown

Its text is not shown, deleted.

modified:spec/node.js+2 added-12 removed
deleted:spec/spec.element-collection.js-83 removed
12process.mixin(require('express/collection'))3process.mixin(require('express/element-collection'))45describe 'Express'6 describe 'ElementCollection'7 8 describe '$("markup string")'9 it 'should return a ElementCollection'10 $('<p>foo</p>').should.be_an_instance_of Collection11 $('<p>foo</p>').should.be_an_instance_of ElementCollection12 end13 14 it 'should wrap with <html><body>.. when not present'15 $('<p>foo</p>').at(0).name().should.eql 'html'16 end17 18 it 'should not wrap with <html><body> when already present'19 $('<html><body></body></html>').at(0).name().should.eql 'html'20 end21 end22 23 describe '#name()'24 it 'should return the first elements name'25 $('<html><body></body></html>').name().should.eql 'html'26 end27 end28 29 describe '#xpath()'30 it 'should find children matching the given xpath'31 $('<li>1</li><li>2</li>').xpath('descendant-or-self::li').length().should.eql 232 $('<li>1</li><li>2</li>').xpath('descendant-or-self::li').at(0).name().should.eql 'li'33 var items = $('<li><p>Foo</p></li><li><p>Bar</p></li>').xpath('descendant-or-self::li')34 items.xpath('descendant-or-self::p').length().should.eql 235 end36 end37 38 describe '#search()'39 it 'should find children matching the given css selector'40 $('<ul><li>foo</li><li>bar</li></ul>').search('ul > li:nth-child(2)').text().should.eql 'bar'41 end42 end43 44 describe '#children()'45 it 'should return children'46 $('<p><em>foo</em><strong>bar</strong></p>').children().length().should.eql 147 $('<p><em>foo</em><strong>bar</strong></p>').xpath('descendant-or-self::p').children().length().should.eql 248 end49 end50 51 describe '#parents()'52 it 'should return parents'53 $('<ul><li></li></ul><ul><li></li></ul>').xpath('descendant-or-self::li').parents().length().should.eql 254 $('<ul><li></li></ul><ul><li></li></ul>').xpath("descendant-or-self::*/*[name() = 'ul' and (position() = 1)]/descendant::li").parents().length().should.eql 155 end56 end57 58 describe '#parent()'59 it 'should return the first parent'60 $('<ul><li></li></ul><ul><li></li></ul>').xpath('descendant-or-self::li').parent().length().should.eql 161 end62 end63 64 describe '#text()'65 it 'should return an elements text'66 $('<p>foo bar</p>').text().should.eql 'foo bar'67 end68 end69 70 describe '#next()'71 it 'should return the next element'72 $('<em>foo</em><p>bar</p>').xpath('descendant-or-self::em').next().text().should.eql 'bar'73 end74 end75 76 describe '#prev()'77 it 'should return the previous element'78 $('<em>foo</em><p>bar</p>').xpath('descendant-or-self::p').prev().text().should.eql 'foo'79 end80 end81 82 end83end
modified:.gitmodules-3 removed
1[submodule "spec/support/libxmljs"]2 path = spec/support/libxmljs3 url = git://github.com/sprsquish/libxmljs.git1[submodule "lib/support/haml"]2 path = lib/support/haml3 url = git://github.com/creationix/haml-js.git4[submodule "lib/support/js-oo"]
5 unmodified lines
modified:Makefile+1 added-10 removed
4 unmodified lines
56init:7 @git submodule init && git submodule update89test: init spec/support/libxmljs/libxmljs.node9test: init10 @$(NODE) spec/node.js all11 12test-independant: init13 @$(NODE) spec/node.js independant14 15test-dependant: init spec/support/libxmljs/libxmljs.node16 @$(NODE) spec/node.js dependant17 12app: app-chat13 14app-chat:15 @$(NODE) examples/chat/app.js
4 unmodified lines
20benchmark:21 @$(NODE) benchmarks/collection.js22 @$(NODE) benchmarks/views.js23 30spec/support/libxmljs/libxmljs.node:31 @scons -C spec/support/libxmljs libxmljs.node32 24.PHONY: init test benchmark app
modified:Readme.md-1 removed