diff --git a/lib/zest.js b/lib/zest.js index f358096..04a2e24 100644 --- a/lib/zest.js +++ b/lib/zest.js @@ -171,6 +171,33 @@ var nth = function(param, test, last) { }; }; +/** + * Handle :column pseudo-classes + */ + function calc_cols (td, cache) { + while (td = td.parentNode) if (td.nodeName === 'TABLE') break; + + var cols = td.getElementsByTagName('col') + var col_list = [] + , col + , col_pos = 0 + , td_pos = 1 + , spans + + for (; col = cols[col_pos]; col_pos++) { + spans = parseInt(col.getAttribute('span')) || 1 + + td_pos += spans + + while (spans) { + col_list[td_pos - spans] = col + spans-- + } + } + + return col_list + } + /** * Simple Selectors */ @@ -412,14 +439,37 @@ var selectors = { ':visited': function() { throw new Error(':visited is not supported.'); }, - ':column': function() { - throw new Error(':column is not supported.'); - }, - ':nth-column': function() { - throw new Error(':nth-column is not supported.'); + ':column': function(selector) { + var test = zest.compile(selector) + + return function(el) { + if (el.parentNode.nodeType !== 1 || el.nodeName !== 'TD') return; + + var rel = child(el.parentNode) + , pos = 0 + , temp_pos + , cols = calc_cols(el) + , spans + + while (rel) { + spans = parseInt(rel.getAttribute('span')) || 1 + if (rel.nodeName === 'TD') pos += spans + if (rel === el) { + while (spans--) { + temp_pos = pos - spans + if (cols[temp_pos] && test(cols[temp_pos])) return true + } + return false; + } + rel = next(rel); + } + }; + };, + ':nth-column': function(param) { + return selectors[':column'](':nth-child(' + param + ')') }, - ':nth-last-column': function() { - throw new Error(':nth-last-column is not supported.'); + ':nth-last-column': function(param) { + return selectors[':column'](':nth-last-child(' + param + ')') }, ':current': function() { throw new Error(':current is not supported.');