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 61d69879..3b71c427 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 3b71c427..a36e34f5 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 a36e34f5..725a1d18 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 725a1d18..3219e96e 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 3219e96e..fccc4420 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 zcmex=pQHEAm;@P_1sVSzVUTBFU}R+k0|qEyWMXDv zWn<^yn#iR?;+B1Vl97jh^& zZ9FI%bn%0VaZ*teCzqJGgrtbvx}>nyN9P&a7buactm7Wa!P7i zdPZheaY<=ec|~Pab4zPmdq-#2q{&mJPMbbs=B!1Fmn>bje8tLDn>KIRx^4T8ox2Vl zK63Qf@e?OcUAlbb>b2`PZr*zM=<$=M&z`?{`Re1R&tJZN`~KtSFOa_&8JNMo0ud08 zq4`UYfr*icg@u`got1@!fsv`4fsu(>kcCyzkWI)jkUgg2R zXn!L7kdEss^=JPXlz*LD=CiQ5aQmNKO+PVqzw-QLnY z@$Au)_5lmJzTQ>$@+he-N+GKTFH_E`y*~Wtar9-A0}FHtk8R?2V0->=>+#oOxz(#r zD@x2NzW(=}=YNKM9)Dh6Rpuy__4}3f>6S}I;O>1gwuT-e>B$L`eC2OtMA!lEgfAbyLD4XlIx2Viy8P2 zaA_~ywfzTwbHo{QuN8j|TxslnP}k1q7xgdf&%*9M**#Yu-J4}JuSxXrKlc9&*Vj9L zl-)k{L-%2Z9`T(!^Zyhn{?)pEaDI#Ve2IT*FQZ(X^JXVzOuzm~=7G~vz614pp0vsb zRfnGq>{)dsu_b=13gZh|Rt-j7(EVBO7qeSE&i*82k6 z+XL$^ByZf*r8#j*^ijE@BZ93DJJlH&&oi4oGOV$?ZpuBGxnl{(6Mnsf3!3cj^$vvm zt^8*Cvex;gsjqtO`kyy`-jNsi&yaoj*O57ux1T);3hSM^XnwqXtcm5zoc|2lmd#wm zFFR@ShLZ=i{J7ibC?= z3H%=uYHokH|IlsvqE~@gavD3k{#h5P_WK3?UY2*vuP%SN{WQn#=06TiylX$XAk6r- z=}rDSXNy+bpZLl1YpYGFWul_?=bGHoPnVZ3pSFJajn6giMqclC)}^Vh75(_(#~u#( zm+@;wZ@x{v^TT!Wh4_>o_m9X6Y?jC&iwRq=v+3wf|Q!le{(_;3??iLv@W^+PQUxdWbs)KA9MO$ zcvj}^svEsKHYnfw>)vhlaW#^^BP-ON$6Yo(>-N#)t`M)Cuixvl>gOLB*-UZ#Y@L@Q zu;9sdr*))Z>g!~=Mu~1y)kc>RX6-ttMQ*<+TzcP_Gtfh{m-!1HSudo>eu&%)935W zTzt%;_go~qo86;b{H)HhXXh=~IkD-q=$_Ize)a$W literal 0 HcmV?d00001 diff --git a/test/test-element-attributes-markdown.html b/test/test-element-attributes-markdown.html new file mode 100644 index 00000000..e547d161 --- /dev/null +++ b/test/test-element-attributes-markdown.html @@ -0,0 +1,84 @@ + + + + + + + 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 00000000..e79806c6 --- /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(); +