From 0bc3a836fc62a25a72ea46758c82b8617d5e93c0 Mon Sep 17 00:00:00 2001 From: VonC Date: Thu, 24 Oct 2013 22:37:55 +0200 Subject: [PATCH 1/6] First implem for adding classes to enclosing elts. Extra text representing classes is detected and correctly removed. Adding attributes isn't working yet. --- plugin/markdown/markdown.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/plugin/markdown/markdown.js b/plugin/markdown/markdown.js index 61d6987..3b71c42 100755 --- a/plugin/markdown/markdown.js +++ b/plugin/markdown/markdown.js @@ -268,6 +268,42 @@ } + /** + * Add classes to the parent element of a text node + * From http://stackoverflow.com/questions/9178174/find-all-text-nodes + */ + function addClasses(element) + { + var mardownClassesInElementsRegex = new RegExp( "{\\\.\s*?([^}]+?)}", 'mg' ); + var mardownClassRegex = new RegExp( "([^\"= ]+?)=\"([^\"=]+?)\"", 'mg' ); + if ( element.childNodes.length > 0 ) { + + for (var i = 0; i < element.childNodes.length; i++) { + addClasses(element.childNodes[i]); + } + } + + if (element.nodeType == Node.TEXT_NODE && /\S/.test(element.nodeValue)) { + + var nodeValue = element.nodeValue; + if ( matches = mardownClassesInElementsRegex.exec( nodeValue ) ) { + + var classes = matches[1]; + console.log("'" + classes + "'"); + nodeValue = nodeValue.substring(0,matches.index) + nodeValue.substring(mardownClassesInElementsRegex.lastIndex) + "ee"; + console.log("'" + nodeValue + "'"); + element.nodeValue = nodeValue; + console.log("'" + element.parentNode.tagName + "'"); + + while( matchesClass = mardownClassRegex.exec( classes ) ) { + console.log("attr='" + matchesClass[1] + "'='" + matchesClass[2] + "'"); + element.parentNode.attributes[matchesClass[1]] = matchesClass[2]; + console.log("=>'" + element.parentNode.attributes[matchesClass[1]] + "'"); + } + } + } + } + /** * Converts any current data-markdown slides in the * DOM to HTML. @@ -289,6 +325,7 @@ var markdown = getMarkdownFromSlide( section ); section.innerHTML = marked( markdown ); + addClasses(section); // If there were notes, we need to re-add them after // having overwritten the section's HTML From d20760f40d377dda29fdc36b5821090ed5c7135b Mon Sep 17 00:00:00 2001 From: VonC Date: Fri, 25 Oct 2013 23:18:10 +0200 Subject: [PATCH 2/6] Uses the right method setAttribute. Works better, and the html elements get their attributes. --- plugin/markdown/markdown.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/markdown/markdown.js b/plugin/markdown/markdown.js index 3b71c42..a36e34f 100755 --- a/plugin/markdown/markdown.js +++ b/plugin/markdown/markdown.js @@ -297,8 +297,8 @@ while( matchesClass = mardownClassRegex.exec( classes ) ) { console.log("attr='" + matchesClass[1] + "'='" + matchesClass[2] + "'"); - element.parentNode.attributes[matchesClass[1]] = matchesClass[2]; - console.log("=>'" + element.parentNode.attributes[matchesClass[1]] + "'"); + element.parentNode.setAttribute(matchesClass[1], matchesClass[2]); + console.log("=>'" + element.parentNode.attributes[matchesClass[1]].nodeValue + "'"); } } } From 28198b2ff0f7429671ed679ecb705e2811bafab5 Mon Sep 17 00:00:00 2001 From: VonC Date: Sat, 26 Oct 2013 21:34:11 +0200 Subject: [PATCH 3/6] Add attributes extracted from an attribute. Allows to add attributes to element of an attribute which contains the attribute pattern. --- plugin/markdown/markdown.js | 68 ++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 23 deletions(-) diff --git a/plugin/markdown/markdown.js b/plugin/markdown/markdown.js index a36e34f..725a1d1 100755 --- a/plugin/markdown/markdown.js +++ b/plugin/markdown/markdown.js @@ -268,39 +268,61 @@ } + /** + * Check if a node value has the attributes pattern. + * If yes, extract it and add that value as one or several attributes + * the the terget element. + * + * You need Cache Killer on Chrome to see the effect on any FOM transformation + * directly on refresh (F5) + * http://stackoverflow.com/questions/5690269/disabling-chrome-cache-for-website-development/7000899#answer-11786277 + */ + function addAttributeInElement(node, elementTarget){ + var mardownClassesInElementsRegex = new RegExp( "{\\\.\s*?([^}]+?)}", 'mg' ); + var mardownClassRegex = new RegExp( "([^\"= ]+?)=\"([^\"=]+?)\"", 'mg' ); + var nodeValue = node.nodeValue; + if ( matches = mardownClassesInElementsRegex.exec( nodeValue ) ) { + + var classes = matches[1]; + console.log("'" + classes + "'"); + nodeValue = nodeValue.substring(0,matches.index) + nodeValue.substring(mardownClassesInElementsRegex.lastIndex) + "ee"; + console.log("'" + nodeValue + "'"); + node.nodeValue = nodeValue; + console.log("'" + elementTarget.tagName + "'"); + + while( matchesClass = mardownClassRegex.exec( classes ) ) { + console.log("attr='" + matchesClass[1] + "'='" + matchesClass[2] + "'"); + elementTarget.setAttribute(matchesClass[1], matchesClass[2]); + console.log("=>'" + elementTarget.attributes[matchesClass[1]].nodeValue + "'"); + } + } + } + /** * Add classes to the parent element of a text node * From http://stackoverflow.com/questions/9178174/find-all-text-nodes */ - function addClasses(element) + function addAttributes(element) { - var mardownClassesInElementsRegex = new RegExp( "{\\\.\s*?([^}]+?)}", 'mg' ); - var mardownClassRegex = new RegExp( "([^\"= ]+?)=\"([^\"=]+?)\"", 'mg' ); if ( element.childNodes.length > 0 ) { for (var i = 0; i < element.childNodes.length; i++) { - addClasses(element.childNodes[i]); + addAttributes(element.childNodes[i]); } } - - if (element.nodeType == Node.TEXT_NODE && /\S/.test(element.nodeValue)) { - - var nodeValue = element.nodeValue; - if ( matches = mardownClassesInElementsRegex.exec( nodeValue ) ) { - - var classes = matches[1]; - console.log("'" + classes + "'"); - nodeValue = nodeValue.substring(0,matches.index) + nodeValue.substring(mardownClassesInElementsRegex.lastIndex) + "ee"; - console.log("'" + nodeValue + "'"); - element.nodeValue = nodeValue; - console.log("'" + element.parentNode.tagName + "'"); - - while( matchesClass = mardownClassRegex.exec( classes ) ) { - console.log("attr='" + matchesClass[1] + "'='" + matchesClass[2] + "'"); - element.parentNode.setAttribute(matchesClass[1], matchesClass[2]); - console.log("=>'" + element.parentNode.attributes[matchesClass[1]].nodeValue + "'"); - } + var nodeValue; + var elementTarget; + if ( element.nodeType == Node.TEXT_NODE && /\S/.test(element.nodeValue) ) { + addAttributeInElement(element, element.parentNode); + } + if ( element.nodeType == Node.ELEMENT_NODE && element.attributes.length > 0 ) { + console.log("Element '" + element.tagName + "' has " + element.attributes.length + " attributes"); + for (iattr=0; iattr Date: Sat, 26 Oct 2013 22:33:43 +0200 Subject: [PATCH 4/6] Cleanup code, remove console log debugs. --- plugin/markdown/markdown.js | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/plugin/markdown/markdown.js b/plugin/markdown/markdown.js index 725a1d1..3219e96 100755 --- a/plugin/markdown/markdown.js +++ b/plugin/markdown/markdown.js @@ -277,52 +277,45 @@ * directly on refresh (F5) * http://stackoverflow.com/questions/5690269/disabling-chrome-cache-for-website-development/7000899#answer-11786277 */ - function addAttributeInElement(node, elementTarget){ + function addAttributeInElement( node, elementTarget ){ var mardownClassesInElementsRegex = new RegExp( "{\\\.\s*?([^}]+?)}", 'mg' ); var mardownClassRegex = new RegExp( "([^\"= ]+?)=\"([^\"=]+?)\"", 'mg' ); var nodeValue = node.nodeValue; if ( matches = mardownClassesInElementsRegex.exec( nodeValue ) ) { var classes = matches[1]; - console.log("'" + classes + "'"); - nodeValue = nodeValue.substring(0,matches.index) + nodeValue.substring(mardownClassesInElementsRegex.lastIndex) + "ee"; - console.log("'" + nodeValue + "'"); + nodeValue = nodeValue.substring( 0, matches.index ) + nodeValue.substring( mardownClassesInElementsRegex.lastIndex ); node.nodeValue = nodeValue; - console.log("'" + elementTarget.tagName + "'"); while( matchesClass = mardownClassRegex.exec( classes ) ) { - console.log("attr='" + matchesClass[1] + "'='" + matchesClass[2] + "'"); elementTarget.setAttribute(matchesClass[1], matchesClass[2]); - console.log("=>'" + elementTarget.attributes[matchesClass[1]].nodeValue + "'"); } } } /** - * Add classes to the parent element of a text node - * From http://stackoverflow.com/questions/9178174/find-all-text-nodes + * Add attributes to the parent element of a text node, + * or the element of an attribute node. */ - function addAttributes(element) + function addAttributes( element ) { if ( element.childNodes.length > 0 ) { - for (var i = 0; i < element.childNodes.length; i++) { - addAttributes(element.childNodes[i]); + for ( var i = 0; i < element.childNodes.length; i++ ) { + addAttributes( element.childNodes[i] ); } } var nodeValue; var elementTarget; + // From http://stackoverflow.com/questions/9178174/find-all-text-nodes if ( element.nodeType == Node.TEXT_NODE && /\S/.test(element.nodeValue) ) { - addAttributeInElement(element, element.parentNode); + addAttributeInElement( element, element.parentNode ); } if ( element.nodeType == Node.ELEMENT_NODE && element.attributes.length > 0 ) { - console.log("Element '" + element.tagName + "' has " + element.attributes.length + " attributes"); - for (iattr=0; iattr Date: Sun, 27 Oct 2013 00:27:44 +0200 Subject: [PATCH 5/6] Add 'data-element-attributes' attr. to 'section'. By default '{\\\.\s*?([^}]+?)}'. --- plugin/markdown/markdown.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/plugin/markdown/markdown.js b/plugin/markdown/markdown.js index 3219e96..fccc442 100755 --- a/plugin/markdown/markdown.js +++ b/plugin/markdown/markdown.js @@ -28,6 +28,7 @@ var DEFAULT_SLIDE_SEPARATOR = '^\n---\n$', DEFAULT_NOTES_SEPARATOR = 'note:'; + DEFAULT_ELEMENT_ATTRIBUTES_SEPARATOR = '{\\\.\s*?([^}]+?)}'; /** @@ -218,7 +219,6 @@ xhr.onreadystatechange = function() { if( xhr.readyState === 4 ) { if ( xhr.status >= 200 && xhr.status < 300 ) { - section.outerHTML = slidify( xhr.responseText, { separator: section.getAttribute( 'data-separator' ), verticalSeparator: section.getAttribute( 'data-vertical' ), @@ -277,8 +277,8 @@ * directly on refresh (F5) * http://stackoverflow.com/questions/5690269/disabling-chrome-cache-for-website-development/7000899#answer-11786277 */ - function addAttributeInElement( node, elementTarget ){ - var mardownClassesInElementsRegex = new RegExp( "{\\\.\s*?([^}]+?)}", 'mg' ); + function addAttributeInElement( node, elementTarget, separator ){ + var mardownClassesInElementsRegex = new RegExp( separator, 'mg' ); var mardownClassRegex = new RegExp( "([^\"= ]+?)=\"([^\"=]+?)\"", 'mg' ); var nodeValue = node.nodeValue; if ( matches = mardownClassesInElementsRegex.exec( nodeValue ) ) { @@ -297,24 +297,24 @@ * Add attributes to the parent element of a text node, * or the element of an attribute node. */ - function addAttributes( element ) + function addAttributes( element, separator ) { if ( element.childNodes.length > 0 ) { for ( var i = 0; i < element.childNodes.length; i++ ) { - addAttributes( element.childNodes[i] ); + addAttributes( element.childNodes[i], separator ); } } var nodeValue; var elementTarget; // From http://stackoverflow.com/questions/9178174/find-all-text-nodes if ( element.nodeType == Node.TEXT_NODE && /\S/.test(element.nodeValue) ) { - addAttributeInElement( element, element.parentNode ); + addAttributeInElement( element, element.parentNode, separator ); } if ( element.nodeType == Node.ELEMENT_NODE && element.attributes.length > 0 ) { for ( iattr=0; iattr Date: Sun, 27 Oct 2013 19:57:12 +0100 Subject: [PATCH 6/6] Add unit-tests for attributes in mardown elements. Test paragraphs, list items and img attributes. --- test/red-curtain-50x50.jpg | Bin 0 -> 1995 bytes test/test-element-attributes-markdown.html | 84 +++++++++++++++++++++ test/test-element-attributes-markdown.js | 38 ++++++++++ 3 files changed, 122 insertions(+) create mode 100644 test/red-curtain-50x50.jpg create mode 100644 test/test-element-attributes-markdown.html create mode 100644 test/test-element-attributes-markdown.js diff --git a/test/red-curtain-50x50.jpg b/test/red-curtain-50x50.jpg new file mode 100644 index 0000000000000000000000000000000000000000..ddfe7a637d747dedc7c7c71a01431e7e76e0eb6f GIT binary patch literal 1995 zcmbW!XH=6}8UWxg`O-)TNgx4q1`|LjA}9z$Q$sOS1%paeihwjh2}KZu6~qE4V1$fV zkY+5$QCSgD9EgD6Vr)nY76?KFLw0KiNqk02m}&~Mx!w5Sao$A7Kg)YXcF-n zBn=#ns7)kkktr04IzdNQo2;uzrjVB|fq|+s2&5VksYb@*@Z|qFpf-Sn0jvNk5M}_t zNidKEgE|1J3W%IClKR$-*=2v*i_>U(Z$6)YjEs;a9T<&ZqQkmkXuujJ-Tk z53?fMyitJCRX(s*xYZOJ>kzumNz-d1NMbMSy>BA?=?v%KLN0Gpk7f6$FK%^Qe9dd= z`QqI9v(JsD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Qi zwUU|^vmXoyc*Ka1Mny}%T5_r^&ecA#JM}yW?Y2qyfPi$><6q*g>2BzuwB*?)SNH@} zZ*-`7_KJ}ZBJ=G#wp+RAt+0fE*S@U&$Up^Mc&^-qPZbQU1l8-DJZRr|6Te5hcuX@W oot#{?sw^S;hm< + + + + + + reveal.js - Test Markdown + + + + + + + +
+
+ + + + + + + + + + + + + diff --git a/test/test-element-attributes-markdown.js b/test/test-element-attributes-markdown.js new file mode 100644 index 0000000..e79806c --- /dev/null +++ b/test/test-element-attributes-markdown.js @@ -0,0 +1,38 @@ + + +Reveal.addEventListener( 'ready', function() { + + QUnit.module( 'Markdown' ); + + test( 'Vertical separator', function() { + strictEqual( document.querySelectorAll( '.reveal .slides>section>section' ).length, 2, 'found two slides' ); + }); + + + test( 'Attributes on vertical slides header', function() { + strictEqual( document.querySelectorAll( '.reveal .slides section>section h2.fragment.fade-out' ).length, 1, 'found one vertical slide with class fragment.fade-out on header' ); + strictEqual( document.querySelectorAll( '.reveal .slides section>section h2.fragment.shrink' ).length, 1, 'found one vertical slide with class fragment.shrink on header' ); + }); + + test( 'Attributes on vertical slides paragraphs', function() { + strictEqual( document.querySelectorAll( '.reveal .slides section>section p.fragment.grow' ).length, 2, 'found a vertical slide with two paragraphs with class fragment.grow' ); + }); + + test( 'Attributes on vertical slides list items', function() { + strictEqual( document.querySelectorAll( '.reveal .slides section>section li.fragment.roll-in' ).length, 3, 'found a vertical slide with three list items with class fragment.roll-in' ); + }); + + test( 'Attributes on horizontal slides paragraphs', function() { + strictEqual( document.querySelectorAll( '.reveal .slides section p.fragment.highlight-red' ).length, 4, 'found a horizontal slide with four paragraphs with class fragment.grow' ); + }); + test( 'Attributes on horizontal slides list items', function() { + strictEqual( document.querySelectorAll( '.reveal .slides section li.fragment.highlight-green' ).length, 5, 'found a horizontal slide with five list items with class fragment.roll-in' ); + }); + test( 'Attributes on horizontal slides list items', function() { + strictEqual( document.querySelectorAll( '.reveal .slides section img.reveal.stretch' ).length, 1, 'found a horizontal slide with stretched image, class img.reveal.stretch' ); + }); + +} ); + +Reveal.initialize(); +