Skip to content

Instantly share code, notes, and snippets.

@emjayess
Created September 13, 2012 22:45
Show Gist options
  • Select an option

  • Save emjayess/3718279 to your computer and use it in GitHub Desktop.

Select an option

Save emjayess/3718279 to your computer and use it in GitHub Desktop.
finds <p>'s within a given context of a web page
(function(){
var context = arguments[0] || document.body
//, nodes = context.childNodes, nLen = nodes.length
, ctxParagraphs = [], ctxConditions = false
, ELM_NODE = 1, TXT_NODE = 3;//, HTML_COMMENT_NODE = 8
function hasClass(ctx, cls) {
return ctx.className
&& new RegExp(
'(^|\\s)' + cls + '(\\s|$)'
)
.test(ctx.className);
}
function seek(ctx) {
if (ctxConditions) return;
var tryClass = arguments[1] ? arguments[1] : false
, isElement = ctx.nodeType == ELM_NODE
, isClassed = hasClass(ctx, tryClass);
//if (tryClass) console.log('seeking .'+tryClass);
ctxConditions = isElement && tryClass && isClassed;
/*
// is element node AND not attempting to match class:
(isElement && !tryClass)
|| // OR
// is element node AND attempting class match AND found match:
(isElement && tryClass && isClassed)
;*/
if ( ctxConditions ) {
// found a useful class-scoped context, use it and get out!
ctxParagraphs = ctx.getElementsByTagName('p');//console.log('context conditions met!');
return;
}
else {//recurse
for (var i=0, l = ctx.childNodes.length; i < l; ++i) {
switch (ctx.childNodes[i].nodeName.toLowerCase()) {
case 'div':
seek(ctx.childNodes[i], tryClass);
break;
}
}
}
}
// seek known content #id markers...
// @todo
// seek known content .class markers...
try {
seek(context, 'birdie'); // miss
seek(context, 'fuckwad'); // miss
seek(context, 'gimmeh'); // miss
seek(context, 'post'); // hit! (wordpress)
}
catch(e) {;}
// if no narrower context was discovered, operate on all <p>'s:
if (ctxParagraphs.length == 0) {
//seek(context);
ctxParagraphs = context.getElementsByTagName('p');
}
var pLen = ctxParagraphs.length; //console.log(pLen); console.log(postParagraphs);
while(pLen--) {
// do shit!
ctxParagraphs[pLen].style.textDecoration = 'line-through';
}
}())
(function(){
var context = arguments[0] || document.body
, pNodes = [], txtNodes = []
, ELM_NODE = 1, TXT_NODE = 3//, HTML_COMMENT_NODE = 8
, whitespace = /^\s*$/;
// more useful!
function seekParaNodes(ctx) {
if (ctx.nodeType == ELM_NODE && ctx.nodeName == 'P') {
pNodes.push(ctx); //console.log(ctx.textContent);
}
else {//recurse
for (var i=0, l = ctx.childNodes.length; i < l; ++i) {
seekParaNodes(ctx.childNodes[i]);
}
}
}
// less useful
function seekTextNodes(ctx) {
if (ctx.nodeType == TXT_NODE) {
if (whitespace.test(ctx.nodeValue)) {
txtNodes.push(ctx);//console.log(ctx.textContent);
}
}
else {//recurse
for (var i=0, l = ctx.childNodes.length; i < l; ++i) {
seekTextNodes(ctx.childNodes[i]);
}
}
}
seekParaNodes(context);
console.log(pNodes);
}(/*document.getElementById('post-155442')*/))
@emjayess
Copy link
Copy Markdown
Author

this answer on stackoverflow helped out with the seekTextNodes() approach using native DOM methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment