-
Notifications
You must be signed in to change notification settings - Fork 1
/
outliner.js
executable file
·11 lines (11 loc) · 5.46 KB
/
outliner.js
1
2
3
4
5
6
7
8
9
10
11
/**
* This code contains an implementation of HTML5 outlining algorithm, as described by WHATWG at [1]
*
* The copyright notice at [2] says:
* (c) Copyright 2004-2009 Apple Computer, Inc., Mozilla Foundation, and Opera Software ASA.
* You are granted a license to use, reproduce and create derivative works of this document.
*
* [1] http://www.whatwg.org/specs/web-apps/current-work/multipage/sections.html#outlines
* [2] http://www.whatwg.org/specs/web-apps/current-work/multipage/index.html
*/
!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,b.HTML5Outline=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b){b.exports=a("./src/createOutline")},{"./src/createOutline":6}],2:[function(a,b){function c(a,b){this.startingNode=a.node,this.sections=[b]}var d=a("./asHTML");c.prototype.getLastSection=function(){return this.sections[this.sections.length-1]},c.prototype.asHTML=function(a){return d(this.sections,a)},b.exports=c},{"./asHTML":5}],3:[function(a,b){function c(a){this.node=a}b.exports=c},{}],4:[function(a,b){function c(a){this.sections=[],this.startingNode=a}c.prototype.append=function(a){a.container=this,this.sections.push(a)},b.exports=c},{}],5:[function(a,b){function c(a){if(a.heading.implied)return"<i>Untitled "+f.getTagName(a.startingNode)+"</i>";var b=f.getRankingHeadingElement(a.heading);if(!b)return"<i>Error: no H1-H6 inside HGROUP</i>";var c=b.textContent;return c?f.escapeHtml(c):"<i>No text content inside "+f.getTagName(b)+"</i>"}function d(a,b){var c=a.startingNode.getAttribute("id");if(c)return c;if(!a.heading.implied){var d=a.heading.getAttribute("id");if(d)return d}var e=a.startingNode;do var f="h5o-"+ ++b.linkCounter;while(e.ownerDocument.getElementById(f));return e.setAttribute("id",f),f}function e(a,b){if("object"!=typeof b&&(b={createLinks:!!b}),!a.length)return"";if(b.skipTopHeader)return e(a[0].sections,{skipToHeader:!1,createLinks:b.createLinks});"undefined"==typeof b.linkCounter&&(b.linkCounter=0);var f=!!b.createLinks,g=[];g.push("<ol>");for(var h=0;h<a.length;h++){var i=a[h];g.push("<li>"),f&&g.push('<a href="#'+d(i,b)+'">'),g.push(c(i)),f&&g.push("</a>"),g.push(e(i.sections,b)),g.push("</li>")}return g.push("</ol>"),g.join("")}var f=a("./utils");b.exports=e},{"./utils":7}],6:[function(a,b){function c(){return j.length?j[j.length-1].node:void 0}function d(a){var b=o.getRankingHeadingElement(a);return b?-parseInt(o.getTagName(b).substr(1)):-1}function e(a){var b=c();if(!o.isHeading(b)&&!o.hasHiddenAttribute(b)){if(o.hasHiddenAttribute(a))return void j.push({node:a});if(o.isSecContent(a))return null!=h&&(i.heading||(i.heading={implied:!0}),j.push(h)),h=new m(a),i=new k(a),void(h.outline=new l(h.node,i));if(o.isSecRoot(a))return null!=h&&j.push(h),h=new m(a),h.parentSection=i,i=new k(a),void(h.outline=new l(h.node,i));if(o.isHeading(a)){if(i.heading)if(h.outline.getLastSection().heading.implied||d(a)>=d(h.outline.getLastSection().heading)){var e=new k(a);h.outline.sections.push(e),i=e,i.heading=a}else{var f=!1,g=i;do{if(d(a)<d(g.heading)){var e=new k(a);g.append(e),i=e,i.heading=a,f=!0}var n=g.container;g=n}while(!f)}else i.heading=a;return void j.push({node:a})}}}function f(a){var b=c();if(b===a&&j.pop(),!o.isHeading(b)&&!o.hasHiddenAttribute(b)){if(!(o.isSecContent(a)&&j.length>0))return o.isSecRoot(a)&&j.length>0?(i.heading||(i.heading={implied:!0}),i=h.parentSection,void(h=j.pop())):o.isSecContent(a)||o.isSecRoot(a)?void(i.heading||(i.heading={implied:!0})):void 0;i.heading||(i.heading={implied:!0});var d=h;h=j.pop(),i=h.outline.getLastSection();for(var e=0;e<d.outline.sections.length;e++)i.append(d.outline.sections[e])}}function g(a){if(!o.isSecContent(a)&&!o.isSecRoot(a))throw new TypeError("Invalid argument: start element must either be sectioning root or sectioning content.");return h=null,i=null,j=[],n(a,e,f),h.outline}var h,i,j,k=a("./Section"),l=a("./Outline"),m=a("./OutlineTarget"),n=a("./walk"),o=a("./utils");b.exports=g},{"./Outline":2,"./OutlineTarget":3,"./Section":4,"./utils":7,"./walk":8}],7:[function(a,b,c){function d(a){return a.tagName.toUpperCase()}function e(a){return function(b){return f(b)&&new RegExp(a,"i").test(d(b))}}function f(a){return a&&a.tagName}function g(a){if(!j(a))throw new Error("Not a heading element");var b=d(a);if("HGROUP"!==b)return a;for(var c=1;6>=c;c++){var e=a.getElementsByTagName("H"+c);if(e.length)return e[0]}return null}function h(a){return(""+a).replace(/&/g,"&").replace(/</g,"<")}function i(a){return f(a)&&a.hasAttribute("hidden")}var j=e("^H[1-6]|HGROUP$");c.getTagName=d,c.hasHiddenAttribute=i,c.isSecRoot=e("^(BLOCKQUOTE|BODY|DETAILS|FIELDSET|FIGURE|TD)$"),c.isSecContent=e("^(ARTICLE|ASIDE|NAV|SECTION)$"),c.isHeading=j,c.getRankingHeadingElement=g,c.escapeHtml=h},{}],8:[function(a,b){b.exports=function(a,b,c){var d=a;a:for(;d;)if(b(d),d.firstChild)d=d.firstChild;else for(;d;){if(c(d),d.nextSibling){d=d.nextSibling;continue a}d=d==a?null:d.parentNode}}},{}]},{},[1])(1)});