cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
               <div id="css-table">
  <div id="col-1">
   <ul>
    <li><a href="#col-1-tab-1">Example 8</a></li>
   </ul>
   <div id="col-1-tab-1">
    <article>
    <b>nested HTML comments:</b><br>
    <textarea class="input" id="html-nested">
<!-- empty comment -->
<!-- nested comments
  <div>
    <!-- blah blah comment -->
    <p>blah blah</p>
    <!-- blat blat comment -->
    <p>blat blat</p>
  </div>
-->
<!-- empty comment -->
<!-- more nested comments
  <div>
    <!-- interior nesting
      <!-- foo foo comment -->
      <p>foo foo </p>
    -->
    <!-- bar bar comment -->
    <p>bar bar</p>
  </div>
-->
<!-- empty comment -->
</textarea>
    <button id="translate" >translate</button> <button id="revert" >revert</button>
    </article>
   </div>
  </div>
  <div id="col-2">
   <ul>
    <li><a href="#col-2-tab-1">discussion</a></li>
   </ul>
   <div id="col-2-tab-1">
    <article>
    <b>discussion:</b><br>
    <p>
    This example illustrates several unique features of <b>apg-exp</b>.
    <ul>
    <li><b>recursion:</b> <a href="http://codepen.io/apg-exp/pen/RaJbaj">Example 6</a> covers the basics on recursion.
    Here it is used to find nested HTML comments.
    The panel at right shows the two recursive pattern syntaxes used.</li>
    <li><b>global mode:</b> This lets us iterate over all occurrences of nested comments in the sample HTML.</li>
    <li><b>the result.rules object:</b> The result.rules object facilitates locating and working
    with the named sub-phrases within the matched phrase.</li>
    </ul>
    </p>
    <p>
    How it works.
    <ul>
    <li><b>the translate button:</b>
    <ul><li>
    Calls the function <code>translate()</code> which iterates over all HTML comments, whether nested or not.
    </li><li>
    For each comment, calls <code>translateComment()</code> which converts any inner, nested comments
    from <span class="nowrap"><code>&lt;!-- --&gt;</code></span> to <span class="nowrap"><code>&lt;!~~ ~~&gt;</code></span>.
    The conversion is done "in place" in a copy of the input string character code array.
    </li></ul>
    </li> 
    <li><b>the revert button:</b>
    <ul><li>
    Calls the function <code>revert()</code> which iterates over all converted HTML comments, whether nested or not.
    </li><li>
    For each converted comment, calls <code>revertComment()</code> which converts all translated comments
    from <span class="nowrap"><code>&lt;!~~ ~~&gt;</code></span> back to <span class="nowrap"><code>&lt;!-- --&gt;</code></span>.
    The conversion is done "in place" in a copy of the input string character code array.
    </li></ul>
    </li>
    </ul>
    </p>
    </article>
   </div>
  </div>
  <div id="col-3">
   <ul>
    <li><a href="#col-3-tab-1">patterns</a></li>
   </ul>
   <div id="col-3-tab-1">
    <article>
    <b>nested pattern syntax:</b><br>
    <textarea class="input" id="nested-pattern" disabled>
</textarea>
    <p>
    <b>revert pattern syntax:</b><br>
    <textarea class="input" id="revert-pattern" disabled>
</textarea>
    </p>
    </article>
   </div>
  </div>
 </div>

            
          
!
            
              /* Pen-specific styles */
* {
  box-sizing: border-box;
}

body {
  color: #000;
  font-size: .8rem;
  line-height: 150%;
}

h1 {
  font-size: 1.75rem;
  margin: 0 0 0.75rem 0;
  font-family: 'Open Sans', sans-serif;
  font-weight: 700;
}

h2 {
  font-family: 'Open Sans', sans-serif;
  font-weight: 700;
}

article {
  font-family: 'Open Sans', sans-serif;
  font-weight: 400;
}

b {
  font-family: 'Open Sans', sans-serif;
  font-weight: 700;
}

i {
  font-family: 'Open Sans', sans-serif;
  font-style: italic;
}

textarea {
  width: 100%;
}

button {
  font-size: 1em;
  cursor: pointer;
  cursor: hand;
  margin: 0px 0px 2px 0px;
  width: 6em;
}

p.title {
  margin: 0px 0px 3px 0px;
}

a.link {
  color: #4284B0;
}

a.link:visited {
  color: #9c4be7;
}

/* Pattern styles */
#css-table {
	display: table;
	width: 100%;
}

#col-1 {
	display: table-cell;
	width: 30%;
	border: 1px solid #4284B0;
}

#col-2 {
	display: table-cell;
	width: 40%;
	border-width: 1px 0px;
	border-style: solid;
	border-color: #4284B0;
}

#col-3 {
	display: table-cell;
	width: 30%;
	border: 1px solid #4284B0;
}

textarea.input {
	font-size: .9rem;
	font-family: monospace;
	height: 20rem;
}
textarea#nested-pattern, textarea#revert-pattern {
  height: 8rem;
}

span.nowrap{
	white-space: nowrap;
}

            
          
!
            
              var strings = {};
var exp = {};
var $translate, $revert, $htmlNested;

// convert array of character codes to string
var arrayToString = function(array){
  var string = "";
  for(var i = 0; i < array.length; i += 1){
    string += String.fromCharCode(array[i]);
  }
  return string;
}

// convert a string to an array of character codes
var stringToArray = function(string){
  var array = [];
  for(var i = 0; i < string.length; i += 1){
    array.push(string.charCodeAt(i));
  }
  return array;
}

// replace all but the first <!-- with <!~~ and all but the last --> with ~~> 
var translateComment = function(input, result){
  for(var i = 1; i < result.rules["L"].length; i += 1){
    var beg = result.rules["L"][i].index;
    // replace "-" (45) with "~" (126)
    input[beg+2] = 126;
    input[beg+3] = 126;
  }
  for(var i = 0; i < result.rules["L"].length-1; i += 1){
    var beg = result.rules["R"][i].index;
    // replace "-" (45) with "~" (126)
    input[beg] = 126;
    input[beg+1] = 126;
  }
}

// replace all <!~~ with <!-- and all ~~> with -->
var revertComment = function(input, result){
  for(var i = 0; i < result.rules["L"].length; i += 1){
    var beg = result.rules["L"][i].index;
    // replace "~" (126) with "-" (45) 
    input[beg+2] = 45;
    input[beg+3] = 45;
    beg = result.rules["R"][i].index;
    input[beg] = 45;
    input[beg+1] = 45;
  }
}

// translate interior nested HTML comments to <!~~ ~~>
var translate = function(){
  var str = $htmlNested.val();
  var result = exp.nested.exec(str);
  var input = result ? stringToArray(str) : [];
  while(result){
    translateComment(input, result);
    result = exp.nested.exec(str);
  }
  if(input.length > 0){
    $htmlNested.val(arrayToString(input));
  }
}

// revert <!~~ ~~> to <!-- --> for all levels of nesting
var revert = function(){
  var str = $htmlNested.val();
  var result = exp.revert.exec(str);
  var input = result ? stringToArray(str) : [];
  while(result){
    revertComment(input, result);
    result = exp.revert.exec(str);
  }
  if(input.length > 0){
    $htmlNested.val(arrayToString(input));
  }
}
$(document).ready(function() {
  // nested comment pattern syntax
  strings.nested = '';
  strings.nested += 'P    = L text 1*(P text) R / L text R\n';
  strings.nested += 'L    = "<!--"\n';
  strings.nested += 'R    = "-->"\n';
  strings.nested += 'text = *(!L !R any)\n';
  strings.nested += 'any  = %d32-126 / %d9-10 / %d13\n';
  
  // nested, converted comment pattern syntax
  strings.revert = '';
  strings.revert += 'P    = L text 1*(P text) R / L text R\n';
  strings.revert += 'L    = "<!~~"\n';
  strings.revert += 'R    = "~~>"\n';
  strings.revert += 'text = *(!L !R any)\n';
  strings.revert += 'any  = %d32-126 / %d9-10 / %d13\n';

  // global mode for iterating over all occurrences
  exp.nested = new ApgExp(strings.nested, "g");
  exp.revert = new ApgExp(strings.revert, "g");
  
  // the jQuery objects
  $translate = $("#translate");
  $translate.click(translate);
  $revert = $("#revert")
  $revert.click(revert);
  $htmlNested = $("#html-nested")

  // page setup
  $("#nested-pattern").val(strings.nested);
  $("#revert-pattern").val(strings.revert);
  $("#col-1").tabs();
  $("#col-2").tabs();
  $("#col-3").tabs();
});

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console