From 3d821342c5c53919db537b41752a737d9395ebb7 Mon Sep 17 00:00:00 2001 From: Charlie Hubbard Date: Sun, 5 Jan 2014 15:37:45 -0500 Subject: [PATCH 01/28] Added a Compile class to run from the command line. --- src/main/java/org/lesscss/Compile.java | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/main/java/org/lesscss/Compile.java diff --git a/src/main/java/org/lesscss/Compile.java b/src/main/java/org/lesscss/Compile.java new file mode 100644 index 0000000..8bd3d35 --- /dev/null +++ b/src/main/java/org/lesscss/Compile.java @@ -0,0 +1,27 @@ +package org.lesscss; + +import org.lesscss.logging.LessLogger; +import org.lesscss.logging.LessLoggerFactory; + +import java.io.File; + +public class Compile { + + private static final LessLogger logger = LessLoggerFactory.getLogger( Compile.class ); + + public static void main(String[] args) throws Exception { + if( args.length < 1 ) { + logger.info("usage: org.lesscss.Compile "); + System.exit(-1); + } + + File output = new File( args[0] + ".css" ); + logger.info("Compiler output = %s", output.getCanonicalPath() ); + + long start = System.currentTimeMillis(); + LessCompiler lessCompiler = new LessCompiler(); + lessCompiler.compile( new File( args[0] ), output ); + long duration = System.currentTimeMillis() - start; + logger.info("Done. %,d ms", duration); + } +} \ No newline at end of file From 38d9754b3d8201b8d2a6799f11cc483b6931fabc Mon Sep 17 00:00:00 2001 From: Charlie Hubbard Date: Sun, 5 Jan 2014 15:39:42 -0500 Subject: [PATCH 02/28] Added HttpResource for pulling in resources using http. --- src/main/java/org/lesscss/HttpResource.java | 58 +++++++++++++++++++++ src/main/java/org/lesscss/LessSource.java | 15 +++++- src/main/java/org/lesscss/Resource.java | 2 +- 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/lesscss/HttpResource.java diff --git a/src/main/java/org/lesscss/HttpResource.java b/src/main/java/org/lesscss/HttpResource.java new file mode 100644 index 0000000..7c3f724 --- /dev/null +++ b/src/main/java/org/lesscss/HttpResource.java @@ -0,0 +1,58 @@ +package org.lesscss; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.net.URLConnection; + +public class HttpResource implements Resource { + + URI url; + + public HttpResource(String url) throws URISyntaxException { + this.url = new URI( url ); + } + + public HttpResource(URI url) { + this.url = url; + } + + public boolean exists() { + try { + URL u = url.toURL(); + URLConnection connection = u.openConnection(); + connection.connect(); + return true; + } catch (IOException e) { + return false; + } + } + + public long lastModified() { + try { + URL u = url.toURL(); + URLConnection connection = u.openConnection(); + return connection.getLastModified(); + } catch( IOException e ) { + return 0; + } + } + + public InputStream getInputStream() throws IOException { + return url.toURL().openStream(); + } + + public Resource createRelative(String relativeResourcePath) throws IOException { + try { + return new HttpResource(url.resolve(new URI(relativeResourcePath))); + } catch (URISyntaxException e) { + throw new IOException( "Could not resolve " + url + " against " + relativeResourcePath, e ); + } + } + + public String getName() { + return url.toASCIIString(); + } +} diff --git a/src/main/java/org/lesscss/LessSource.java b/src/main/java/org/lesscss/LessSource.java index 0b99aa7..a9bed8e 100644 --- a/src/main/java/org/lesscss/LessSource.java +++ b/src/main/java/org/lesscss/LessSource.java @@ -17,6 +17,7 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.net.URISyntaxException; import java.nio.charset.Charset; import java.util.LinkedHashMap; import java.util.Map; @@ -198,7 +199,7 @@ private void resolveImports() throws IOException { logger.debug("Importing %s", importedResource); if( !imports.containsKey(importedResource) ) { - LessSource importedLessSource = new LessSource(resource.createRelative(importedResource)); + LessSource importedLessSource = new LessSource(getImportedResource(importedResource)); imports.put(importedResource, importedLessSource); normalizedContent = includeImportedContent(importedLessSource, importMatcher); @@ -211,6 +212,18 @@ private void resolveImports() throws IOException { } } + private Resource getImportedResource(String importedResource) throws IOException { + try { + if( importedResource.startsWith("http:") ) { + return new HttpResource(importedResource); + } else { + return resource.createRelative(importedResource); + } + } catch (URISyntaxException e) { + throw new IOException( importedResource, e ); + } + } + private String includeImportedContent(LessSource importedLessSource, Matcher importMatcher) { StringBuilder builder = new StringBuilder(); builder.append(normalizedContent.substring(0, importMatcher.start(1))); diff --git a/src/main/java/org/lesscss/Resource.java b/src/main/java/org/lesscss/Resource.java index 7d6ca51..1e40082 100644 --- a/src/main/java/org/lesscss/Resource.java +++ b/src/main/java/org/lesscss/Resource.java @@ -41,7 +41,7 @@ public interface Resource { * @param relativeResourcePath String relative resource path * @return Resource relative resource */ - Resource createRelative(String relativeResourcePath); + Resource createRelative(String relativeResourcePath) throws IOException; /** * Returns a unique name for this resource. (ie file name for files) From d34d75f13d43bad7753c8fee1660015742bdc708 Mon Sep 17 00:00:00 2001 From: Charlie Hubbard Date: Sun, 12 Jan 2014 21:43:05 -0500 Subject: [PATCH 03/28] Added HttpResource for pulling in resources using http/https. --- src/main/java/org/lesscss/LessSource.java | 2 +- src/test/resources/import/less/http_import.less | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/import/less/http_import.less diff --git a/src/main/java/org/lesscss/LessSource.java b/src/main/java/org/lesscss/LessSource.java index a9bed8e..399a3ba 100644 --- a/src/main/java/org/lesscss/LessSource.java +++ b/src/main/java/org/lesscss/LessSource.java @@ -214,7 +214,7 @@ private void resolveImports() throws IOException { private Resource getImportedResource(String importedResource) throws IOException { try { - if( importedResource.startsWith("http:") ) { + if( importedResource.startsWith("http:") || importedResource.startsWith("https:") ) { return new HttpResource(importedResource); } else { return resource.createRelative(importedResource); diff --git a/src/test/resources/import/less/http_import.less b/src/test/resources/import/less/http_import.less new file mode 100644 index 0000000..e846e4b --- /dev/null +++ b/src/test/resources/import/less/http_import.less @@ -0,0 +1,6 @@ +//@import "http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css"; +@import "https://raw2.github.com/FortAwesome/Font-Awesome/master/less/font-awesome.less"; + +body { + background-color: #eeeeee; +} \ No newline at end of file From b9510c5b526f51dfdef80a8e75ed7fea2817077b Mon Sep 17 00:00:00 2001 From: Charlie Hubbard Date: Fri, 17 Jan 2014 15:37:12 -0500 Subject: [PATCH 04/28] Make Java 1.5 compatible. Fixed pom issues. --- pom.xml | 4 ++-- src/main/java/org/lesscss/HttpResource.java | 2 +- src/main/java/org/lesscss/LessSource.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 947be7f..2616ef9 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.lesscss lesscss - 1.4.1-SNAPSHOT + 1.5.1-SNAPSHOT jar Official LESS CSS Compiler for Java Official LESS CSS Compiler for Java @@ -76,7 +76,7 @@ org.slf4j slf4j-simple 1.7.2 - testRuntime + runtime diff --git a/src/main/java/org/lesscss/HttpResource.java b/src/main/java/org/lesscss/HttpResource.java index 7c3f724..042a23f 100644 --- a/src/main/java/org/lesscss/HttpResource.java +++ b/src/main/java/org/lesscss/HttpResource.java @@ -48,7 +48,7 @@ public Resource createRelative(String relativeResourcePath) throws IOException { try { return new HttpResource(url.resolve(new URI(relativeResourcePath))); } catch (URISyntaxException e) { - throw new IOException( "Could not resolve " + url + " against " + relativeResourcePath, e ); + throw (IOException)new IOException( "Could not resolve " + url + " against " + relativeResourcePath ).initCause(e); } } diff --git a/src/main/java/org/lesscss/LessSource.java b/src/main/java/org/lesscss/LessSource.java index 399a3ba..30b6f98 100644 --- a/src/main/java/org/lesscss/LessSource.java +++ b/src/main/java/org/lesscss/LessSource.java @@ -220,7 +220,7 @@ private Resource getImportedResource(String importedResource) throws IOException return resource.createRelative(importedResource); } } catch (URISyntaxException e) { - throw new IOException( importedResource, e ); + throw (IOException)new IOException( importedResource ).initCause(e); } } From d12b9cd98a0734d0b5b5fbf8db591eabc8930310 Mon Sep 17 00:00:00 2001 From: Charlie Hubbard Date: Sat, 18 Jan 2014 13:34:47 -0500 Subject: [PATCH 05/28] Added unit test for testing http imports. --- src/test/java/integration/ImportIT.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/java/integration/ImportIT.java b/src/test/java/integration/ImportIT.java index 0e27e27..9e6e885 100644 --- a/src/test/java/integration/ImportIT.java +++ b/src/test/java/integration/ImportIT.java @@ -32,4 +32,9 @@ public void testImportEndsInLess() throws Exception { public void testImportSingleQuotes() throws Exception { testCompile(toFile("import/less/import_quotes.less"), toFile("import/css/import.css")); } + + @Test + public void testHttpImport() throws Exception { + testCompile(toFile("import/less/http_import.less"), toFile("import/css/http_import.css")); + } } From efe41866e3007702d0f91fe960d5728ef142974c Mon Sep 17 00:00:00 2001 From: Charlie Hubbard Date: Sat, 18 Jan 2014 13:35:16 -0500 Subject: [PATCH 06/28] Added unit test for testing http imports. --- src/test/resources/import/css/http_import.css | 1341 +++++++++++++++++ 1 file changed, 1341 insertions(+) create mode 100644 src/test/resources/import/css/http_import.css diff --git a/src/test/resources/import/css/http_import.css b/src/test/resources/import/css/http_import.css new file mode 100644 index 0000000..3bbdf33 --- /dev/null +++ b/src/test/resources/import/css/http_import.css @@ -0,0 +1,1341 @@ +/*! + * Font Awesome 4.0.3 by @davegandy - http://fontawesome.io - @fontawesome + * License - http://fontawesome.io/license (Font: SIL OFL 1.1, CSS: MIT License) + */ +/* FONT PATH + * -------------------------- */ +@font-face { + font-family: 'FontAwesome'; + src: url('../fonts/fontawesome-webfont.eot?v=4.0.3'); + src: url('../fonts/fontawesome-webfont.eot?#iefix&v=4.0.3') format('embedded-opentype'), url('../fonts/fontawesome-webfont.woff?v=4.0.3') format('woff'), url('../fonts/fontawesome-webfont.ttf?v=4.0.3') format('truetype'), url('../fonts/fontawesome-webfont.svg?v=4.0.3#fontawesomeregular') format('svg'); + font-weight: normal; + font-style: normal; +} +.fa { + display: inline-block; + font-family: FontAwesome; + font-style: normal; + font-weight: normal; + line-height: 1; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} +/* makes the font 33% larger relative to the icon container */ +.fa-lg { + font-size: 1.3333333333333333em; + line-height: 0.75em; + vertical-align: -15%; +} +.fa-2x { + font-size: 2em; +} +.fa-3x { + font-size: 3em; +} +.fa-4x { + font-size: 4em; +} +.fa-5x { + font-size: 5em; +} +.fa-fw { + width: 1.2857142857142858em; + text-align: center; +} +.fa-ul { + padding-left: 0; + margin-left: 2.142857142857143em; + list-style-type: none; +} +.fa-ul > li { + position: relative; +} +.fa-li { + position: absolute; + left: -2.142857142857143em; + width: 2.142857142857143em; + top: 0.14285714285714285em; + text-align: center; +} +.fa-li.fa-lg { + left: -1.8571428571428572em; +} +.fa-border { + padding: .2em .25em .15em; + border: solid 0.08em #eeeeee; + border-radius: .1em; +} +.pull-right { + float: right; +} +.pull-left { + float: left; +} +.fa.pull-left { + margin-right: .3em; +} +.fa.pull-right { + margin-left: .3em; +} +.fa-spin { + -webkit-animation: spin 2s infinite linear; + -moz-animation: spin 2s infinite linear; + -o-animation: spin 2s infinite linear; + animation: spin 2s infinite linear; +} +@-moz-keyframes spin { + 0% { + -moz-transform: rotate(0deg); + } + 100% { + -moz-transform: rotate(359deg); + } +} +@-webkit-keyframes spin { + 0% { + -webkit-transform: rotate(0deg); + } + 100% { + -webkit-transform: rotate(359deg); + } +} +@-o-keyframes spin { + 0% { + -o-transform: rotate(0deg); + } + 100% { + -o-transform: rotate(359deg); + } +} +@-ms-keyframes spin { + 0% { + -ms-transform: rotate(0deg); + } + 100% { + -ms-transform: rotate(359deg); + } +} +@keyframes spin { + 0% { + transform: rotate(0deg); + } + 100% { + transform: rotate(359deg); + } +} +.fa-rotate-90 { + filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=1); + -webkit-transform: rotate(90deg); + -moz-transform: rotate(90deg); + -ms-transform: rotate(90deg); + -o-transform: rotate(90deg); + transform: rotate(90deg); +} +.fa-rotate-180 { + filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=2); + -webkit-transform: rotate(180deg); + -moz-transform: rotate(180deg); + -ms-transform: rotate(180deg); + -o-transform: rotate(180deg); + transform: rotate(180deg); +} +.fa-rotate-270 { + filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3); + -webkit-transform: rotate(270deg); + -moz-transform: rotate(270deg); + -ms-transform: rotate(270deg); + -o-transform: rotate(270deg); + transform: rotate(270deg); +} +.fa-flip-horizontal { + filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=0, mirror=1); + -webkit-transform: scale(-1, 1); + -moz-transform: scale(-1, 1); + -ms-transform: scale(-1, 1); + -o-transform: scale(-1, 1); + transform: scale(-1, 1); +} +.fa-flip-vertical { + filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=2, mirror=1); + -webkit-transform: scale(1, -1); + -moz-transform: scale(1, -1); + -ms-transform: scale(1, -1); + -o-transform: scale(1, -1); + transform: scale(1, -1); +} +.fa-stack { + position: relative; + display: inline-block; + width: 2em; + height: 2em; + line-height: 2em; + vertical-align: middle; +} +.fa-stack-1x, +.fa-stack-2x { + position: absolute; + left: 0; + width: 100%; + text-align: center; +} +.fa-stack-1x { + line-height: inherit; +} +.fa-stack-2x { + font-size: 2em; +} +.fa-inverse { + color: #ffffff; +} +/* Font Awesome uses the Unicode Private Use Area (PUA) to ensure screen + readers do not read off random characters that represent icons */ +.fa-glass:before { + content: "\f000"; +} +.fa-music:before { + content: "\f001"; +} +.fa-search:before { + content: "\f002"; +} +.fa-envelope-o:before { + content: "\f003"; +} +.fa-heart:before { + content: "\f004"; +} +.fa-star:before { + content: "\f005"; +} +.fa-star-o:before { + content: "\f006"; +} +.fa-user:before { + content: "\f007"; +} +.fa-film:before { + content: "\f008"; +} +.fa-th-large:before { + content: "\f009"; +} +.fa-th:before { + content: "\f00a"; +} +.fa-th-list:before { + content: "\f00b"; +} +.fa-check:before { + content: "\f00c"; +} +.fa-times:before { + content: "\f00d"; +} +.fa-search-plus:before { + content: "\f00e"; +} +.fa-search-minus:before { + content: "\f010"; +} +.fa-power-off:before { + content: "\f011"; +} +.fa-signal:before { + content: "\f012"; +} +.fa-gear:before, +.fa-cog:before { + content: "\f013"; +} +.fa-trash-o:before { + content: "\f014"; +} +.fa-home:before { + content: "\f015"; +} +.fa-file-o:before { + content: "\f016"; +} +.fa-clock-o:before { + content: "\f017"; +} +.fa-road:before { + content: "\f018"; +} +.fa-download:before { + content: "\f019"; +} +.fa-arrow-circle-o-down:before { + content: "\f01a"; +} +.fa-arrow-circle-o-up:before { + content: "\f01b"; +} +.fa-inbox:before { + content: "\f01c"; +} +.fa-play-circle-o:before { + content: "\f01d"; +} +.fa-rotate-right:before, +.fa-repeat:before { + content: "\f01e"; +} +.fa-refresh:before { + content: "\f021"; +} +.fa-list-alt:before { + content: "\f022"; +} +.fa-lock:before { + content: "\f023"; +} +.fa-flag:before { + content: "\f024"; +} +.fa-headphones:before { + content: "\f025"; +} +.fa-volume-off:before { + content: "\f026"; +} +.fa-volume-down:before { + content: "\f027"; +} +.fa-volume-up:before { + content: "\f028"; +} +.fa-qrcode:before { + content: "\f029"; +} +.fa-barcode:before { + content: "\f02a"; +} +.fa-tag:before { + content: "\f02b"; +} +.fa-tags:before { + content: "\f02c"; +} +.fa-book:before { + content: "\f02d"; +} +.fa-bookmark:before { + content: "\f02e"; +} +.fa-print:before { + content: "\f02f"; +} +.fa-camera:before { + content: "\f030"; +} +.fa-font:before { + content: "\f031"; +} +.fa-bold:before { + content: "\f032"; +} +.fa-italic:before { + content: "\f033"; +} +.fa-text-height:before { + content: "\f034"; +} +.fa-text-width:before { + content: "\f035"; +} +.fa-align-left:before { + content: "\f036"; +} +.fa-align-center:before { + content: "\f037"; +} +.fa-align-right:before { + content: "\f038"; +} +.fa-align-justify:before { + content: "\f039"; +} +.fa-list:before { + content: "\f03a"; +} +.fa-dedent:before, +.fa-outdent:before { + content: "\f03b"; +} +.fa-indent:before { + content: "\f03c"; +} +.fa-video-camera:before { + content: "\f03d"; +} +.fa-picture-o:before { + content: "\f03e"; +} +.fa-pencil:before { + content: "\f040"; +} +.fa-map-marker:before { + content: "\f041"; +} +.fa-adjust:before { + content: "\f042"; +} +.fa-tint:before { + content: "\f043"; +} +.fa-edit:before, +.fa-pencil-square-o:before { + content: "\f044"; +} +.fa-share-square-o:before { + content: "\f045"; +} +.fa-check-square-o:before { + content: "\f046"; +} +.fa-arrows:before { + content: "\f047"; +} +.fa-step-backward:before { + content: "\f048"; +} +.fa-fast-backward:before { + content: "\f049"; +} +.fa-backward:before { + content: "\f04a"; +} +.fa-play:before { + content: "\f04b"; +} +.fa-pause:before { + content: "\f04c"; +} +.fa-stop:before { + content: "\f04d"; +} +.fa-forward:before { + content: "\f04e"; +} +.fa-fast-forward:before { + content: "\f050"; +} +.fa-step-forward:before { + content: "\f051"; +} +.fa-eject:before { + content: "\f052"; +} +.fa-chevron-left:before { + content: "\f053"; +} +.fa-chevron-right:before { + content: "\f054"; +} +.fa-plus-circle:before { + content: "\f055"; +} +.fa-minus-circle:before { + content: "\f056"; +} +.fa-times-circle:before { + content: "\f057"; +} +.fa-check-circle:before { + content: "\f058"; +} +.fa-question-circle:before { + content: "\f059"; +} +.fa-info-circle:before { + content: "\f05a"; +} +.fa-crosshairs:before { + content: "\f05b"; +} +.fa-times-circle-o:before { + content: "\f05c"; +} +.fa-check-circle-o:before { + content: "\f05d"; +} +.fa-ban:before { + content: "\f05e"; +} +.fa-arrow-left:before { + content: "\f060"; +} +.fa-arrow-right:before { + content: "\f061"; +} +.fa-arrow-up:before { + content: "\f062"; +} +.fa-arrow-down:before { + content: "\f063"; +} +.fa-mail-forward:before, +.fa-share:before { + content: "\f064"; +} +.fa-expand:before { + content: "\f065"; +} +.fa-compress:before { + content: "\f066"; +} +.fa-plus:before { + content: "\f067"; +} +.fa-minus:before { + content: "\f068"; +} +.fa-asterisk:before { + content: "\f069"; +} +.fa-exclamation-circle:before { + content: "\f06a"; +} +.fa-gift:before { + content: "\f06b"; +} +.fa-leaf:before { + content: "\f06c"; +} +.fa-fire:before { + content: "\f06d"; +} +.fa-eye:before { + content: "\f06e"; +} +.fa-eye-slash:before { + content: "\f070"; +} +.fa-warning:before, +.fa-exclamation-triangle:before { + content: "\f071"; +} +.fa-plane:before { + content: "\f072"; +} +.fa-calendar:before { + content: "\f073"; +} +.fa-random:before { + content: "\f074"; +} +.fa-comment:before { + content: "\f075"; +} +.fa-magnet:before { + content: "\f076"; +} +.fa-chevron-up:before { + content: "\f077"; +} +.fa-chevron-down:before { + content: "\f078"; +} +.fa-retweet:before { + content: "\f079"; +} +.fa-shopping-cart:before { + content: "\f07a"; +} +.fa-folder:before { + content: "\f07b"; +} +.fa-folder-open:before { + content: "\f07c"; +} +.fa-arrows-v:before { + content: "\f07d"; +} +.fa-arrows-h:before { + content: "\f07e"; +} +.fa-bar-chart-o:before { + content: "\f080"; +} +.fa-twitter-square:before { + content: "\f081"; +} +.fa-facebook-square:before { + content: "\f082"; +} +.fa-camera-retro:before { + content: "\f083"; +} +.fa-key:before { + content: "\f084"; +} +.fa-gears:before, +.fa-cogs:before { + content: "\f085"; +} +.fa-comments:before { + content: "\f086"; +} +.fa-thumbs-o-up:before { + content: "\f087"; +} +.fa-thumbs-o-down:before { + content: "\f088"; +} +.fa-star-half:before { + content: "\f089"; +} +.fa-heart-o:before { + content: "\f08a"; +} +.fa-sign-out:before { + content: "\f08b"; +} +.fa-linkedin-square:before { + content: "\f08c"; +} +.fa-thumb-tack:before { + content: "\f08d"; +} +.fa-external-link:before { + content: "\f08e"; +} +.fa-sign-in:before { + content: "\f090"; +} +.fa-trophy:before { + content: "\f091"; +} +.fa-github-square:before { + content: "\f092"; +} +.fa-upload:before { + content: "\f093"; +} +.fa-lemon-o:before { + content: "\f094"; +} +.fa-phone:before { + content: "\f095"; +} +.fa-square-o:before { + content: "\f096"; +} +.fa-bookmark-o:before { + content: "\f097"; +} +.fa-phone-square:before { + content: "\f098"; +} +.fa-twitter:before { + content: "\f099"; +} +.fa-facebook:before { + content: "\f09a"; +} +.fa-github:before { + content: "\f09b"; +} +.fa-unlock:before { + content: "\f09c"; +} +.fa-credit-card:before { + content: "\f09d"; +} +.fa-rss:before { + content: "\f09e"; +} +.fa-hdd-o:before { + content: "\f0a0"; +} +.fa-bullhorn:before { + content: "\f0a1"; +} +.fa-bell:before { + content: "\f0f3"; +} +.fa-certificate:before { + content: "\f0a3"; +} +.fa-hand-o-right:before { + content: "\f0a4"; +} +.fa-hand-o-left:before { + content: "\f0a5"; +} +.fa-hand-o-up:before { + content: "\f0a6"; +} +.fa-hand-o-down:before { + content: "\f0a7"; +} +.fa-arrow-circle-left:before { + content: "\f0a8"; +} +.fa-arrow-circle-right:before { + content: "\f0a9"; +} +.fa-arrow-circle-up:before { + content: "\f0aa"; +} +.fa-arrow-circle-down:before { + content: "\f0ab"; +} +.fa-globe:before { + content: "\f0ac"; +} +.fa-wrench:before { + content: "\f0ad"; +} +.fa-tasks:before { + content: "\f0ae"; +} +.fa-filter:before { + content: "\f0b0"; +} +.fa-briefcase:before { + content: "\f0b1"; +} +.fa-arrows-alt:before { + content: "\f0b2"; +} +.fa-group:before, +.fa-users:before { + content: "\f0c0"; +} +.fa-chain:before, +.fa-link:before { + content: "\f0c1"; +} +.fa-cloud:before { + content: "\f0c2"; +} +.fa-flask:before { + content: "\f0c3"; +} +.fa-cut:before, +.fa-scissors:before { + content: "\f0c4"; +} +.fa-copy:before, +.fa-files-o:before { + content: "\f0c5"; +} +.fa-paperclip:before { + content: "\f0c6"; +} +.fa-save:before, +.fa-floppy-o:before { + content: "\f0c7"; +} +.fa-square:before { + content: "\f0c8"; +} +.fa-bars:before { + content: "\f0c9"; +} +.fa-list-ul:before { + content: "\f0ca"; +} +.fa-list-ol:before { + content: "\f0cb"; +} +.fa-strikethrough:before { + content: "\f0cc"; +} +.fa-underline:before { + content: "\f0cd"; +} +.fa-table:before { + content: "\f0ce"; +} +.fa-magic:before { + content: "\f0d0"; +} +.fa-truck:before { + content: "\f0d1"; +} +.fa-pinterest:before { + content: "\f0d2"; +} +.fa-pinterest-square:before { + content: "\f0d3"; +} +.fa-google-plus-square:before { + content: "\f0d4"; +} +.fa-google-plus:before { + content: "\f0d5"; +} +.fa-money:before { + content: "\f0d6"; +} +.fa-caret-down:before { + content: "\f0d7"; +} +.fa-caret-up:before { + content: "\f0d8"; +} +.fa-caret-left:before { + content: "\f0d9"; +} +.fa-caret-right:before { + content: "\f0da"; +} +.fa-columns:before { + content: "\f0db"; +} +.fa-unsorted:before, +.fa-sort:before { + content: "\f0dc"; +} +.fa-sort-down:before, +.fa-sort-asc:before { + content: "\f0dd"; +} +.fa-sort-up:before, +.fa-sort-desc:before { + content: "\f0de"; +} +.fa-envelope:before { + content: "\f0e0"; +} +.fa-linkedin:before { + content: "\f0e1"; +} +.fa-rotate-left:before, +.fa-undo:before { + content: "\f0e2"; +} +.fa-legal:before, +.fa-gavel:before { + content: "\f0e3"; +} +.fa-dashboard:before, +.fa-tachometer:before { + content: "\f0e4"; +} +.fa-comment-o:before { + content: "\f0e5"; +} +.fa-comments-o:before { + content: "\f0e6"; +} +.fa-flash:before, +.fa-bolt:before { + content: "\f0e7"; +} +.fa-sitemap:before { + content: "\f0e8"; +} +.fa-umbrella:before { + content: "\f0e9"; +} +.fa-paste:before, +.fa-clipboard:before { + content: "\f0ea"; +} +.fa-lightbulb-o:before { + content: "\f0eb"; +} +.fa-exchange:before { + content: "\f0ec"; +} +.fa-cloud-download:before { + content: "\f0ed"; +} +.fa-cloud-upload:before { + content: "\f0ee"; +} +.fa-user-md:before { + content: "\f0f0"; +} +.fa-stethoscope:before { + content: "\f0f1"; +} +.fa-suitcase:before { + content: "\f0f2"; +} +.fa-bell-o:before { + content: "\f0a2"; +} +.fa-coffee:before { + content: "\f0f4"; +} +.fa-cutlery:before { + content: "\f0f5"; +} +.fa-file-text-o:before { + content: "\f0f6"; +} +.fa-building-o:before { + content: "\f0f7"; +} +.fa-hospital-o:before { + content: "\f0f8"; +} +.fa-ambulance:before { + content: "\f0f9"; +} +.fa-medkit:before { + content: "\f0fa"; +} +.fa-fighter-jet:before { + content: "\f0fb"; +} +.fa-beer:before { + content: "\f0fc"; +} +.fa-h-square:before { + content: "\f0fd"; +} +.fa-plus-square:before { + content: "\f0fe"; +} +.fa-angle-double-left:before { + content: "\f100"; +} +.fa-angle-double-right:before { + content: "\f101"; +} +.fa-angle-double-up:before { + content: "\f102"; +} +.fa-angle-double-down:before { + content: "\f103"; +} +.fa-angle-left:before { + content: "\f104"; +} +.fa-angle-right:before { + content: "\f105"; +} +.fa-angle-up:before { + content: "\f106"; +} +.fa-angle-down:before { + content: "\f107"; +} +.fa-desktop:before { + content: "\f108"; +} +.fa-laptop:before { + content: "\f109"; +} +.fa-tablet:before { + content: "\f10a"; +} +.fa-mobile-phone:before, +.fa-mobile:before { + content: "\f10b"; +} +.fa-circle-o:before { + content: "\f10c"; +} +.fa-quote-left:before { + content: "\f10d"; +} +.fa-quote-right:before { + content: "\f10e"; +} +.fa-spinner:before { + content: "\f110"; +} +.fa-circle:before { + content: "\f111"; +} +.fa-mail-reply:before, +.fa-reply:before { + content: "\f112"; +} +.fa-github-alt:before { + content: "\f113"; +} +.fa-folder-o:before { + content: "\f114"; +} +.fa-folder-open-o:before { + content: "\f115"; +} +.fa-smile-o:before { + content: "\f118"; +} +.fa-frown-o:before { + content: "\f119"; +} +.fa-meh-o:before { + content: "\f11a"; +} +.fa-gamepad:before { + content: "\f11b"; +} +.fa-keyboard-o:before { + content: "\f11c"; +} +.fa-flag-o:before { + content: "\f11d"; +} +.fa-flag-checkered:before { + content: "\f11e"; +} +.fa-terminal:before { + content: "\f120"; +} +.fa-code:before { + content: "\f121"; +} +.fa-reply-all:before { + content: "\f122"; +} +.fa-mail-reply-all:before { + content: "\f122"; +} +.fa-star-half-empty:before, +.fa-star-half-full:before, +.fa-star-half-o:before { + content: "\f123"; +} +.fa-location-arrow:before { + content: "\f124"; +} +.fa-crop:before { + content: "\f125"; +} +.fa-code-fork:before { + content: "\f126"; +} +.fa-unlink:before, +.fa-chain-broken:before { + content: "\f127"; +} +.fa-question:before { + content: "\f128"; +} +.fa-info:before { + content: "\f129"; +} +.fa-exclamation:before { + content: "\f12a"; +} +.fa-superscript:before { + content: "\f12b"; +} +.fa-subscript:before { + content: "\f12c"; +} +.fa-eraser:before { + content: "\f12d"; +} +.fa-puzzle-piece:before { + content: "\f12e"; +} +.fa-microphone:before { + content: "\f130"; +} +.fa-microphone-slash:before { + content: "\f131"; +} +.fa-shield:before { + content: "\f132"; +} +.fa-calendar-o:before { + content: "\f133"; +} +.fa-fire-extinguisher:before { + content: "\f134"; +} +.fa-rocket:before { + content: "\f135"; +} +.fa-maxcdn:before { + content: "\f136"; +} +.fa-chevron-circle-left:before { + content: "\f137"; +} +.fa-chevron-circle-right:before { + content: "\f138"; +} +.fa-chevron-circle-up:before { + content: "\f139"; +} +.fa-chevron-circle-down:before { + content: "\f13a"; +} +.fa-html5:before { + content: "\f13b"; +} +.fa-css3:before { + content: "\f13c"; +} +.fa-anchor:before { + content: "\f13d"; +} +.fa-unlock-alt:before { + content: "\f13e"; +} +.fa-bullseye:before { + content: "\f140"; +} +.fa-ellipsis-h:before { + content: "\f141"; +} +.fa-ellipsis-v:before { + content: "\f142"; +} +.fa-rss-square:before { + content: "\f143"; +} +.fa-play-circle:before { + content: "\f144"; +} +.fa-ticket:before { + content: "\f145"; +} +.fa-minus-square:before { + content: "\f146"; +} +.fa-minus-square-o:before { + content: "\f147"; +} +.fa-level-up:before { + content: "\f148"; +} +.fa-level-down:before { + content: "\f149"; +} +.fa-check-square:before { + content: "\f14a"; +} +.fa-pencil-square:before { + content: "\f14b"; +} +.fa-external-link-square:before { + content: "\f14c"; +} +.fa-share-square:before { + content: "\f14d"; +} +.fa-compass:before { + content: "\f14e"; +} +.fa-toggle-down:before, +.fa-caret-square-o-down:before { + content: "\f150"; +} +.fa-toggle-up:before, +.fa-caret-square-o-up:before { + content: "\f151"; +} +.fa-toggle-right:before, +.fa-caret-square-o-right:before { + content: "\f152"; +} +.fa-euro:before, +.fa-eur:before { + content: "\f153"; +} +.fa-gbp:before { + content: "\f154"; +} +.fa-dollar:before, +.fa-usd:before { + content: "\f155"; +} +.fa-rupee:before, +.fa-inr:before { + content: "\f156"; +} +.fa-cny:before, +.fa-rmb:before, +.fa-yen:before, +.fa-jpy:before { + content: "\f157"; +} +.fa-ruble:before, +.fa-rouble:before, +.fa-rub:before { + content: "\f158"; +} +.fa-won:before, +.fa-krw:before { + content: "\f159"; +} +.fa-bitcoin:before, +.fa-btc:before { + content: "\f15a"; +} +.fa-file:before { + content: "\f15b"; +} +.fa-file-text:before { + content: "\f15c"; +} +.fa-sort-alpha-asc:before { + content: "\f15d"; +} +.fa-sort-alpha-desc:before { + content: "\f15e"; +} +.fa-sort-amount-asc:before { + content: "\f160"; +} +.fa-sort-amount-desc:before { + content: "\f161"; +} +.fa-sort-numeric-asc:before { + content: "\f162"; +} +.fa-sort-numeric-desc:before { + content: "\f163"; +} +.fa-thumbs-up:before { + content: "\f164"; +} +.fa-thumbs-down:before { + content: "\f165"; +} +.fa-youtube-square:before { + content: "\f166"; +} +.fa-youtube:before { + content: "\f167"; +} +.fa-xing:before { + content: "\f168"; +} +.fa-xing-square:before { + content: "\f169"; +} +.fa-youtube-play:before { + content: "\f16a"; +} +.fa-dropbox:before { + content: "\f16b"; +} +.fa-stack-overflow:before { + content: "\f16c"; +} +.fa-instagram:before { + content: "\f16d"; +} +.fa-flickr:before { + content: "\f16e"; +} +.fa-adn:before { + content: "\f170"; +} +.fa-bitbucket:before { + content: "\f171"; +} +.fa-bitbucket-square:before { + content: "\f172"; +} +.fa-tumblr:before { + content: "\f173"; +} +.fa-tumblr-square:before { + content: "\f174"; +} +.fa-long-arrow-down:before { + content: "\f175"; +} +.fa-long-arrow-up:before { + content: "\f176"; +} +.fa-long-arrow-left:before { + content: "\f177"; +} +.fa-long-arrow-right:before { + content: "\f178"; +} +.fa-apple:before { + content: "\f179"; +} +.fa-windows:before { + content: "\f17a"; +} +.fa-android:before { + content: "\f17b"; +} +.fa-linux:before { + content: "\f17c"; +} +.fa-dribbble:before { + content: "\f17d"; +} +.fa-skype:before { + content: "\f17e"; +} +.fa-foursquare:before { + content: "\f180"; +} +.fa-trello:before { + content: "\f181"; +} +.fa-female:before { + content: "\f182"; +} +.fa-male:before { + content: "\f183"; +} +.fa-gittip:before { + content: "\f184"; +} +.fa-sun-o:before { + content: "\f185"; +} +.fa-moon-o:before { + content: "\f186"; +} +.fa-archive:before { + content: "\f187"; +} +.fa-bug:before { + content: "\f188"; +} +.fa-vk:before { + content: "\f189"; +} +.fa-weibo:before { + content: "\f18a"; +} +.fa-renren:before { + content: "\f18b"; +} +.fa-pagelines:before { + content: "\f18c"; +} +.fa-stack-exchange:before { + content: "\f18d"; +} +.fa-arrow-circle-o-right:before { + content: "\f18e"; +} +.fa-arrow-circle-o-left:before { + content: "\f190"; +} +.fa-toggle-left:before, +.fa-caret-square-o-left:before { + content: "\f191"; +} +.fa-dot-circle-o:before { + content: "\f192"; +} +.fa-wheelchair:before { + content: "\f193"; +} +.fa-vimeo-square:before { + content: "\f194"; +} +.fa-turkish-lira:before, +.fa-try:before { + content: "\f195"; +} +.fa-plus-square-o:before { + content: "\f196"; +} +body { + background-color: #eeeeee; +} From 776d714385e5afd980a103d8dd96da9841c5c1c4 Mon Sep 17 00:00:00 2001 From: Doug Haber Date: Sat, 11 Jan 2014 22:23:01 -0500 Subject: [PATCH 07/28] Initial pass at a Java Less Compiler that uses the rhino version from the less-rhino fork. Based off of lesscss-java but tries to be as slim as possible and remove most code that isn't just passing parameters to the less rhino code. --- src/main/java/org/lesscss/Compile.java | 5 +- src/main/java/org/lesscss/LessCompiler.java | 158 +- .../resources/META-INF/less-rhino-1.5.1.js | 6831 +++++++++++++++++ .../java/org/lesscss/LessCompilerTest.java | 119 +- 4 files changed, 7016 insertions(+), 97 deletions(-) create mode 100644 src/main/resources/META-INF/less-rhino-1.5.1.js diff --git a/src/main/java/org/lesscss/Compile.java b/src/main/java/org/lesscss/Compile.java index 8bd3d35..a3f40b7 100644 --- a/src/main/java/org/lesscss/Compile.java +++ b/src/main/java/org/lesscss/Compile.java @@ -4,6 +4,7 @@ import org.lesscss.logging.LessLoggerFactory; import java.io.File; +import java.util.Arrays; public class Compile { @@ -19,8 +20,10 @@ public static void main(String[] args) throws Exception { logger.info("Compiler output = %s", output.getCanonicalPath() ); long start = System.currentTimeMillis(); - LessCompiler lessCompiler = new LessCompiler(); + LessCompiler lessCompiler = new LessCompiler(Arrays.asList("-ru")); + lessCompiler.compile( new File( args[0] ), output ); + long duration = System.currentTimeMillis() - start; logger.info("Done. %,d ms", duration); } diff --git a/src/main/java/org/lesscss/LessCompiler.java b/src/main/java/org/lesscss/LessCompiler.java index de8d59c..3edca27 100644 --- a/src/main/java/org/lesscss/LessCompiler.java +++ b/src/main/java/org/lesscss/LessCompiler.java @@ -14,19 +14,27 @@ */ package org.lesscss; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.PrintStream; +import java.io.PrintWriter; import java.net.URL; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; + import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.StringUtils; import org.lesscss.logging.LessLogger; import org.lesscss.logging.LessLoggerFactory; import org.mozilla.javascript.Context; import org.mozilla.javascript.Function; import org.mozilla.javascript.JavaScriptException; +import org.mozilla.javascript.Script; import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.ScriptableObject; import org.mozilla.javascript.tools.shell.Global; @@ -60,14 +68,15 @@ public class LessCompiler { private static final LessLogger logger = LessLoggerFactory.getLogger(LessCompiler.class); - private URL envJs = LessCompiler.class.getClassLoader().getResource("META-INF/env.rhino.js"); - private URL lessJs = LessCompiler.class.getClassLoader().getResource("META-INF/less-1.5.1.js"); - private URL lesscJs = LessCompiler.class.getClassLoader().getResource("META-INF/lessc.js"); + private URL lessJs = LessCompiler.class.getClassLoader().getResource("META-INF/less-rhino-1.5.1.js"); private List customJs = Collections.emptyList(); - private boolean compress = false; + private List options = Collections.emptyList(); + private Boolean compress = null; private String encoding = null; private Scriptable scope; + private ByteArrayOutputStream out; + private Function compiler; /** * Constructs a new LessCompiler. @@ -76,12 +85,31 @@ public LessCompiler() { } /** + * Constructs a new LessCompiler. + */ + public LessCompiler(List options) { + this.options = new ArrayList(options); + } + + public List getOptions() { + return Collections.unmodifiableList(options); + } + + public void setOptions(List options) { + if (scope != null) { + throw new IllegalStateException("This method can only be called before init()"); + } + + this.options = new ArrayList(options); + } + + /** * Returns the Envjs JavaScript file used by the compiler. * * @return The Envjs JavaScript file used by the compiler. */ public URL getEnvJs() { - return envJs; + throw new IllegalArgumentException("EnvJs is no longer supported. You don't need this if you use a less-rhino-.js build like the default."); } /** @@ -91,10 +119,7 @@ public URL getEnvJs() { * @param envJs The Envjs JavaScript file used by the compiler. */ public synchronized void setEnvJs(URL envJs) { - if (scope != null) { - throw new IllegalStateException("This method can only be called before init()"); - } - this.envJs = envJs; + throw new IllegalArgumentException("EnvJs is no longer supported. You don't need this if you use a less-rhino-.js build like the default."); } /** @@ -161,7 +186,9 @@ public synchronized void setCustomJs(List customJs) { * @return Whether the compiler will compress the CSS. */ public boolean isCompress() { - return compress; + return (compress != null && compress.booleanValue()) || + options.contains("compress") || + options.contains("x"); } /** @@ -211,21 +238,24 @@ public synchronized void init() { try { Context cx = Context.enter(); - cx.setOptimizationLevel(-1); + //cx.setOptimizationLevel(-1); cx.setLanguageVersion(Context.VERSION_1_7); Global global = new Global(); - global.init(cx); - + global.init(cx); scope = cx.initStandardObjects(global); scope.put("logger", scope, Context.toObject(logger, scope)); - + + out = new ByteArrayOutputStream(); + global.setOut(new PrintStream(out)); + + // Load the compiler into a function we can run + compiler = (Function) cx.compileReader(new InputStreamReader(lessJs.openConnection().getInputStream()), lessJs.toString(), 1, null); + List jsUrls = new ArrayList(); - jsUrls.add(envJs); - jsUrls.add(lessJs); - jsUrls.add(lesscJs); jsUrls.addAll( customJs ); + // load any custom JS for(URL url : jsUrls) { InputStreamReader inputStreamReader = new InputStreamReader(url.openConnection().getInputStream()); try{ @@ -234,6 +264,7 @@ public synchronized void init() { inputStreamReader.close(); } } + } catch (Exception e) { String message = "Failed to initialize LESS compiler."; @@ -255,9 +286,9 @@ public synchronized void init() { * @return The CSS. */ public String compile(String input) throws LessException { - return compile( input, ""); + return compile(input, ""); } - + /** * Compiles the LESS input String to CSS, but specifies the source name String. * @@ -268,23 +299,73 @@ public String compile(String input) throws LessException { * @throws LessException any error encountered by the compiler */ public String compile(String input, String name) throws LessException { - synchronized(this){ - if (scope == null) { - init(); - } + File tempFile = null; + try { + tempFile = File.createTempFile("tmp", "less.tmp"); + PrintWriter writer = new PrintWriter(tempFile); + writer.print(input); + writer.close(); + + return compile( tempFile, ""); + } catch (IOException e) { + throw new LessException(e); + + } finally { + tempFile.delete(); } + } + + /** + * Compiles the LESS input String to CSS, but specifies the source name String. The entire + * method is synchronized so that two threads don't read the output at the same time. + * + * @param input The LESS input String to compile + * @param name The source's name String to provide better error messages. + * @return the CSS. + * + * @throws LessException any error encountered by the compiler + */ + public synchronized String compile(File input, String name) throws LessException { + if (scope == null) { + init(); + } long start = System.currentTimeMillis(); - try { + try { + Context cx = Context.enter(); - Function lessc = (Function)scope.get("lessc", scope); - Object result = lessc.call( cx, scope, null, new Object[] {name, input, compress}); + + // The scope for compiling + ScriptableObject compileScope = (ScriptableObject)cx.newObject(scope); + + // give it a reference to the parent scope + compileScope.setPrototype(scope); + compileScope.setParentScope(null); + + // Copy the default options + List options = new ArrayList(this.options); + // Set up the arguments for + options.add(input.getAbsolutePath()); + + // Add compress if the value is set for backward compatibility + if (this.compress != null && this.compress.booleanValue()) { + options.add("-x"); + } + + Scriptable argsObj = cx.newArray(compileScope, options.toArray(new Object[options.size()])); + //Scriptable argsObj = cx.newArray(compileScope, new Object[] {"-ru", "c.less"}); + compileScope.defineProperty("arguments", argsObj, ScriptableObject.DONTENUM); + + // invoke the compiler - we don't pass arguments here because its a script not a real function + // and we don't care about the result because its written to the output stream (out) + compiler.call(cx, compileScope, null, new Object[] {}); + if (logger.isDebugEnabled()) { logger.debug("Finished compilation of LESS source in %,d ms.", System.currentTimeMillis() - start ); } - return result.toString(); + return StringUtils.isNotBlank(this.encoding) ? out.toString(encoding) : out.toString(); } catch (Exception e) { if (e instanceof JavaScriptException) { @@ -323,6 +404,10 @@ public String compile(String input, String name) throws LessException { } throw new LessException(e); }finally{ + // reset our ouput stream so we don't copy data on the next invocation + out.reset(); + + // we're done with this invocation Context.exit(); } } @@ -335,8 +420,7 @@ public String compile(String input, String name) throws LessException { * @throws IOException If the LESS file cannot be read. */ public String compile(File input) throws IOException, LessException { - LessSource lessSource = new LessSource(new FileResource(input)); - return compile(lessSource); + return compile(input, input.getName()); } /** @@ -359,16 +443,12 @@ public void compile(File input, File output) throws IOException, LessException { * @throws IOException If the LESS file cannot be read or the output file cannot be written. */ public void compile(File input, File output, boolean force) throws IOException, LessException { - LessSource lessSource = new LessSource(new FileResource(input)); - compile(lessSource, output, force); - } + if (force || !output.exists() || output.lastModified() < input.lastModified()) { + String data = compile(input); + FileUtils.writeStringToFile(output, data, encoding); + } + } - /** - * Compiles the input LessSource to CSS. - * - * @param input The input LessSource to compile. - * @return The CSS. - */ public String compile(LessSource input) throws LessException { return compile(input.getNormalizedContent(), input.getName()); } @@ -397,5 +477,5 @@ public void compile(LessSource input, File output, boolean force) throws IOExcep String data = compile(input); FileUtils.writeStringToFile(output, data, encoding); } - } + } } diff --git a/src/main/resources/META-INF/less-rhino-1.5.1.js b/src/main/resources/META-INF/less-rhino-1.5.1.js new file mode 100644 index 0000000..4f4c4b6 --- /dev/null +++ b/src/main/resources/META-INF/less-rhino-1.5.1.js @@ -0,0 +1,6831 @@ +/* LESS.js v1.5.1 RHINO | Copyright (c) 2009-2013, Alexis Sellier */ + +// +// Stub out `require` in rhino +// +function require(arg) { + var split = arg.split('/'); + var resultModule = split.length == 1 ? less.modules[split[0]] : less[split[1]]; + if (!resultModule) { + throw { message: "Cannot find module '" + arg + "'"}; + } + return resultModule; +} + + +if (typeof(window) === 'undefined') { less = {} } +else { less = window.less = {} } +tree = less.tree = {}; +less.mode = 'rhino'; + +(function() { + + console = function() { + var stdout = java.lang.System.out; + var stderr = java.lang.System.err; + + function doLog(out, type) { + return function() { + var args = java.lang.reflect.Array.newInstance(java.lang.Object, arguments.length - 1); + var format = arguments[0]; + var conversionIndex = 0; + // need to look for %d (integer) conversions because in Javascript all numbers are doubles + for (var i = 1; i < arguments.length; i++) { + var arg = arguments[i]; + if (conversionIndex != -1) { + conversionIndex = format.indexOf('%', conversionIndex); + } + if (conversionIndex >= 0 && conversionIndex < format.length) { + var conversion = format.charAt(conversionIndex + 1); + if (conversion === 'd' && typeof arg === 'number') { + arg = new java.lang.Integer(new java.lang.Double(arg).intValue()); + } + conversionIndex++; + } + args[i-1] = arg; + } + try { + out.println(type + java.lang.String.format(format, args)); + } catch(ex) { + stderr.println(ex); + } + } + } + return { + log: doLog(stdout, ''), + info: doLog(stdout, 'INFO: '), + error: doLog(stderr, 'ERROR: '), + warn: doLog(stderr, 'WARN: ') + }; + }(); + + less.modules = {}; + + less.modules.path = { + join: function() { + var parts = []; + for (i in arguments) { + parts = parts.concat(arguments[i].split(/\/|\\/)); + } + var result = []; + for (i in parts) { + var part = parts[i]; + if (part === '..' && result.length > 0) { + result.pop(); + } else if (part === '' && result.length > 0) { + // skip + } else if (part !== '.') { + if (part.slice(-1)==='\\' || part.slice(-1)==='/') { + part = part.slice(0, -1); + } + result.push(part); + } + } + return result.join('/'); + }, + dirname: function(p) { + var path = p.split('/'); + path.pop(); + return path.join('/'); + }, + basename: function(p, ext) { + var base = p.split('/').pop(); + if (ext) { + var index = base.lastIndexOf(ext); + if (base.length === index + ext.length) { + base = base.substr(0, index); + } + } + return base; + }, + extname: function(p) { + var index = p.lastIndexOf('.'); + return index > 0 ? p.substring(index) : ''; + } + }; + + less.modules.fs = { + readFileSync: function(name) { + // read a file into a byte array + var file = new java.io.File(name); + var stream = new java.io.FileInputStream(file); + var buffer = []; + var c; + while ((c = stream.read()) != -1) { + buffer.push(c); + } + stream.close(); + return { + length: buffer.length, + toString: function(enc) { + if (enc === 'base64') { + return encodeBase64Bytes(buffer); + } else if (enc) { + return java.lang.String["(byte[],java.lang.String)"](buffer, enc); + } else { + return java.lang.String["(byte[])"](buffer); + } + } + }; + } + }; + + less.encoder = { + encodeBase64: function(str) { + return encodeBase64String(str); + } + }; + + // --------------------------------------------------------------------------------------------- + // private helper functions + // --------------------------------------------------------------------------------------------- + + function encodeBase64Bytes(bytes) { + // requires at least a JRE Platform 6 (or JAXB 1.0 on the classpath) + return javax.xml.bind.DatatypeConverter.printBase64Binary(bytes) + } + function encodeBase64String(str) { + return encodeBase64Bytes(new java.lang.String(str).getBytes()); + } + +})(); + +var less, tree; + +// Node.js does not have a header file added which defines less +if (less === undefined) { + less = exports; + tree = require('./tree'); + less.mode = 'node'; +} +// +// less.js - parser +// +// A relatively straight-forward predictive parser. +// There is no tokenization/lexing stage, the input is parsed +// in one sweep. +// +// To make the parser fast enough to run in the browser, several +// optimization had to be made: +// +// - Matching and slicing on a huge input is often cause of slowdowns. +// The solution is to chunkify the input into smaller strings. +// The chunks are stored in the `chunks` var, +// `j` holds the current chunk index, and `current` holds +// the index of the current chunk in relation to `input`. +// This gives us an almost 4x speed-up. +// +// - In many cases, we don't need to match individual tokens; +// for example, if a value doesn't hold any variables, operations +// or dynamic references, the parser can effectively 'skip' it, +// treating it as a literal. +// An example would be '1px solid #000' - which evaluates to itself, +// we don't need to know what the individual components are. +// The drawback, of course is that you don't get the benefits of +// syntax-checking on the CSS. This gives us a 50% speed-up in the parser, +// and a smaller speed-up in the code-gen. +// +// +// Token matching is done with the `$` function, which either takes +// a terminal string or regexp, or a non-terminal function to call. +// It also takes care of moving all the indices forwards. +// +// +less.Parser = function Parser(env) { + var input, // LeSS input string + i, // current index in `input` + j, // current chunk + temp, // temporarily holds a chunk's state, for backtracking + memo, // temporarily holds `i`, when backtracking + furthest, // furthest index the parser has gone to + chunks, // chunkified input + current, // index of current chunk, in `input` + parser, + rootFilename = env && env.filename; + + // Top parser on an import tree must be sure there is one "env" + // which will then be passed around by reference. + if (!(env instanceof tree.parseEnv)) { + env = new tree.parseEnv(env); + } + + var imports = this.imports = { + paths: env.paths || [], // Search paths, when importing + queue: [], // Files which haven't been imported yet + files: env.files, // Holds the imported parse trees + contents: env.contents, // Holds the imported file contents + mime: env.mime, // MIME type of .less files + error: null, // Error in parsing/evaluating an import + push: function (path, currentFileInfo, importOptions, callback) { + var parserImports = this; + this.queue.push(path); + + var fileParsedFunc = function (e, root, fullPath) { + parserImports.queue.splice(parserImports.queue.indexOf(path), 1); // Remove the path from the queue + + var importedPreviously = fullPath in parserImports.files || fullPath === rootFilename; + + parserImports.files[fullPath] = root; // Store the root + + if (e && !parserImports.error) { parserImports.error = e; } + + callback(e, root, importedPreviously, fullPath); + }; + + if (less.Parser.importer) { + less.Parser.importer(path, currentFileInfo, fileParsedFunc, env); + } else { + less.Parser.fileLoader(path, currentFileInfo, function(e, contents, fullPath, newFileInfo) { + if (e) {fileParsedFunc(e); return;} + + var newEnv = new tree.parseEnv(env); + + newEnv.currentFileInfo = newFileInfo; + newEnv.processImports = false; + newEnv.contents[fullPath] = contents; + + if (currentFileInfo.reference || importOptions.reference) { + newFileInfo.reference = true; + } + + if (importOptions.inline) { + fileParsedFunc(null, contents, fullPath); + } else { + new(less.Parser)(newEnv).parse(contents, function (e, root) { + fileParsedFunc(e, root, fullPath); + }); + } + }, env); + } + } + }; + + function save() { temp = chunks[j], memo = i, current = i; } + function restore() { chunks[j] = temp, i = memo, current = i; } + + function sync() { + if (i > current) { + chunks[j] = chunks[j].slice(i - current); + current = i; + } + } + function isWhitespace(c) { + // Could change to \s? + var code = c.charCodeAt(0); + return code === 32 || code === 10 || code === 9; + } + // + // Parse from a token, regexp or string, and move forward if match + // + function $(tok) { + var match, length; + + // + // Non-terminal + // + if (tok instanceof Function) { + return tok.call(parser.parsers); + // + // Terminal + // + // Either match a single character in the input, + // or match a regexp in the current chunk (chunk[j]). + // + } else if (typeof(tok) === 'string') { + match = input.charAt(i) === tok ? tok : null; + length = 1; + sync (); + } else { + sync (); + + if (match = tok.exec(chunks[j])) { + length = match[0].length; + } else { + return null; + } + } + + // The match is confirmed, add the match length to `i`, + // and consume any extra white-space characters (' ' || '\n') + // which come after that. The reason for this is that LeSS's + // grammar is mostly white-space insensitive. + // + if (match) { + skipWhitespace(length); + + if(typeof(match) === 'string') { + return match; + } else { + return match.length === 1 ? match[0] : match; + } + } + } + + function skipWhitespace(length) { + var oldi = i, oldj = j, + endIndex = i + chunks[j].length, + mem = i += length; + + while (i < endIndex) { + if (! isWhitespace(input.charAt(i))) { break; } + i++; + } + chunks[j] = chunks[j].slice(length + (i - mem)); + current = i; + + if (chunks[j].length === 0 && j < chunks.length - 1) { j++; } + + return oldi !== i || oldj !== j; + } + + function expect(arg, msg) { + var result = $(arg); + if (! result) { + error(msg || (typeof(arg) === 'string' ? "expected '" + arg + "' got '" + input.charAt(i) + "'" + : "unexpected token")); + } else { + return result; + } + } + + function error(msg, type) { + var e = new Error(msg); + e.index = i; + e.type = type || 'Syntax'; + throw e; + } + + // Same as $(), but don't change the state of the parser, + // just return the match. + function peek(tok) { + if (typeof(tok) === 'string') { + return input.charAt(i) === tok; + } else { + return tok.test(chunks[j]); + } + } + + function getInput(e, env) { + if (e.filename && env.currentFileInfo.filename && (e.filename !== env.currentFileInfo.filename)) { + return parser.imports.contents[e.filename]; + } else { + return input; + } + } + + function getLocation(index, inputStream) { + var n = index + 1, + line = null, + column = -1; + + while (--n >= 0 && inputStream.charAt(n) !== '\n') { + column++; + } + + if (typeof index === 'number') { + line = (inputStream.slice(0, index).match(/\n/g) || "").length; + } + + return { + line: line, + column: column + }; + } + + function getDebugInfo(index, inputStream, env) { + var filename = env.currentFileInfo.filename; + if(less.mode !== 'browser' && less.mode !== 'rhino') { + filename = require('path').resolve(filename); + } + + return { + lineNumber: getLocation(index, inputStream).line + 1, + fileName: filename + }; + } + + function LessError(e, env) { + var input = getInput(e, env), + loc = getLocation(e.index, input), + line = loc.line, + col = loc.column, + callLine = e.call && getLocation(e.call, input).line, + lines = input.split('\n'); + + this.type = e.type || 'Syntax'; + this.message = e.message; + this.filename = e.filename || env.currentFileInfo.filename; + this.index = e.index; + this.line = typeof(line) === 'number' ? line + 1 : null; + this.callLine = callLine + 1; + this.callExtract = lines[callLine]; + this.stack = e.stack; + this.column = col; + this.extract = [ + lines[line - 1], + lines[line], + lines[line + 1] + ]; + } + + LessError.prototype = new Error(); + LessError.prototype.constructor = LessError; + + this.env = env = env || {}; + + // The optimization level dictates the thoroughness of the parser, + // the lower the number, the less nodes it will create in the tree. + // This could matter for debugging, or if you want to access + // the individual nodes in the tree. + this.optimization = ('optimization' in this.env) ? this.env.optimization : 1; + + // + // The Parser + // + return parser = { + + imports: imports, + // + // Parse an input string into an abstract syntax tree, + // call `callback` when done. + // + parse: function (str, callback) { + var root, line, lines, error = null; + + i = j = current = furthest = 0; + input = str.replace(/\r\n/g, '\n'); + + // Remove potential UTF Byte Order Mark + input = input.replace(/^\uFEFF/, ''); + + parser.imports.contents[env.currentFileInfo.filename] = input; + + // Split the input into chunks. + chunks = (function (chunks) { + var j = 0, + skip = /(?:@\{[\w-]+\}|[^"'`\{\}\/\(\)\\])+/g, + comment = /\/\*(?:[^*]|\*+[^\/*])*\*+\/|\/\/.*/g, + string = /"((?:[^"\\\r\n]|\\.)*)"|'((?:[^'\\\r\n]|\\.)*)'|`((?:[^`]|\\.)*)`/g, + level = 0, + match, + chunk = chunks[0], + inParam; + + for (var i = 0, c, cc; i < input.length;) { + skip.lastIndex = i; + if (match = skip.exec(input)) { + if (match.index === i) { + i += match[0].length; + chunk.push(match[0]); + } + } + c = input.charAt(i); + comment.lastIndex = string.lastIndex = i; + + if (match = string.exec(input)) { + if (match.index === i) { + i += match[0].length; + chunk.push(match[0]); + continue; + } + } + + if (!inParam && c === '/') { + cc = input.charAt(i + 1); + if (cc === '/' || cc === '*') { + if (match = comment.exec(input)) { + if (match.index === i) { + i += match[0].length; + chunk.push(match[0]); + continue; + } + } + } + } + + switch (c) { + case '{': + if (!inParam) { + level++; + chunk.push(c); + break; + } + /* falls through */ + case '}': + if (!inParam) { + level--; + chunk.push(c); + chunks[++j] = chunk = []; + break; + } + /* falls through */ + case '(': + if (!inParam) { + inParam = true; + chunk.push(c); + break; + } + /* falls through */ + case ')': + if (inParam) { + inParam = false; + chunk.push(c); + break; + } + /* falls through */ + default: + chunk.push(c); + } + + i++; + } + if (level !== 0) { + error = new(LessError)({ + index: i-1, + type: 'Parse', + message: (level > 0) ? "missing closing `}`" : "missing opening `{`", + filename: env.currentFileInfo.filename + }, env); + } + + return chunks.map(function (c) { return c.join(''); }); + })([[]]); + + if (error) { + return callback(new(LessError)(error, env)); + } + + // Start with the primary rule. + // The whole syntax tree is held under a Ruleset node, + // with the `root` property set to true, so no `{}` are + // output. The callback is called when the input is parsed. + try { + root = new(tree.Ruleset)([], $(this.parsers.primary)); + root.root = true; + root.firstRoot = true; + } catch (e) { + return callback(new(LessError)(e, env)); + } + + root.toCSS = (function (evaluate) { + return function (options, variables) { + options = options || {}; + var evaldRoot, + css, + evalEnv = new tree.evalEnv(options); + + // + // Allows setting variables with a hash, so: + // + // `{ color: new(tree.Color)('#f01') }` will become: + // + // new(tree.Rule)('@color', + // new(tree.Value)([ + // new(tree.Expression)([ + // new(tree.Color)('#f01') + // ]) + // ]) + // ) + // + if (typeof(variables) === 'object' && !Array.isArray(variables)) { + variables = Object.keys(variables).map(function (k) { + var value = variables[k]; + + if (! (value instanceof tree.Value)) { + if (! (value instanceof tree.Expression)) { + value = new(tree.Expression)([value]); + } + value = new(tree.Value)([value]); + } + return new(tree.Rule)('@' + k, value, false, null, 0); + }); + evalEnv.frames = [new(tree.Ruleset)(null, variables)]; + } + + try { + var preEvalVisitors = [], + visitors = [ + new(tree.joinSelectorVisitor)(), + new(tree.processExtendsVisitor)(), + new(tree.toCSSVisitor)({compress: Boolean(options.compress)}) + ], i, root = this; + + if (options.plugins) { + for(i =0; i < options.plugins.length; i++) { + if (options.plugins[i].isPreEvalVisitor) { + preEvalVisitors.push(options.plugins[i]); + } else { + if (options.plugins[i].isPreVisitor) { + visitors.splice(0, 0, options.plugins[i]); + } else { + visitors.push(options.plugins[i]); + } + } + } + } + + for(i = 0; i < preEvalVisitors.length; i++) { + preEvalVisitors[i].run(root); + } + + evaldRoot = evaluate.call(root, evalEnv); + + for(i = 0; i < visitors.length; i++) { + visitors[i].run(evaldRoot); + } + + if (options.sourceMap) { + evaldRoot = new tree.sourceMapOutput( + { + writeSourceMap: options.writeSourceMap, + rootNode: evaldRoot, + contentsMap: parser.imports.contents, + sourceMapFilename: options.sourceMapFilename, + sourceMapURL: options.sourceMapURL, + outputFilename: options.sourceMapOutputFilename, + sourceMapBasepath: options.sourceMapBasepath, + sourceMapRootpath: options.sourceMapRootpath, + outputSourceFiles: options.outputSourceFiles, + sourceMapGenerator: options.sourceMapGenerator + }); + } + + css = evaldRoot.toCSS({ + compress: Boolean(options.compress), + dumpLineNumbers: env.dumpLineNumbers, + strictUnits: Boolean(options.strictUnits)}); + } catch (e) { + throw new(LessError)(e, env); + } + + if (options.cleancss && less.mode === 'node') { + var CleanCSS = require('clean-css'), + cleancssOptions = options.cleancssOptions || {}; + + if (cleancssOptions.keepSpecialComments === undefined) { + cleancssOptions.keepSpecialComments = "*"; + } + cleancssOptions.processImport = false; + cleancssOptions.noRebase = true; + if (cleancssOptions.noAdvanced === undefined) { + cleancssOptions.noAdvanced = true; + } + + return new CleanCSS(cleancssOptions).minify(css); + } else if (options.compress) { + return css.replace(/(^(\s)+)|((\s)+$)/g, ""); + } else { + return css; + } + }; + })(root.eval); + + // If `i` is smaller than the `input.length - 1`, + // it means the parser wasn't able to parse the whole + // string, so we've got a parsing error. + // + // We try to extract a \n delimited string, + // showing the line where the parse error occured. + // We split it up into two parts (the part which parsed, + // and the part which didn't), so we can color them differently. + if (i < input.length - 1) { + i = furthest; + var loc = getLocation(i, input); + lines = input.split('\n'); + line = loc.line + 1; + + error = { + type: "Parse", + message: "Unrecognised input", + index: i, + filename: env.currentFileInfo.filename, + line: line, + column: loc.column, + extract: [ + lines[line - 2], + lines[line - 1], + lines[line] + ] + }; + } + + var finish = function (e) { + e = error || e || parser.imports.error; + + if (e) { + if (!(e instanceof LessError)) { + e = new(LessError)(e, env); + } + + return callback(e); + } + else { + return callback(null, root); + } + }; + + if (env.processImports !== false) { + new tree.importVisitor(this.imports, finish) + .run(root); + } else { + return finish(); + } + }, + + // + // Here in, the parsing rules/functions + // + // The basic structure of the syntax tree generated is as follows: + // + // Ruleset -> Rule -> Value -> Expression -> Entity + // + // Here's some LESS code: + // + // .class { + // color: #fff; + // border: 1px solid #000; + // width: @w + 4px; + // > .child {...} + // } + // + // And here's what the parse tree might look like: + // + // Ruleset (Selector '.class', [ + // Rule ("color", Value ([Expression [Color #fff]])) + // Rule ("border", Value ([Expression [Dimension 1px][Keyword "solid"][Color #000]])) + // Rule ("width", Value ([Expression [Operation "+" [Variable "@w"][Dimension 4px]]])) + // Ruleset (Selector [Element '>', '.child'], [...]) + // ]) + // + // In general, most rules will try to parse a token with the `$()` function, and if the return + // value is truly, will return a new node, of the relevant type. Sometimes, we need to check + // first, before parsing, that's when we use `peek()`. + // + parsers: { + // + // The `primary` rule is the *entry* and *exit* point of the parser. + // The rules here can appear at any level of the parse tree. + // + // The recursive nature of the grammar is an interplay between the `block` + // rule, which represents `{ ... }`, the `ruleset` rule, and this `primary` rule, + // as represented by this simplified grammar: + // + // primary → (ruleset | rule)+ + // ruleset → selector+ block + // block → '{' primary '}' + // + // Only at one point is the primary rule not called from the + // block rule: at the root level. + // + primary: function () { + var node, root = []; + + while ((node = $(this.extendRule) || $(this.mixin.definition) || $(this.rule) || $(this.ruleset) || + $(this.mixin.call) || $(this.comment) || $(this.directive)) + || $(/^[\s\n]+/) || $(/^;+/)) { + node && root.push(node); + } + return root; + }, + + // We create a Comment node for CSS comments `/* */`, + // but keep the LeSS comments `//` silent, by just skipping + // over them. + comment: function () { + var comment; + + if (input.charAt(i) !== '/') { return; } + + if (input.charAt(i + 1) === '/') { + return new(tree.Comment)($(/^\/\/.*/), true, i, env.currentFileInfo); + } else if (comment = $(/^\/\*(?:[^*]|\*+[^\/*])*\*+\/\n?/)) { + return new(tree.Comment)(comment, false, i, env.currentFileInfo); + } + }, + + comments: function () { + var comment, comments = []; + + while(comment = $(this.comment)) { + comments.push(comment); + } + + return comments; + }, + + // + // Entities are tokens which can be found inside an Expression + // + entities: { + // + // A string, which supports escaping " and ' + // + // "milky way" 'he\'s the one!' + // + quoted: function () { + var str, j = i, e, index = i; + + if (input.charAt(j) === '~') { j++, e = true; } // Escaped strings + if (input.charAt(j) !== '"' && input.charAt(j) !== "'") { return; } + + e && $('~'); + + if (str = $(/^"((?:[^"\\\r\n]|\\.)*)"|'((?:[^'\\\r\n]|\\.)*)'/)) { + return new(tree.Quoted)(str[0], str[1] || str[2], e, index, env.currentFileInfo); + } + }, + + // + // A catch-all word, such as: + // + // black border-collapse + // + keyword: function () { + var k; + + if (k = $(/^[_A-Za-z-][_A-Za-z0-9-]*/)) { + var color = tree.Color.fromKeyword(k); + if (color) { + return color; + } + return new(tree.Keyword)(k); + } + }, + + // + // A function call + // + // rgb(255, 0, 255) + // + // We also try to catch IE's `alpha()`, but let the `alpha` parser + // deal with the details. + // + // The arguments are parsed with the `entities.arguments` parser. + // + call: function () { + var name, nameLC, args, alpha_ret, index = i; + + if (! (name = /^([\w-]+|%|progid:[\w\.]+)\(/.exec(chunks[j]))) { return; } + + name = name[1]; + nameLC = name.toLowerCase(); + + if (nameLC === 'url') { return null; } + else { i += name.length; } + + if (nameLC === 'alpha') { + alpha_ret = $(this.alpha); + if(typeof alpha_ret !== 'undefined') { + return alpha_ret; + } + } + + $('('); // Parse the '(' and consume whitespace. + + args = $(this.entities.arguments); + + if (! $(')')) { + return; + } + + if (name) { return new(tree.Call)(name, args, index, env.currentFileInfo); } + }, + arguments: function () { + var args = [], arg; + + while (arg = $(this.entities.assignment) || $(this.expression)) { + args.push(arg); + if (! $(',')) { + break; + } + } + return args; + }, + literal: function () { + return $(this.entities.dimension) || + $(this.entities.color) || + $(this.entities.quoted) || + $(this.entities.unicodeDescriptor); + }, + + // Assignments are argument entities for calls. + // They are present in ie filter properties as shown below. + // + // filter: progid:DXImageTransform.Microsoft.Alpha( *opacity=50* ) + // + + assignment: function () { + var key, value; + if ((key = $(/^\w+(?=\s?=)/i)) && $('=') && (value = $(this.entity))) { + return new(tree.Assignment)(key, value); + } + }, + + // + // Parse url() tokens + // + // We use a specific rule for urls, because they don't really behave like + // standard function calls. The difference is that the argument doesn't have + // to be enclosed within a string, so it can't be parsed as an Expression. + // + url: function () { + var value; + + if (input.charAt(i) !== 'u' || !$(/^url\(/)) { + return; + } + + value = $(this.entities.quoted) || $(this.entities.variable) || + $(/^(?:(?:\\[\(\)'"])|[^\(\)'"])+/) || ""; + + expect(')'); + + /*jshint eqnull:true */ + return new(tree.URL)((value.value != null || value instanceof tree.Variable) + ? value : new(tree.Anonymous)(value), env.currentFileInfo); + }, + + // + // A Variable entity, such as `@fink`, in + // + // width: @fink + 2px + // + // We use a different parser for variable definitions, + // see `parsers.variable`. + // + variable: function () { + var name, index = i; + + if (input.charAt(i) === '@' && (name = $(/^@@?[\w-]+/))) { + return new(tree.Variable)(name, index, env.currentFileInfo); + } + }, + + // A variable entity useing the protective {} e.g. @{var} + variableCurly: function () { + var curly, index = i; + + if (input.charAt(i) === '@' && (curly = $(/^@\{([\w-]+)\}/))) { + return new(tree.Variable)("@" + curly[1], index, env.currentFileInfo); + } + }, + + // + // A Hexadecimal color + // + // #4F3C2F + // + // `rgb` and `hsl` colors are parsed through the `entities.call` parser. + // + color: function () { + var rgb; + + if (input.charAt(i) === '#' && (rgb = $(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/))) { + return new(tree.Color)(rgb[1]); + } + }, + + // + // A Dimension, that is, a number and a unit + // + // 0.5em 95% + // + dimension: function () { + var value, c = input.charCodeAt(i); + //Is the first char of the dimension 0-9, '.', '+' or '-' + if ((c > 57 || c < 43) || c === 47 || c == 44) { + return; + } + + if (value = $(/^([+-]?\d*\.?\d+)(%|[a-z]+)?/)) { + return new(tree.Dimension)(value[1], value[2]); + } + }, + + // + // A unicode descriptor, as is used in unicode-range + // + // U+0?? or U+00A1-00A9 + // + unicodeDescriptor: function () { + var ud; + + if (ud = $(/^U\+[0-9a-fA-F?]+(\-[0-9a-fA-F?]+)?/)) { + return new(tree.UnicodeDescriptor)(ud[0]); + } + }, + + // + // JavaScript code to be evaluated + // + // `window.location.href` + // + javascript: function () { + var str, j = i, e; + + if (input.charAt(j) === '~') { j++; e = true; } // Escaped strings + if (input.charAt(j) !== '`') { return; } + if (env.javascriptEnabled !== undefined && !env.javascriptEnabled) { + error("You are using JavaScript, which has been disabled."); + } + + if (e) { $('~'); } + + if (str = $(/^`([^`]*)`/)) { + return new(tree.JavaScript)(str[1], i, e); + } + } + }, + + // + // The variable part of a variable definition. Used in the `rule` parser + // + // @fink: + // + variable: function () { + var name; + + if (input.charAt(i) === '@' && (name = $(/^(@[\w-]+)\s*:/))) { return name[1]; } + }, + + // + // extend syntax - used to extend selectors + // + extend: function(isRule) { + var elements, e, index = i, option, extendList = []; + + if (!$(isRule ? /^&:extend\(/ : /^:extend\(/)) { return; } + + do { + option = null; + elements = []; + while (true) { + option = $(/^(all)(?=\s*(\)|,))/); + if (option) { break; } + e = $(this.element); + if (!e) { break; } + elements.push(e); + } + + option = option && option[1]; + + extendList.push(new(tree.Extend)(new(tree.Selector)(elements), option, index)); + + } while($(",")); + + expect(/^\)/); + + if (isRule) { + expect(/^;/); + } + + return extendList; + }, + + // + // extendRule - used in a rule to extend all the parent selectors + // + extendRule: function() { + return this.extend(true); + }, + + // + // Mixins + // + mixin: { + // + // A Mixin call, with an optional argument list + // + // #mixins > .square(#fff); + // .rounded(4px, black); + // .button; + // + // The `while` loop is there because mixins can be + // namespaced, but we only support the child and descendant + // selector for now. + // + call: function () { + var elements = [], e, c, args, index = i, s = input.charAt(i), important = false; + + if (s !== '.' && s !== '#') { return; } + + save(); // stop us absorbing part of an invalid selector + + while (e = $(/^[#.](?:[\w-]|\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+/)) { + elements.push(new(tree.Element)(c, e, i, env.currentFileInfo)); + c = $('>'); + } + if ($('(')) { + args = this.mixin.args.call(this, true).args; + expect(')'); + } + + args = args || []; + + if ($(this.important)) { + important = true; + } + + if (elements.length > 0 && ($(';') || peek('}'))) { + return new(tree.mixin.Call)(elements, args, index, env.currentFileInfo, important); + } + + restore(); + }, + args: function (isCall) { + var expressions = [], argsSemiColon = [], isSemiColonSeperated, argsComma = [], expressionContainsNamed, name, nameLoop, value, arg, + returner = {args:null, variadic: false}; + while (true) { + if (isCall) { + arg = $(this.expression); + } else { + $(this.comments); + if (input.charAt(i) === '.' && $(/^\.{3}/)) { + returner.variadic = true; + if ($(";") && !isSemiColonSeperated) { + isSemiColonSeperated = true; + } + (isSemiColonSeperated ? argsSemiColon : argsComma) + .push({ variadic: true }); + break; + } + arg = $(this.entities.variable) || $(this.entities.literal) + || $(this.entities.keyword); + } + + if (!arg) { + break; + } + + nameLoop = null; + if (arg.throwAwayComments) { + arg.throwAwayComments(); + } + value = arg; + var val = null; + + if (isCall) { + // Variable + if (arg.value.length == 1) { + val = arg.value[0]; + } + } else { + val = arg; + } + + if (val && val instanceof tree.Variable) { + if ($(':')) { + if (expressions.length > 0) { + if (isSemiColonSeperated) { + error("Cannot mix ; and , as delimiter types"); + } + expressionContainsNamed = true; + } + value = expect(this.expression); + nameLoop = (name = val.name); + } else if (!isCall && $(/^\.{3}/)) { + returner.variadic = true; + if ($(";") && !isSemiColonSeperated) { + isSemiColonSeperated = true; + } + (isSemiColonSeperated ? argsSemiColon : argsComma) + .push({ name: arg.name, variadic: true }); + break; + } else if (!isCall) { + name = nameLoop = val.name; + value = null; + } + } + + if (value) { + expressions.push(value); + } + + argsComma.push({ name:nameLoop, value:value }); + + if ($(',')) { + continue; + } + + if ($(';') || isSemiColonSeperated) { + + if (expressionContainsNamed) { + error("Cannot mix ; and , as delimiter types"); + } + + isSemiColonSeperated = true; + + if (expressions.length > 1) { + value = new(tree.Value)(expressions); + } + argsSemiColon.push({ name:name, value:value }); + + name = null; + expressions = []; + expressionContainsNamed = false; + } + } + + returner.args = isSemiColonSeperated ? argsSemiColon : argsComma; + return returner; + }, + // + // A Mixin definition, with a list of parameters + // + // .rounded (@radius: 2px, @color) { + // ... + // } + // + // Until we have a finer grained state-machine, we have to + // do a look-ahead, to make sure we don't have a mixin call. + // See the `rule` function for more information. + // + // We start by matching `.rounded (`, and then proceed on to + // the argument list, which has optional default values. + // We store the parameters in `params`, with a `value` key, + // if there is a value, such as in the case of `@radius`. + // + // Once we've got our params list, and a closing `)`, we parse + // the `{...}` block. + // + definition: function () { + var name, params = [], match, ruleset, cond, variadic = false; + if ((input.charAt(i) !== '.' && input.charAt(i) !== '#') || + peek(/^[^{]*\}/)) { + return; + } + + save(); + + if (match = $(/^([#.](?:[\w-]|\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+)\s*\(/)) { + name = match[1]; + + var argInfo = this.mixin.args.call(this, false); + params = argInfo.args; + variadic = argInfo.variadic; + + // .mixincall("@{a}"); + // looks a bit like a mixin definition.. so we have to be nice and restore + if (!$(')')) { + furthest = i; + restore(); + } + + $(this.comments); + + if ($(/^when/)) { // Guard + cond = expect(this.conditions, 'expected condition'); + } + + ruleset = $(this.block); + + if (ruleset) { + return new(tree.mixin.Definition)(name, params, ruleset, cond, variadic); + } else { + restore(); + } + } + } + }, + + // + // Entities are the smallest recognized token, + // and can be found inside a rule's value. + // + entity: function () { + return $(this.entities.literal) || $(this.entities.variable) || $(this.entities.url) || + $(this.entities.call) || $(this.entities.keyword) ||$(this.entities.javascript) || + $(this.comment); + }, + + // + // A Rule terminator. Note that we use `peek()` to check for '}', + // because the `block` rule will be expecting it, but we still need to make sure + // it's there, if ';' was ommitted. + // + end: function () { + return $(';') || peek('}'); + }, + + // + // IE's alpha function + // + // alpha(opacity=88) + // + alpha: function () { + var value; + + if (! $(/^\(opacity=/i)) { return; } + if (value = $(/^\d+/) || $(this.entities.variable)) { + expect(')'); + return new(tree.Alpha)(value); + } + }, + + // + // A Selector Element + // + // div + // + h1 + // #socks + // input[type="text"] + // + // Elements are the building blocks for Selectors, + // they are made out of a `Combinator` (see combinator rule), + // and an element name, such as a tag a class, or `*`. + // + element: function () { + var e, c, v; + + c = $(this.combinator); + + e = $(/^(?:\d+\.\d+|\d+)%/) || $(/^(?:[.#]?|:*)(?:[\w-]|[^\x00-\x9f]|\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+/) || + $('*') || $('&') || $(this.attribute) || $(/^\([^()@]+\)/) || $(/^[\.#](?=@)/) || $(this.entities.variableCurly); + + if (! e) { + if ($('(')) { + if ((v = ($(this.selector))) && + $(')')) { + e = new(tree.Paren)(v); + } + } + } + + if (e) { return new(tree.Element)(c, e, i, env.currentFileInfo); } + }, + + // + // Combinators combine elements together, in a Selector. + // + // Because our parser isn't white-space sensitive, special care + // has to be taken, when parsing the descendant combinator, ` `, + // as it's an empty space. We have to check the previous character + // in the input, to see if it's a ` ` character. More info on how + // we deal with this in *combinator.js*. + // + combinator: function () { + var c = input.charAt(i); + + if (c === '>' || c === '+' || c === '~' || c === '|') { + i++; + while (input.charAt(i).match(/\s/)) { i++; } + return new(tree.Combinator)(c); + } else if (input.charAt(i - 1).match(/\s/)) { + return new(tree.Combinator)(" "); + } else { + return new(tree.Combinator)(null); + } + }, + // + // A CSS selector (see selector below) + // with less extensions e.g. the ability to extend and guard + // + lessSelector: function () { + return this.selector(true); + }, + // + // A CSS Selector + // + // .class > div + h1 + // li a:hover + // + // Selectors are made out of one or more Elements, see above. + // + selector: function (isLess) { + var e, elements = [], c, extend, extendList = [], when, condition; + + while ((isLess && (extend = $(this.extend))) || (isLess && (when = $(/^when/))) || (e = $(this.element))) { + if (when) { + condition = expect(this.conditions, 'expected condition'); + } else if (condition) { + error("CSS guard can only be used at the end of selector"); + } else if (extend) { + extendList.push.apply(extendList, extend); + } else { + if (extendList.length) { + error("Extend can only be used at the end of selector"); + } + c = input.charAt(i); + elements.push(e); + e = null; + } + if (c === '{' || c === '}' || c === ';' || c === ',' || c === ')') { + break; + } + } + + if (elements.length > 0) { return new(tree.Selector)(elements, extendList, condition, i, env.currentFileInfo); } + if (extendList.length) { error("Extend must be used to extend a selector, it cannot be used on its own"); } + }, + attribute: function () { + var key, val, op; + + if (! $('[')) { return; } + + if (!(key = $(this.entities.variableCurly))) { + key = expect(/^(?:[_A-Za-z0-9-\*]*\|)?(?:[_A-Za-z0-9-]|\\.)+/); + } + + if ((op = $(/^[|~*$^]?=/))) { + val = $(this.entities.quoted) || $(/^[0-9]+%/) || $(/^[\w-]+/) || $(this.entities.variableCurly); + } + + expect(']'); + + return new(tree.Attribute)(key, op, val); + }, + + // + // The `block` rule is used by `ruleset` and `mixin.definition`. + // It's a wrapper around the `primary` rule, with added `{}`. + // + block: function () { + var content; + if ($('{') && (content = $(this.primary)) && $('}')) { + return content; + } + }, + + // + // div, .class, body > p {...} + // + ruleset: function () { + var selectors = [], s, rules, debugInfo; + + save(); + + if (env.dumpLineNumbers) { + debugInfo = getDebugInfo(i, input, env); + } + + while (s = $(this.lessSelector)) { + selectors.push(s); + $(this.comments); + if (! $(',')) { break; } + if (s.condition) { + error("Guards are only currently allowed on a single selector."); + } + $(this.comments); + } + + if (selectors.length > 0 && (rules = $(this.block))) { + var ruleset = new(tree.Ruleset)(selectors, rules, env.strictImports); + if (env.dumpLineNumbers) { + ruleset.debugInfo = debugInfo; + } + return ruleset; + } else { + // Backtrack + furthest = i; + restore(); + } + }, + rule: function (tryAnonymous) { + var name, value, c = input.charAt(i), important, merge = false; + save(); + + if (c === '.' || c === '#' || c === '&') { return; } + + if (name = $(this.variable) || $(this.ruleProperty)) { + // prefer to try to parse first if its a variable or we are compressing + // but always fallback on the other one + value = !tryAnonymous && (env.compress || (name.charAt(0) === '@')) ? + ($(this.value) || $(this.anonymousValue)) : + ($(this.anonymousValue) || $(this.value)); + + + important = $(this.important); + if (name[name.length-1] === "+") { + merge = true; + name = name.substr(0, name.length - 1); + } + + if (value && $(this.end)) { + return new (tree.Rule)(name, value, important, merge, memo, env.currentFileInfo); + } else { + furthest = i; + restore(); + if (value && !tryAnonymous) { + return this.rule(true); + } + } + } + }, + anonymousValue: function () { + var match; + if (match = /^([^@+\/'"*`(;{}-]*);/.exec(chunks[j])) { + i += match[0].length - 1; + return new(tree.Anonymous)(match[1]); + } + }, + + // + // An @import directive + // + // @import "lib"; + // + // Depending on our environemnt, importing is done differently: + // In the browser, it's an XHR request, in Node, it would be a + // file-system operation. The function used for importing is + // stored in `import`, which we pass to the Import constructor. + // + "import": function () { + var path, features, index = i; + + save(); + + var dir = $(/^@import?\s+/); + + var options = (dir ? $(this.importOptions) : null) || {}; + + if (dir && (path = $(this.entities.quoted) || $(this.entities.url))) { + features = $(this.mediaFeatures); + if ($(';')) { + features = features && new(tree.Value)(features); + return new(tree.Import)(path, features, options, index, env.currentFileInfo); + } + } + + restore(); + }, + + importOptions: function() { + var o, options = {}, optionName, value; + + // list of options, surrounded by parens + if (! $('(')) { return null; } + do { + if (o = $(this.importOption)) { + optionName = o; + value = true; + switch(optionName) { + case "css": + optionName = "less"; + value = false; + break; + case "once": + optionName = "multiple"; + value = false; + break; + } + options[optionName] = value; + if (! $(',')) { break; } + } + } while (o); + expect(')'); + return options; + }, + + importOption: function() { + var opt = $(/^(less|css|multiple|once|inline|reference)/); + if (opt) { + return opt[1]; + } + }, + + mediaFeature: function () { + var e, p, nodes = []; + + do { + if (e = ($(this.entities.keyword) || $(this.entities.variable))) { + nodes.push(e); + } else if ($('(')) { + p = $(this.property); + e = $(this.value); + if ($(')')) { + if (p && e) { + nodes.push(new(tree.Paren)(new(tree.Rule)(p, e, null, null, i, env.currentFileInfo, true))); + } else if (e) { + nodes.push(new(tree.Paren)(e)); + } else { + return null; + } + } else { return null; } + } + } while (e); + + if (nodes.length > 0) { + return new(tree.Expression)(nodes); + } + }, + + mediaFeatures: function () { + var e, features = []; + + do { + if (e = $(this.mediaFeature)) { + features.push(e); + if (! $(',')) { break; } + } else if (e = $(this.entities.variable)) { + features.push(e); + if (! $(',')) { break; } + } + } while (e); + + return features.length > 0 ? features : null; + }, + + media: function () { + var features, rules, media, debugInfo; + + if (env.dumpLineNumbers) { + debugInfo = getDebugInfo(i, input, env); + } + + if ($(/^@media/)) { + features = $(this.mediaFeatures); + + if (rules = $(this.block)) { + media = new(tree.Media)(rules, features, i, env.currentFileInfo); + if (env.dumpLineNumbers) { + media.debugInfo = debugInfo; + } + return media; + } + } + }, + + // + // A CSS Directive + // + // @charset "utf-8"; + // + directive: function () { + var name, value, rules, nonVendorSpecificName, + hasBlock, hasIdentifier, hasExpression, identifier; + + if (input.charAt(i) !== '@') { return; } + + if (value = $(this['import']) || $(this.media)) { + return value; + } + + save(); + + name = $(/^@[a-z-]+/); + + if (!name) { return; } + + nonVendorSpecificName = name; + if (name.charAt(1) == '-' && name.indexOf('-', 2) > 0) { + nonVendorSpecificName = "@" + name.slice(name.indexOf('-', 2) + 1); + } + + switch(nonVendorSpecificName) { + case "@font-face": + hasBlock = true; + break; + case "@viewport": + case "@top-left": + case "@top-left-corner": + case "@top-center": + case "@top-right": + case "@top-right-corner": + case "@bottom-left": + case "@bottom-left-corner": + case "@bottom-center": + case "@bottom-right": + case "@bottom-right-corner": + case "@left-top": + case "@left-middle": + case "@left-bottom": + case "@right-top": + case "@right-middle": + case "@right-bottom": + hasBlock = true; + break; + case "@host": + case "@page": + case "@document": + case "@supports": + case "@keyframes": + hasBlock = true; + hasIdentifier = true; + break; + case "@namespace": + hasExpression = true; + break; + } + + if (hasIdentifier) { + identifier = ($(/^[^{]+/) || '').trim(); + if (identifier) { + name += " " + identifier; + } + } + + if (hasBlock) + { + if (rules = $(this.block)) { + return new(tree.Directive)(name, rules, i, env.currentFileInfo); + } + } else { + if ((value = hasExpression ? $(this.expression) : $(this.entity)) && $(';')) { + var directive = new(tree.Directive)(name, value, i, env.currentFileInfo); + if (env.dumpLineNumbers) { + directive.debugInfo = getDebugInfo(i, input, env); + } + return directive; + } + } + + restore(); + }, + + // + // A Value is a comma-delimited list of Expressions + // + // font-family: Baskerville, Georgia, serif; + // + // In a Rule, a Value represents everything after the `:`, + // and before the `;`. + // + value: function () { + var e, expressions = []; + + while (e = $(this.expression)) { + expressions.push(e); + if (! $(',')) { break; } + } + + if (expressions.length > 0) { + return new(tree.Value)(expressions); + } + }, + important: function () { + if (input.charAt(i) === '!') { + return $(/^! *important/); + } + }, + sub: function () { + var a, e; + + if ($('(')) { + if (a = $(this.addition)) { + e = new(tree.Expression)([a]); + expect(')'); + e.parens = true; + return e; + } + } + }, + multiplication: function () { + var m, a, op, operation, isSpaced; + if (m = $(this.operand)) { + isSpaced = isWhitespace(input.charAt(i - 1)); + while (!peek(/^\/[*\/]/) && (op = ($('/') || $('*')))) { + if (a = $(this.operand)) { + m.parensInOp = true; + a.parensInOp = true; + operation = new(tree.Operation)(op, [operation || m, a], isSpaced); + isSpaced = isWhitespace(input.charAt(i - 1)); + } else { + break; + } + } + return operation || m; + } + }, + addition: function () { + var m, a, op, operation, isSpaced; + if (m = $(this.multiplication)) { + isSpaced = isWhitespace(input.charAt(i - 1)); + while ((op = $(/^[-+]\s+/) || (!isSpaced && ($('+') || $('-')))) && + (a = $(this.multiplication))) { + m.parensInOp = true; + a.parensInOp = true; + operation = new(tree.Operation)(op, [operation || m, a], isSpaced); + isSpaced = isWhitespace(input.charAt(i - 1)); + } + return operation || m; + } + }, + conditions: function () { + var a, b, index = i, condition; + + if (a = $(this.condition)) { + while (peek(/^,\s*(not\s*)?\(/) && $(',') && (b = $(this.condition))) { + condition = new(tree.Condition)('or', condition || a, b, index); + } + return condition || a; + } + }, + condition: function () { + var a, b, c, op, index = i, negate = false; + + if ($(/^not/)) { negate = true; } + expect('('); + if (a = $(this.addition) || $(this.entities.keyword) || $(this.entities.quoted)) { + if (op = $(/^(?:>=|<=|=<|[<=>])/)) { + if (b = $(this.addition) || $(this.entities.keyword) || $(this.entities.quoted)) { + c = new(tree.Condition)(op, a, b, index, negate); + } else { + error('expected expression'); + } + } else { + c = new(tree.Condition)('=', a, new(tree.Keyword)('true'), index, negate); + } + expect(')'); + return $(/^and/) ? new(tree.Condition)('and', c, $(this.condition)) : c; + } + }, + + // + // An operand is anything that can be part of an operation, + // such as a Color, or a Variable + // + operand: function () { + var negate, p = input.charAt(i + 1); + + if (input.charAt(i) === '-' && (p === '@' || p === '(')) { negate = $('-'); } + var o = $(this.sub) || $(this.entities.dimension) || + $(this.entities.color) || $(this.entities.variable) || + $(this.entities.call); + + if (negate) { + o.parensInOp = true; + o = new(tree.Negative)(o); + } + + return o; + }, + + // + // Expressions either represent mathematical operations, + // or white-space delimited Entities. + // + // 1px solid black + // @var * 2 + // + expression: function () { + var e, delim, entities = []; + + while (e = $(this.addition) || $(this.entity)) { + entities.push(e); + // operations do not allow keyword "/" dimension (e.g. small/20px) so we support that here + if (!peek(/^\/[\/*]/) && (delim = $('/'))) { + entities.push(new(tree.Anonymous)(delim)); + } + } + if (entities.length > 0) { + return new(tree.Expression)(entities); + } + }, + property: function () { + var name; + + if (name = $(/^(\*?-?[_a-zA-Z0-9-]+)\s*:/)) { + return name[1]; + } + }, + ruleProperty: function () { + var name; + + if (name = $(/^(\*?-?[_a-zA-Z0-9-]+)\s*(\+?)\s*:/)) { + return name[1] + (name[2] || ""); + } + } + } + }; +}; + + +(function (tree) { + +tree.functions = { + rgb: function (r, g, b) { + return this.rgba(r, g, b, 1.0); + }, + rgba: function (r, g, b, a) { + var rgb = [r, g, b].map(function (c) { return scaled(c, 255); }); + a = number(a); + return new(tree.Color)(rgb, a); + }, + hsl: function (h, s, l) { + return this.hsla(h, s, l, 1.0); + }, + hsla: function (h, s, l, a) { + function hue(h) { + h = h < 0 ? h + 1 : (h > 1 ? h - 1 : h); + if (h * 6 < 1) { return m1 + (m2 - m1) * h * 6; } + else if (h * 2 < 1) { return m2; } + else if (h * 3 < 2) { return m1 + (m2 - m1) * (2/3 - h) * 6; } + else { return m1; } + } + + h = (number(h) % 360) / 360; + s = clamp(number(s)); l = clamp(number(l)); a = clamp(number(a)); + + var m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s; + var m1 = l * 2 - m2; + + return this.rgba(hue(h + 1/3) * 255, + hue(h) * 255, + hue(h - 1/3) * 255, + a); + }, + + hsv: function(h, s, v) { + return this.hsva(h, s, v, 1.0); + }, + + hsva: function(h, s, v, a) { + h = ((number(h) % 360) / 360) * 360; + s = number(s); v = number(v); a = number(a); + + var i, f; + i = Math.floor((h / 60) % 6); + f = (h / 60) - i; + + var vs = [v, + v * (1 - s), + v * (1 - f * s), + v * (1 - (1 - f) * s)]; + var perm = [[0, 3, 1], + [2, 0, 1], + [1, 0, 3], + [1, 2, 0], + [3, 1, 0], + [0, 1, 2]]; + + return this.rgba(vs[perm[i][0]] * 255, + vs[perm[i][1]] * 255, + vs[perm[i][2]] * 255, + a); + }, + + hue: function (color) { + return new(tree.Dimension)(Math.round(color.toHSL().h)); + }, + saturation: function (color) { + return new(tree.Dimension)(Math.round(color.toHSL().s * 100), '%'); + }, + lightness: function (color) { + return new(tree.Dimension)(Math.round(color.toHSL().l * 100), '%'); + }, + hsvhue: function(color) { + return new(tree.Dimension)(Math.round(color.toHSV().h)); + }, + hsvsaturation: function (color) { + return new(tree.Dimension)(Math.round(color.toHSV().s * 100), '%'); + }, + hsvvalue: function (color) { + return new(tree.Dimension)(Math.round(color.toHSV().v * 100), '%'); + }, + red: function (color) { + return new(tree.Dimension)(color.rgb[0]); + }, + green: function (color) { + return new(tree.Dimension)(color.rgb[1]); + }, + blue: function (color) { + return new(tree.Dimension)(color.rgb[2]); + }, + alpha: function (color) { + return new(tree.Dimension)(color.toHSL().a); + }, + luma: function (color) { + return new(tree.Dimension)(Math.round(color.luma() * color.alpha * 100), '%'); + }, + saturate: function (color, amount) { + // filter: saturate(3.2); + // should be kept as is, so check for color + if (!color.rgb) { + return null; + } + var hsl = color.toHSL(); + + hsl.s += amount.value / 100; + hsl.s = clamp(hsl.s); + return hsla(hsl); + }, + desaturate: function (color, amount) { + var hsl = color.toHSL(); + + hsl.s -= amount.value / 100; + hsl.s = clamp(hsl.s); + return hsla(hsl); + }, + lighten: function (color, amount) { + var hsl = color.toHSL(); + + hsl.l += amount.value / 100; + hsl.l = clamp(hsl.l); + return hsla(hsl); + }, + darken: function (color, amount) { + var hsl = color.toHSL(); + + hsl.l -= amount.value / 100; + hsl.l = clamp(hsl.l); + return hsla(hsl); + }, + fadein: function (color, amount) { + var hsl = color.toHSL(); + + hsl.a += amount.value / 100; + hsl.a = clamp(hsl.a); + return hsla(hsl); + }, + fadeout: function (color, amount) { + var hsl = color.toHSL(); + + hsl.a -= amount.value / 100; + hsl.a = clamp(hsl.a); + return hsla(hsl); + }, + fade: function (color, amount) { + var hsl = color.toHSL(); + + hsl.a = amount.value / 100; + hsl.a = clamp(hsl.a); + return hsla(hsl); + }, + spin: function (color, amount) { + var hsl = color.toHSL(); + var hue = (hsl.h + amount.value) % 360; + + hsl.h = hue < 0 ? 360 + hue : hue; + + return hsla(hsl); + }, + // + // Copyright (c) 2006-2009 Hampton Catlin, Nathan Weizenbaum, and Chris Eppstein + // http://sass-lang.com + // + mix: function (color1, color2, weight) { + if (!weight) { + weight = new(tree.Dimension)(50); + } + var p = weight.value / 100.0; + var w = p * 2 - 1; + var a = color1.toHSL().a - color2.toHSL().a; + + var w1 = (((w * a == -1) ? w : (w + a) / (1 + w * a)) + 1) / 2.0; + var w2 = 1 - w1; + + var rgb = [color1.rgb[0] * w1 + color2.rgb[0] * w2, + color1.rgb[1] * w1 + color2.rgb[1] * w2, + color1.rgb[2] * w1 + color2.rgb[2] * w2]; + + var alpha = color1.alpha * p + color2.alpha * (1 - p); + + return new(tree.Color)(rgb, alpha); + }, + greyscale: function (color) { + return this.desaturate(color, new(tree.Dimension)(100)); + }, + contrast: function (color, dark, light, threshold) { + // filter: contrast(3.2); + // should be kept as is, so check for color + if (!color.rgb) { + return null; + } + if (typeof light === 'undefined') { + light = this.rgba(255, 255, 255, 1.0); + } + if (typeof dark === 'undefined') { + dark = this.rgba(0, 0, 0, 1.0); + } + //Figure out which is actually light and dark! + if (dark.luma() > light.luma()) { + var t = light; + light = dark; + dark = t; + } + if (typeof threshold === 'undefined') { + threshold = 0.43; + } else { + threshold = number(threshold); + } + if ((color.luma() * color.alpha) < threshold) { + return light; + } else { + return dark; + } + }, + e: function (str) { + return new(tree.Anonymous)(str instanceof tree.JavaScript ? str.evaluated : str); + }, + escape: function (str) { + return new(tree.Anonymous)(encodeURI(str.value).replace(/=/g, "%3D").replace(/:/g, "%3A").replace(/#/g, "%23").replace(/;/g, "%3B").replace(/\(/g, "%28").replace(/\)/g, "%29")); + }, + '%': function (quoted /* arg, arg, ...*/) { + var args = Array.prototype.slice.call(arguments, 1), + str = quoted.value; + + for (var i = 0; i < args.length; i++) { + /*jshint loopfunc:true */ + str = str.replace(/%[sda]/i, function(token) { + var value = token.match(/s/i) ? args[i].value : args[i].toCSS(); + return token.match(/[A-Z]$/) ? encodeURIComponent(value) : value; + }); + } + str = str.replace(/%%/g, '%'); + return new(tree.Quoted)('"' + str + '"', str); + }, + unit: function (val, unit) { + if(!(val instanceof tree.Dimension)) { + throw { type: "Argument", message: "the first argument to unit must be a number" + (val instanceof tree.Operation ? ". Have you forgotten parenthesis?" : "") }; + } + return new(tree.Dimension)(val.value, unit ? unit.toCSS() : ""); + }, + convert: function (val, unit) { + return val.convertTo(unit.value); + }, + round: function (n, f) { + var fraction = typeof(f) === "undefined" ? 0 : f.value; + return this._math(function(num) { return num.toFixed(fraction); }, null, n); + }, + pi: function () { + return new(tree.Dimension)(Math.PI); + }, + mod: function(a, b) { + return new(tree.Dimension)(a.value % b.value, a.unit); + }, + pow: function(x, y) { + if (typeof x === "number" && typeof y === "number") { + x = new(tree.Dimension)(x); + y = new(tree.Dimension)(y); + } else if (!(x instanceof tree.Dimension) || !(y instanceof tree.Dimension)) { + throw { type: "Argument", message: "arguments must be numbers" }; + } + + return new(tree.Dimension)(Math.pow(x.value, y.value), x.unit); + }, + _math: function (fn, unit, n) { + if (n instanceof tree.Dimension) { + /*jshint eqnull:true */ + return new(tree.Dimension)(fn(parseFloat(n.value)), unit == null ? n.unit : unit); + } else if (typeof(n) === 'number') { + return fn(n); + } else { + throw { type: "Argument", message: "argument must be a number" }; + } + }, + _minmax: function (isMin, args) { + args = Array.prototype.slice.call(args); + switch(args.length) { + case 0: throw { type: "Argument", message: "one or more arguments required" }; + case 1: return args[0]; + } + var i, j, current, currentUnified, referenceUnified, unit, + order = [], // elems only contains original argument values. + values = {}; // key is the unit.toString() for unified tree.Dimension values, + // value is the index into the order array. + for (i = 0; i < args.length; i++) { + current = args[i]; + if (!(current instanceof tree.Dimension)) { + order.push(current); + continue; + } + currentUnified = current.unify(); + unit = currentUnified.unit.toString(); + j = values[unit]; + if (j === undefined) { + values[unit] = order.length; + order.push(current); + continue; + } + referenceUnified = order[j].unify(); + if ( isMin && currentUnified.value < referenceUnified.value || + !isMin && currentUnified.value > referenceUnified.value) { + order[j] = current; + } + } + if (order.length == 1) { + return order[0]; + } + args = order.map(function (a) { return a.toCSS(this.env); }) + .join(this.env.compress ? "," : ", "); + return new(tree.Anonymous)((isMin ? "min" : "max") + "(" + args + ")"); + }, + min: function () { + return this._minmax(true, arguments); + }, + max: function () { + return this._minmax(false, arguments); + }, + argb: function (color) { + return new(tree.Anonymous)(color.toARGB()); + + }, + percentage: function (n) { + return new(tree.Dimension)(n.value * 100, '%'); + }, + color: function (n) { + if (n instanceof tree.Quoted) { + var colorCandidate = n.value, + returnColor; + returnColor = tree.Color.fromKeyword(colorCandidate); + if (returnColor) { + return returnColor; + } + if (/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/.test(colorCandidate)) { + return new(tree.Color)(colorCandidate.slice(1)); + } + throw { type: "Argument", message: "argument must be a color keyword or 3/6 digit hex e.g. #FFF" }; + } else { + throw { type: "Argument", message: "argument must be a string" }; + } + }, + iscolor: function (n) { + return this._isa(n, tree.Color); + }, + isnumber: function (n) { + return this._isa(n, tree.Dimension); + }, + isstring: function (n) { + return this._isa(n, tree.Quoted); + }, + iskeyword: function (n) { + return this._isa(n, tree.Keyword); + }, + isurl: function (n) { + return this._isa(n, tree.URL); + }, + ispixel: function (n) { + return this.isunit(n, 'px'); + }, + ispercentage: function (n) { + return this.isunit(n, '%'); + }, + isem: function (n) { + return this.isunit(n, 'em'); + }, + isunit: function (n, unit) { + return (n instanceof tree.Dimension) && n.unit.is(unit.value || unit) ? tree.True : tree.False; + }, + _isa: function (n, Type) { + return (n instanceof Type) ? tree.True : tree.False; + }, + tint: function(color, amount) { + return this.mix(this.rgb(255,255,255), color, amount); + }, + shade: function(color, amount) { + return this.mix(this.rgb(0, 0, 0), color, amount); + }, + extract: function(values, index) { + index = index.value - 1; // (1-based index) + // handle non-array values as an array of length 1 + // return 'undefined' if index is invalid + return Array.isArray(values.value) + ? values.value[index] : Array(values)[index]; + }, + length: function(values) { + var n = Array.isArray(values.value) ? values.value.length : 1; + return new tree.Dimension(n); + }, + + "data-uri": function(mimetypeNode, filePathNode) { + + if (typeof window !== 'undefined') { + return new tree.URL(filePathNode || mimetypeNode, this.currentFileInfo).eval(this.env); + } + + var mimetype = mimetypeNode.value; + var filePath = (filePathNode && filePathNode.value); + + var fs = require('fs'), + path = require('path'), + useBase64 = false; + + if (arguments.length < 2) { + filePath = mimetype; + } + + if (this.env.isPathRelative(filePath)) { + if (this.currentFileInfo.relativeUrls) { + filePath = path.join(this.currentFileInfo.currentDirectory, filePath); + } else { + filePath = path.join(this.currentFileInfo.entryPath, filePath); + } + } + + // detect the mimetype if not given + if (arguments.length < 2) { + var mime; + try { + mime = require('mime'); + } catch (ex) { + mime = tree._mime; + } + + mimetype = mime.lookup(filePath); + + // use base 64 unless it's an ASCII or UTF-8 format + var charset = mime.charsets.lookup(mimetype); + useBase64 = ['US-ASCII', 'UTF-8'].indexOf(charset) < 0; + if (useBase64) { mimetype += ';base64'; } + } + else { + useBase64 = /;base64$/.test(mimetype); + } + + var buf = fs.readFileSync(filePath); + + // IE8 cannot handle a data-uri larger than 32KB. If this is exceeded + // and the --ieCompat flag is enabled, return a normal url() instead. + var DATA_URI_MAX_KB = 32, + fileSizeInKB = parseInt((buf.length / 1024), 10); + if (fileSizeInKB >= DATA_URI_MAX_KB) { + + if (this.env.ieCompat !== false) { + if (!this.env.silent) { + console.warn("Skipped data-uri embedding of %s because its size (%dKB) exceeds IE8-safe %dKB!", filePath, fileSizeInKB, DATA_URI_MAX_KB); + } + + return new tree.URL(filePathNode || mimetypeNode, this.currentFileInfo).eval(this.env); + } + } + + buf = useBase64 ? buf.toString('base64') + : encodeURIComponent(buf); + + var uri = "'data:" + mimetype + ',' + buf + "'"; + return new(tree.URL)(new(tree.Anonymous)(uri)); + }, + + "svg-gradient": function(direction) { + + function throwArgumentDescriptor() { + throw { type: "Argument", message: "svg-gradient expects direction, start_color [start_position], [color position,]..., end_color [end_position]" }; + } + + if (arguments.length < 3) { + throwArgumentDescriptor(); + } + var stops = Array.prototype.slice.call(arguments, 1), + gradientDirectionSvg, + gradientType = "linear", + rectangleDimension = 'x="0" y="0" width="1" height="1"', + useBase64 = true, + renderEnv = {compress: false}, + returner, + directionValue = direction.toCSS(renderEnv), + i, color, position, positionValue, alpha; + + switch (directionValue) { + case "to bottom": + gradientDirectionSvg = 'x1="0%" y1="0%" x2="0%" y2="100%"'; + break; + case "to right": + gradientDirectionSvg = 'x1="0%" y1="0%" x2="100%" y2="0%"'; + break; + case "to bottom right": + gradientDirectionSvg = 'x1="0%" y1="0%" x2="100%" y2="100%"'; + break; + case "to top right": + gradientDirectionSvg = 'x1="0%" y1="100%" x2="100%" y2="0%"'; + break; + case "ellipse": + case "ellipse at center": + gradientType = "radial"; + gradientDirectionSvg = 'cx="50%" cy="50%" r="75%"'; + rectangleDimension = 'x="-50" y="-50" width="101" height="101"'; + break; + default: + throw { type: "Argument", message: "svg-gradient direction must be 'to bottom', 'to right', 'to bottom right', 'to top right' or 'ellipse at center'" }; + } + returner = '' + + '' + + '<' + gradientType + 'Gradient id="gradient" gradientUnits="userSpaceOnUse" ' + gradientDirectionSvg + '>'; + + for (i = 0; i < stops.length; i+= 1) { + if (stops[i].value) { + color = stops[i].value[0]; + position = stops[i].value[1]; + } else { + color = stops[i]; + position = undefined; + } + + if (!(color instanceof tree.Color) || (!((i === 0 || i+1 === stops.length) && position === undefined) && !(position instanceof tree.Dimension))) { + throwArgumentDescriptor(); + } + positionValue = position ? position.toCSS(renderEnv) : i === 0 ? "0%" : "100%"; + alpha = color.alpha; + returner += ''; + } + returner += '' + + ''; + + if (useBase64) { + try { + returner = require('./encoder').encodeBase64(returner); // TODO browser implementation + } catch(e) { + useBase64 = false; + } + } + + returner = "'data:image/svg+xml" + (useBase64 ? ";base64" : "") + "," + returner + "'"; + return new(tree.URL)(new(tree.Anonymous)(returner)); + } +}; + +// these static methods are used as a fallback when the optional 'mime' dependency is missing +tree._mime = { + // this map is intentionally incomplete + // if you want more, install 'mime' dep + _types: { + '.htm' : 'text/html', + '.html': 'text/html', + '.gif' : 'image/gif', + '.jpg' : 'image/jpeg', + '.jpeg': 'image/jpeg', + '.png' : 'image/png' + }, + lookup: function (filepath) { + var ext = require('path').extname(filepath), + type = tree._mime._types[ext]; + if (type === undefined) { + throw new Error('Optional dependency "mime" is required for ' + ext); + } + return type; + }, + charsets: { + lookup: function (type) { + // assumes all text types are UTF-8 + return type && (/^text\//).test(type) ? 'UTF-8' : ''; + } + } +}; + +var mathFunctions = [{name:"ceil"}, {name:"floor"}, {name: "sqrt"}, {name:"abs"}, + {name:"tan", unit: ""}, {name:"sin", unit: ""}, {name:"cos", unit: ""}, + {name:"atan", unit: "rad"}, {name:"asin", unit: "rad"}, {name:"acos", unit: "rad"}], + createMathFunction = function(name, unit) { + return function(n) { + /*jshint eqnull:true */ + if (unit != null) { + n = n.unify(); + } + return this._math(Math[name], unit, n); + }; + }; + +for(var i = 0; i < mathFunctions.length; i++) { + tree.functions[mathFunctions[i].name] = createMathFunction(mathFunctions[i].name, mathFunctions[i].unit); +} + +// Color Blending +// ref: http://www.w3.org/TR/compositing-1 + +function colorBlend(mode, color1, color2) { + var ab = color1.alpha, cb, // backdrop + as = color2.alpha, cs, // source + ar, cr, r = []; // result + + ar = as + ab * (1 - as); + for (var i = 0; i < 3; i++) { + cb = color1.rgb[i] / 255; + cs = color2.rgb[i] / 255; + cr = mode(cb, cs); + if (ar) { + cr = (as * cs + ab * (cb + - as * (cb + cs - cr))) / ar; + } + r[i] = cr * 255; + } + + return new(tree.Color)(r, ar); +} + +var colorBlendMode = { + multiply: function(cb, cs) { + return cb * cs; + }, + screen: function(cb, cs) { + return cb + cs - cb * cs; + }, + overlay: function(cb, cs) { + cb *= 2; + return (cb <= 1) + ? colorBlendMode.multiply(cb, cs) + : colorBlendMode.screen(cb - 1, cs); + }, + softlight: function(cb, cs) { + var d = 1, e = cb; + if (cs > 0.5) { + e = 1; + d = (cb > 0.25) ? Math.sqrt(cb) + : ((16 * cb - 12) * cb + 4) * cb; + } + return cb - (1 - 2 * cs) * e * (d - cb); + }, + hardlight: function(cb, cs) { + return colorBlendMode.overlay(cs, cb); + }, + difference: function(cb, cs) { + return Math.abs(cb - cs); + }, + exclusion: function(cb, cs) { + return cb + cs - 2 * cb * cs; + }, + + // non-w3c functions: + average: function(cb, cs) { + return (cb + cs) / 2; + }, + negation: function(cb, cs) { + return 1 - Math.abs(cb + cs - 1); + } +}; + +function colorBlendInit() { + for (var f in colorBlendMode) { + tree.functions[f] = colorBlend.bind(null, colorBlendMode[f]); + } +} colorBlendInit(); + +// ~ End of Color Blending + +function hsla(color) { + return tree.functions.hsla(color.h, color.s, color.l, color.a); +} + +function scaled(n, size) { + if (n instanceof tree.Dimension && n.unit.is('%')) { + return parseFloat(n.value * size / 100); + } else { + return number(n); + } +} + +function number(n) { + if (n instanceof tree.Dimension) { + return parseFloat(n.unit.is('%') ? n.value / 100 : n.value); + } else if (typeof(n) === 'number') { + return n; + } else { + throw { + error: "RuntimeError", + message: "color functions take numbers as parameters" + }; + } +} + +function clamp(val) { + return Math.min(1, Math.max(0, val)); +} + +tree.functionCall = function(env, currentFileInfo) { + this.env = env; + this.currentFileInfo = currentFileInfo; +}; + +tree.functionCall.prototype = tree.functions; + +})(require('./tree')); + +(function (tree) { + tree.colors = { + 'aliceblue':'#f0f8ff', + 'antiquewhite':'#faebd7', + 'aqua':'#00ffff', + 'aquamarine':'#7fffd4', + 'azure':'#f0ffff', + 'beige':'#f5f5dc', + 'bisque':'#ffe4c4', + 'black':'#000000', + 'blanchedalmond':'#ffebcd', + 'blue':'#0000ff', + 'blueviolet':'#8a2be2', + 'brown':'#a52a2a', + 'burlywood':'#deb887', + 'cadetblue':'#5f9ea0', + 'chartreuse':'#7fff00', + 'chocolate':'#d2691e', + 'coral':'#ff7f50', + 'cornflowerblue':'#6495ed', + 'cornsilk':'#fff8dc', + 'crimson':'#dc143c', + 'cyan':'#00ffff', + 'darkblue':'#00008b', + 'darkcyan':'#008b8b', + 'darkgoldenrod':'#b8860b', + 'darkgray':'#a9a9a9', + 'darkgrey':'#a9a9a9', + 'darkgreen':'#006400', + 'darkkhaki':'#bdb76b', + 'darkmagenta':'#8b008b', + 'darkolivegreen':'#556b2f', + 'darkorange':'#ff8c00', + 'darkorchid':'#9932cc', + 'darkred':'#8b0000', + 'darksalmon':'#e9967a', + 'darkseagreen':'#8fbc8f', + 'darkslateblue':'#483d8b', + 'darkslategray':'#2f4f4f', + 'darkslategrey':'#2f4f4f', + 'darkturquoise':'#00ced1', + 'darkviolet':'#9400d3', + 'deeppink':'#ff1493', + 'deepskyblue':'#00bfff', + 'dimgray':'#696969', + 'dimgrey':'#696969', + 'dodgerblue':'#1e90ff', + 'firebrick':'#b22222', + 'floralwhite':'#fffaf0', + 'forestgreen':'#228b22', + 'fuchsia':'#ff00ff', + 'gainsboro':'#dcdcdc', + 'ghostwhite':'#f8f8ff', + 'gold':'#ffd700', + 'goldenrod':'#daa520', + 'gray':'#808080', + 'grey':'#808080', + 'green':'#008000', + 'greenyellow':'#adff2f', + 'honeydew':'#f0fff0', + 'hotpink':'#ff69b4', + 'indianred':'#cd5c5c', + 'indigo':'#4b0082', + 'ivory':'#fffff0', + 'khaki':'#f0e68c', + 'lavender':'#e6e6fa', + 'lavenderblush':'#fff0f5', + 'lawngreen':'#7cfc00', + 'lemonchiffon':'#fffacd', + 'lightblue':'#add8e6', + 'lightcoral':'#f08080', + 'lightcyan':'#e0ffff', + 'lightgoldenrodyellow':'#fafad2', + 'lightgray':'#d3d3d3', + 'lightgrey':'#d3d3d3', + 'lightgreen':'#90ee90', + 'lightpink':'#ffb6c1', + 'lightsalmon':'#ffa07a', + 'lightseagreen':'#20b2aa', + 'lightskyblue':'#87cefa', + 'lightslategray':'#778899', + 'lightslategrey':'#778899', + 'lightsteelblue':'#b0c4de', + 'lightyellow':'#ffffe0', + 'lime':'#00ff00', + 'limegreen':'#32cd32', + 'linen':'#faf0e6', + 'magenta':'#ff00ff', + 'maroon':'#800000', + 'mediumaquamarine':'#66cdaa', + 'mediumblue':'#0000cd', + 'mediumorchid':'#ba55d3', + 'mediumpurple':'#9370d8', + 'mediumseagreen':'#3cb371', + 'mediumslateblue':'#7b68ee', + 'mediumspringgreen':'#00fa9a', + 'mediumturquoise':'#48d1cc', + 'mediumvioletred':'#c71585', + 'midnightblue':'#191970', + 'mintcream':'#f5fffa', + 'mistyrose':'#ffe4e1', + 'moccasin':'#ffe4b5', + 'navajowhite':'#ffdead', + 'navy':'#000080', + 'oldlace':'#fdf5e6', + 'olive':'#808000', + 'olivedrab':'#6b8e23', + 'orange':'#ffa500', + 'orangered':'#ff4500', + 'orchid':'#da70d6', + 'palegoldenrod':'#eee8aa', + 'palegreen':'#98fb98', + 'paleturquoise':'#afeeee', + 'palevioletred':'#d87093', + 'papayawhip':'#ffefd5', + 'peachpuff':'#ffdab9', + 'peru':'#cd853f', + 'pink':'#ffc0cb', + 'plum':'#dda0dd', + 'powderblue':'#b0e0e6', + 'purple':'#800080', + 'red':'#ff0000', + 'rosybrown':'#bc8f8f', + 'royalblue':'#4169e1', + 'saddlebrown':'#8b4513', + 'salmon':'#fa8072', + 'sandybrown':'#f4a460', + 'seagreen':'#2e8b57', + 'seashell':'#fff5ee', + 'sienna':'#a0522d', + 'silver':'#c0c0c0', + 'skyblue':'#87ceeb', + 'slateblue':'#6a5acd', + 'slategray':'#708090', + 'slategrey':'#708090', + 'snow':'#fffafa', + 'springgreen':'#00ff7f', + 'steelblue':'#4682b4', + 'tan':'#d2b48c', + 'teal':'#008080', + 'thistle':'#d8bfd8', + 'tomato':'#ff6347', + 'turquoise':'#40e0d0', + 'violet':'#ee82ee', + 'wheat':'#f5deb3', + 'white':'#ffffff', + 'whitesmoke':'#f5f5f5', + 'yellow':'#ffff00', + 'yellowgreen':'#9acd32' + }; +})(require('./tree')); + +(function (tree) { + +tree.debugInfo = function(env, ctx, lineSeperator) { + var result=""; + if (env.dumpLineNumbers && !env.compress) { + switch(env.dumpLineNumbers) { + case 'comments': + result = tree.debugInfo.asComment(ctx); + break; + case 'mediaquery': + result = tree.debugInfo.asMediaQuery(ctx); + break; + case 'all': + result = tree.debugInfo.asComment(ctx) + (lineSeperator || "") + tree.debugInfo.asMediaQuery(ctx); + break; + } + } + return result; +}; + +tree.debugInfo.asComment = function(ctx) { + return '/* line ' + ctx.debugInfo.lineNumber + ', ' + ctx.debugInfo.fileName + ' */\n'; +}; + +tree.debugInfo.asMediaQuery = function(ctx) { + return '@media -sass-debug-info{filename{font-family:' + + ('file://' + ctx.debugInfo.fileName).replace(/([.:/\\])/g, function (a) { + if (a == '\\') { + a = '\/'; + } + return '\\' + a; + }) + + '}line{font-family:\\00003' + ctx.debugInfo.lineNumber + '}}\n'; +}; + +tree.find = function (obj, fun) { + for (var i = 0, r; i < obj.length; i++) { + if (r = fun.call(obj, obj[i])) { return r; } + } + return null; +}; + +tree.jsify = function (obj) { + if (Array.isArray(obj.value) && (obj.value.length > 1)) { + return '[' + obj.value.map(function (v) { return v.toCSS(false); }).join(', ') + ']'; + } else { + return obj.toCSS(false); + } +}; + +tree.toCSS = function (env) { + var strs = []; + this.genCSS(env, { + add: function(chunk, fileInfo, index) { + strs.push(chunk); + }, + isEmpty: function () { + return strs.length === 0; + } + }); + return strs.join(''); +}; + +tree.outputRuleset = function (env, output, rules) { + output.add((env.compress ? '{' : ' {\n')); + env.tabLevel = (env.tabLevel || 0) + 1; + var tabRuleStr = env.compress ? '' : Array(env.tabLevel + 1).join(" "), + tabSetStr = env.compress ? '' : Array(env.tabLevel).join(" "); + for(var i = 0; i < rules.length; i++) { + output.add(tabRuleStr); + rules[i].genCSS(env, output); + output.add(env.compress ? '' : '\n'); + } + env.tabLevel--; + output.add(tabSetStr + "}"); +}; + +})(require('./tree')); + +(function (tree) { + +tree.Alpha = function (val) { + this.value = val; +}; +tree.Alpha.prototype = { + type: "Alpha", + accept: function (visitor) { + this.value = visitor.visit(this.value); + }, + eval: function (env) { + if (this.value.eval) { return new tree.Alpha(this.value.eval(env)); } + return this; + }, + genCSS: function (env, output) { + output.add("alpha(opacity="); + + if (this.value.genCSS) { + this.value.genCSS(env, output); + } else { + output.add(this.value); + } + + output.add(")"); + }, + toCSS: tree.toCSS +}; + +})(require('../tree')); + +(function (tree) { + +tree.Anonymous = function (string, index, currentFileInfo, mapLines) { + this.value = string.value || string; + this.index = index; + this.mapLines = mapLines; + this.currentFileInfo = currentFileInfo; +}; +tree.Anonymous.prototype = { + type: "Anonymous", + eval: function () { return this; }, + compare: function (x) { + if (!x.toCSS) { + return -1; + } + + var left = this.toCSS(), + right = x.toCSS(); + + if (left === right) { + return 0; + } + + return left < right ? -1 : 1; + }, + genCSS: function (env, output) { + output.add(this.value, this.currentFileInfo, this.index, this.mapLines); + }, + toCSS: tree.toCSS +}; + +})(require('../tree')); + +(function (tree) { + +tree.Assignment = function (key, val) { + this.key = key; + this.value = val; +}; +tree.Assignment.prototype = { + type: "Assignment", + accept: function (visitor) { + this.value = visitor.visit(this.value); + }, + eval: function (env) { + if (this.value.eval) { + return new(tree.Assignment)(this.key, this.value.eval(env)); + } + return this; + }, + genCSS: function (env, output) { + output.add(this.key + '='); + if (this.value.genCSS) { + this.value.genCSS(env, output); + } else { + output.add(this.value); + } + }, + toCSS: tree.toCSS +}; + +})(require('../tree')); + +(function (tree) { + +// +// A function call node. +// +tree.Call = function (name, args, index, currentFileInfo) { + this.name = name; + this.args = args; + this.index = index; + this.currentFileInfo = currentFileInfo; +}; +tree.Call.prototype = { + type: "Call", + accept: function (visitor) { + this.args = visitor.visit(this.args); + }, + // + // When evaluating a function call, + // we either find the function in `tree.functions` [1], + // in which case we call it, passing the evaluated arguments, + // if this returns null or we cannot find the function, we + // simply print it out as it appeared originally [2]. + // + // The *functions.js* file contains the built-in functions. + // + // The reason why we evaluate the arguments, is in the case where + // we try to pass a variable to a function, like: `saturate(@color)`. + // The function should receive the value, not the variable. + // + eval: function (env) { + var args = this.args.map(function (a) { return a.eval(env); }), + nameLC = this.name.toLowerCase(), + result, func; + + if (nameLC in tree.functions) { // 1. + try { + func = new tree.functionCall(env, this.currentFileInfo); + result = func[nameLC].apply(func, args); + /*jshint eqnull:true */ + if (result != null) { + return result; + } + } catch (e) { + throw { type: e.type || "Runtime", + message: "error evaluating function `" + this.name + "`" + + (e.message ? ': ' + e.message : ''), + index: this.index, filename: this.currentFileInfo.filename }; + } + } + + return new tree.Call(this.name, args, this.index, this.currentFileInfo); + }, + + genCSS: function (env, output) { + output.add(this.name + "(", this.currentFileInfo, this.index); + + for(var i = 0; i < this.args.length; i++) { + this.args[i].genCSS(env, output); + if (i + 1 < this.args.length) { + output.add(", "); + } + } + + output.add(")"); + }, + + toCSS: tree.toCSS +}; + +})(require('../tree')); + +(function (tree) { +// +// RGB Colors - #ff0014, #eee +// +tree.Color = function (rgb, a) { + // + // The end goal here, is to parse the arguments + // into an integer triplet, such as `128, 255, 0` + // + // This facilitates operations and conversions. + // + if (Array.isArray(rgb)) { + this.rgb = rgb; + } else if (rgb.length == 6) { + this.rgb = rgb.match(/.{2}/g).map(function (c) { + return parseInt(c, 16); + }); + } else { + this.rgb = rgb.split('').map(function (c) { + return parseInt(c + c, 16); + }); + } + this.alpha = typeof(a) === 'number' ? a : 1; +}; + +var transparentKeyword = "transparent"; + +tree.Color.prototype = { + type: "Color", + eval: function () { return this; }, + luma: function () { return (0.2126 * this.rgb[0] / 255) + (0.7152 * this.rgb[1] / 255) + (0.0722 * this.rgb[2] / 255); }, + + genCSS: function (env, output) { + output.add(this.toCSS(env)); + }, + toCSS: function (env, doNotCompress) { + var compress = env && env.compress && !doNotCompress; + + // If we have some transparency, the only way to represent it + // is via `rgba`. Otherwise, we use the hex representation, + // which has better compatibility with older browsers. + // Values are capped between `0` and `255`, rounded and zero-padded. + if (this.alpha < 1.0) { + if (this.alpha === 0 && this.isTransparentKeyword) { + return transparentKeyword; + } + return "rgba(" + this.rgb.map(function (c) { + return Math.round(c); + }).concat(this.alpha).join(',' + (compress ? '' : ' ')) + ")"; + } else { + var color = this.toRGB(); + + if (compress) { + var splitcolor = color.split(''); + + // Convert color to short format + if (splitcolor[1] === splitcolor[2] && splitcolor[3] === splitcolor[4] && splitcolor[5] === splitcolor[6]) { + color = '#' + splitcolor[1] + splitcolor[3] + splitcolor[5]; + } + } + + return color; + } + }, + + // + // Operations have to be done per-channel, if not, + // channels will spill onto each other. Once we have + // our result, in the form of an integer triplet, + // we create a new Color node to hold the result. + // + operate: function (env, op, other) { + var rgb = []; + var alpha = this.alpha * (1 - other.alpha) + other.alpha; + for (var c = 0; c < 3; c++) { + rgb[c] = tree.operate(env, op, this.rgb[c], other.rgb[c]); + } + return new(tree.Color)(rgb, alpha); + }, + + toRGB: function () { + return '#' + this.rgb.map(function (i) { + i = Math.round(i); + i = (i > 255 ? 255 : (i < 0 ? 0 : i)).toString(16); + return i.length === 1 ? '0' + i : i; + }).join(''); + }, + + toHSL: function () { + var r = this.rgb[0] / 255, + g = this.rgb[1] / 255, + b = this.rgb[2] / 255, + a = this.alpha; + + var max = Math.max(r, g, b), min = Math.min(r, g, b); + var h, s, l = (max + min) / 2, d = max - min; + + if (max === min) { + h = s = 0; + } else { + s = l > 0.5 ? d / (2 - max - min) : d / (max + min); + + switch (max) { + case r: h = (g - b) / d + (g < b ? 6 : 0); break; + case g: h = (b - r) / d + 2; break; + case b: h = (r - g) / d + 4; break; + } + h /= 6; + } + return { h: h * 360, s: s, l: l, a: a }; + }, + //Adapted from http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript + toHSV: function () { + var r = this.rgb[0] / 255, + g = this.rgb[1] / 255, + b = this.rgb[2] / 255, + a = this.alpha; + + var max = Math.max(r, g, b), min = Math.min(r, g, b); + var h, s, v = max; + + var d = max - min; + if (max === 0) { + s = 0; + } else { + s = d / max; + } + + if (max === min) { + h = 0; + } else { + switch(max){ + case r: h = (g - b) / d + (g < b ? 6 : 0); break; + case g: h = (b - r) / d + 2; break; + case b: h = (r - g) / d + 4; break; + } + h /= 6; + } + return { h: h * 360, s: s, v: v, a: a }; + }, + toARGB: function () { + var argb = [Math.round(this.alpha * 255)].concat(this.rgb); + return '#' + argb.map(function (i) { + i = Math.round(i); + i = (i > 255 ? 255 : (i < 0 ? 0 : i)).toString(16); + return i.length === 1 ? '0' + i : i; + }).join(''); + }, + compare: function (x) { + if (!x.rgb) { + return -1; + } + + return (x.rgb[0] === this.rgb[0] && + x.rgb[1] === this.rgb[1] && + x.rgb[2] === this.rgb[2] && + x.alpha === this.alpha) ? 0 : -1; + } +}; + +tree.Color.fromKeyword = function(keyword) { + if (tree.colors.hasOwnProperty(keyword)) { + // detect named color + return new(tree.Color)(tree.colors[keyword].slice(1)); + } + if (keyword === transparentKeyword) { + var transparent = new(tree.Color)([0, 0, 0], 0); + transparent.isTransparentKeyword = true; + return transparent; + } +}; + + +})(require('../tree')); + +(function (tree) { + +tree.Comment = function (value, silent, index, currentFileInfo) { + this.value = value; + this.silent = !!silent; + this.currentFileInfo = currentFileInfo; +}; +tree.Comment.prototype = { + type: "Comment", + genCSS: function (env, output) { + if (this.debugInfo) { + output.add(tree.debugInfo(env, this), this.currentFileInfo, this.index); + } + output.add(this.value.trim()); //TODO shouldn't need to trim, we shouldn't grab the \n + }, + toCSS: tree.toCSS, + isSilent: function(env) { + var isReference = (this.currentFileInfo && this.currentFileInfo.reference && !this.isReferenced), + isCompressed = env.compress && !this.value.match(/^\/\*!/); + return this.silent || isReference || isCompressed; + }, + eval: function () { return this; }, + markReferenced: function () { + this.isReferenced = true; + } +}; + +})(require('../tree')); + +(function (tree) { + +tree.Condition = function (op, l, r, i, negate) { + this.op = op.trim(); + this.lvalue = l; + this.rvalue = r; + this.index = i; + this.negate = negate; +}; +tree.Condition.prototype = { + type: "Condition", + accept: function (visitor) { + this.lvalue = visitor.visit(this.lvalue); + this.rvalue = visitor.visit(this.rvalue); + }, + eval: function (env) { + var a = this.lvalue.eval(env), + b = this.rvalue.eval(env); + + var i = this.index, result; + + result = (function (op) { + switch (op) { + case 'and': + return a && b; + case 'or': + return a || b; + default: + if (a.compare) { + result = a.compare(b); + } else if (b.compare) { + result = b.compare(a); + } else { + throw { type: "Type", + message: "Unable to perform comparison", + index: i }; + } + switch (result) { + case -1: return op === '<' || op === '=<' || op === '<='; + case 0: return op === '=' || op === '>=' || op === '=<' || op === '<='; + case 1: return op === '>' || op === '>='; + } + } + })(this.op); + return this.negate ? !result : result; + } +}; + +})(require('../tree')); + +(function (tree) { + +// +// A number with a unit +// +tree.Dimension = function (value, unit) { + this.value = parseFloat(value); + this.unit = (unit && unit instanceof tree.Unit) ? unit : + new(tree.Unit)(unit ? [unit] : undefined); +}; + +tree.Dimension.prototype = { + type: "Dimension", + accept: function (visitor) { + this.unit = visitor.visit(this.unit); + }, + eval: function (env) { + return this; + }, + toColor: function () { + return new(tree.Color)([this.value, this.value, this.value]); + }, + genCSS: function (env, output) { + if ((env && env.strictUnits) && !this.unit.isSingular()) { + throw new Error("Multiple units in dimension. Correct the units or use the unit function. Bad unit: "+this.unit.toString()); + } + + var value = this.value, + strValue = String(value); + + if (value !== 0 && value < 0.000001 && value > -0.000001) { + // would be output 1e-6 etc. + strValue = value.toFixed(20).replace(/0+$/, ""); + } + + if (env && env.compress) { + // Zero values doesn't need a unit + if (value === 0 && this.unit.isLength()) { + output.add(strValue); + return; + } + + // Float values doesn't need a leading zero + if (value > 0 && value < 1) { + strValue = (strValue).substr(1); + } + } + + output.add(strValue); + this.unit.genCSS(env, output); + }, + toCSS: tree.toCSS, + + // In an operation between two Dimensions, + // we default to the first Dimension's unit, + // so `1px + 2` will yield `3px`. + operate: function (env, op, other) { + /*jshint noempty:false */ + var value = tree.operate(env, op, this.value, other.value), + unit = this.unit.clone(); + + if (op === '+' || op === '-') { + if (unit.numerator.length === 0 && unit.denominator.length === 0) { + unit.numerator = other.unit.numerator.slice(0); + unit.denominator = other.unit.denominator.slice(0); + } else if (other.unit.numerator.length === 0 && unit.denominator.length === 0) { + // do nothing + } else { + other = other.convertTo(this.unit.usedUnits()); + + if(env.strictUnits && other.unit.toString() !== unit.toString()) { + throw new Error("Incompatible units. Change the units or use the unit function. Bad units: '" + unit.toString() + + "' and '" + other.unit.toString() + "'."); + } + + value = tree.operate(env, op, this.value, other.value); + } + } else if (op === '*') { + unit.numerator = unit.numerator.concat(other.unit.numerator).sort(); + unit.denominator = unit.denominator.concat(other.unit.denominator).sort(); + unit.cancel(); + } else if (op === '/') { + unit.numerator = unit.numerator.concat(other.unit.denominator).sort(); + unit.denominator = unit.denominator.concat(other.unit.numerator).sort(); + unit.cancel(); + } + return new(tree.Dimension)(value, unit); + }, + + compare: function (other) { + if (other instanceof tree.Dimension) { + var a = this.unify(), b = other.unify(), + aValue = a.value, bValue = b.value; + + if (bValue > aValue) { + return -1; + } else if (bValue < aValue) { + return 1; + } else { + if (!b.unit.isEmpty() && a.unit.compare(b.unit) !== 0) { + return -1; + } + return 0; + } + } else { + return -1; + } + }, + + unify: function () { + return this.convertTo({ length: 'm', duration: 's', angle: 'rad' }); + }, + + convertTo: function (conversions) { + var value = this.value, unit = this.unit.clone(), + i, groupName, group, targetUnit, derivedConversions = {}, applyUnit; + + if (typeof conversions === 'string') { + for(i in tree.UnitConversions) { + if (tree.UnitConversions[i].hasOwnProperty(conversions)) { + derivedConversions = {}; + derivedConversions[i] = conversions; + } + } + conversions = derivedConversions; + } + applyUnit = function (atomicUnit, denominator) { + /*jshint loopfunc:true */ + if (group.hasOwnProperty(atomicUnit)) { + if (denominator) { + value = value / (group[atomicUnit] / group[targetUnit]); + } else { + value = value * (group[atomicUnit] / group[targetUnit]); + } + + return targetUnit; + } + + return atomicUnit; + }; + + for (groupName in conversions) { + if (conversions.hasOwnProperty(groupName)) { + targetUnit = conversions[groupName]; + group = tree.UnitConversions[groupName]; + + unit.map(applyUnit); + } + } + + unit.cancel(); + + return new(tree.Dimension)(value, unit); + } +}; + +// http://www.w3.org/TR/css3-values/#absolute-lengths +tree.UnitConversions = { + length: { + 'm': 1, + 'cm': 0.01, + 'mm': 0.001, + 'in': 0.0254, + 'pt': 0.0254 / 72, + 'pc': 0.0254 / 72 * 12 + }, + duration: { + 's': 1, + 'ms': 0.001 + }, + angle: { + 'rad': 1/(2*Math.PI), + 'deg': 1/360, + 'grad': 1/400, + 'turn': 1 + } +}; + +tree.Unit = function (numerator, denominator, backupUnit) { + this.numerator = numerator ? numerator.slice(0).sort() : []; + this.denominator = denominator ? denominator.slice(0).sort() : []; + this.backupUnit = backupUnit; +}; + +tree.Unit.prototype = { + type: "Unit", + clone: function () { + return new tree.Unit(this.numerator.slice(0), this.denominator.slice(0), this.backupUnit); + }, + genCSS: function (env, output) { + if (this.numerator.length >= 1) { + output.add(this.numerator[0]); + } else + if (this.denominator.length >= 1) { + output.add(this.denominator[0]); + } else + if ((!env || !env.strictUnits) && this.backupUnit) { + output.add(this.backupUnit); + } + }, + toCSS: tree.toCSS, + + toString: function () { + var i, returnStr = this.numerator.join("*"); + for (i = 0; i < this.denominator.length; i++) { + returnStr += "/" + this.denominator[i]; + } + return returnStr; + }, + + compare: function (other) { + return this.is(other.toString()) ? 0 : -1; + }, + + is: function (unitString) { + return this.toString() === unitString; + }, + + isLength: function () { + return Boolean(this.toCSS().match(/px|em|%|in|cm|mm|pc|pt|ex/)); + }, + + isEmpty: function () { + return this.numerator.length === 0 && this.denominator.length === 0; + }, + + isSingular: function() { + return this.numerator.length <= 1 && this.denominator.length === 0; + }, + + map: function(callback) { + var i; + + for (i = 0; i < this.numerator.length; i++) { + this.numerator[i] = callback(this.numerator[i], false); + } + + for (i = 0; i < this.denominator.length; i++) { + this.denominator[i] = callback(this.denominator[i], true); + } + }, + + usedUnits: function() { + var group, result = {}, mapUnit; + + mapUnit = function (atomicUnit) { + /*jshint loopfunc:true */ + if (group.hasOwnProperty(atomicUnit) && !result[groupName]) { + result[groupName] = atomicUnit; + } + + return atomicUnit; + }; + + for (var groupName in tree.UnitConversions) { + if (tree.UnitConversions.hasOwnProperty(groupName)) { + group = tree.UnitConversions[groupName]; + + this.map(mapUnit); + } + } + + return result; + }, + + cancel: function () { + var counter = {}, atomicUnit, i, backup; + + for (i = 0; i < this.numerator.length; i++) { + atomicUnit = this.numerator[i]; + if (!backup) { + backup = atomicUnit; + } + counter[atomicUnit] = (counter[atomicUnit] || 0) + 1; + } + + for (i = 0; i < this.denominator.length; i++) { + atomicUnit = this.denominator[i]; + if (!backup) { + backup = atomicUnit; + } + counter[atomicUnit] = (counter[atomicUnit] || 0) - 1; + } + + this.numerator = []; + this.denominator = []; + + for (atomicUnit in counter) { + if (counter.hasOwnProperty(atomicUnit)) { + var count = counter[atomicUnit]; + + if (count > 0) { + for (i = 0; i < count; i++) { + this.numerator.push(atomicUnit); + } + } else if (count < 0) { + for (i = 0; i < -count; i++) { + this.denominator.push(atomicUnit); + } + } + } + } + + if (this.numerator.length === 0 && this.denominator.length === 0 && backup) { + this.backupUnit = backup; + } + + this.numerator.sort(); + this.denominator.sort(); + } +}; + +})(require('../tree')); + +(function (tree) { + +tree.Directive = function (name, value, index, currentFileInfo) { + this.name = name; + + if (Array.isArray(value)) { + this.rules = [new(tree.Ruleset)([], value)]; + this.rules[0].allowImports = true; + } else { + this.value = value; + } + this.currentFileInfo = currentFileInfo; + +}; +tree.Directive.prototype = { + type: "Directive", + accept: function (visitor) { + this.rules = visitor.visit(this.rules); + this.value = visitor.visit(this.value); + }, + genCSS: function (env, output) { + output.add(this.name, this.currentFileInfo, this.index); + if (this.rules) { + tree.outputRuleset(env, output, this.rules); + } else { + output.add(' '); + this.value.genCSS(env, output); + output.add(';'); + } + }, + toCSS: tree.toCSS, + eval: function (env) { + var evaldDirective = this; + if (this.rules) { + env.frames.unshift(this); + evaldDirective = new(tree.Directive)(this.name, null, this.index, this.currentFileInfo); + evaldDirective.rules = [this.rules[0].eval(env)]; + evaldDirective.rules[0].root = true; + env.frames.shift(); + } + return evaldDirective; + }, + variable: function (name) { return tree.Ruleset.prototype.variable.call(this.rules[0], name); }, + find: function () { return tree.Ruleset.prototype.find.apply(this.rules[0], arguments); }, + rulesets: function () { return tree.Ruleset.prototype.rulesets.apply(this.rules[0]); }, + markReferenced: function () { + var i, rules; + this.isReferenced = true; + if (this.rules) { + rules = this.rules[0].rules; + for (i = 0; i < rules.length; i++) { + if (rules[i].markReferenced) { + rules[i].markReferenced(); + } + } + } + } +}; + +})(require('../tree')); + +(function (tree) { + +tree.Element = function (combinator, value, index, currentFileInfo) { + this.combinator = combinator instanceof tree.Combinator ? + combinator : new(tree.Combinator)(combinator); + + if (typeof(value) === 'string') { + this.value = value.trim(); + } else if (value) { + this.value = value; + } else { + this.value = ""; + } + this.index = index; + this.currentFileInfo = currentFileInfo; +}; +tree.Element.prototype = { + type: "Element", + accept: function (visitor) { + this.combinator = visitor.visit(this.combinator); + this.value = visitor.visit(this.value); + }, + eval: function (env) { + return new(tree.Element)(this.combinator, + this.value.eval ? this.value.eval(env) : this.value, + this.index, + this.currentFileInfo); + }, + genCSS: function (env, output) { + output.add(this.toCSS(env), this.currentFileInfo, this.index); + }, + toCSS: function (env) { + var value = (this.value.toCSS ? this.value.toCSS(env) : this.value); + if (value === '' && this.combinator.value.charAt(0) === '&') { + return ''; + } else { + return this.combinator.toCSS(env || {}) + value; + } + } +}; + +tree.Attribute = function (key, op, value) { + this.key = key; + this.op = op; + this.value = value; +}; +tree.Attribute.prototype = { + type: "Attribute", + accept: function (visitor) { + this.value = visitor.visit(this.value); + }, + eval: function (env) { + return new(tree.Attribute)(this.key.eval ? this.key.eval(env) : this.key, + this.op, (this.value && this.value.eval) ? this.value.eval(env) : this.value); + }, + genCSS: function (env, output) { + output.add(this.toCSS(env)); + }, + toCSS: function (env) { + var value = this.key.toCSS ? this.key.toCSS(env) : this.key; + + if (this.op) { + value += this.op; + value += (this.value.toCSS ? this.value.toCSS(env) : this.value); + } + + return '[' + value + ']'; + } +}; + +tree.Combinator = function (value) { + if (value === ' ') { + this.value = ' '; + } else { + this.value = value ? value.trim() : ""; + } +}; +tree.Combinator.prototype = { + type: "Combinator", + _outputMap: { + '' : '', + ' ' : ' ', + ':' : ' :', + '+' : ' + ', + '~' : ' ~ ', + '>' : ' > ', + '|' : '|' + }, + _outputMapCompressed: { + '' : '', + ' ' : ' ', + ':' : ' :', + '+' : '+', + '~' : '~', + '>' : '>', + '|' : '|' + }, + genCSS: function (env, output) { + output.add((env.compress ? this._outputMapCompressed : this._outputMap)[this.value]); + }, + toCSS: tree.toCSS +}; + +})(require('../tree')); + +(function (tree) { + +tree.Expression = function (value) { this.value = value; }; +tree.Expression.prototype = { + type: "Expression", + accept: function (visitor) { + this.value = visitor.visit(this.value); + }, + eval: function (env) { + var returnValue, + inParenthesis = this.parens && !this.parensInOp, + doubleParen = false; + if (inParenthesis) { + env.inParenthesis(); + } + if (this.value.length > 1) { + returnValue = new(tree.Expression)(this.value.map(function (e) { + return e.eval(env); + })); + } else if (this.value.length === 1) { + if (this.value[0].parens && !this.value[0].parensInOp) { + doubleParen = true; + } + returnValue = this.value[0].eval(env); + } else { + returnValue = this; + } + if (inParenthesis) { + env.outOfParenthesis(); + } + if (this.parens && this.parensInOp && !(env.isMathOn()) && !doubleParen) { + returnValue = new(tree.Paren)(returnValue); + } + return returnValue; + }, + genCSS: function (env, output) { + for(var i = 0; i < this.value.length; i++) { + this.value[i].genCSS(env, output); + if (i + 1 < this.value.length) { + output.add(" "); + } + } + }, + toCSS: tree.toCSS, + throwAwayComments: function () { + this.value = this.value.filter(function(v) { + return !(v instanceof tree.Comment); + }); + } +}; + +})(require('../tree')); + +(function (tree) { + +tree.Extend = function Extend(selector, option, index) { + this.selector = selector; + this.option = option; + this.index = index; + + switch(option) { + case "all": + this.allowBefore = true; + this.allowAfter = true; + break; + default: + this.allowBefore = false; + this.allowAfter = false; + break; + } +}; + +tree.Extend.prototype = { + type: "Extend", + accept: function (visitor) { + this.selector = visitor.visit(this.selector); + }, + eval: function (env) { + return new(tree.Extend)(this.selector.eval(env), this.option, this.index); + }, + clone: function (env) { + return new(tree.Extend)(this.selector, this.option, this.index); + }, + findSelfSelectors: function (selectors) { + var selfElements = [], + i, + selectorElements; + + for(i = 0; i < selectors.length; i++) { + selectorElements = selectors[i].elements; + // duplicate the logic in genCSS function inside the selector node. + // future TODO - move both logics into the selector joiner visitor + if (i > 0 && selectorElements.length && selectorElements[0].combinator.value === "") { + selectorElements[0].combinator.value = ' '; + } + selfElements = selfElements.concat(selectors[i].elements); + } + + this.selfSelectors = [{ elements: selfElements }]; + } +}; + +})(require('../tree')); + +(function (tree) { +// +// CSS @import node +// +// The general strategy here is that we don't want to wait +// for the parsing to be completed, before we start importing +// the file. That's because in the context of a browser, +// most of the time will be spent waiting for the server to respond. +// +// On creation, we push the import path to our import queue, though +// `import,push`, we also pass it a callback, which it'll call once +// the file has been fetched, and parsed. +// +tree.Import = function (path, features, options, index, currentFileInfo) { + this.options = options; + this.index = index; + this.path = path; + this.features = features; + this.currentFileInfo = currentFileInfo; + + if (this.options.less !== undefined || this.options.inline) { + this.css = !this.options.less || this.options.inline; + } else { + var pathValue = this.getPath(); + if (pathValue && /css([\?;].*)?$/.test(pathValue)) { + this.css = true; + } + } +}; + +// +// The actual import node doesn't return anything, when converted to CSS. +// The reason is that it's used at the evaluation stage, so that the rules +// it imports can be treated like any other rules. +// +// In `eval`, we make sure all Import nodes get evaluated, recursively, so +// we end up with a flat structure, which can easily be imported in the parent +// ruleset. +// +tree.Import.prototype = { + type: "Import", + accept: function (visitor) { + this.features = visitor.visit(this.features); + this.path = visitor.visit(this.path); + if (!this.options.inline) { + this.root = visitor.visit(this.root); + } + }, + genCSS: function (env, output) { + if (this.css) { + output.add("@import ", this.currentFileInfo, this.index); + this.path.genCSS(env, output); + if (this.features) { + output.add(" "); + this.features.genCSS(env, output); + } + output.add(';'); + } + }, + toCSS: tree.toCSS, + getPath: function () { + if (this.path instanceof tree.Quoted) { + var path = this.path.value; + return (this.css !== undefined || /(\.[a-z]*$)|([\?;].*)$/.test(path)) ? path : path + '.less'; + } else if (this.path instanceof tree.URL) { + return this.path.value.value; + } + return null; + }, + evalForImport: function (env) { + return new(tree.Import)(this.path.eval(env), this.features, this.options, this.index, this.currentFileInfo); + }, + evalPath: function (env) { + var path = this.path.eval(env); + var rootpath = this.currentFileInfo && this.currentFileInfo.rootpath; + + if (!(path instanceof tree.URL)) { + if (rootpath) { + var pathValue = path.value; + // Add the base path if the import is relative + if (pathValue && env.isPathRelative(pathValue)) { + path.value = rootpath +pathValue; + } + } + path.value = env.normalizePath(path.value); + } + + return path; + }, + eval: function (env) { + var ruleset, features = this.features && this.features.eval(env); + + if (this.skip) { return []; } + + if (this.options.inline) { + //todo needs to reference css file not import + var contents = new(tree.Anonymous)(this.root, 0, {filename: this.importedFilename}, true); + return this.features ? new(tree.Media)([contents], this.features.value) : [contents]; + } else if (this.css) { + var newImport = new(tree.Import)(this.evalPath(env), features, this.options, this.index); + if (!newImport.css && this.error) { + throw this.error; + } + return newImport; + } else { + ruleset = new(tree.Ruleset)([], this.root.rules.slice(0)); + + ruleset.evalImports(env); + + return this.features ? new(tree.Media)(ruleset.rules, this.features.value) : ruleset.rules; + } + } +}; + +})(require('../tree')); + +(function (tree) { + +tree.JavaScript = function (string, index, escaped) { + this.escaped = escaped; + this.expression = string; + this.index = index; +}; +tree.JavaScript.prototype = { + type: "JavaScript", + eval: function (env) { + var result, + that = this, + context = {}; + + var expression = this.expression.replace(/@\{([\w-]+)\}/g, function (_, name) { + return tree.jsify(new(tree.Variable)('@' + name, that.index).eval(env)); + }); + + try { + expression = new(Function)('return (' + expression + ')'); + } catch (e) { + throw { message: "JavaScript evaluation error: " + e.message + " from `" + expression + "`" , + index: this.index }; + } + + for (var k in env.frames[0].variables()) { + /*jshint loopfunc:true */ + context[k.slice(1)] = { + value: env.frames[0].variables()[k].value, + toJS: function () { + return this.value.eval(env).toCSS(); + } + }; + } + + try { + result = expression.call(context); + } catch (e) { + throw { message: "JavaScript evaluation error: '" + e.name + ': ' + e.message.replace(/["]/g, "'") + "'" , + index: this.index }; + } + if (typeof(result) === 'string') { + return new(tree.Quoted)('"' + result + '"', result, this.escaped, this.index); + } else if (Array.isArray(result)) { + return new(tree.Anonymous)(result.join(', ')); + } else { + return new(tree.Anonymous)(result); + } + } +}; + +})(require('../tree')); + + +(function (tree) { + +tree.Keyword = function (value) { this.value = value; }; +tree.Keyword.prototype = { + type: "Keyword", + eval: function () { return this; }, + genCSS: function (env, output) { + output.add(this.value); + }, + toCSS: tree.toCSS, + compare: function (other) { + if (other instanceof tree.Keyword) { + return other.value === this.value ? 0 : 1; + } else { + return -1; + } + } +}; + +tree.True = new(tree.Keyword)('true'); +tree.False = new(tree.Keyword)('false'); + +})(require('../tree')); + +(function (tree) { + +tree.Media = function (value, features, index, currentFileInfo) { + this.index = index; + this.currentFileInfo = currentFileInfo; + + var selectors = this.emptySelectors(); + + this.features = new(tree.Value)(features); + this.rules = [new(tree.Ruleset)(selectors, value)]; + this.rules[0].allowImports = true; +}; +tree.Media.prototype = { + type: "Media", + accept: function (visitor) { + this.features = visitor.visit(this.features); + this.rules = visitor.visit(this.rules); + }, + genCSS: function (env, output) { + output.add('@media ', this.currentFileInfo, this.index); + this.features.genCSS(env, output); + tree.outputRuleset(env, output, this.rules); + }, + toCSS: tree.toCSS, + eval: function (env) { + if (!env.mediaBlocks) { + env.mediaBlocks = []; + env.mediaPath = []; + } + + var media = new(tree.Media)([], [], this.index, this.currentFileInfo); + if(this.debugInfo) { + this.rules[0].debugInfo = this.debugInfo; + media.debugInfo = this.debugInfo; + } + var strictMathBypass = false; + if (!env.strictMath) { + strictMathBypass = true; + env.strictMath = true; + } + try { + media.features = this.features.eval(env); + } + finally { + if (strictMathBypass) { + env.strictMath = false; + } + } + + env.mediaPath.push(media); + env.mediaBlocks.push(media); + + env.frames.unshift(this.rules[0]); + media.rules = [this.rules[0].eval(env)]; + env.frames.shift(); + + env.mediaPath.pop(); + + return env.mediaPath.length === 0 ? media.evalTop(env) : + media.evalNested(env); + }, + variable: function (name) { return tree.Ruleset.prototype.variable.call(this.rules[0], name); }, + find: function () { return tree.Ruleset.prototype.find.apply(this.rules[0], arguments); }, + rulesets: function () { return tree.Ruleset.prototype.rulesets.apply(this.rules[0]); }, + emptySelectors: function() { + var el = new(tree.Element)('', '&', this.index, this.currentFileInfo); + return [new(tree.Selector)([el], null, null, this.index, this.currentFileInfo)]; + }, + markReferenced: function () { + var i, rules = this.rules[0].rules; + this.isReferenced = true; + for (i = 0; i < rules.length; i++) { + if (rules[i].markReferenced) { + rules[i].markReferenced(); + } + } + }, + + evalTop: function (env) { + var result = this; + + // Render all dependent Media blocks. + if (env.mediaBlocks.length > 1) { + var selectors = this.emptySelectors(); + result = new(tree.Ruleset)(selectors, env.mediaBlocks); + result.multiMedia = true; + } + + delete env.mediaBlocks; + delete env.mediaPath; + + return result; + }, + evalNested: function (env) { + var i, value, + path = env.mediaPath.concat([this]); + + // Extract the media-query conditions separated with `,` (OR). + for (i = 0; i < path.length; i++) { + value = path[i].features instanceof tree.Value ? + path[i].features.value : path[i].features; + path[i] = Array.isArray(value) ? value : [value]; + } + + // Trace all permutations to generate the resulting media-query. + // + // (a, b and c) with nested (d, e) -> + // a and d + // a and e + // b and c and d + // b and c and e + this.features = new(tree.Value)(this.permute(path).map(function (path) { + path = path.map(function (fragment) { + return fragment.toCSS ? fragment : new(tree.Anonymous)(fragment); + }); + + for(i = path.length - 1; i > 0; i--) { + path.splice(i, 0, new(tree.Anonymous)("and")); + } + + return new(tree.Expression)(path); + })); + + // Fake a tree-node that doesn't output anything. + return new(tree.Ruleset)([], []); + }, + permute: function (arr) { + if (arr.length === 0) { + return []; + } else if (arr.length === 1) { + return arr[0]; + } else { + var result = []; + var rest = this.permute(arr.slice(1)); + for (var i = 0; i < rest.length; i++) { + for (var j = 0; j < arr[0].length; j++) { + result.push([arr[0][j]].concat(rest[i])); + } + } + return result; + } + }, + bubbleSelectors: function (selectors) { + this.rules = [new(tree.Ruleset)(selectors.slice(0), [this.rules[0]])]; + } +}; + +})(require('../tree')); + +(function (tree) { + +tree.mixin = {}; +tree.mixin.Call = function (elements, args, index, currentFileInfo, important) { + this.selector = new(tree.Selector)(elements); + this.arguments = args; + this.index = index; + this.currentFileInfo = currentFileInfo; + this.important = important; +}; +tree.mixin.Call.prototype = { + type: "MixinCall", + accept: function (visitor) { + this.selector = visitor.visit(this.selector); + this.arguments = visitor.visit(this.arguments); + }, + eval: function (env) { + var mixins, mixin, args, rules = [], match = false, i, m, f, isRecursive, isOneFound, rule; + + args = this.arguments && this.arguments.map(function (a) { + return { name: a.name, value: a.value.eval(env) }; + }); + + for (i = 0; i < env.frames.length; i++) { + if ((mixins = env.frames[i].find(this.selector)).length > 0) { + isOneFound = true; + for (m = 0; m < mixins.length; m++) { + mixin = mixins[m]; + isRecursive = false; + for(f = 0; f < env.frames.length; f++) { + if ((!(mixin instanceof tree.mixin.Definition)) && mixin === (env.frames[f].originalRuleset || env.frames[f])) { + isRecursive = true; + break; + } + } + if (isRecursive) { + continue; + } + if (mixin.matchArgs(args, env)) { + if (!mixin.matchCondition || mixin.matchCondition(args, env)) { + try { + if (!(mixin instanceof tree.mixin.Definition)) { + mixin = new tree.mixin.Definition("", [], mixin.rules, null, false); + mixin.originalRuleset = mixins[m].originalRuleset || mixins[m]; + } + //if (this.important) { + // isImportant = env.isImportant; + // env.isImportant = true; + //} + Array.prototype.push.apply( + rules, mixin.eval(env, args, this.important).rules); + //if (this.important) { + // env.isImportant = isImportant; + //} + } catch (e) { + throw { message: e.message, index: this.index, filename: this.currentFileInfo.filename, stack: e.stack }; + } + } + match = true; + } + } + if (match) { + if (!this.currentFileInfo || !this.currentFileInfo.reference) { + for (i = 0; i < rules.length; i++) { + rule = rules[i]; + if (rule.markReferenced) { + rule.markReferenced(); + } + } + } + return rules; + } + } + } + if (isOneFound) { + throw { type: 'Runtime', + message: 'No matching definition was found for `' + + this.selector.toCSS().trim() + '(' + + (args ? args.map(function (a) { + var argValue = ""; + if (a.name) { + argValue += a.name + ":"; + } + if (a.value.toCSS) { + argValue += a.value.toCSS(); + } else { + argValue += "???"; + } + return argValue; + }).join(', ') : "") + ")`", + index: this.index, filename: this.currentFileInfo.filename }; + } else { + throw { type: 'Name', + message: this.selector.toCSS().trim() + " is undefined", + index: this.index, filename: this.currentFileInfo.filename }; + } + } +}; + +tree.mixin.Definition = function (name, params, rules, condition, variadic) { + this.name = name; + this.selectors = [new(tree.Selector)([new(tree.Element)(null, name, this.index, this.currentFileInfo)])]; + this.params = params; + this.condition = condition; + this.variadic = variadic; + this.arity = params.length; + this.rules = rules; + this._lookups = {}; + this.required = params.reduce(function (count, p) { + if (!p.name || (p.name && !p.value)) { return count + 1; } + else { return count; } + }, 0); + this.parent = tree.Ruleset.prototype; + this.frames = []; +}; +tree.mixin.Definition.prototype = { + type: "MixinDefinition", + accept: function (visitor) { + this.params = visitor.visit(this.params); + this.rules = visitor.visit(this.rules); + this.condition = visitor.visit(this.condition); + }, + variable: function (name) { return this.parent.variable.call(this, name); }, + variables: function () { return this.parent.variables.call(this); }, + find: function () { return this.parent.find.apply(this, arguments); }, + rulesets: function () { return this.parent.rulesets.apply(this); }, + + evalParams: function (env, mixinEnv, args, evaldArguments) { + /*jshint boss:true */ + var frame = new(tree.Ruleset)(null, []), + varargs, arg, + params = this.params.slice(0), + i, j, val, name, isNamedFound, argIndex; + + mixinEnv = new tree.evalEnv(mixinEnv, [frame].concat(mixinEnv.frames)); + + if (args) { + args = args.slice(0); + + for(i = 0; i < args.length; i++) { + arg = args[i]; + if (name = (arg && arg.name)) { + isNamedFound = false; + for(j = 0; j < params.length; j++) { + if (!evaldArguments[j] && name === params[j].name) { + evaldArguments[j] = arg.value.eval(env); + frame.rules.unshift(new(tree.Rule)(name, arg.value.eval(env))); + isNamedFound = true; + break; + } + } + if (isNamedFound) { + args.splice(i, 1); + i--; + continue; + } else { + throw { type: 'Runtime', message: "Named argument for " + this.name + + ' ' + args[i].name + ' not found' }; + } + } + } + } + argIndex = 0; + for (i = 0; i < params.length; i++) { + if (evaldArguments[i]) { continue; } + + arg = args && args[argIndex]; + + if (name = params[i].name) { + if (params[i].variadic && args) { + varargs = []; + for (j = argIndex; j < args.length; j++) { + varargs.push(args[j].value.eval(env)); + } + frame.rules.unshift(new(tree.Rule)(name, new(tree.Expression)(varargs).eval(env))); + } else { + val = arg && arg.value; + if (val) { + val = val.eval(env); + } else if (params[i].value) { + val = params[i].value.eval(mixinEnv); + frame.resetCache(); + } else { + throw { type: 'Runtime', message: "wrong number of arguments for " + this.name + + ' (' + args.length + ' for ' + this.arity + ')' }; + } + + frame.rules.unshift(new(tree.Rule)(name, val)); + evaldArguments[i] = val; + } + } + + if (params[i].variadic && args) { + for (j = argIndex; j < args.length; j++) { + evaldArguments[j] = args[j].value.eval(env); + } + } + argIndex++; + } + + return frame; + }, + eval: function (env, args, important) { + var _arguments = [], + mixinFrames = this.frames.concat(env.frames), + frame = this.evalParams(env, new(tree.evalEnv)(env, mixinFrames), args, _arguments), + rules, ruleset; + + frame.rules.unshift(new(tree.Rule)('@arguments', new(tree.Expression)(_arguments).eval(env))); + + rules = this.rules.slice(0); + + ruleset = new(tree.Ruleset)(null, rules); + ruleset.originalRuleset = this; + ruleset = ruleset.eval(new(tree.evalEnv)(env, [this, frame].concat(mixinFrames))); + if (important) { + ruleset = this.parent.makeImportant.apply(ruleset); + } + return ruleset; + }, + matchCondition: function (args, env) { + if (this.condition && !this.condition.eval( + new(tree.evalEnv)(env, + [this.evalParams(env, new(tree.evalEnv)(env, this.frames.concat(env.frames)), args, [])] // the parameter variables + .concat(this.frames) // the parent namespace/mixin frames + .concat(env.frames)))) { // the current environment frames + return false; + } + return true; + }, + matchArgs: function (args, env) { + var argsLength = (args && args.length) || 0, len; + + if (! this.variadic) { + if (argsLength < this.required) { return false; } + if (argsLength > this.params.length) { return false; } + } else { + if (argsLength < (this.required - 1)) { return false; } + } + + len = Math.min(argsLength, this.arity); + + for (var i = 0; i < len; i++) { + if (!this.params[i].name && !this.params[i].variadic) { + if (args[i].value.eval(env).toCSS() != this.params[i].value.eval(env).toCSS()) { + return false; + } + } + } + return true; + } +}; + +})(require('../tree')); + +(function (tree) { + +tree.Negative = function (node) { + this.value = node; +}; +tree.Negative.prototype = { + type: "Negative", + accept: function (visitor) { + this.value = visitor.visit(this.value); + }, + genCSS: function (env, output) { + output.add('-'); + this.value.genCSS(env, output); + }, + toCSS: tree.toCSS, + eval: function (env) { + if (env.isMathOn()) { + return (new(tree.Operation)('*', [new(tree.Dimension)(-1), this.value])).eval(env); + } + return new(tree.Negative)(this.value.eval(env)); + } +}; + +})(require('../tree')); + +(function (tree) { + +tree.Operation = function (op, operands, isSpaced) { + this.op = op.trim(); + this.operands = operands; + this.isSpaced = isSpaced; +}; +tree.Operation.prototype = { + type: "Operation", + accept: function (visitor) { + this.operands = visitor.visit(this.operands); + }, + eval: function (env) { + var a = this.operands[0].eval(env), + b = this.operands[1].eval(env); + + if (env.isMathOn()) { + if (a instanceof tree.Dimension && b instanceof tree.Color) { + a = a.toColor(); + } + if (b instanceof tree.Dimension && a instanceof tree.Color) { + b = b.toColor(); + } + if (!a.operate) { + throw { type: "Operation", + message: "Operation on an invalid type" }; + } + + return a.operate(env, this.op, b); + } else { + return new(tree.Operation)(this.op, [a, b], this.isSpaced); + } + }, + genCSS: function (env, output) { + this.operands[0].genCSS(env, output); + if (this.isSpaced) { + output.add(" "); + } + output.add(this.op); + if (this.isSpaced) { + output.add(" "); + } + this.operands[1].genCSS(env, output); + }, + toCSS: tree.toCSS +}; + +tree.operate = function (env, op, a, b) { + switch (op) { + case '+': return a + b; + case '-': return a - b; + case '*': return a * b; + case '/': return a / b; + } +}; + +})(require('../tree')); + + +(function (tree) { + +tree.Paren = function (node) { + this.value = node; +}; +tree.Paren.prototype = { + type: "Paren", + accept: function (visitor) { + this.value = visitor.visit(this.value); + }, + genCSS: function (env, output) { + output.add('('); + this.value.genCSS(env, output); + output.add(')'); + }, + toCSS: tree.toCSS, + eval: function (env) { + return new(tree.Paren)(this.value.eval(env)); + } +}; + +})(require('../tree')); + +(function (tree) { + +tree.Quoted = function (str, content, escaped, index, currentFileInfo) { + this.escaped = escaped; + this.value = content || ''; + this.quote = str.charAt(0); + this.index = index; + this.currentFileInfo = currentFileInfo; +}; +tree.Quoted.prototype = { + type: "Quoted", + genCSS: function (env, output) { + if (!this.escaped) { + output.add(this.quote, this.currentFileInfo, this.index); + } + output.add(this.value); + if (!this.escaped) { + output.add(this.quote); + } + }, + toCSS: tree.toCSS, + eval: function (env) { + var that = this; + var value = this.value.replace(/`([^`]+)`/g, function (_, exp) { + return new(tree.JavaScript)(exp, that.index, true).eval(env).value; + }).replace(/@\{([\w-]+)\}/g, function (_, name) { + var v = new(tree.Variable)('@' + name, that.index, that.currentFileInfo).eval(env, true); + return (v instanceof tree.Quoted) ? v.value : v.toCSS(); + }); + return new(tree.Quoted)(this.quote + value + this.quote, value, this.escaped, this.index, this.currentFileInfo); + }, + compare: function (x) { + if (!x.toCSS) { + return -1; + } + + var left = this.toCSS(), + right = x.toCSS(); + + if (left === right) { + return 0; + } + + return left < right ? -1 : 1; + } +}; + +})(require('../tree')); + +(function (tree) { + +tree.Rule = function (name, value, important, merge, index, currentFileInfo, inline) { + this.name = name; + this.value = (value instanceof tree.Value) ? value : new(tree.Value)([value]); + this.important = important ? ' ' + important.trim() : ''; + this.merge = merge; + this.index = index; + this.currentFileInfo = currentFileInfo; + this.inline = inline || false; + this.variable = (name.charAt(0) === '@'); +}; + +tree.Rule.prototype = { + type: "Rule", + accept: function (visitor) { + this.value = visitor.visit(this.value); + }, + genCSS: function (env, output) { + output.add(this.name + (env.compress ? ':' : ': '), this.currentFileInfo, this.index); + try { + this.value.genCSS(env, output); + } + catch(e) { + e.index = this.index; + e.filename = this.currentFileInfo.filename; + throw e; + } + output.add(this.important + ((this.inline || (env.lastRule && env.compress)) ? "" : ";"), this.currentFileInfo, this.index); + }, + toCSS: tree.toCSS, + eval: function (env) { + var strictMathBypass = false; + if (this.name === "font" && !env.strictMath) { + strictMathBypass = true; + env.strictMath = true; + } + try { + return new(tree.Rule)(this.name, + this.value.eval(env), + this.important, + this.merge, + this.index, this.currentFileInfo, this.inline); + } + catch(e) { + if (e.index === undefined) { + e.index = this.index; + } + throw e; + } + finally { + if (strictMathBypass) { + env.strictMath = false; + } + } + }, + makeImportant: function () { + return new(tree.Rule)(this.name, + this.value, + "!important", + this.merge, + this.index, this.currentFileInfo, this.inline); + } +}; + +})(require('../tree')); + +(function (tree) { + +tree.Ruleset = function (selectors, rules, strictImports) { + this.selectors = selectors; + this.rules = rules; + this._lookups = {}; + this.strictImports = strictImports; +}; +tree.Ruleset.prototype = { + type: "Ruleset", + accept: function (visitor) { + if (this.paths) { + for(var i = 0; i < this.paths.length; i++) { + this.paths[i] = visitor.visit(this.paths[i]); + } + } else { + this.selectors = visitor.visit(this.selectors); + } + this.rules = visitor.visit(this.rules); + }, + eval: function (env) { + var selectors = this.selectors && this.selectors.map(function (s) { return s.eval(env); }); + var ruleset = new(tree.Ruleset)(selectors, this.rules.slice(0), this.strictImports); + var rules; + var rule; + var i; + + ruleset.originalRuleset = this; + ruleset.root = this.root; + ruleset.firstRoot = this.firstRoot; + ruleset.allowImports = this.allowImports; + + if(this.debugInfo) { + ruleset.debugInfo = this.debugInfo; + } + + // push the current ruleset to the frames stack + env.frames.unshift(ruleset); + + // currrent selectors + if (!env.selectors) { + env.selectors = []; + } + env.selectors.unshift(this.selectors); + + // Evaluate imports + if (ruleset.root || ruleset.allowImports || !ruleset.strictImports) { + ruleset.evalImports(env); + } + + // Store the frames around mixin definitions, + // so they can be evaluated like closures when the time comes. + for (i = 0; i < ruleset.rules.length; i++) { + if (ruleset.rules[i] instanceof tree.mixin.Definition) { + ruleset.rules[i].frames = env.frames.slice(0); + } + } + + var mediaBlockCount = (env.mediaBlocks && env.mediaBlocks.length) || 0; + + // Evaluate mixin calls. + for (i = 0; i < ruleset.rules.length; i++) { + if (ruleset.rules[i] instanceof tree.mixin.Call) { + /*jshint loopfunc:true */ + rules = ruleset.rules[i].eval(env).filter(function(r) { + if ((r instanceof tree.Rule) && r.variable) { + // do not pollute the scope if the variable is + // already there. consider returning false here + // but we need a way to "return" variable from mixins + return !(ruleset.variable(r.name)); + } + return true; + }); + ruleset.rules.splice.apply(ruleset.rules, [i, 1].concat(rules)); + i += rules.length-1; + ruleset.resetCache(); + } + } + + // Evaluate everything else + for (i = 0; i < ruleset.rules.length; i++) { + rule = ruleset.rules[i]; + + if (! (rule instanceof tree.mixin.Definition)) { + ruleset.rules[i] = rule.eval ? rule.eval(env) : rule; + } + } + + // Pop the stack + env.frames.shift(); + env.selectors.shift(); + + if (env.mediaBlocks) { + for (i = mediaBlockCount; i < env.mediaBlocks.length; i++) { + env.mediaBlocks[i].bubbleSelectors(selectors); + } + } + + return ruleset; + }, + evalImports: function(env) { + var i, rules; + for (i = 0; i < this.rules.length; i++) { + if (this.rules[i] instanceof tree.Import) { + rules = this.rules[i].eval(env); + if (typeof rules.length === "number") { + this.rules.splice.apply(this.rules, [i, 1].concat(rules)); + i+= rules.length-1; + } else { + this.rules.splice(i, 1, rules); + } + this.resetCache(); + } + } + }, + makeImportant: function() { + return new tree.Ruleset(this.selectors, this.rules.map(function (r) { + if (r.makeImportant) { + return r.makeImportant(); + } else { + return r; + } + }), this.strictImports); + }, + matchArgs: function (args) { + return !args || args.length === 0; + }, + matchCondition: function (args, env) { + var lastSelector = this.selectors[this.selectors.length-1]; + if (lastSelector.condition && + !lastSelector.condition.eval( + new(tree.evalEnv)(env, + env.frames))) { + return false; + } + return true; + }, + resetCache: function () { + this._rulesets = null; + this._variables = null; + this._lookups = {}; + }, + variables: function () { + if (this._variables) { return this._variables; } + else { + return this._variables = this.rules.reduce(function (hash, r) { + if (r instanceof tree.Rule && r.variable === true) { + hash[r.name] = r; + } + return hash; + }, {}); + } + }, + variable: function (name) { + return this.variables()[name]; + }, + rulesets: function () { + return this.rules.filter(function (r) { + return (r instanceof tree.Ruleset) || (r instanceof tree.mixin.Definition); + }); + }, + find: function (selector, self) { + self = self || this; + var rules = [], match, + key = selector.toCSS(); + + if (key in this._lookups) { return this._lookups[key]; } + + this.rulesets().forEach(function (rule) { + if (rule !== self) { + for (var j = 0; j < rule.selectors.length; j++) { + if (match = selector.match(rule.selectors[j])) { + if (selector.elements.length > match) { + Array.prototype.push.apply(rules, rule.find( + new(tree.Selector)(selector.elements.slice(match)), self)); + } else { + rules.push(rule); + } + break; + } + } + } + }); + return this._lookups[key] = rules; + }, + genCSS: function (env, output) { + var i, j, + ruleNodes = [], + rulesetNodes = [], + debugInfo, // Line number debugging + rule, + firstRuleset = true, + path; + + env.tabLevel = (env.tabLevel || 0); + + if (!this.root) { + env.tabLevel++; + } + + var tabRuleStr = env.compress ? '' : Array(env.tabLevel + 1).join(" "), + tabSetStr = env.compress ? '' : Array(env.tabLevel).join(" "); + + for (i = 0; i < this.rules.length; i++) { + rule = this.rules[i]; + if (rule.rules || (rule instanceof tree.Media) || rule instanceof tree.Directive || (this.root && rule instanceof tree.Comment)) { + rulesetNodes.push(rule); + } else { + ruleNodes.push(rule); + } + } + + // If this is the root node, we don't render + // a selector, or {}. + if (!this.root) { + debugInfo = tree.debugInfo(env, this, tabSetStr); + + if (debugInfo) { + output.add(debugInfo); + output.add(tabSetStr); + } + + for(i = 0; i < this.paths.length; i++) { + path = this.paths[i]; + env.firstSelector = true; + for(j = 0; j < path.length; j++) { + path[j].genCSS(env, output); + env.firstSelector = false; + } + if (i + 1 < this.paths.length) { + output.add(env.compress ? ',' : (',\n' + tabSetStr)); + } + } + + output.add((env.compress ? '{' : ' {\n') + tabRuleStr); + } + + // Compile rules and rulesets + for (i = 0; i < ruleNodes.length; i++) { + rule = ruleNodes[i]; + + // @page{ directive ends up with root elements inside it, a mix of rules and rulesets + // In this instance we do not know whether it is the last property + if (i + 1 === ruleNodes.length && (!this.root || rulesetNodes.length === 0 || this.firstRoot)) { + env.lastRule = true; + } + + if (rule.genCSS) { + rule.genCSS(env, output); + } else if (rule.value) { + output.add(rule.value.toString()); + } + + if (!env.lastRule) { + output.add(env.compress ? '' : ('\n' + tabRuleStr)); + } else { + env.lastRule = false; + } + } + + if (!this.root) { + output.add((env.compress ? '}' : '\n' + tabSetStr + '}')); + env.tabLevel--; + } + + for (i = 0; i < rulesetNodes.length; i++) { + if (ruleNodes.length && firstRuleset) { + output.add((env.compress ? "" : "\n") + (this.root ? tabRuleStr : tabSetStr)); + } + if (!firstRuleset) { + output.add((env.compress ? "" : "\n") + (this.root ? tabRuleStr : tabSetStr)); + } + firstRuleset = false; + rulesetNodes[i].genCSS(env, output); + } + + if (!output.isEmpty() && !env.compress && this.firstRoot) { + output.add('\n'); + } + }, + + toCSS: tree.toCSS, + + markReferenced: function () { + for (var s = 0; s < this.selectors.length; s++) { + this.selectors[s].markReferenced(); + } + }, + + joinSelectors: function (paths, context, selectors) { + for (var s = 0; s < selectors.length; s++) { + this.joinSelector(paths, context, selectors[s]); + } + }, + + joinSelector: function (paths, context, selector) { + + var i, j, k, + hasParentSelector, newSelectors, el, sel, parentSel, + newSelectorPath, afterParentJoin, newJoinedSelector, + newJoinedSelectorEmpty, lastSelector, currentElements, + selectorsMultiplied; + + for (i = 0; i < selector.elements.length; i++) { + el = selector.elements[i]; + if (el.value === '&') { + hasParentSelector = true; + } + } + + if (!hasParentSelector) { + if (context.length > 0) { + for (i = 0; i < context.length; i++) { + paths.push(context[i].concat(selector)); + } + } + else { + paths.push([selector]); + } + return; + } + + // The paths are [[Selector]] + // The first list is a list of comma seperated selectors + // The inner list is a list of inheritance seperated selectors + // e.g. + // .a, .b { + // .c { + // } + // } + // == [[.a] [.c]] [[.b] [.c]] + // + + // the elements from the current selector so far + currentElements = []; + // the current list of new selectors to add to the path. + // We will build it up. We initiate it with one empty selector as we "multiply" the new selectors + // by the parents + newSelectors = [[]]; + + for (i = 0; i < selector.elements.length; i++) { + el = selector.elements[i]; + // non parent reference elements just get added + if (el.value !== "&") { + currentElements.push(el); + } else { + // the new list of selectors to add + selectorsMultiplied = []; + + // merge the current list of non parent selector elements + // on to the current list of selectors to add + if (currentElements.length > 0) { + this.mergeElementsOnToSelectors(currentElements, newSelectors); + } + + // loop through our current selectors + for (j = 0; j < newSelectors.length; j++) { + sel = newSelectors[j]; + // if we don't have any parent paths, the & might be in a mixin so that it can be used + // whether there are parents or not + if (context.length === 0) { + // the combinator used on el should now be applied to the next element instead so that + // it is not lost + if (sel.length > 0) { + sel[0].elements = sel[0].elements.slice(0); + sel[0].elements.push(new(tree.Element)(el.combinator, '', 0, el.index, el.currentFileInfo)); + } + selectorsMultiplied.push(sel); + } + else { + // and the parent selectors + for (k = 0; k < context.length; k++) { + parentSel = context[k]; + // We need to put the current selectors + // then join the last selector's elements on to the parents selectors + + // our new selector path + newSelectorPath = []; + // selectors from the parent after the join + afterParentJoin = []; + newJoinedSelectorEmpty = true; + + //construct the joined selector - if & is the first thing this will be empty, + // if not newJoinedSelector will be the last set of elements in the selector + if (sel.length > 0) { + newSelectorPath = sel.slice(0); + lastSelector = newSelectorPath.pop(); + newJoinedSelector = selector.createDerived(lastSelector.elements.slice(0)); + newJoinedSelectorEmpty = false; + } + else { + newJoinedSelector = selector.createDerived([]); + } + + //put together the parent selectors after the join + if (parentSel.length > 1) { + afterParentJoin = afterParentJoin.concat(parentSel.slice(1)); + } + + if (parentSel.length > 0) { + newJoinedSelectorEmpty = false; + + // join the elements so far with the first part of the parent + newJoinedSelector.elements.push(new(tree.Element)(el.combinator, parentSel[0].elements[0].value, el.index, el.currentFileInfo)); + newJoinedSelector.elements = newJoinedSelector.elements.concat(parentSel[0].elements.slice(1)); + } + + if (!newJoinedSelectorEmpty) { + // now add the joined selector + newSelectorPath.push(newJoinedSelector); + } + + // and the rest of the parent + newSelectorPath = newSelectorPath.concat(afterParentJoin); + + // add that to our new set of selectors + selectorsMultiplied.push(newSelectorPath); + } + } + } + + // our new selectors has been multiplied, so reset the state + newSelectors = selectorsMultiplied; + currentElements = []; + } + } + + // if we have any elements left over (e.g. .a& .b == .b) + // add them on to all the current selectors + if (currentElements.length > 0) { + this.mergeElementsOnToSelectors(currentElements, newSelectors); + } + + for (i = 0; i < newSelectors.length; i++) { + if (newSelectors[i].length > 0) { + paths.push(newSelectors[i]); + } + } + }, + + mergeElementsOnToSelectors: function(elements, selectors) { + var i, sel; + + if (selectors.length === 0) { + selectors.push([ new(tree.Selector)(elements) ]); + return; + } + + for (i = 0; i < selectors.length; i++) { + sel = selectors[i]; + + // if the previous thing in sel is a parent this needs to join on to it + if (sel.length > 0) { + sel[sel.length - 1] = sel[sel.length - 1].createDerived(sel[sel.length - 1].elements.concat(elements)); + } + else { + sel.push(new(tree.Selector)(elements)); + } + } + } +}; +})(require('../tree')); + +(function (tree) { + +tree.Selector = function (elements, extendList, condition, index, currentFileInfo, isReferenced) { + this.elements = elements; + this.extendList = extendList || []; + this.condition = condition; + this.currentFileInfo = currentFileInfo || {}; + this.isReferenced = isReferenced; + if (!condition) { + this.evaldCondition = true; + } +}; +tree.Selector.prototype = { + type: "Selector", + accept: function (visitor) { + this.elements = visitor.visit(this.elements); + this.extendList = visitor.visit(this.extendList); + this.condition = visitor.visit(this.condition); + }, + createDerived: function(elements, extendList, evaldCondition) { + /*jshint eqnull:true */ + evaldCondition = evaldCondition != null ? evaldCondition : this.evaldCondition; + var newSelector = new(tree.Selector)(elements, extendList || this.extendList, this.condition, this.index, this.currentFileInfo, this.isReferenced); + newSelector.evaldCondition = evaldCondition; + return newSelector; + }, + match: function (other) { + var elements = this.elements, + len = elements.length, + oelements, olen, max, i; + + oelements = other.elements.slice( + (other.elements.length && other.elements[0].value === "&") ? 1 : 0); + olen = oelements.length; + max = Math.min(len, olen); + + if (olen === 0 || len < olen) { + return 0; + } else { + for (i = 0; i < max; i++) { + if (elements[i].value !== oelements[i].value) { + return 0; + } + } + } + return max; // return number of matched selectors + }, + eval: function (env) { + var evaldCondition = this.condition && this.condition.eval(env); + + return this.createDerived(this.elements.map(function (e) { + return e.eval(env); + }), this.extendList.map(function(extend) { + return extend.eval(env); + }), evaldCondition); + }, + genCSS: function (env, output) { + var i, element; + if ((!env || !env.firstSelector) && this.elements[0].combinator.value === "") { + output.add(' ', this.currentFileInfo, this.index); + } + if (!this._css) { + //TODO caching? speed comparison? + for(i = 0; i < this.elements.length; i++) { + element = this.elements[i]; + element.genCSS(env, output); + } + } + }, + toCSS: tree.toCSS, + markReferenced: function () { + this.isReferenced = true; + }, + getIsReferenced: function() { + return !this.currentFileInfo.reference || this.isReferenced; + }, + getIsOutput: function() { + return this.evaldCondition; + } +}; + +})(require('../tree')); + +(function (tree) { + +tree.UnicodeDescriptor = function (value) { + this.value = value; +}; +tree.UnicodeDescriptor.prototype = { + type: "UnicodeDescriptor", + genCSS: function (env, output) { + output.add(this.value); + }, + toCSS: tree.toCSS, + eval: function () { return this; } +}; + +})(require('../tree')); + +(function (tree) { + +tree.URL = function (val, currentFileInfo) { + this.value = val; + this.currentFileInfo = currentFileInfo; +}; +tree.URL.prototype = { + type: "Url", + accept: function (visitor) { + this.value = visitor.visit(this.value); + }, + genCSS: function (env, output) { + output.add("url("); + this.value.genCSS(env, output); + output.add(")"); + }, + toCSS: tree.toCSS, + eval: function (ctx) { + var val = this.value.eval(ctx), rootpath; + + // Add the base path if the URL is relative + rootpath = this.currentFileInfo && this.currentFileInfo.rootpath; + if (rootpath && typeof val.value === "string" && ctx.isPathRelative(val.value)) { + if (!val.quote) { + rootpath = rootpath.replace(/[\(\)'"\s]/g, function(match) { return "\\"+match; }); + } + val.value = rootpath + val.value; + } + + val.value = ctx.normalizePath(val.value); + + return new(tree.URL)(val, null); + } +}; + +})(require('../tree')); + +(function (tree) { + +tree.Value = function (value) { + this.value = value; +}; +tree.Value.prototype = { + type: "Value", + accept: function (visitor) { + this.value = visitor.visit(this.value); + }, + eval: function (env) { + if (this.value.length === 1) { + return this.value[0].eval(env); + } else { + return new(tree.Value)(this.value.map(function (v) { + return v.eval(env); + })); + } + }, + genCSS: function (env, output) { + var i; + for(i = 0; i < this.value.length; i++) { + this.value[i].genCSS(env, output); + if (i+1 < this.value.length) { + output.add((env && env.compress) ? ',' : ', '); + } + } + }, + toCSS: tree.toCSS +}; + +})(require('../tree')); + +(function (tree) { + +tree.Variable = function (name, index, currentFileInfo) { + this.name = name; + this.index = index; + this.currentFileInfo = currentFileInfo; +}; +tree.Variable.prototype = { + type: "Variable", + eval: function (env) { + var variable, v, name = this.name; + + if (name.indexOf('@@') === 0) { + name = '@' + new(tree.Variable)(name.slice(1)).eval(env).value; + } + + if (this.evaluating) { + throw { type: 'Name', + message: "Recursive variable definition for " + name, + filename: this.currentFileInfo.file, + index: this.index }; + } + + this.evaluating = true; + + if (variable = tree.find(env.frames, function (frame) { + if (v = frame.variable(name)) { + return v.value.eval(env); + } + })) { + this.evaluating = false; + return variable; + } + else { + throw { type: 'Name', + message: "variable " + name + " is undefined", + filename: this.currentFileInfo.filename, + index: this.index }; + } + } +}; + +})(require('../tree')); + +(function (tree) { + + var parseCopyProperties = [ + 'paths', // option - unmodified - paths to search for imports on + 'optimization', // option - optimization level (for the chunker) + 'files', // list of files that have been imported, used for import-once + 'contents', // browser-only, contents of all the files + 'relativeUrls', // option - whether to adjust URL's to be relative + 'rootpath', // option - rootpath to append to URL's + 'strictImports', // option - + 'insecure', // option - whether to allow imports from insecure ssl hosts + 'dumpLineNumbers', // option - whether to dump line numbers + 'compress', // option - whether to compress + 'processImports', // option - whether to process imports. if false then imports will not be imported + 'syncImport', // option - whether to import synchronously + 'javascriptEnabled',// option - whether JavaScript is enabled. if undefined, defaults to true + 'mime', // browser only - mime type for sheet import + 'useFileCache', // browser only - whether to use the per file session cache + 'currentFileInfo' // information about the current file - for error reporting and importing and making urls relative etc. + ]; + + //currentFileInfo = { + // 'relativeUrls' - option - whether to adjust URL's to be relative + // 'filename' - full resolved filename of current file + // 'rootpath' - path to append to normal URLs for this node + // 'currentDirectory' - path to the current file, absolute + // 'rootFilename' - filename of the base file + // 'entryPath' - absolute path to the entry file + // 'reference' - whether the file should not be output and only output parts that are referenced + + tree.parseEnv = function(options) { + copyFromOriginal(options, this, parseCopyProperties); + + if (!this.contents) { this.contents = {}; } + if (!this.files) { this.files = {}; } + + if (!this.currentFileInfo) { + var filename = (options && options.filename) || "input"; + var entryPath = filename.replace(/[^\/\\]*$/, ""); + if (options) { + options.filename = null; + } + this.currentFileInfo = { + filename: filename, + relativeUrls: this.relativeUrls, + rootpath: (options && options.rootpath) || "", + currentDirectory: entryPath, + entryPath: entryPath, + rootFilename: filename + }; + } + }; + + var evalCopyProperties = [ + 'silent', // whether to swallow errors and warnings + 'verbose', // whether to log more activity + 'compress', // whether to compress + 'yuicompress', // whether to compress with the outside tool yui compressor + 'ieCompat', // whether to enforce IE compatibility (IE8 data-uri) + 'strictMath', // whether math has to be within parenthesis + 'strictUnits', // whether units need to evaluate correctly + 'cleancss', // whether to compress with clean-css + 'sourceMap', // whether to output a source map + 'importMultiple'// whether we are currently importing multiple copies + ]; + + tree.evalEnv = function(options, frames) { + copyFromOriginal(options, this, evalCopyProperties); + + this.frames = frames || []; + }; + + tree.evalEnv.prototype.inParenthesis = function () { + if (!this.parensStack) { + this.parensStack = []; + } + this.parensStack.push(true); + }; + + tree.evalEnv.prototype.outOfParenthesis = function () { + this.parensStack.pop(); + }; + + tree.evalEnv.prototype.isMathOn = function () { + return this.strictMath ? (this.parensStack && this.parensStack.length) : true; + }; + + tree.evalEnv.prototype.isPathRelative = function (path) { + return !/^(?:[a-z-]+:|\/)/.test(path); + }; + + tree.evalEnv.prototype.normalizePath = function( path ) { + var + segments = path.split("/").reverse(), + segment; + + path = []; + while (segments.length !== 0 ) { + segment = segments.pop(); + switch( segment ) { + case ".": + break; + case "..": + if ((path.length === 0) || (path[path.length - 1] === "..")) { + path.push( segment ); + } else { + path.pop(); + } + break; + default: + path.push( segment ); + break; + } + } + + return path.join("/"); + }; + + //todo - do the same for the toCSS env + //tree.toCSSEnv = function (options) { + //}; + + var copyFromOriginal = function(original, destination, propertiesToCopy) { + if (!original) { return; } + + for(var i = 0; i < propertiesToCopy.length; i++) { + if (original.hasOwnProperty(propertiesToCopy[i])) { + destination[propertiesToCopy[i]] = original[propertiesToCopy[i]]; + } + } + }; + +})(require('./tree')); + +(function (tree) { + + tree.visitor = function(implementation) { + this._implementation = implementation; + }; + + tree.visitor.prototype = { + visit: function(node) { + + if (node instanceof Array) { + return this.visitArray(node); + } + + if (!node || !node.type) { + return node; + } + + var funcName = "visit" + node.type, + func = this._implementation[funcName], + visitArgs, newNode; + if (func) { + visitArgs = {visitDeeper: true}; + newNode = func.call(this._implementation, node, visitArgs); + if (this._implementation.isReplacing) { + node = newNode; + } + } + if ((!visitArgs || visitArgs.visitDeeper) && node && node.accept) { + node.accept(this); + } + funcName = funcName + "Out"; + if (this._implementation[funcName]) { + this._implementation[funcName](node); + } + return node; + }, + visitArray: function(nodes) { + var i, newNodes = []; + for(i = 0; i < nodes.length; i++) { + var evald = this.visit(nodes[i]); + if (evald instanceof Array) { + evald = this.flatten(evald); + newNodes = newNodes.concat(evald); + } else { + newNodes.push(evald); + } + } + if (this._implementation.isReplacing) { + return newNodes; + } + return nodes; + }, + doAccept: function (node) { + node.accept(this); + }, + flatten: function(arr, master) { + return arr.reduce(this.flattenReduce.bind(this), master || []); + }, + flattenReduce: function(sum, element) { + if (element instanceof Array) { + sum = this.flatten(element, sum); + } else { + sum.push(element); + } + return sum; + } + }; + +})(require('./tree')); +(function (tree) { + tree.importVisitor = function(importer, finish, evalEnv) { + this._visitor = new tree.visitor(this); + this._importer = importer; + this._finish = finish; + this.env = evalEnv || new tree.evalEnv(); + this.importCount = 0; + }; + + tree.importVisitor.prototype = { + isReplacing: true, + run: function (root) { + var error; + try { + // process the contents + this._visitor.visit(root); + } + catch(e) { + error = e; + } + + this.isFinished = true; + + if (this.importCount === 0) { + this._finish(error); + } + }, + visitImport: function (importNode, visitArgs) { + var importVisitor = this, + evaldImportNode, + inlineCSS = importNode.options.inline; + + if (!importNode.css || inlineCSS) { + + try { + evaldImportNode = importNode.evalForImport(this.env); + } catch(e){ + if (!e.filename) { e.index = importNode.index; e.filename = importNode.currentFileInfo.filename; } + // attempt to eval properly and treat as css + importNode.css = true; + // if that fails, this error will be thrown + importNode.error = e; + } + + if (evaldImportNode && (!evaldImportNode.css || inlineCSS)) { + importNode = evaldImportNode; + this.importCount++; + var env = new tree.evalEnv(this.env, this.env.frames.slice(0)); + + if (importNode.options.multiple) { + env.importMultiple = true; + } + + this._importer.push(importNode.getPath(), importNode.currentFileInfo, importNode.options, function (e, root, imported, fullPath) { + if (e && !e.filename) { e.index = importNode.index; e.filename = importNode.currentFileInfo.filename; } + + if (imported && !env.importMultiple) { importNode.skip = imported; } + + var subFinish = function(e) { + importVisitor.importCount--; + + if (importVisitor.importCount === 0 && importVisitor.isFinished) { + importVisitor._finish(e); + } + }; + + if (root) { + importNode.root = root; + importNode.importedFilename = fullPath; + if (!inlineCSS && !importNode.skip) { + new(tree.importVisitor)(importVisitor._importer, subFinish, env) + .run(root); + return; + } + } + + subFinish(); + }); + } + } + visitArgs.visitDeeper = false; + return importNode; + }, + visitRule: function (ruleNode, visitArgs) { + visitArgs.visitDeeper = false; + return ruleNode; + }, + visitDirective: function (directiveNode, visitArgs) { + this.env.frames.unshift(directiveNode); + return directiveNode; + }, + visitDirectiveOut: function (directiveNode) { + this.env.frames.shift(); + }, + visitMixinDefinition: function (mixinDefinitionNode, visitArgs) { + this.env.frames.unshift(mixinDefinitionNode); + return mixinDefinitionNode; + }, + visitMixinDefinitionOut: function (mixinDefinitionNode) { + this.env.frames.shift(); + }, + visitRuleset: function (rulesetNode, visitArgs) { + this.env.frames.unshift(rulesetNode); + return rulesetNode; + }, + visitRulesetOut: function (rulesetNode) { + this.env.frames.shift(); + }, + visitMedia: function (mediaNode, visitArgs) { + this.env.frames.unshift(mediaNode.ruleset); + return mediaNode; + }, + visitMediaOut: function (mediaNode) { + this.env.frames.shift(); + } + }; + +})(require('./tree')); +(function (tree) { + tree.joinSelectorVisitor = function() { + this.contexts = [[]]; + this._visitor = new tree.visitor(this); + }; + + tree.joinSelectorVisitor.prototype = { + run: function (root) { + return this._visitor.visit(root); + }, + visitRule: function (ruleNode, visitArgs) { + visitArgs.visitDeeper = false; + }, + visitMixinDefinition: function (mixinDefinitionNode, visitArgs) { + visitArgs.visitDeeper = false; + }, + + visitRuleset: function (rulesetNode, visitArgs) { + var context = this.contexts[this.contexts.length - 1]; + var paths = []; + this.contexts.push(paths); + + if (! rulesetNode.root) { + rulesetNode.selectors = rulesetNode.selectors.filter(function(selector) { return selector.getIsOutput(); }); + if (rulesetNode.selectors.length === 0) { + rulesetNode.rules.length = 0; + } + rulesetNode.joinSelectors(paths, context, rulesetNode.selectors); + rulesetNode.paths = paths; + } + }, + visitRulesetOut: function (rulesetNode) { + this.contexts.length = this.contexts.length - 1; + }, + visitMedia: function (mediaNode, visitArgs) { + var context = this.contexts[this.contexts.length - 1]; + mediaNode.rules[0].root = (context.length === 0 || context[0].multiMedia); + } + }; + +})(require('./tree')); +(function (tree) { + tree.toCSSVisitor = function(env) { + this._visitor = new tree.visitor(this); + this._env = env; + }; + + tree.toCSSVisitor.prototype = { + isReplacing: true, + run: function (root) { + return this._visitor.visit(root); + }, + + visitRule: function (ruleNode, visitArgs) { + if (ruleNode.variable) { + return []; + } + return ruleNode; + }, + + visitMixinDefinition: function (mixinNode, visitArgs) { + return []; + }, + + visitExtend: function (extendNode, visitArgs) { + return []; + }, + + visitComment: function (commentNode, visitArgs) { + if (commentNode.isSilent(this._env)) { + return []; + } + return commentNode; + }, + + visitMedia: function(mediaNode, visitArgs) { + mediaNode.accept(this._visitor); + visitArgs.visitDeeper = false; + + if (!mediaNode.rules.length) { + return []; + } + return mediaNode; + }, + + visitDirective: function(directiveNode, visitArgs) { + if (directiveNode.currentFileInfo.reference && !directiveNode.isReferenced) { + return []; + } + if (directiveNode.name === "@charset") { + // Only output the debug info together with subsequent @charset definitions + // a comment (or @media statement) before the actual @charset directive would + // be considered illegal css as it has to be on the first line + if (this.charset) { + if (directiveNode.debugInfo) { + var comment = new tree.Comment("/* " + directiveNode.toCSS(this._env).replace(/\n/g, "")+" */\n"); + comment.debugInfo = directiveNode.debugInfo; + return this._visitor.visit(comment); + } + return []; + } + this.charset = true; + } + return directiveNode; + }, + + checkPropertiesInRoot: function(rules) { + var ruleNode; + for(var i = 0; i < rules.length; i++) { + ruleNode = rules[i]; + if (ruleNode instanceof tree.Rule && !ruleNode.variable) { + throw { message: "properties must be inside selector blocks, they cannot be in the root.", + index: ruleNode.index, filename: ruleNode.currentFileInfo ? ruleNode.currentFileInfo.filename : null}; + } + } + }, + + visitRuleset: function (rulesetNode, visitArgs) { + var rule, rulesets = []; + if (rulesetNode.firstRoot) { + this.checkPropertiesInRoot(rulesetNode.rules); + } + if (! rulesetNode.root) { + + rulesetNode.paths = rulesetNode.paths + .filter(function(p) { + var i; + if (p[0].elements[0].combinator.value === ' ') { + p[0].elements[0].combinator = new(tree.Combinator)(''); + } + for(i = 0; i < p.length; i++) { + if (p[i].getIsReferenced() && p[i].getIsOutput()) { + return true; + } + } + return false; + }); + + // Compile rules and rulesets + for (var i = 0; i < rulesetNode.rules.length; i++) { + rule = rulesetNode.rules[i]; + + if (rule.rules) { + // visit because we are moving them out from being a child + rulesets.push(this._visitor.visit(rule)); + rulesetNode.rules.splice(i, 1); + i--; + continue; + } + } + // accept the visitor to remove rules and refactor itself + // then we can decide now whether we want it or not + if (rulesetNode.rules.length > 0) { + rulesetNode.accept(this._visitor); + } + visitArgs.visitDeeper = false; + + this._mergeRules(rulesetNode.rules); + this._removeDuplicateRules(rulesetNode.rules); + + // now decide whether we keep the ruleset + if (rulesetNode.rules.length > 0 && rulesetNode.paths.length > 0) { + rulesets.splice(0, 0, rulesetNode); + } + } else { + rulesetNode.accept(this._visitor); + visitArgs.visitDeeper = false; + if (rulesetNode.firstRoot || rulesetNode.rules.length > 0) { + rulesets.splice(0, 0, rulesetNode); + } + } + if (rulesets.length === 1) { + return rulesets[0]; + } + return rulesets; + }, + + _removeDuplicateRules: function(rules) { + // remove duplicates + var ruleCache = {}, + ruleList, rule, i; + for(i = rules.length - 1; i >= 0 ; i--) { + rule = rules[i]; + if (rule instanceof tree.Rule) { + if (!ruleCache[rule.name]) { + ruleCache[rule.name] = rule; + } else { + ruleList = ruleCache[rule.name]; + if (ruleList instanceof tree.Rule) { + ruleList = ruleCache[rule.name] = [ruleCache[rule.name].toCSS(this._env)]; + } + var ruleCSS = rule.toCSS(this._env); + if (ruleList.indexOf(ruleCSS) !== -1) { + rules.splice(i, 1); + } else { + ruleList.push(ruleCSS); + } + } + } + } + }, + + _mergeRules: function (rules) { + var groups = {}, + parts, + rule, + key; + + for (var i = 0; i < rules.length; i++) { + rule = rules[i]; + + if ((rule instanceof tree.Rule) && rule.merge) { + key = [rule.name, + rule.important ? "!" : ""].join(","); + + if (!groups[key]) { + parts = groups[key] = []; + } else { + rules.splice(i--, 1); + } + + parts.push(rule); + } + } + + Object.keys(groups).map(function (k) { + parts = groups[k]; + + if (parts.length > 1) { + rule = parts[0]; + + rule.value = new (tree.Value)(parts.map(function (p) { + return p.value; + })); + } + }); + } + }; + +})(require('./tree')); +(function (tree) { + /*jshint loopfunc:true */ + + tree.extendFinderVisitor = function() { + this._visitor = new tree.visitor(this); + this.contexts = []; + this.allExtendsStack = [[]]; + }; + + tree.extendFinderVisitor.prototype = { + run: function (root) { + root = this._visitor.visit(root); + root.allExtends = this.allExtendsStack[0]; + return root; + }, + visitRule: function (ruleNode, visitArgs) { + visitArgs.visitDeeper = false; + }, + visitMixinDefinition: function (mixinDefinitionNode, visitArgs) { + visitArgs.visitDeeper = false; + }, + visitRuleset: function (rulesetNode, visitArgs) { + + if (rulesetNode.root) { + return; + } + + var i, j, extend, allSelectorsExtendList = [], extendList; + + // get &:extend(.a); rules which apply to all selectors in this ruleset + for(i = 0; i < rulesetNode.rules.length; i++) { + if (rulesetNode.rules[i] instanceof tree.Extend) { + allSelectorsExtendList.push(rulesetNode.rules[i]); + rulesetNode.extendOnEveryPath = true; + } + } + + // now find every selector and apply the extends that apply to all extends + // and the ones which apply to an individual extend + for(i = 0; i < rulesetNode.paths.length; i++) { + var selectorPath = rulesetNode.paths[i], + selector = selectorPath[selectorPath.length-1]; + extendList = selector.extendList.slice(0).concat(allSelectorsExtendList).map(function(allSelectorsExtend) { + return allSelectorsExtend.clone(); + }); + for(j = 0; j < extendList.length; j++) { + this.foundExtends = true; + extend = extendList[j]; + extend.findSelfSelectors(selectorPath); + extend.ruleset = rulesetNode; + if (j === 0) { extend.firstExtendOnThisSelectorPath = true; } + this.allExtendsStack[this.allExtendsStack.length-1].push(extend); + } + } + + this.contexts.push(rulesetNode.selectors); + }, + visitRulesetOut: function (rulesetNode) { + if (!rulesetNode.root) { + this.contexts.length = this.contexts.length - 1; + } + }, + visitMedia: function (mediaNode, visitArgs) { + mediaNode.allExtends = []; + this.allExtendsStack.push(mediaNode.allExtends); + }, + visitMediaOut: function (mediaNode) { + this.allExtendsStack.length = this.allExtendsStack.length - 1; + }, + visitDirective: function (directiveNode, visitArgs) { + directiveNode.allExtends = []; + this.allExtendsStack.push(directiveNode.allExtends); + }, + visitDirectiveOut: function (directiveNode) { + this.allExtendsStack.length = this.allExtendsStack.length - 1; + } + }; + + tree.processExtendsVisitor = function() { + this._visitor = new tree.visitor(this); + }; + + tree.processExtendsVisitor.prototype = { + run: function(root) { + var extendFinder = new tree.extendFinderVisitor(); + extendFinder.run(root); + if (!extendFinder.foundExtends) { return root; } + root.allExtends = root.allExtends.concat(this.doExtendChaining(root.allExtends, root.allExtends)); + this.allExtendsStack = [root.allExtends]; + return this._visitor.visit(root); + }, + doExtendChaining: function (extendsList, extendsListTarget, iterationCount) { + // + // chaining is different from normal extension.. if we extend an extend then we are not just copying, altering and pasting + // the selector we would do normally, but we are also adding an extend with the same target selector + // this means this new extend can then go and alter other extends + // + // this method deals with all the chaining work - without it, extend is flat and doesn't work on other extend selectors + // this is also the most expensive.. and a match on one selector can cause an extension of a selector we had already processed if + // we look at each selector at a time, as is done in visitRuleset + + var extendIndex, targetExtendIndex, matches, extendsToAdd = [], newSelector, extendVisitor = this, selectorPath, extend, targetExtend, newExtend; + + iterationCount = iterationCount || 0; + + //loop through comparing every extend with every target extend. + // a target extend is the one on the ruleset we are looking at copy/edit/pasting in place + // e.g. .a:extend(.b) {} and .b:extend(.c) {} then the first extend extends the second one + // and the second is the target. + // the seperation into two lists allows us to process a subset of chains with a bigger set, as is the + // case when processing media queries + for(extendIndex = 0; extendIndex < extendsList.length; extendIndex++){ + for(targetExtendIndex = 0; targetExtendIndex < extendsListTarget.length; targetExtendIndex++){ + + extend = extendsList[extendIndex]; + targetExtend = extendsListTarget[targetExtendIndex]; + + // look for circular references + if (this.inInheritanceChain(targetExtend, extend)) { continue; } + + // find a match in the target extends self selector (the bit before :extend) + selectorPath = [targetExtend.selfSelectors[0]]; + matches = extendVisitor.findMatch(extend, selectorPath); + + if (matches.length) { + + // we found a match, so for each self selector.. + extend.selfSelectors.forEach(function(selfSelector) { + + // process the extend as usual + newSelector = extendVisitor.extendSelector(matches, selectorPath, selfSelector); + + // but now we create a new extend from it + newExtend = new(tree.Extend)(targetExtend.selector, targetExtend.option, 0); + newExtend.selfSelectors = newSelector; + + // add the extend onto the list of extends for that selector + newSelector[newSelector.length-1].extendList = [newExtend]; + + // record that we need to add it. + extendsToAdd.push(newExtend); + newExtend.ruleset = targetExtend.ruleset; + + //remember its parents for circular references + newExtend.parents = [targetExtend, extend]; + + // only process the selector once.. if we have :extend(.a,.b) then multiple + // extends will look at the same selector path, so when extending + // we know that any others will be duplicates in terms of what is added to the css + if (targetExtend.firstExtendOnThisSelectorPath) { + newExtend.firstExtendOnThisSelectorPath = true; + targetExtend.ruleset.paths.push(newSelector); + } + }); + } + } + } + + if (extendsToAdd.length) { + // try to detect circular references to stop a stack overflow. + // may no longer be needed. + this.extendChainCount++; + if (iterationCount > 100) { + var selectorOne = "{unable to calculate}"; + var selectorTwo = "{unable to calculate}"; + try + { + selectorOne = extendsToAdd[0].selfSelectors[0].toCSS(); + selectorTwo = extendsToAdd[0].selector.toCSS(); + } + catch(e) {} + throw {message: "extend circular reference detected. One of the circular extends is currently:"+selectorOne+":extend(" + selectorTwo+")"}; + } + + // now process the new extends on the existing rules so that we can handle a extending b extending c ectending d extending e... + return extendsToAdd.concat(extendVisitor.doExtendChaining(extendsToAdd, extendsListTarget, iterationCount+1)); + } else { + return extendsToAdd; + } + }, + inInheritanceChain: function (possibleParent, possibleChild) { + if (possibleParent === possibleChild) { + return true; + } + if (possibleChild.parents) { + if (this.inInheritanceChain(possibleParent, possibleChild.parents[0])) { + return true; + } + if (this.inInheritanceChain(possibleParent, possibleChild.parents[1])) { + return true; + } + } + return false; + }, + visitRule: function (ruleNode, visitArgs) { + visitArgs.visitDeeper = false; + }, + visitMixinDefinition: function (mixinDefinitionNode, visitArgs) { + visitArgs.visitDeeper = false; + }, + visitSelector: function (selectorNode, visitArgs) { + visitArgs.visitDeeper = false; + }, + visitRuleset: function (rulesetNode, visitArgs) { + if (rulesetNode.root) { + return; + } + var matches, pathIndex, extendIndex, allExtends = this.allExtendsStack[this.allExtendsStack.length-1], selectorsToAdd = [], extendVisitor = this, selectorPath; + + // look at each selector path in the ruleset, find any extend matches and then copy, find and replace + + for(extendIndex = 0; extendIndex < allExtends.length; extendIndex++) { + for(pathIndex = 0; pathIndex < rulesetNode.paths.length; pathIndex++) { + + selectorPath = rulesetNode.paths[pathIndex]; + + // extending extends happens initially, before the main pass + if (rulesetNode.extendOnEveryPath || selectorPath[selectorPath.length-1].extendList.length) { continue; } + + matches = this.findMatch(allExtends[extendIndex], selectorPath); + + if (matches.length) { + + allExtends[extendIndex].selfSelectors.forEach(function(selfSelector) { + selectorsToAdd.push(extendVisitor.extendSelector(matches, selectorPath, selfSelector)); + }); + } + } + } + rulesetNode.paths = rulesetNode.paths.concat(selectorsToAdd); + }, + findMatch: function (extend, haystackSelectorPath) { + // + // look through the haystack selector path to try and find the needle - extend.selector + // returns an array of selector matches that can then be replaced + // + var haystackSelectorIndex, hackstackSelector, hackstackElementIndex, haystackElement, + targetCombinator, i, + extendVisitor = this, + needleElements = extend.selector.elements, + potentialMatches = [], potentialMatch, matches = []; + + // loop through the haystack elements + for(haystackSelectorIndex = 0; haystackSelectorIndex < haystackSelectorPath.length; haystackSelectorIndex++) { + hackstackSelector = haystackSelectorPath[haystackSelectorIndex]; + + for(hackstackElementIndex = 0; hackstackElementIndex < hackstackSelector.elements.length; hackstackElementIndex++) { + + haystackElement = hackstackSelector.elements[hackstackElementIndex]; + + // if we allow elements before our match we can add a potential match every time. otherwise only at the first element. + if (extend.allowBefore || (haystackSelectorIndex === 0 && hackstackElementIndex === 0)) { + potentialMatches.push({pathIndex: haystackSelectorIndex, index: hackstackElementIndex, matched: 0, initialCombinator: haystackElement.combinator}); + } + + for(i = 0; i < potentialMatches.length; i++) { + potentialMatch = potentialMatches[i]; + + // selectors add " " onto the first element. When we use & it joins the selectors together, but if we don't + // then each selector in haystackSelectorPath has a space before it added in the toCSS phase. so we need to work out + // what the resulting combinator will be + targetCombinator = haystackElement.combinator.value; + if (targetCombinator === '' && hackstackElementIndex === 0) { + targetCombinator = ' '; + } + + // if we don't match, null our match to indicate failure + if (!extendVisitor.isElementValuesEqual(needleElements[potentialMatch.matched].value, haystackElement.value) || + (potentialMatch.matched > 0 && needleElements[potentialMatch.matched].combinator.value !== targetCombinator)) { + potentialMatch = null; + } else { + potentialMatch.matched++; + } + + // if we are still valid and have finished, test whether we have elements after and whether these are allowed + if (potentialMatch) { + potentialMatch.finished = potentialMatch.matched === needleElements.length; + if (potentialMatch.finished && + (!extend.allowAfter && (hackstackElementIndex+1 < hackstackSelector.elements.length || haystackSelectorIndex+1 < haystackSelectorPath.length))) { + potentialMatch = null; + } + } + // if null we remove, if not, we are still valid, so either push as a valid match or continue + if (potentialMatch) { + if (potentialMatch.finished) { + potentialMatch.length = needleElements.length; + potentialMatch.endPathIndex = haystackSelectorIndex; + potentialMatch.endPathElementIndex = hackstackElementIndex + 1; // index after end of match + potentialMatches.length = 0; // we don't allow matches to overlap, so start matching again + matches.push(potentialMatch); + } + } else { + potentialMatches.splice(i, 1); + i--; + } + } + } + } + return matches; + }, + isElementValuesEqual: function(elementValue1, elementValue2) { + if (typeof elementValue1 === "string" || typeof elementValue2 === "string") { + return elementValue1 === elementValue2; + } + if (elementValue1 instanceof tree.Attribute) { + if (elementValue1.op !== elementValue2.op || elementValue1.key !== elementValue2.key) { + return false; + } + if (!elementValue1.value || !elementValue2.value) { + if (elementValue1.value || elementValue2.value) { + return false; + } + return true; + } + elementValue1 = elementValue1.value.value || elementValue1.value; + elementValue2 = elementValue2.value.value || elementValue2.value; + return elementValue1 === elementValue2; + } + elementValue1 = elementValue1.value; + elementValue2 = elementValue2.value; + if (elementValue1 instanceof tree.Selector) { + if (!(elementValue2 instanceof tree.Selector) || elementValue1.elements.length !== elementValue2.elements.length) { + return false; + } + for(var i = 0; i currentSelectorPathIndex && currentSelectorPathElementIndex > 0) { + path[path.length - 1].elements = path[path.length - 1].elements.concat(selectorPath[currentSelectorPathIndex].elements.slice(currentSelectorPathElementIndex)); + currentSelectorPathElementIndex = 0; + currentSelectorPathIndex++; + } + + newElements = selector.elements + .slice(currentSelectorPathElementIndex, match.index) + .concat([firstElement]) + .concat(replacementSelector.elements.slice(1)); + + if (currentSelectorPathIndex === match.pathIndex && matchIndex > 0) { + path[path.length - 1].elements = + path[path.length - 1].elements.concat(newElements); + } else { + path = path.concat(selectorPath.slice(currentSelectorPathIndex, match.pathIndex)); + + path.push(new tree.Selector( + newElements + )); + } + currentSelectorPathIndex = match.endPathIndex; + currentSelectorPathElementIndex = match.endPathElementIndex; + if (currentSelectorPathElementIndex >= selectorPath[currentSelectorPathIndex].elements.length) { + currentSelectorPathElementIndex = 0; + currentSelectorPathIndex++; + } + } + + if (currentSelectorPathIndex < selectorPath.length && currentSelectorPathElementIndex > 0) { + path[path.length - 1].elements = path[path.length - 1].elements.concat(selectorPath[currentSelectorPathIndex].elements.slice(currentSelectorPathElementIndex)); + currentSelectorPathIndex++; + } + + path = path.concat(selectorPath.slice(currentSelectorPathIndex, selectorPath.length)); + + return path; + }, + visitRulesetOut: function (rulesetNode) { + }, + visitMedia: function (mediaNode, visitArgs) { + var newAllExtends = mediaNode.allExtends.concat(this.allExtendsStack[this.allExtendsStack.length-1]); + newAllExtends = newAllExtends.concat(this.doExtendChaining(newAllExtends, mediaNode.allExtends)); + this.allExtendsStack.push(newAllExtends); + }, + visitMediaOut: function (mediaNode) { + this.allExtendsStack.length = this.allExtendsStack.length - 1; + }, + visitDirective: function (directiveNode, visitArgs) { + var newAllExtends = directiveNode.allExtends.concat(this.allExtendsStack[this.allExtendsStack.length-1]); + newAllExtends = newAllExtends.concat(this.doExtendChaining(newAllExtends, directiveNode.allExtends)); + this.allExtendsStack.push(newAllExtends); + }, + visitDirectiveOut: function (directiveNode) { + this.allExtendsStack.length = this.allExtendsStack.length - 1; + } + }; + +})(require('./tree')); + +(function (tree) { + + tree.sourceMapOutput = function (options) { + this._css = []; + this._rootNode = options.rootNode; + this._writeSourceMap = options.writeSourceMap; + this._contentsMap = options.contentsMap; + this._sourceMapFilename = options.sourceMapFilename; + this._outputFilename = options.outputFilename; + this._sourceMapURL = options.sourceMapURL; + this._sourceMapBasepath = options.sourceMapBasepath; + this._sourceMapRootpath = options.sourceMapRootpath; + this._outputSourceFiles = options.outputSourceFiles; + this._sourceMapGeneratorConstructor = options.sourceMapGenerator || require("source-map").SourceMapGenerator; + + if (this._sourceMapRootpath && this._sourceMapRootpath.charAt(this._sourceMapRootpath.length-1) !== '/') { + this._sourceMapRootpath += '/'; + } + + this._lineNumber = 0; + this._column = 0; + }; + + tree.sourceMapOutput.prototype.normalizeFilename = function(filename) { + if (this._sourceMapBasepath && filename.indexOf(this._sourceMapBasepath) === 0) { + filename = filename.substring(this._sourceMapBasepath.length); + if (filename.charAt(0) === '\\' || filename.charAt(0) === '/') { + filename = filename.substring(1); + } + } + return (this._sourceMapRootpath || "") + filename.replace(/\\/g, '/'); + }; + + tree.sourceMapOutput.prototype.add = function(chunk, fileInfo, index, mapLines) { + + //ignore adding empty strings + if (!chunk) { + return; + } + + var lines, + sourceLines, + columns, + sourceColumns, + i; + + if (fileInfo) { + var inputSource = this._contentsMap[fileInfo.filename].substring(0, index); + sourceLines = inputSource.split("\n"); + sourceColumns = sourceLines[sourceLines.length-1]; + } + + lines = chunk.split("\n"); + columns = lines[lines.length-1]; + + if (fileInfo) { + if (!mapLines) { + this._sourceMapGenerator.addMapping({ generated: { line: this._lineNumber + 1, column: this._column}, + original: { line: sourceLines.length, column: sourceColumns.length}, + source: this.normalizeFilename(fileInfo.filename)}); + } else { + for(i = 0; i < lines.length; i++) { + this._sourceMapGenerator.addMapping({ generated: { line: this._lineNumber + i + 1, column: i === 0 ? this._column : 0}, + original: { line: sourceLines.length + i, column: i === 0 ? sourceColumns.length : 0}, + source: this.normalizeFilename(fileInfo.filename)}); + } + } + } + + if (lines.length === 1) { + this._column += columns.length; + } else { + this._lineNumber += lines.length - 1; + this._column = columns.length; + } + + this._css.push(chunk); + }; + + tree.sourceMapOutput.prototype.isEmpty = function() { + return this._css.length === 0; + }; + + tree.sourceMapOutput.prototype.toCSS = function(env) { + this._sourceMapGenerator = new this._sourceMapGeneratorConstructor({ file: this._outputFilename, sourceRoot: null }); + + if (this._outputSourceFiles) { + for(var filename in this._contentsMap) { + this._sourceMapGenerator.setSourceContent(this.normalizeFilename(filename), this._contentsMap[filename]); + } + } + + this._rootNode.genCSS(env, this); + + if (this._css.length > 0) { + var sourceMapURL, + sourceMapContent = JSON.stringify(this._sourceMapGenerator.toJSON()); + + if (this._sourceMapURL) { + sourceMapURL = this._sourceMapURL; + } else if (this._sourceMapFilename) { + sourceMapURL = this.normalizeFilename(this._sourceMapFilename); + } + + if (this._writeSourceMap) { + this._writeSourceMap(sourceMapContent); + } else { + sourceMapURL = "data:application/json," + encodeURIComponent(sourceMapContent); + } + + if (sourceMapURL) { + this._css.push("/*# sourceMappingURL=" + sourceMapURL + " */"); + } + } + + return this._css.join(''); + }; + +})(require('./tree')); + +/*global name:true, less, loadStyleSheet, initRhinoTest, os */ + +if (typeof initRhinoTest === 'function') { // definition of additional test functions (see rhino/test-header.js) + initRhinoTest(); +} + +function formatError(ctx, options) { + options = options || {}; + + var message = ""; + var extract = ctx.extract; + var error = []; +// var stylize = options.color ? require('./lessc_helper').stylize : function (str) { return str; }; + var stylize = function (str) { return str; }; + + // only output a stack if it isn't a less error + if (ctx.stack && !ctx.type) { return stylize(ctx.stack, 'red'); } + + if (!ctx.hasOwnProperty('index') || !extract) { + return ctx.stack || ctx.message; + } + + if (typeof(extract[0]) === 'string') { + error.push(stylize((ctx.line - 1) + ' ' + extract[0], 'grey')); + } + + if (typeof(extract[1]) === 'string') { + var errorTxt = ctx.line + ' '; + if (extract[1]) { + errorTxt += extract[1].slice(0, ctx.column) + + stylize(stylize(stylize(extract[1][ctx.column], 'bold') + + extract[1].slice(ctx.column + 1), 'red'), 'inverse'); + } + error.push(errorTxt); + } + + if (typeof(extract[2]) === 'string') { + error.push(stylize((ctx.line + 1) + ' ' + extract[2], 'grey')); + } + error = error.join('\n') + stylize('', 'reset') + '\n'; + + message += stylize(ctx.type + 'Error: ' + ctx.message, 'red'); + ctx.filename && (message += stylize(' in ', 'red') + ctx.filename + + stylize(' on line ' + ctx.line + ', column ' + (ctx.column + 1) + ':', 'grey')); + + message += '\n' + error; + + if (ctx.callLine) { + message += stylize('from ', 'red') + (ctx.filename || '') + '/n'; + message += stylize(ctx.callLine, 'grey') + ' ' + ctx.callExtract + '/n'; + } + + return message; +} + +function writeError(ctx, options) { + options = options || {}; + if (options.silent) { return; } + print(formatError(ctx, options)); +} + +function loadStyleSheet(sheet, callback, reload, remaining) { + var endOfPath = Math.max(name.lastIndexOf('/'), name.lastIndexOf('\\')), + sheetName = name.slice(0, endOfPath + 1) + sheet.href, + contents = sheet.contents || {}, + input = readFile(sheetName); + + input = input.replace(/^\xEF\xBB\xBF/, ''); + + contents[sheetName] = input; + + var parser = new less.Parser({ + paths: [sheet.href.replace(/[\w\.-]+$/, '')], + contents: contents + }); + parser.parse(input, function (e, root) { + if (e) { + return writeError(e); + } + try { + callback(e, root, input, sheet, { local: false, lastModified: 0, remaining: remaining }, sheetName); + } catch(e) { + writeError(e); + } + }); +} + +less.Parser.fileLoader = function (file, currentFileInfo, callback, env) { + + var href = file; + if (currentFileInfo && currentFileInfo.currentDirectory && !/^\//.test(file)) { + href = less.modules.path.join(currentFileInfo.currentDirectory, file); + } + + var path = less.modules.path.dirname(href); + + var newFileInfo = { + currentDirectory: path + '/', + filename: href + }; + + if (currentFileInfo) { + newFileInfo.entryPath = currentFileInfo.entryPath; + newFileInfo.rootpath = currentFileInfo.rootpath; + newFileInfo.rootFilename = currentFileInfo.rootFilename; + newFileInfo.relativeUrls = currentFileInfo.relativeUrls; + } else { + newFileInfo.entryPath = path; + newFileInfo.rootpath = less.rootpath || path; + newFileInfo.rootFilename = href; + newFileInfo.relativeUrls = env.relativeUrls; + } + + var j = file.lastIndexOf('/'); + if(newFileInfo.relativeUrls && !/^(?:[a-z-]+:|\/)/.test(file) && j != -1) { + var relativeSubDirectory = file.slice(0, j+1); + newFileInfo.rootpath = newFileInfo.rootpath + relativeSubDirectory; // append (sub|sup) directory path of imported file + } + newFileInfo.currentDirectory = path; + newFileInfo.filename = href; + + var data = null; + try { + data = readFile(href); + } catch (e) { + callback({ type: 'File', message: "'" + less.modules.path.basename(href) + "' wasn't found" }); + return; + } + + try { + callback(null, data, href, newFileInfo, { lastModified: 0 }); + } catch (e) { + callback(e, null, href); + } +}; + + +function writeFile(filename, content) { + var fstream = new java.io.FileWriter(filename); + var out = new java.io.BufferedWriter(fstream); + out.write(content); + out.close(); +} + +// Command line integration via Rhino +(function (args) { + + var options = { + depends: false, + compress: false, + cleancss: false, + max_line_len: -1, + optimization: 1, + silent: false, + verbose: false, + lint: false, + paths: [], + color: true, + strictImports: false, + rootpath: '', + relativeUrls: false, + ieCompat: true, + strictMath: false, + strictUnits: false + }; + var continueProcessing = true, + currentErrorcode; + + var checkArgFunc = function(arg, option) { + if (!option) { + print(arg + " option requires a parameter"); + continueProcessing = false; + return false; + } + return true; + }; + + var checkBooleanArg = function(arg) { + var onOff = /^((on|t|true|y|yes)|(off|f|false|n|no))$/i.exec(arg); + if (!onOff) { + print(" unable to parse "+arg+" as a boolean. use one of on/t/true/y/yes/off/f/false/n/no"); + continueProcessing = false; + return false; + } + return Boolean(onOff[2]); + }; + + var warningMessages = ""; + + args = args.filter(function (arg) { + var match; + + if (match = arg.match(/^-I(.+)$/)) { + options.paths.push(match[1]); + return false; + } + + if (match = arg.match(/^--?([a-z][0-9a-z-]*)(?:=(.*))?$/i)) { arg = match[1]; } // was (?:=([^\s]*)), check! + else { return arg; } + + switch (arg) { + case 'v': + case 'version': + console.log("lessc " + less.version.join('.') + " (LESS Compiler) [JavaScript]"); + continueProcessing = false; + break; + case 'verbose': + options.verbose = true; + break; + case 's': + case 'silent': + options.silent = true; + break; + case 'l': + case 'lint': + options.lint = true; + break; + case 'strict-imports': + options.strictImports = true; + break; + case 'h': + case 'help': + //TODO +// require('../lib/less/lessc_helper').printUsage(); + continueProcessing = false; + break; + case 'x': + case 'compress': + options.compress = true; + break; + case 'M': + case 'depends': + options.depends = true; + break; + case 'yui-compress': + warningMessages += "yui-compress option has been removed. assuming clean-css."; + options.cleancss = true; + break; + case 'clean-css': + options.cleancss = true; + break; + case 'max-line-len': + if (checkArgFunc(arg, match[2])) { + options.maxLineLen = parseInt(match[2], 10); + if (options.maxLineLen <= 0) { + options.maxLineLen = -1; + } + } + break; + case 'no-color': + options.color = false; + break; + case 'no-ie-compat': + options.ieCompat = false; + break; + case 'no-js': + options.javascriptEnabled = false; + break; + case 'include-path': + if (checkArgFunc(arg, match[2])) { + options.paths = match[2].split(os.type().match(/Windows/) ? ';' : ':') + .map(function(p) { + if (p) { +// return path.resolve(process.cwd(), p); + return p; + } + }); + } + break; + case 'O0': options.optimization = 0; break; + case 'O1': options.optimization = 1; break; + case 'O2': options.optimization = 2; break; + case 'line-numbers': + if (checkArgFunc(arg, match[2])) { + options.dumpLineNumbers = match[2]; + } + break; + case 'source-map': + if (!match[2]) { + options.sourceMap = true; + } else { + options.sourceMap = match[2]; + } + break; + case 'source-map-rootpath': + if (checkArgFunc(arg, match[2])) { + options.sourceMapRootpath = match[2]; + } + break; + case 'source-map-inline': + options.outputSourceFiles = true; + break; + case 'rp': + case 'rootpath': + if (checkArgFunc(arg, match[2])) { + options.rootpath = match[2].replace(/\\/g, '/'); + } + break; + case "ru": + case "relative-urls": + options.relativeUrls = true; + break; + case "sm": + case "strict-math": + if (checkArgFunc(arg, match[2])) { + options.strictMath = checkBooleanArg(match[2]); + } + break; + case "su": + case "strict-units": + if (checkArgFunc(arg, match[2])) { + options.strictUnits = checkBooleanArg(match[2]); + } + break; + default: + console.log('invalid option ' + arg); + continueProcessing = false; + } + }); + + if (!continueProcessing) { + return; + } + + var name = args[0]; + if (name && name != '-') { +// name = path.resolve(process.cwd(), name); + } + var output = args[1]; + var outputbase = args[1]; + if (output) { + options.sourceMapOutputFilename = output; +// output = path.resolve(process.cwd(), output); + if (warningMessages) { + console.log(warningMessages); + } + } + +// options.sourceMapBasepath = process.cwd(); + options.sourceMapBasepath = ''; + + if (options.sourceMap === true) { + if (!output) { + console.log("the sourcemap option only has an optional filename if the css filename is given"); + return; + } + options.sourceMapFullFilename = options.sourceMapOutputFilename + ".map"; + options.sourceMap = less.modules.path.basename(options.sourceMapFullFilename); + } + + if (!name) { + console.log("lessc: no inout files"); + console.log(""); + // TODO +// require('../lib/less/lessc_helper').printUsage(); + currentErrorcode = 1; + return; + } + +// var ensureDirectory = function (filepath) { +// var dir = path.dirname(filepath), +// cmd, +// existsSync = fs.existsSync || path.existsSync; +// if (!existsSync(dir)) { +// if (mkdirp === undefined) { +// try {mkdirp = require('mkdirp');} +// catch(e) { mkdirp = null; } +// } +// cmd = mkdirp && mkdirp.sync || fs.mkdirSync; +// cmd(dir); +// } +// }; + + if (options.depends) { + if (!outputbase) { + console.log("option --depends requires an output path to be specified"); + return; + } + console.log(outputbase + ": "); + } + + if (!name) { + console.log('No files present in the fileset'); + quit(1); + } + + var input = readFile(name, 'utf-8'); + + if (!input) { + console.log('lesscss: couldn\'t open file ' + name); + quit(1); + } + + options.filename = name; + var result; + try { + var parser = new less.Parser(options); + parser.parse(input, function (e, root) { + if (e) { + writeError(e, options); + quit(1); + } else { + result = root.toCSS(options); + if (output) { + writeFile(output, result); + console.log("Written to " + output); + } else { + print(result); + } + quit(0); + } + }); + } + catch(e) { + writeError(e, options); + quit(1); + } + console.log("done"); +}(arguments)); diff --git a/src/test/java/org/lesscss/LessCompilerTest.java b/src/test/java/org/lesscss/LessCompilerTest.java index 4b4d6cf..3b8494f 100644 --- a/src/test/java/org/lesscss/LessCompilerTest.java +++ b/src/test/java/org/lesscss/LessCompilerTest.java @@ -24,6 +24,7 @@ import static org.powermock.api.mockito.PowerMockito.verifyStatic; import static org.powermock.api.mockito.PowerMockito.when; import static org.powermock.api.mockito.PowerMockito.whenNew; + import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -33,6 +34,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; + import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.reflect.FieldUtils; import org.junit.Before; @@ -43,11 +45,12 @@ import org.mozilla.javascript.Context; import org.mozilla.javascript.Function; import org.mozilla.javascript.JavaScriptException; +import org.mozilla.javascript.Script; import org.mozilla.javascript.Scriptable; +import org.mozilla.javascript.ScriptableObject; import org.mozilla.javascript.tools.shell.Global; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; - import org.lesscss.LessCompiler; import org.lesscss.LessException; import org.lesscss.LessSource; @@ -65,7 +68,7 @@ public class LessCompilerTest { @Mock private Context cx; @Mock private Global global; @Mock private Scriptable scope; - @Mock private Function doIt; + @Mock private InterpretedFunction compiler; @Mock private URL envJsFile; @Mock private URLConnection envJsURLConnection; @@ -83,9 +86,13 @@ public class LessCompilerTest { @Mock private File outputFile; @Mock private LessSource lessSource; + @Mock private ScriptableObject compileScope; + private String less = "less"; private String css = "css"; + public abstract static class InterpretedFunction implements Script, Function {} + @Before public void setUp() throws Exception { lessCompiler = new LessCompiler(); @@ -101,7 +108,7 @@ public void testNewLessCompiler() throws Exception { assertEquals(Collections.EMPTY_LIST, FieldUtils.readField(lessCompiler, "customJs", true)); } - @Test + @Test(expected = IllegalArgumentException.class) public void testSetEnvJs() throws Exception { URL envJsFile = new File("my-env.js").toURI().toURL(); lessCompiler.setEnvJs(envJsFile); @@ -142,7 +149,7 @@ public void testInit() throws Exception { whenNew(Global.class).withNoArguments().thenReturn(global); when(cx.initStandardObjects(global)).thenReturn(scope); - when(cx.compileFunction(scope, COMPILE_STRING, "doIt.js", 1, null)).thenReturn(doIt); + when(cx.compileReader(new InputStreamReader(lessJsInputStream), lessJsURLToString, 1, null)).thenReturn(compiler); when(envJsFile.openConnection()).thenReturn(envJsURLConnection); when(envJsFile.toString()).thenReturn(envJsURLToString); @@ -154,7 +161,6 @@ public void testInit() throws Exception { when(lessJsURLConnection.getInputStream()).thenReturn(lessJsInputStream); whenNew(InputStreamReader.class).withArguments(lessJsInputStream).thenReturn(lessJsInputStreamReader); - lessCompiler.setEnvJs(envJsFile); lessCompiler.setLessJs(lessJsFile); lessCompiler.init(); @@ -177,8 +183,8 @@ public void testInit() throws Exception { verify(cx).evaluateReader(scope, lessJsInputStreamReader, lessJsURLToString, 1, null); } - @Test(expected = IllegalStateException.class) - public void testInitThrowsIllegalStateExceptionWhenNotAbleToInitilize() throws Exception { + @Test(expected = IllegalArgumentException.class) + public void testInitThrowsIllegalArgumentExceptionWhenNotAbleToInitilize() throws Exception { mockStatic(Context.class); when(Context.enter()).thenReturn(cx); @@ -213,7 +219,7 @@ public void testCompileStringWhenNotInitialized() throws Exception { whenNew(Global.class).withNoArguments().thenReturn(global); when(cx.initStandardObjects(global)).thenReturn(scope); - when(cx.compileFunction(scope, COMPILE_STRING, "doIt.js", 1, null)).thenReturn(doIt); + when(cx.compileReader(new InputStreamReader(lessJsInputStream), lessJsURLToString, 1, null)).thenReturn(compiler); when(envJsFile.openConnection()).thenReturn(envJsURLConnection); when(envJsFile.toString()).thenReturn(envJsURLToString); @@ -225,7 +231,7 @@ public void testCompileStringWhenNotInitialized() throws Exception { when(lessJsURLConnection.getInputStream()).thenReturn(lessJsInputStream); whenNew(InputStreamReader.class).withArguments(lessJsInputStream).thenReturn(lessJsInputStreamReader); - when(doIt.call(cx, scope, null, new Object[]{less, false})).thenReturn(css); + when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); lessCompiler.setEnvJs(envJsFile); lessCompiler.setLessJs(lessJsFile); @@ -251,7 +257,7 @@ public void testCompileStringWhenNotInitialized() throws Exception { verify(cx).compileFunction(scope, COMPILE_STRING, "doIt.js", 1, null); - verify(doIt).call(cx, scope, null, new Object[]{less, false}); + verify(compiler).call(cx, compileScope, null, new Object[] {}); } @Test @@ -259,13 +265,13 @@ public void testCompileStringToString() throws Exception { mockStatic(Context.class); when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); - FieldUtils.writeField(lessCompiler, "doIt", doIt, true); + FieldUtils.writeField(lessCompiler, "compiler", compiler, true); - when(doIt.call(cx, scope, null, new Object[]{less, false})).thenReturn(css); + when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); assertEquals(css, lessCompiler.compile(less)); - verify(doIt).call(cx, scope, null, new Object[]{less, false}); + verify(compiler).call(cx, compileScope, null, new Object[] {}); } @Test @@ -273,19 +279,19 @@ public void testCompileFileToString() throws Exception { mockStatic(Context.class); when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); - FieldUtils.writeField(lessCompiler, "doIt", doIt, true); + FieldUtils.writeField(lessCompiler, "compiler", compiler, true); whenNew(LessSource.class).withArguments(inputFile).thenReturn(lessSource); when(lessSource.getNormalizedContent()).thenReturn(less); - when(doIt.call(cx, scope, null, new Object[]{less, false})).thenReturn(css); + when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); assertEquals(css, lessCompiler.compile(inputFile)); verifyNew(LessSource.class).withArguments(inputFile); verify(lessSource).getNormalizedContent(); - verify(doIt).call(cx, scope, null, new Object[]{less, false}); + verify(compiler).call(cx, compileScope, null, new Object[] {}); } @Test @@ -293,12 +299,12 @@ public void testCompileFileToFile() throws Exception { mockStatic(Context.class); when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); - FieldUtils.writeField(lessCompiler, "doIt", doIt, true); + FieldUtils.writeField(lessCompiler, "compiler", compiler, true); whenNew(LessSource.class).withArguments(inputFile).thenReturn(lessSource); when(lessSource.getNormalizedContent()).thenReturn(less); - when(doIt.call(cx, scope, null, new Object[]{less, false})).thenReturn(css); + when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); mockStatic(FileUtils.class); @@ -307,7 +313,7 @@ public void testCompileFileToFile() throws Exception { verifyNew(LessSource.class).withArguments(inputFile); verify(lessSource).getNormalizedContent(); - verify(doIt).call(cx, scope, null, new Object[]{less, false}); + verify(compiler).call(cx, compileScope, null, new Object[] {}); verifyStatic(); FileUtils.writeStringToFile(outputFile, css, (String) null); @@ -318,12 +324,12 @@ public void testCompileFileToFileWithForceTrue() throws Exception { mockStatic(Context.class); when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); - FieldUtils.writeField(lessCompiler, "doIt", doIt, true); + FieldUtils.writeField(lessCompiler, "compiler", compiler, true); whenNew(LessSource.class).withArguments(inputFile).thenReturn(lessSource); when(lessSource.getNormalizedContent()).thenReturn(less); - when(doIt.call(cx, scope, null, new Object[]{less, false})).thenReturn(css); + when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); mockStatic(FileUtils.class); @@ -332,7 +338,7 @@ public void testCompileFileToFileWithForceTrue() throws Exception { verifyNew(LessSource.class).withArguments(inputFile); verify(lessSource).getNormalizedContent(); - verify(doIt).call(cx, scope, null, new Object[]{less, false}); + verify(compiler).call(cx, compileScope, null, new Object[] {}); verifyStatic(); FileUtils.writeStringToFile(outputFile, css, (String) null); @@ -343,14 +349,14 @@ public void testCompileFileToFileWithForceFalseAndOutputNotExists() throws Excep mockStatic(Context.class); when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); - FieldUtils.writeField(lessCompiler, "doIt", doIt, true); + FieldUtils.writeField(lessCompiler, "compiler", compiler, true); when(outputFile.exists()).thenReturn(false); whenNew(LessSource.class).withArguments(inputFile).thenReturn(lessSource); when(lessSource.getNormalizedContent()).thenReturn(less); - when(doIt.call(cx, scope, null, new Object[]{less, false})).thenReturn(css); + when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); mockStatic(FileUtils.class); @@ -362,7 +368,7 @@ public void testCompileFileToFileWithForceFalseAndOutputNotExists() throws Excep verify(lessSource).getNormalizedContent(); - verify(doIt).call(cx, scope, null, new Object[]{less, false}); + verify(compiler).call(cx, compileScope, null, new Object[] {}); verifyStatic(); FileUtils.writeStringToFile(outputFile, css, (String) null); @@ -373,17 +379,16 @@ public void testCompileFileToFileWithForceFalseAndOutputExistsAndLessSourceModif mockStatic(Context.class); when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); - FieldUtils.writeField(lessCompiler, "doIt", doIt, true); + FieldUtils.writeField(lessCompiler, "compiler", compiler, true); - whenNew(LessSource.class).withArguments(inputFile).thenReturn(lessSource); + when(cx.newObject(scope)).thenReturn(compileScope); when(outputFile.exists()).thenReturn(true); when(outputFile.lastModified()).thenReturn(1l); - when(lessSource.getLastModifiedIncludingImports()).thenReturn(2l); - when(lessSource.getNormalizedContent()).thenReturn(less); - - when(doIt.call(cx, scope, null, new Object[]{less, false})).thenReturn(css); + when(inputFile.lastModified()).thenReturn(2l); + + when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); mockStatic(FileUtils.class); @@ -397,7 +402,7 @@ public void testCompileFileToFileWithForceFalseAndOutputExistsAndLessSourceModif verify(lessSource).getLastModifiedIncludingImports(); verify(lessSource).getNormalizedContent(); - verify(doIt).call(cx, scope, null, new Object[]{less, false}); + verify(compiler).call(cx, compileScope, null, new Object[] {}); verifyStatic(); FileUtils.writeStringToFile(outputFile, css, (String) null); @@ -408,7 +413,7 @@ public void testCompileFileToFileWithForceFalseAndOutputExistsAndLessSourceNotMo mockStatic(Context.class); when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); - FieldUtils.writeField(lessCompiler, "doIt", doIt, true); + FieldUtils.writeField(lessCompiler, "compiler", compiler, true); whenNew(LessSource.class).withArguments(inputFile).thenReturn(lessSource); @@ -432,17 +437,17 @@ public void testCompileLessSourceToString() throws Exception { mockStatic(Context.class); when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); - FieldUtils.writeField(lessCompiler, "doIt", doIt, true); + FieldUtils.writeField(lessCompiler, "compiler", compiler, true); when(lessSource.getNormalizedContent()).thenReturn(less); - when(doIt.call(cx, scope, null, new Object[]{less, false})).thenReturn(css); + when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); assertEquals(css, lessCompiler.compile(lessSource)); verify(lessSource).getNormalizedContent(); - verify(doIt).call(cx, scope, null, new Object[]{less, false}); + verify(compiler).call(cx, compileScope, null, new Object[] {}); } @Test @@ -450,11 +455,11 @@ public void testCompileLessSourceToFile() throws Exception { mockStatic(Context.class); when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); - FieldUtils.writeField(lessCompiler, "doIt", doIt, true); + FieldUtils.writeField(lessCompiler, "compiler", compiler, true); when(lessSource.getNormalizedContent()).thenReturn(less); - when(doIt.call(cx, scope, null, new Object[]{less, false})).thenReturn(css); + when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); mockStatic(FileUtils.class); @@ -462,7 +467,7 @@ public void testCompileLessSourceToFile() throws Exception { verify(lessSource).getNormalizedContent(); - verify(doIt).call(cx, scope, null, new Object[]{less, false}); + verify(compiler).call(cx, compileScope, null, new Object[] {}); verifyStatic(); FileUtils.writeStringToFile(outputFile, css, (String) null); @@ -473,11 +478,11 @@ public void testCompileLessSourceToFileWithForceTrue() throws Exception { mockStatic(Context.class); when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); - FieldUtils.writeField(lessCompiler, "doIt", doIt, true); + FieldUtils.writeField(lessCompiler, "compiler", compiler, true); when(lessSource.getNormalizedContent()).thenReturn(less); - when(doIt.call(cx, scope, null, new Object[]{less, false})).thenReturn(css); + when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); mockStatic(FileUtils.class); @@ -485,7 +490,7 @@ public void testCompileLessSourceToFileWithForceTrue() throws Exception { verify(lessSource).getNormalizedContent(); - verify(doIt).call(cx, scope, null, new Object[]{less, false}); + verify(compiler).call(cx, compileScope, null, new Object[] {}); verifyStatic(); FileUtils.writeStringToFile(outputFile, css, (String) null); @@ -496,13 +501,13 @@ public void testCompileLessSourceToFileWithForceFalseAndOutputNotExists() throws mockStatic(Context.class); when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); - FieldUtils.writeField(lessCompiler, "doIt", doIt, true); + FieldUtils.writeField(lessCompiler, "compiler", compiler, true); when(outputFile.exists()).thenReturn(false); when(lessSource.getNormalizedContent()).thenReturn(less); - when(doIt.call(cx, scope, null, new Object[]{less, false})).thenReturn(css); + when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); mockStatic(FileUtils.class); @@ -512,7 +517,7 @@ public void testCompileLessSourceToFileWithForceFalseAndOutputNotExists() throws verify(lessSource).getNormalizedContent(); - verify(doIt).call(cx, scope, null, new Object[]{less, false}); + verify(compiler).call(cx, compileScope, null, new Object[] {}); verifyStatic(); FileUtils.writeStringToFile(outputFile, css, (String) null); @@ -523,7 +528,7 @@ public void testCompileLessSourceToFileWithForceFalseAndOutputExistsAndLessSourc mockStatic(Context.class); when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); - FieldUtils.writeField(lessCompiler, "doIt", doIt, true); + FieldUtils.writeField(lessCompiler, "compiler", compiler, true); when(outputFile.exists()).thenReturn(true); when(outputFile.lastModified()).thenReturn(1l); @@ -531,7 +536,7 @@ public void testCompileLessSourceToFileWithForceFalseAndOutputExistsAndLessSourc when(lessSource.getLastModifiedIncludingImports()).thenReturn(2l); when(lessSource.getNormalizedContent()).thenReturn(less); - when(doIt.call(cx, scope, null, new Object[]{less, false})).thenReturn(css); + when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); mockStatic(FileUtils.class); @@ -543,7 +548,7 @@ public void testCompileLessSourceToFileWithForceFalseAndOutputExistsAndLessSourc verify(lessSource).getLastModifiedIncludingImports(); verify(lessSource).getNormalizedContent(); - verify(doIt).call(cx, scope, null, new Object[]{less, false}); + verify(compiler).call(cx, compileScope, null, new Object[] {}); verifyStatic(); FileUtils.writeStringToFile(outputFile, css, (String) null); @@ -554,7 +559,7 @@ public void testCompileLessSourceToFileWithForceFalseAndOutputExistsAndLessSourc mockStatic(Context.class); when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); - FieldUtils.writeField(lessCompiler, "doIt", doIt, true); + FieldUtils.writeField(lessCompiler, "compiler", compiler, true); when(outputFile.exists()).thenReturn(true); when(outputFile.lastModified()).thenReturn(2l); @@ -574,10 +579,10 @@ public void testCompileThrowsLessExceptionWhenCompilationFails() throws Exceptio mockStatic(Context.class); when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); - FieldUtils.writeField(lessCompiler, "doIt", doIt, true); + FieldUtils.writeField(lessCompiler, "compiler", compiler, true); JavaScriptException javaScriptException = new JavaScriptException(null, null, 0); - when(doIt.call(cx, scope, null, new Object[]{less, false})).thenThrow(javaScriptException); + when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(javaScriptException); assertEquals(css, lessCompiler.compile(less)); } @@ -588,13 +593,13 @@ public void testCompress() throws Exception { when(Context.enter()).thenReturn(cx); lessCompiler.setCompress(true); FieldUtils.writeField(lessCompiler, "scope", scope, true); - FieldUtils.writeField(lessCompiler, "doIt", doIt, true); + FieldUtils.writeField(lessCompiler, "compiler", compiler, true); - when(doIt.call(cx, scope, null, new Object[]{less, true})).thenReturn(css); + when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); assertEquals(css, lessCompiler.compile(less)); - verify(doIt).call(cx, scope, null, new Object[]{less, true}); + verify(compiler).call(cx, compileScope, null, new Object[] {}); } @Test @@ -603,12 +608,12 @@ public void testEncoding() throws Exception { when(Context.enter()).thenReturn(cx); lessCompiler.setEncoding("utf-8"); FieldUtils.writeField(lessCompiler, "scope", scope, true); - FieldUtils.writeField(lessCompiler, "doIt", doIt, true); + FieldUtils.writeField(lessCompiler, "compiler", compiler, true); whenNew(LessSource.class).withArguments(inputFile).thenReturn(lessSource); when(lessSource.getNormalizedContent()).thenReturn(less); - when(doIt.call(cx, scope, null, new Object[]{less, false})).thenReturn(css); + when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); mockStatic(FileUtils.class); @@ -617,7 +622,7 @@ public void testEncoding() throws Exception { verifyNew(LessSource.class).withArguments(inputFile); verify(lessSource).getNormalizedContent(); - verify(doIt).call(cx, scope, null, new Object[]{less, false}); + verify(compiler).call(cx, compileScope, null, new Object[] {}); verifyStatic(); FileUtils.writeStringToFile(outputFile, css, "utf-8"); From d044aae34c0140d561b9eab05c14d31ae19bb59c Mon Sep 17 00:00:00 2001 From: Doug Haber Date: Tue, 21 Jan 2014 11:40:16 -0500 Subject: [PATCH 08/28] Fix compiler tests --- src/main/java/org/lesscss/LessCompiler.java | 8 +- .../java/org/lesscss/LessCompilerTest.java | 139 +++++++++--------- 2 files changed, 76 insertions(+), 71 deletions(-) diff --git a/src/main/java/org/lesscss/LessCompiler.java b/src/main/java/org/lesscss/LessCompiler.java index 3edca27..781d0c2 100644 --- a/src/main/java/org/lesscss/LessCompiler.java +++ b/src/main/java/org/lesscss/LessCompiler.java @@ -301,11 +301,9 @@ public String compile(String input) throws LessException { public String compile(String input, String name) throws LessException { File tempFile = null; try { - tempFile = File.createTempFile("tmp", "less.tmp"); - PrintWriter writer = new PrintWriter(tempFile); - writer.print(input); - writer.close(); - + tempFile = File.createTempFile("tmp", "less.tmp"); + FileUtils.writeStringToFile(tempFile, input, this.encoding); + return compile( tempFile, ""); } catch (IOException e) { throw new LessException(e); diff --git a/src/test/java/org/lesscss/LessCompilerTest.java b/src/test/java/org/lesscss/LessCompilerTest.java index 3b8494f..58b4b07 100644 --- a/src/test/java/org/lesscss/LessCompilerTest.java +++ b/src/test/java/org/lesscss/LessCompilerTest.java @@ -25,6 +25,7 @@ import static org.powermock.api.mockito.PowerMockito.when; import static org.powermock.api.mockito.PowerMockito.whenNew; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -78,7 +79,7 @@ public class LessCompilerTest { @Mock private URL lessJsFile; @Mock private URLConnection lessJsURLConnection; - private static final String lessJsURLToString = "less.js"; + private static final String lessJsURLToString = "less-rhino-1.5.1.js"; @Mock private InputStream lessJsInputStream; @Mock private InputStreamReader lessJsInputStreamReader; @@ -87,6 +88,7 @@ public class LessCompilerTest { @Mock private LessSource lessSource; @Mock private ScriptableObject compileScope; + @Mock private ByteArrayOutputStream out; private String less = "less"; private String css = "css"; @@ -103,8 +105,7 @@ public void setUp() throws Exception { @Test public void testNewLessCompiler() throws Exception { - assertEquals(LessCompiler.class.getClassLoader().getResource("META-INF/env.rhino.js"), FieldUtils.readField(lessCompiler, "envJs", true)); - assertEquals(LessCompiler.class.getClassLoader().getResource("META-INF/less.js"), FieldUtils.readField(lessCompiler, "lessJs", true)); + assertEquals(LessCompiler.class.getClassLoader().getResource("META-INF/less-rhino-1.5.1.js"), FieldUtils.readField(lessCompiler, "lessJs", true)); assertEquals(Collections.EMPTY_LIST, FieldUtils.readField(lessCompiler, "customJs", true)); } @@ -149,7 +150,7 @@ public void testInit() throws Exception { whenNew(Global.class).withNoArguments().thenReturn(global); when(cx.initStandardObjects(global)).thenReturn(scope); - when(cx.compileReader(new InputStreamReader(lessJsInputStream), lessJsURLToString, 1, null)).thenReturn(compiler); + when(cx.compileReader(lessJsInputStreamReader, lessJsURLToString, 1, null)).thenReturn(compiler); when(envJsFile.openConnection()).thenReturn(envJsURLConnection); when(envJsFile.toString()).thenReturn(envJsURLToString); @@ -166,21 +167,21 @@ public void testInit() throws Exception { verifyStatic(); Context.enter(); - verify(cx).setOptimizationLevel(-1); + //verify(cx).setOptimizationLevel(-1); verify(cx).setLanguageVersion(Context.VERSION_1_7); verifyNew(Global.class).withNoArguments(); verify(global).init(cx); // verify(envJsFile).openConnection(); - verify(envJsURLConnection).getInputStream(); - verifyNew(InputStreamReader.class).withArguments(envJsInputStream); - verify(cx).evaluateReader(scope, envJsInputStreamReader, envJsURLToString, 1, null); + //verify(envJsURLConnection).getInputStream(); + //verifyNew(InputStreamReader.class).withArguments(envJsInputStream); + //verify(cx).evaluateReader(scope, envJsInputStreamReader, envJsURLToString, 1, null); // verify(lessJsFile).openConnection(); verify(lessJsURLConnection).getInputStream(); verifyNew(InputStreamReader.class).withArguments(lessJsInputStream); - verify(cx).evaluateReader(scope, lessJsInputStreamReader, lessJsURLToString, 1, null); + verify(cx).compileReader(lessJsInputStreamReader, lessJsURLToString, 1, null); } @Test(expected = IllegalArgumentException.class) @@ -191,6 +192,7 @@ public void testInitThrowsIllegalArgumentExceptionWhenNotAbleToInitilize() throw whenNew(Global.class).withNoArguments().thenReturn(global); when(cx.initStandardObjects(global)).thenReturn(scope); + when(cx.compileReader(lessJsInputStreamReader, lessJsURLToString, 1, null)).thenReturn(compiler); when(envJsFile.openConnection()).thenThrow(new IOException()); @@ -219,7 +221,7 @@ public void testCompileStringWhenNotInitialized() throws Exception { whenNew(Global.class).withNoArguments().thenReturn(global); when(cx.initStandardObjects(global)).thenReturn(scope); - when(cx.compileReader(new InputStreamReader(lessJsInputStream), lessJsURLToString, 1, null)).thenReturn(compiler); + when(cx.compileReader(lessJsInputStreamReader, lessJsURLToString, 1, null)).thenReturn(compiler); when(envJsFile.openConnection()).thenReturn(envJsURLConnection); when(envJsFile.toString()).thenReturn(envJsURLToString); @@ -231,32 +233,31 @@ public void testCompileStringWhenNotInitialized() throws Exception { when(lessJsURLConnection.getInputStream()).thenReturn(lessJsInputStream); whenNew(InputStreamReader.class).withArguments(lessJsInputStream).thenReturn(lessJsInputStreamReader); - when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); + when(cx.newObject(scope)).thenReturn(compileScope); + whenNew(ByteArrayOutputStream.class).withNoArguments().thenReturn(out); + when(out.toString()).thenReturn(css); - lessCompiler.setEnvJs(envJsFile); lessCompiler.setLessJs(lessJsFile); assertEquals(css, lessCompiler.compile(less)); //verifyStatic(); - verify(cx).setOptimizationLevel(-1); + //verify(cx).setOptimizationLevel(-1); verify(cx).setLanguageVersion(Context.VERSION_1_7); verifyNew(Global.class).withNoArguments(); verify(global).init(cx); // verify(envJsFile).openConnection(); - verify(envJsURLConnection).getInputStream(); - verifyNew(InputStreamReader.class).withArguments(envJsInputStream); - verify(cx).evaluateReader(scope, envJsInputStreamReader, envJsURLToString, 1, null); + //verify(envJsURLConnection).getInputStream(); + //verifyNew(InputStreamReader.class).withArguments(envJsInputStream); + //verify(cx).evaluateReader(scope, envJsInputStreamReader, envJsURLToString, 1, null); // verify(lessJsFile).openConnection(); verify(lessJsURLConnection).getInputStream(); verifyNew(InputStreamReader.class).withArguments(lessJsInputStream); - verify(cx).evaluateReader(scope, lessJsInputStreamReader, lessJsURLToString, 1, null); - - verify(cx).compileFunction(scope, COMPILE_STRING, "doIt.js", 1, null); - + verify(cx).compileReader(lessJsInputStreamReader, lessJsURLToString, 1, null); + verify(compiler).call(cx, compileScope, null, new Object[] {}); } @@ -266,8 +267,10 @@ public void testCompileStringToString() throws Exception { when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); FieldUtils.writeField(lessCompiler, "compiler", compiler, true); + FieldUtils.writeField(lessCompiler, "out", out, true); + when(cx.newObject(scope)).thenReturn(compileScope); - when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); + when(out.toString()).thenReturn(css); assertEquals(css, lessCompiler.compile(less)); @@ -280,17 +283,16 @@ public void testCompileFileToString() throws Exception { when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); FieldUtils.writeField(lessCompiler, "compiler", compiler, true); + FieldUtils.writeField(lessCompiler, "out", out, true); + when(cx.newObject(scope)).thenReturn(compileScope); whenNew(LessSource.class).withArguments(inputFile).thenReturn(lessSource); when(lessSource.getNormalizedContent()).thenReturn(less); - when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); + when(out.toString()).thenReturn(css); assertEquals(css, lessCompiler.compile(inputFile)); - verifyNew(LessSource.class).withArguments(inputFile); - verify(lessSource).getNormalizedContent(); - verify(compiler).call(cx, compileScope, null, new Object[] {}); } @@ -300,19 +302,18 @@ public void testCompileFileToFile() throws Exception { when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); FieldUtils.writeField(lessCompiler, "compiler", compiler, true); + FieldUtils.writeField(lessCompiler, "out", out, true); + when(cx.newObject(scope)).thenReturn(compileScope); whenNew(LessSource.class).withArguments(inputFile).thenReturn(lessSource); when(lessSource.getNormalizedContent()).thenReturn(less); - when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); + when(out.toString()).thenReturn(css); mockStatic(FileUtils.class); lessCompiler.compile(inputFile, outputFile); - - verifyNew(LessSource.class).withArguments(inputFile); - verify(lessSource).getNormalizedContent(); - + verify(compiler).call(cx, compileScope, null, new Object[] {}); verifyStatic(); @@ -325,19 +326,18 @@ public void testCompileFileToFileWithForceTrue() throws Exception { when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); FieldUtils.writeField(lessCompiler, "compiler", compiler, true); + FieldUtils.writeField(lessCompiler, "out", out, true); + when(cx.newObject(scope)).thenReturn(compileScope); whenNew(LessSource.class).withArguments(inputFile).thenReturn(lessSource); when(lessSource.getNormalizedContent()).thenReturn(less); - when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); + when(out.toString()).thenReturn(css); mockStatic(FileUtils.class); lessCompiler.compile(inputFile, outputFile, true); - - verifyNew(LessSource.class).withArguments(inputFile); - verify(lessSource).getNormalizedContent(); - + verify(compiler).call(cx, compileScope, null, new Object[] {}); verifyStatic(); @@ -350,24 +350,22 @@ public void testCompileFileToFileWithForceFalseAndOutputNotExists() throws Excep when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); FieldUtils.writeField(lessCompiler, "compiler", compiler, true); + FieldUtils.writeField(lessCompiler, "out", out, true); + when(cx.newObject(scope)).thenReturn(compileScope); when(outputFile.exists()).thenReturn(false); whenNew(LessSource.class).withArguments(inputFile).thenReturn(lessSource); when(lessSource.getNormalizedContent()).thenReturn(less); - when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); + when(out.toString()).thenReturn(css); mockStatic(FileUtils.class); lessCompiler.compile(inputFile, outputFile, false); - verifyNew(LessSource.class).withArguments(inputFile); - verify(outputFile).exists(); - - verify(lessSource).getNormalizedContent(); - + verify(compiler).call(cx, compileScope, null, new Object[] {}); verifyStatic(); @@ -380,7 +378,7 @@ public void testCompileFileToFileWithForceFalseAndOutputExistsAndLessSourceModif when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); FieldUtils.writeField(lessCompiler, "compiler", compiler, true); - + FieldUtils.writeField(lessCompiler, "out", out, true); when(cx.newObject(scope)).thenReturn(compileScope); when(outputFile.exists()).thenReturn(true); @@ -388,20 +386,15 @@ public void testCompileFileToFileWithForceFalseAndOutputExistsAndLessSourceModif when(inputFile.lastModified()).thenReturn(2l); - when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); + when(out.toString()).thenReturn(css); mockStatic(FileUtils.class); lessCompiler.compile(inputFile, outputFile, false); - - verifyNew(LessSource.class).withArguments(inputFile); - + verify(outputFile).exists(); verify(outputFile).lastModified(); - - verify(lessSource).getLastModifiedIncludingImports(); - verify(lessSource).getNormalizedContent(); - + verify(compiler).call(cx, compileScope, null, new Object[] {}); verifyStatic(); @@ -414,6 +407,8 @@ public void testCompileFileToFileWithForceFalseAndOutputExistsAndLessSourceNotMo when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); FieldUtils.writeField(lessCompiler, "compiler", compiler, true); + FieldUtils.writeField(lessCompiler, "out", out, true); + when(cx.newObject(scope)).thenReturn(compileScope); whenNew(LessSource.class).withArguments(inputFile).thenReturn(lessSource); @@ -423,13 +418,10 @@ public void testCompileFileToFileWithForceFalseAndOutputExistsAndLessSourceNotMo when(lessSource.getLastModifiedIncludingImports()).thenReturn(1l); lessCompiler.compile(inputFile, outputFile, false); - - verifyNew(LessSource.class).withArguments(inputFile); - + verify(outputFile).exists(); verify(outputFile).lastModified(); - verify(lessSource).getLastModifiedIncludingImports(); } @Test @@ -438,10 +430,12 @@ public void testCompileLessSourceToString() throws Exception { when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); FieldUtils.writeField(lessCompiler, "compiler", compiler, true); + FieldUtils.writeField(lessCompiler, "out", out, true); + when(cx.newObject(scope)).thenReturn(compileScope); when(lessSource.getNormalizedContent()).thenReturn(less); - when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); + when(out.toString()).thenReturn(css); assertEquals(css, lessCompiler.compile(lessSource)); @@ -456,10 +450,12 @@ public void testCompileLessSourceToFile() throws Exception { when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); FieldUtils.writeField(lessCompiler, "compiler", compiler, true); + FieldUtils.writeField(lessCompiler, "out", out, true); + when(cx.newObject(scope)).thenReturn(compileScope); when(lessSource.getNormalizedContent()).thenReturn(less); - when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); + when(out.toString()).thenReturn(css); mockStatic(FileUtils.class); @@ -479,10 +475,12 @@ public void testCompileLessSourceToFileWithForceTrue() throws Exception { when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); FieldUtils.writeField(lessCompiler, "compiler", compiler, true); + FieldUtils.writeField(lessCompiler, "out", out, true); + when(cx.newObject(scope)).thenReturn(compileScope); when(lessSource.getNormalizedContent()).thenReturn(less); - when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); + when(out.toString()).thenReturn(css); mockStatic(FileUtils.class); @@ -502,12 +500,14 @@ public void testCompileLessSourceToFileWithForceFalseAndOutputNotExists() throws when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); FieldUtils.writeField(lessCompiler, "compiler", compiler, true); + FieldUtils.writeField(lessCompiler, "out", out, true); + when(cx.newObject(scope)).thenReturn(compileScope); when(outputFile.exists()).thenReturn(false); when(lessSource.getNormalizedContent()).thenReturn(less); - when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); + when(out.toString()).thenReturn(css); mockStatic(FileUtils.class); @@ -529,6 +529,8 @@ public void testCompileLessSourceToFileWithForceFalseAndOutputExistsAndLessSourc when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); FieldUtils.writeField(lessCompiler, "compiler", compiler, true); + FieldUtils.writeField(lessCompiler, "out", out, true); + when(cx.newObject(scope)).thenReturn(compileScope); when(outputFile.exists()).thenReturn(true); when(outputFile.lastModified()).thenReturn(1l); @@ -536,7 +538,7 @@ public void testCompileLessSourceToFileWithForceFalseAndOutputExistsAndLessSourc when(lessSource.getLastModifiedIncludingImports()).thenReturn(2l); when(lessSource.getNormalizedContent()).thenReturn(less); - when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); + when(out.toString()).thenReturn(css); mockStatic(FileUtils.class); @@ -560,6 +562,8 @@ public void testCompileLessSourceToFileWithForceFalseAndOutputExistsAndLessSourc when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); FieldUtils.writeField(lessCompiler, "compiler", compiler, true); + FieldUtils.writeField(lessCompiler, "out", out, true); + when(cx.newObject(scope)).thenReturn(compileScope); when(outputFile.exists()).thenReturn(true); when(outputFile.lastModified()).thenReturn(2l); @@ -580,9 +584,11 @@ public void testCompileThrowsLessExceptionWhenCompilationFails() throws Exceptio when(Context.enter()).thenReturn(cx); FieldUtils.writeField(lessCompiler, "scope", scope, true); FieldUtils.writeField(lessCompiler, "compiler", compiler, true); + FieldUtils.writeField(lessCompiler, "out", out, true); + when(cx.newObject(scope)).thenReturn(compileScope); JavaScriptException javaScriptException = new JavaScriptException(null, null, 0); - when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(javaScriptException); + when(compiler.call(cx, compileScope, null, new Object[] {})).thenThrow(javaScriptException); assertEquals(css, lessCompiler.compile(less)); } @@ -594,8 +600,10 @@ public void testCompress() throws Exception { lessCompiler.setCompress(true); FieldUtils.writeField(lessCompiler, "scope", scope, true); FieldUtils.writeField(lessCompiler, "compiler", compiler, true); + FieldUtils.writeField(lessCompiler, "out", out, true); + when(cx.newObject(scope)).thenReturn(compileScope); - when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); + when(out.toString()).thenReturn(css); assertEquals(css, lessCompiler.compile(less)); @@ -609,19 +617,18 @@ public void testEncoding() throws Exception { lessCompiler.setEncoding("utf-8"); FieldUtils.writeField(lessCompiler, "scope", scope, true); FieldUtils.writeField(lessCompiler, "compiler", compiler, true); + FieldUtils.writeField(lessCompiler, "out", out, true); + when(cx.newObject(scope)).thenReturn(compileScope); whenNew(LessSource.class).withArguments(inputFile).thenReturn(lessSource); when(lessSource.getNormalizedContent()).thenReturn(less); - when(compiler.call(cx, compileScope, null, new Object[] {})).thenReturn(css); + when(out.toString("utf-8")).thenReturn(css); mockStatic(FileUtils.class); lessCompiler.compile(inputFile, outputFile); - - verifyNew(LessSource.class).withArguments(inputFile); - verify(lessSource).getNormalizedContent(); - + verify(compiler).call(cx, compileScope, null, new Object[] {}); verifyStatic(); From 547ebbcffc403f5bfc4f8407fe79ffbf7f886b0b Mon Sep 17 00:00:00 2001 From: Doug Haber Date: Tue, 21 Jan 2014 12:09:25 -0500 Subject: [PATCH 09/28] Fix some tests --- .../java/integration/AbstractCompileIT.java | 8 ++++++-- src/test/java/integration/LessExceptionIT.java | 17 +++-------------- src/test/java/org/lesscss/LessSourceTest.java | 5 ++++- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/test/java/integration/AbstractCompileIT.java b/src/test/java/integration/AbstractCompileIT.java index 10d0736..3128545 100644 --- a/src/test/java/integration/AbstractCompileIT.java +++ b/src/test/java/integration/AbstractCompileIT.java @@ -35,9 +35,13 @@ public void setUp() throws Exception { } protected void testCompile(File lessFile, File cssFile) throws Exception { - String expected = FileUtils.readFileToString(cssFile); String actual = lessCompiler.compile(lessFile); - assertEquals(expected.replace("\r\n", "\n"), actual); + String expected = FileUtils.readFileToString(cssFile); + + expected = expected.replace("\r\n", "\n"); + expected += "\n"; + + assertEquals(expected, actual); } protected void testCompile(File lessFile, File cssFile, boolean compress) throws Exception { diff --git a/src/test/java/integration/LessExceptionIT.java b/src/test/java/integration/LessExceptionIT.java index d31ffeb..b77930f 100644 --- a/src/test/java/integration/LessExceptionIT.java +++ b/src/test/java/integration/LessExceptionIT.java @@ -14,26 +14,15 @@ */ package integration; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import org.junit.Test; -import org.lesscss.LessException; +import org.junit.Test; public class LessExceptionIT extends AbstractCompileIT { @Test public void testException() throws Exception { - try { - lessCompiler.compile("a { color: @linkColor; }"); - fail(); - } - catch (LessException e) { - System.out.println("m:" + e.getMessage()); - assertTrue(e instanceof LessException); - assertEquals("@(1.0,11.0): variable @linkColor is undefined\n" + - "a { color: @linkColor; }", e.getMessage() ); - } + String output = lessCompiler.compile("a { color: @linkColor; }"); + assertTrue(output.startsWith("NameError: variable @linkColor is undefined in")); } } diff --git a/src/test/java/org/lesscss/LessSourceTest.java b/src/test/java/org/lesscss/LessSourceTest.java index dae84f5..553c355 100644 --- a/src/test/java/org/lesscss/LessSourceTest.java +++ b/src/test/java/org/lesscss/LessSourceTest.java @@ -24,6 +24,7 @@ import org.powermock.modules.junit4.PowerMockRunner; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.net.URL; import java.nio.charset.Charset; @@ -43,6 +44,7 @@ public class LessSourceTest { private LessSource lessSource; @Mock private File file; + @Mock private FileInputStream fileInputStream; @Mock private LessSource import1; @Mock private LessSource import2; @@ -136,13 +138,14 @@ private String readLessSourceWithEncoding(String encoding) throws IOException, I } - private File mockFile(boolean fileExists, String content, String absolutePath) throws IOException { + private File mockFile(boolean fileExists, String content, String absolutePath) throws Exception, IOException { when(file.exists()).thenReturn(fileExists); mockStatic(FileUtils.class); when(FileUtils.readFileToString(file)).thenReturn(content); when(file.getAbsolutePath()).thenReturn(absolutePath); when(file.lastModified()).thenReturn(lastModified); when(file.getParent()).thenReturn("folder"); + whenNew(FileInputStream.class).withArguments(file).thenReturn(fileInputStream); return file; } } From 92a627270d899ac4355ab0d163c7dee527fff39f Mon Sep 17 00:00:00 2001 From: Doug Haber Date: Wed, 22 Jan 2014 22:28:11 -0500 Subject: [PATCH 10/28] 1. Remove unnecessary files 2. Update Compile to allow regular lessc arguments 3. Update LessCompiler to use default rhino less/lessc from less project 4. Update tests --- src/main/java/org/lesscss/Compile.java | 13 +- src/main/java/org/lesscss/LessCompiler.java | 56 +- src/main/resources/META-INF/env.rhino.js | 13994 ---------------- src/main/resources/META-INF/less-1.5.1.js | 6941 -------- ...ess-rhino-1.5.1.js => less-rhino-1.6.1.js} | 4273 +++-- src/main/resources/META-INF/less.js | 11 - .../resources/META-INF/lessc-rhino-1.6.1.js | 449 + src/main/resources/META-INF/lessc.js | 12 - .../java/org/lesscss/LessCompilerTest.java | 17 +- .../resources/compatibility/custom.color.js | 4 +- .../resources/compatibility/custom.math.js | 4 +- 11 files changed, 3699 insertions(+), 22075 deletions(-) delete mode 100644 src/main/resources/META-INF/env.rhino.js delete mode 100644 src/main/resources/META-INF/less-1.5.1.js rename src/main/resources/META-INF/{less-rhino-1.5.1.js => less-rhino-1.6.1.js} (62%) delete mode 100644 src/main/resources/META-INF/less.js create mode 100644 src/main/resources/META-INF/lessc-rhino-1.6.1.js delete mode 100644 src/main/resources/META-INF/lessc.js diff --git a/src/main/java/org/lesscss/Compile.java b/src/main/java/org/lesscss/Compile.java index a3f40b7..bf5c815 100644 --- a/src/main/java/org/lesscss/Compile.java +++ b/src/main/java/org/lesscss/Compile.java @@ -5,6 +5,7 @@ import java.io.File; import java.util.Arrays; +import java.util.List; public class Compile { @@ -12,17 +13,21 @@ public class Compile { public static void main(String[] args) throws Exception { if( args.length < 1 ) { - logger.info("usage: org.lesscss.Compile "); + logger.info("usage: org.lesscss.Compile "); System.exit(-1); } + + List argList = Arrays.asList(args); + String fileName = argList.get(argList.size() - 1); + argList = argList.subList(0, argList.size() - 1); - File output = new File( args[0] + ".css" ); + File output = new File( fileName + ".css" ); logger.info("Compiler output = %s", output.getCanonicalPath() ); long start = System.currentTimeMillis(); - LessCompiler lessCompiler = new LessCompiler(Arrays.asList("-ru")); + LessCompiler lessCompiler = new LessCompiler(argList); - lessCompiler.compile( new File( args[0] ), output ); + lessCompiler.compile( new File( fileName ), output ); long duration = System.currentTimeMillis() - start; logger.info("Done. %,d ms", duration); diff --git a/src/main/java/org/lesscss/LessCompiler.java b/src/main/java/org/lesscss/LessCompiler.java index 781d0c2..72783ba 100644 --- a/src/main/java/org/lesscss/LessCompiler.java +++ b/src/main/java/org/lesscss/LessCompiler.java @@ -17,10 +17,12 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintStream; import java.io.PrintWriter; +import java.io.SequenceInputStream; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; @@ -68,7 +70,8 @@ public class LessCompiler { private static final LessLogger logger = LessLoggerFactory.getLogger(LessCompiler.class); - private URL lessJs = LessCompiler.class.getClassLoader().getResource("META-INF/less-rhino-1.5.1.js"); + private URL lessJs = LessCompiler.class.getClassLoader().getResource("META-INF/less-rhino-1.6.1.js"); + private URL lesscJs = LessCompiler.class.getClassLoader().getResource("META-INF/lessc-rhino-1.6.1.js"); private List customJs = Collections.emptyList(); private List options = Collections.emptyList(); private Boolean compress = null; @@ -144,6 +147,28 @@ public synchronized void setLessJs(URL lessJs) { this.lessJs = lessJs; } + /** + * Returns the LESSC JavaScript file used by the compiler. + * COMPILE_STRING + * @return The LESSC JavaScript file used by the compiler. + */ + public URL getLesscJs() { + return lesscJs; + } + + /** + * Sets the LESSC JavaScript file used by the compiler. + * Must be set before {@link #init()} is called. + * + * @param lesscJs LESSC JavaScript file used by the compiler. + */ + public synchronized void setLesscJs(URL lesscJs) { + if (scope != null) { + throw new IllegalStateException("This method can only be called before init()"); + } + this.lesscJs = lesscJs; + } + /** * Returns the custom JavaScript files used by the compiler. * @@ -249,21 +274,24 @@ public synchronized void init() { out = new ByteArrayOutputStream(); global.setOut(new PrintStream(out)); - // Load the compiler into a function we can run - compiler = (Function) cx.compileReader(new InputStreamReader(lessJs.openConnection().getInputStream()), lessJs.toString(), 1, null); + // Combine all of the streams (less, custom, lessc) into one big stream + List streams = new ArrayList(); - List jsUrls = new ArrayList(); - jsUrls.addAll( customJs ); - - // load any custom JS - for(URL url : jsUrls) { - InputStreamReader inputStreamReader = new InputStreamReader(url.openConnection().getInputStream()); - try{ - cx.evaluateReader(scope, inputStreamReader, url.toString(), 1, null); - }finally{ - inputStreamReader.close(); - } + // less should be first + streams.add(lessJs.openConnection().getInputStream()); + + // then the custom js so it has a chance to add any hooks + for(URL url : customJs) { + streams.add(url.openConnection().getInputStream()); } + + // then the lessc so we can do the compile + streams.add(lesscJs.openConnection().getInputStream()); + + InputStreamReader reader = new InputStreamReader(new SequenceInputStream(Collections.enumeration(streams))); + + // Load the streams into a function we can run + compiler = (Function) cx.compileReader(reader, lessJs.toString(), 1, null); } catch (Exception e) { diff --git a/src/main/resources/META-INF/env.rhino.js b/src/main/resources/META-INF/env.rhino.js deleted file mode 100644 index b511fd7..0000000 --- a/src/main/resources/META-INF/env.rhino.js +++ /dev/null @@ -1,13994 +0,0 @@ -// Override the print function so that the messages go to the configured loggerthe configured logger -print = function(message) { - logger.debug(message); -}; - -/* - * Envjs core-env.1.2.13 - * Pure JavaScript Browser Environment - * By John Resig and the Envjs Team - * Copyright 2008-2010 John Resig, under the MIT License - */ - -var Envjs = function(){ - var i, - name, - override = function(){ - for(i=0;i and the Envjs Team - * Copyright 2008-2010 John Resig, under the MIT License - */ - -//CLOSURE_START -(function(){ - - - - - -/** - * @author john resig - */ -// Helper method for extending one object with another. -function __extend__(a,b) { - for ( var i in b ) { - var g = b.__lookupGetter__(i), s = b.__lookupSetter__(i); - if ( g || s ) { - if ( g ) { a.__defineGetter__(i, g); } - if ( s ) { a.__defineSetter__(i, s); } - } else { - a[i] = b[i]; - } - } return a; -} - -/** - * Writes message to system out - * @param {String} message - */ -Envjs.log = function(message){}; - -/** - * Constants providing enumerated levels for logging in modules - */ -Envjs.DEBUG = 1; -Envjs.INFO = 2; -Envjs.WARN = 3; -Envjs.ERROR = 3; -Envjs.NONE = 3; - -/** - * Writes error info out to console - * @param {Error} e - */ -Envjs.lineSource = function(e){}; - - -/** - * TODO: used in ./event/eventtarget.js - * @param {Object} event - */ -Envjs.defaultEventBehaviors = {}; - - -/** - * describes which script src values will trigger Envjs to load - * the script like a browser would - */ -Envjs.scriptTypes = { - "text/javascript" :false, - "text/envjs" :true -}; - -/** - * will be called when loading a script throws an error - * @param {Object} script - * @param {Object} e - */ -Envjs.onScriptLoadError = function(script, e){ - console.log('error loading script %s %s', script, e); -}; - - -/** - * load and execute script tag text content - * @param {Object} script - */ -Envjs.loadInlineScript = function(script){ - var tmpFile; - tmpFile = Envjs.writeToTempFile(script.text, 'js') ; - load(tmpFile); -}; - -/** - * Should evaluate script in some context - * @param {Object} context - * @param {Object} source - * @param {Object} name - */ -Envjs.eval = function(context, source, name){}; - - -/** - * Executes a script tag - * @param {Object} script - * @param {Object} parser - */ -Envjs.loadLocalScript = function(script){ - //console.log("loading script %s", script); - var types, - src, - i, - base, - filename, - xhr; - - if(script.type){ - types = script.type.split(";"); - for(i=0;i - * - Via an innerHTML parse of a - * - A modificiation of the 'src' attribute of an Image/HTMLImageElement - * - * NOTE: this is optional API. If this doesn't exist then the default - * 'loaded' event occurs. - * - * @param node {Object} the node - * @param node the src value - * @return 'true' to indicate the 'load' succeed, false otherwise - */ -Envjs.loadImage = function(node, src) { - return true; -}; - - -/** - * A 'link' was requested by the document. Typically this occurs when: - * - During inital parse of a - * - Via an innerHTML parse of a - * - A modificiation of the 'href' attribute on a node in the tree - * - * @param node {Object} is the link node in question - * @param href {String} is the href. - * - * Return 'true' to indicate that the 'load' was successful, or false - * otherwise. The appropriate event is then triggered. - * - * NOTE: this is optional API. If this doesn't exist then the default - * 'loaded' event occurs - */ -Envjs.loadLink = function(node, href) { - return true; -}; - -(function(){ - - -/* - * cookie handling - * Private internal helper class used to save/retreive cookies - */ - -/** - * Specifies the location of the cookie file - */ -Envjs.cookieFile = function(){ - return 'file://'+Envjs.homedir+'/.cookies'; -}; - -/** - * saves cookies to a local file - * @param {Object} htmldoc - */ -Envjs.saveCookies = function(){ - var cookiejson = JSON.stringify(Envjs.cookies.peristent,null,'\t'); - //console.log('persisting cookies %s', cookiejson); - Envjs.writeToFile(cookiejson, Envjs.cookieFile()); -}; - -/** - * loads cookies from a local file - * @param {Object} htmldoc - */ -Envjs.loadCookies = function(){ - var cookiejson, - js; - try{ - cookiejson = Envjs.readFromFile(Envjs.cookieFile()) - js = JSON.parse(cookiejson, null, '\t'); - }catch(e){ - //console.log('failed to load cookies %s', e); - js = {}; - } - return js; -}; - -Envjs.cookies = { - persistent:{ - //domain - key on domain name { - //path - key on path { - //name - key on name { - //value : cookie value - //other cookie properties - //} - //} - //} - //expire - provides a timestamp for expiring the cookie - //cookie - the cookie! - }, - temporary:{//transient is a reserved word :( - //like above - } -}; - -var __cookies__; - -//HTMLDocument cookie -Envjs.setCookie = function(url, cookie){ - var i, - index, - name, - value, - properties = {}, - attr, - attrs; - url = Envjs.urlsplit(url); - if(cookie) - attrs = cookie.split(";"); - else - return; - - //for now the strategy is to simply create a json object - //and post it to a file in the .cookies.js file. I hate parsing - //dates so I decided not to implement support for 'expires' - //(which is deprecated) and instead focus on the easier 'max-age' - //(which succeeds 'expires') - cookie = {};//keyword properties of the cookie - cookie['domain'] = url.hostname; - cookie['path'] = url.path||'/'; - for(i=0;i -1){ - name = __trim__(attrs[i].slice(0,index)); - value = __trim__(attrs[i].slice(index+1)); - if(name=='max-age'){ - //we'll have to when to check these - //and garbage collect expired cookies - cookie[name] = parseInt(value, 10); - } else if( name == 'domain' ){ - if(__domainValid__(url, value)){ - cookie['domain'] = value; - } - } else if( name == 'path' ){ - //not sure of any special logic for path - cookie['path'] = value; - } else { - //its not a cookie keyword so store it in our array of properties - //and we'll serialize individually in a moment - properties[name] = value; - } - }else{ - if( attrs[i] == 'secure' ){ - cookie[attrs[i]] = true; - } - } - } - if(!('max-age' in cookie)){ - //it's a transient cookie so it only lasts as long as - //the window.location remains the same (ie in-memory cookie) - __mergeCookie__(Envjs.cookies.temporary, cookie, properties); - }else{ - //the cookie is persistent - __mergeCookie__(Envjs.cookies.persistent, cookie, properties); - Envjs.saveCookies(); - } -}; - -function __domainValid__(url, value){ - var i, - domainParts = url.hostname.split('.').reverse(), - newDomainParts = value.split('.').reverse(); - if(newDomainParts.length > 1){ - for(i=0;i -1) { - for (name in cookies[domain][path]) { - // console.log('cookie domain path name %s', name); - cookieString += - ((i++ > 0)?'; ':'') + - name + "=" + - cookies[domain][path][name].value; - } - } - } - } - } - return cookieString; -}; - -function __mergeCookie__(target, cookie, properties){ - var name, now; - if(!target[cookie.domain]){ - target[cookie.domain] = {}; - } - if(!target[cookie.domain][cookie.path]){ - target[cookie.domain][cookie.path] = {}; - } - for(name in properties){ - now = new Date().getTime(); - target[cookie.domain][cookie.path][name] = { - "value":properties[name], - "secure":cookie.secure, - "max-age":cookie['max-age'], - "date-created":now, - "expiration":(cookie['max-age']===0) ? - 0 : - now + cookie['max-age'] - }; - //console.log('cookie is %o',target[cookie.domain][cookie.path][name]); - } -}; - -})();//end cookies -/* - http://www.JSON.org/json2.js - 2008-07-15 - - Public Domain. - - NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. - - See http://www.JSON.org/js.html - - - This code should be minified before deployment. - See http://javascript.crockford.com/jsmin.html - - USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO - NOT CONTROL. -*/ -try{ JSON; }catch(e){ -JSON = function () { - - function f(n) { - // Format integers to have at least two digits. - return n < 10 ? '0' + n : n; - } - - Date.prototype.toJSON = function (key) { - - return this.getUTCFullYear() + '-' + - f(this.getUTCMonth() + 1) + '-' + - f(this.getUTCDate()) + 'T' + - f(this.getUTCHours()) + ':' + - f(this.getUTCMinutes()) + ':' + - f(this.getUTCSeconds()) + 'Z'; - }; - - String.prototype.toJSON = function (key) { - return String(this); - }; - Number.prototype.toJSON = - Boolean.prototype.toJSON = function (key) { - return this.valueOf(); - }; - - var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, - escapeable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, - gap, - indent, - meta = { // table of character substitutions - '\b': '\\b', - '\t': '\\t', - '\n': '\\n', - '\f': '\\f', - '\r': '\\r', - '"' : '\\"', - '\\': '\\\\' - }, - rep; - - - function quote(string) { - - escapeable.lastIndex = 0; - return escapeable.test(string) ? - '"' + string.replace(escapeable, function (a) { - var c = meta[a]; - if (typeof c === 'string') { - return c; - } - return '\\u' + ('0000' + - (+(a.charCodeAt(0))).toString(16)).slice(-4); - }) + '"' : - '"' + string + '"'; - } - - - function str(key, holder) { - - var i, // The loop counter. - k, // The member key. - v, // The member value. - length, - mind = gap, - partial, - value = holder[key]; - - if (value && typeof value === 'object' && - typeof value.toJSON === 'function') { - value = value.toJSON(key); - } - if (typeof rep === 'function') { - value = rep.call(holder, key, value); - } - - switch (typeof value) { - case 'string': - return quote(value); - - case 'number': - return isFinite(value) ? String(value) : 'null'; - - case 'boolean': - case 'null': - - return String(value); - - case 'object': - - if (!value) { - return 'null'; - } - gap += indent; - partial = []; - - if (typeof value.length === 'number' && - !(value.propertyIsEnumerable('length'))) { - - length = value.length; - for (i = 0; i < length; i += 1) { - partial[i] = str(i, value) || 'null'; - } - - v = partial.length === 0 ? '[]' : - gap ? '[\n' + gap + - partial.join(',\n' + gap) + '\n' + - mind + ']' : - '[' + partial.join(',') + ']'; - gap = mind; - return v; - } - - if (rep && typeof rep === 'object') { - length = rep.length; - for (i = 0; i < length; i += 1) { - k = rep[i]; - if (typeof k === 'string') { - v = str(k, value); - if (v) { - partial.push(quote(k) + (gap ? ': ' : ':') + v); - } - } - } - } else { - - for (k in value) { - if (Object.hasOwnProperty.call(value, k)) { - v = str(k, value); - if (v) { - partial.push(quote(k) + (gap ? ': ' : ':') + v); - } - } - } - } - - v = partial.length === 0 ? '{}' : - gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + - mind + '}' : '{' + partial.join(',') + '}'; - gap = mind; - return v; - } - } - - return { - stringify: function (value, replacer, space) { - - var i; - gap = ''; - indent = ''; - - if (typeof space === 'number') { - for (i = 0; i < space; i += 1) { - indent += ' '; - } - - } else if (typeof space === 'string') { - indent = space; - } - - rep = replacer; - if (replacer && typeof replacer !== 'function' && - (typeof replacer !== 'object' || - typeof replacer.length !== 'number')) { - throw new Error('JSON.stringify'); - } - - return str('', {'': value}); - }, - - - parse: function (text, reviver) { - var j; - function walk(holder, key) { - var k, v, value = holder[key]; - if (value && typeof value === 'object') { - for (k in value) { - if (Object.hasOwnProperty.call(value, k)) { - v = walk(value, k); - if (v !== undefined) { - value[k] = v; - } else { - delete value[k]; - } - } - } - } - return reviver.call(holder, key, value); - } - - cx.lastIndex = 0; - if (cx.test(text)) { - text = text.replace(cx, function (a) { - return '\\u' + ('0000' + - (+(a.charCodeAt(0))).toString(16)).slice(-4); - }); - } - - - if (/^[\],:{}\s]*$/. -test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@'). -replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']'). -replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { - - j = eval('(' + text + ')'); - - return typeof reviver === 'function' ? - walk({'': j}, '') : j; - } - - throw new SyntaxError('JSON.parse'); - } - }; -}(); - -} - -/** - * synchronizes thread modifications - * @param {Function} fn - */ -Envjs.sync = function(fn){}; - -/** - * sleep thread for specified duration - * @param {Object} millseconds - */ -Envjs.sleep = function(millseconds){}; - -/** - * Interval to wait on event loop when nothing is happening - */ -Envjs.WAIT_INTERVAL = 20;//milliseconds - -/* - * Copyright (c) 2010 Nick Galbreath - * http://code.google.com/p/stringencoders/source/browse/#svn/trunk/javascript - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, - * copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following - * conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES - * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - */ - -/* - * url processing in the spirit of python's urlparse module - * see `pydoc urlparse` or - * http://docs.python.org/library/urlparse.html - * - * urlsplit: break apart a URL into components - * urlunsplit: reconsistute a URL from componets - * urljoin: join an absolute and another URL - * urldefrag: remove the fragment from a URL - * - * Take a look at the tests in urlparse-test.html - * - * On URL Normalization: - * - * urlsplit only does minor normalization the components Only scheme - * and hostname are lowercased urljoin does a bit more, normalizing - * paths with "." and "..". - - * urlnormalize adds additional normalization - * - * * removes default port numbers - * http://abc.com:80/ -> http://abc.com/, etc - * * normalizes path - * http://abc.com -> http://abc.com/ - * and other "." and ".." cleanups - * * if file, remove query and fragment - * - * It does not do: - * * normalizes escaped hex values - * http://abc.com/%7efoo -> http://abc.com/%7Efoo - * * normalize '+' <--> '%20' - * - * Differences with Python - * - * The javascript urlsplit returns a normal object with the following - * properties: scheme, netloc, hostname, port, path, query, fragment. - * All properties are read-write. - * - * In python, the resulting object is not a dict, but a specialized, - * read-only, and has alternative tuple interface (e.g. obj[0] == - * obj.scheme). It's not clear why such a simple function requires - * a unique datastructure. - * - * urlunsplit in javascript takes an duck-typed object, - * { scheme: 'http', netloc: 'abc.com', ...} - * while in * python it takes a list-like object. - * ['http', 'abc.com'... ] - * - * For all functions, the javascript version use - * hostname+port if netloc is missing. In python - * hostname+port were always ignored. - * - * Similar functionality in different languages: - * - * http://php.net/manual/en/function.parse-url.php - * returns assocative array but cannot handle relative URL - * - * TODO: test allowfragments more - * TODO: test netloc missing, but hostname present - */ - -var urlparse = {}; - -// Unlike to be useful standalone -// -// NORMALIZE PATH with "../" and "./" -// http://en.wikipedia.org/wiki/URL_normalization -// http://tools.ietf.org/html/rfc3986#section-5.2.3 -// -urlparse.normalizepath = function(path) -{ - if (!path || path === '/') { - return '/'; - } - - var parts = path.split('/'); - - var newparts = []; - // make sure path always starts with '/' - if (parts[0]) { - newparts.push(''); - } - - for (var i = 0; i < parts.length; ++i) { - if (parts[i] === '..') { - if (newparts.length > 1) { - newparts.pop(); - } else { - newparts.push(parts[i]); - } - } else if (parts[i] != '.') { - newparts.push(parts[i]); - } - } - - path = newparts.join('/'); - if (!path) { - path = '/'; - } - return path; -}; - -// -// Does many of the normalizations that the stock -// python urlsplit/urlunsplit/urljoin neglects -// -// Doesn't do hex-escape normalization on path or query -// %7e -> %7E -// Nor, '+' <--> %20 translation -// -urlparse.urlnormalize = function(url) -{ - var parts = urlparse.urlsplit(url); - switch (parts.scheme) { - case 'file': - // files can't have query strings - // and we don't bother with fragments - parts.query = ''; - parts.fragment = ''; - break; - case 'http': - case 'https': - // remove default port - if ((parts.scheme === 'http' && parts.port == 80) || - (parts.scheme === 'https' && parts.port == 443)) { - parts.port = null; - // hostname is already lower case - parts.netloc = parts.hostname; - } - break; - default: - // if we don't have specific normalizations for this - // scheme, return the original url unmolested - return url; - } - - // for [file|http|https]. Not sure about other schemes - parts.path = urlparse.normalizepath(parts.path); - - return urlparse.urlunsplit(parts); -}; - -urlparse.urldefrag = function(url) -{ - var idx = url.indexOf('#'); - if (idx == -1) { - return [ url, '' ]; - } else { - return [ url.substr(0,idx), url.substr(idx+1) ]; - } -}; - -urlparse.urlsplit = function(url, default_scheme, allow_fragments) -{ - var leftover; - - if (typeof allow_fragments === 'undefined') { - allow_fragments = true; - } - - // scheme (optional), host, port - var fullurl = /^([A-Za-z]+)?(:?\/\/)([0-9.\-A-Za-z]*)(?::(\d+))?(.*)$/; - // path, query, fragment - var parse_leftovers = /([^?#]*)?(?:\?([^#]*))?(?:#(.*))?$/; - - var o = {}; - - var parts = url.match(fullurl); - if (parts) { - o.scheme = parts[1] || default_scheme || ''; - o.hostname = parts[3].toLowerCase() || ''; - o.port = parseInt(parts[4],10) || ''; - // Probably should grab the netloc from regexp - // and then parse again for hostname/port - - o.netloc = parts[3]; - if (parts[4]) { - o.netloc += ':' + parts[4]; - } - - leftover = parts[5]; - } else { - o.scheme = default_scheme || ''; - o.netloc = ''; - o.hostname = ''; - leftover = url; - } - o.scheme = o.scheme.toLowerCase(); - - parts = leftover.match(parse_leftovers); - - o.path = parts[1] || ''; - o.query = parts[2] || ''; - - if (allow_fragments) { - o.fragment = parts[3] || ''; - } else { - o.fragment = ''; - } - - return o; -}; - -urlparse.urlunsplit = function(o) { - var s = ''; - if (o.scheme) { - s += o.scheme + '://'; - } - - if (o.netloc) { - if (s == '') { - s += '//'; - } - s += o.netloc; - } else if (o.hostname) { - // extension. Python only uses netloc - if (s == '') { - s += '//'; - } - s += o.hostname; - if (o.port) { - s += ':' + o.port; - } - } - - if (o.path) { - s += o.path; - } - - if (o.query) { - s += '?' + o.query; - } - if (o.fragment) { - s += '#' + o.fragment; - } - return s; -}; - -urlparse.urljoin = function(base, url, allow_fragments) -{ - if (typeof allow_fragments === 'undefined') { - allow_fragments = true; - } - - var url_parts = urlparse.urlsplit(url); - - // if url parts has a scheme (i.e. absolute) - // then nothing to do - if (url_parts.scheme) { - if (! allow_fragments) { - return url; - } else { - return urlparse.urldefrag(url)[0]; - } - } - var base_parts = urlparse.urlsplit(base); - - // copy base, only if not present - if (!base_parts.scheme) { - base_parts.scheme = url_parts.scheme; - } - - // copy netloc, only if not present - if (!base_parts.netloc || !base_parts.hostname) { - base_parts.netloc = url_parts.netloc; - base_parts.hostname = url_parts.hostname; - base_parts.port = url_parts.port; - } - - // paths - if (url_parts.path.length > 0) { - if (url_parts.path.charAt(0) == '/') { - base_parts.path = url_parts.path; - } else { - // relative path.. get rid of "current filename" and - // replace. Same as var parts = - // base_parts.path.split('/'); parts[parts.length-1] = - // url_parts.path; base_parts.path = parts.join('/'); - var idx = base_parts.path.lastIndexOf('/'); - if (idx == -1) { - base_parts.path = url_parts.path; - } else { - base_parts.path = base_parts.path.substr(0,idx) + '/' + - url_parts.path; - } - } - } - - // clean up path - base_parts.path = urlparse.normalizepath(base_parts.path); - - // copy query string - base_parts.query = url_parts.query; - - // copy fragments - if (allow_fragments) { - base_parts.fragment = url_parts.fragment; - } else { - base_parts.fragment = ''; - } - - return urlparse.urlunsplit(base_parts); -}; - -/** - * getcwd - named after posix call of same name (see 'man 2 getcwd') - * - */ -Envjs.getcwd = function() { - return '.'; -}; - -/** - * resolves location relative to doc location - * - * @param {Object} path Relative or absolute URL - * @param {Object} base (semi-optional) The base url used in resolving "path" above - */ -Envjs.uri = function(path, base) { - //console.log('constructing uri from path %s and base %s', path, base); - - // Semi-common trick is to make an iframe with src='javascript:false' - // (or some equivalent). By returning '', the load is skipped - if (path.indexOf('javascript') === 0) { - return ''; - } - - // if path is absolute, then just normalize and return - if (path.match('^[a-zA-Z]+://')) { - return urlparse.urlnormalize(path); - } - - // interesting special case, a few very large websites use - // '//foo/bar/' to mean 'http://foo/bar' - if (path.match('^//')) { - path = 'http:' + path; - } - - // if base not passed in, try to get it from document - // Ideally I would like the caller to pass in document.baseURI to - // make this more self-sufficient and testable - if (!base && document) { - base = document.baseURI; - } - - // about:blank doesn't count - if (base === 'about:blank'){ - base = ''; - } - - // if base is still empty, then we are in QA mode loading local - // files. Get current working directory - if (!base) { - base = 'file://' + Envjs.getcwd() + '/'; - } - // handles all cases if path is abosulte or relative to base - // 3rd arg is "false" --> remove fragments - var newurl = urlparse.urlnormalize(urlparse.urljoin(base, path, false)); - - return newurl; -}; - - - -/** - * Used in the XMLHttpRquest implementation to run a - * request in a seperate thread - * @param {Object} fn - */ -Envjs.runAsync = function(fn){}; - - -/** - * Used to write to a local file - * @param {Object} text - * @param {Object} url - */ -Envjs.writeToFile = function(text, url){}; - - -/** - * Used to write to a local file - * @param {Object} text - * @param {Object} suffix - */ -Envjs.writeToTempFile = function(text, suffix){}; - -/** - * Used to read the contents of a local file - * @param {Object} url - */ -Envjs.readFromFile = function(url){}; - -/** - * Used to delete a local file - * @param {Object} url - */ -Envjs.deleteFile = function(url){}; - -/** - * establishes connection and calls responsehandler - * @param {Object} xhr - * @param {Object} responseHandler - * @param {Object} data - */ -Envjs.connection = function(xhr, responseHandler, data){}; - - -__extend__(Envjs, urlparse); - -/** - * Makes an object window-like by proxying object accessors - * @param {Object} scope - * @param {Object} parent - */ -Envjs.proxy = function(scope, parent, aliasList){}; - -Envjs.javaEnabled = false; - -Envjs.homedir = ''; -Envjs.tmpdir = ''; -Envjs.os_name = ''; -Envjs.os_arch = ''; -Envjs.os_version = ''; -Envjs.lang = ''; -Envjs.platform = ''; - -/** - * - * @param {Object} frameElement - * @param {Object} url - */ -Envjs.loadFrame = function(frame, url){ - try { - if(frame.contentWindow){ - //mark for garbage collection - frame.contentWindow = null; - } - - //create a new scope for the window proxy - //platforms will need to override this function - //to make sure the scope is global-like - frame.contentWindow = (function(){return this;})(); - new Window(frame.contentWindow, window); - - //I dont think frames load asynchronously in firefox - //and I think the tests have verified this but for - //some reason I'm less than confident... Are there cases? - frame.contentDocument = frame.contentWindow.document; - frame.contentDocument.async = false; - if(url){ - //console.log('envjs.loadFrame async %s', frame.contentDocument.async); - frame.contentWindow.location = url; - } - } catch(e) { - console.log("failed to load frame content: from %s %s", url, e); - } -}; - - -// The following are in rhino/window.js -// TODO: Envjs.unloadFrame -// TODO: Envjs.proxy - -/** - * @author john resig & the envjs team - * @uri http://www.envjs.com/ - * @copyright 2008-2010 - * @license MIT - */ -//CLOSURE_END -}()); -/* - * Envjs rhino-env.1.2.13 - * Pure JavaScript Browser Environment - * By John Resig and the Envjs Team - * Copyright 2008-2010 John Resig, under the MIT License - */ - -var __context__ = Packages.org.mozilla.javascript.Context.getCurrentContext(); - -Envjs.platform = "Rhino"; -Envjs.revision = "1.7.0.rc2"; - -/* - * Envjs rhino-env.1.2.13 - * Pure JavaScript Browser Environment - * By John Resig and the Envjs Team - * Copyright 2008-2010 John Resig, under the MIT License - */ - -//CLOSURE_START -(function(){ - - - - - -/** - * @author john resig - */ -// Helper method for extending one object with another. -function __extend__(a,b) { - for ( var i in b ) { - var g = b.__lookupGetter__(i), s = b.__lookupSetter__(i); - if ( g || s ) { - if ( g ) { a.__defineGetter__(i, g); } - if ( s ) { a.__defineSetter__(i, s); } - } else { - a[i] = b[i]; - } - } return a; -} - -/** - * Writes message to system out. - * - * Some sites redefine 'print' as in 'window.print', so instead of - * printing to stdout, you are popping open a new window, which might - * call print, etc, etc,etc This can cause infinite loops and can - * exhausing all memory. - * - * By defining this upfront now, Envjs.log will always call the native 'print' - * function - * - * @param {Object} message - */ -Envjs.log = print; - -Envjs.lineSource = function(e){ - return e&&e.rhinoException?e.rhinoException.lineSource():"(line ?)"; -}; -/** - * load and execute script tag text content - * @param {Object} script - */ -Envjs.loadInlineScript = function(script){ - if(script.ownerDocument.ownerWindow){ - Envjs.eval( - script.ownerDocument.ownerWindow, - script.text, - 'eval('+script.text.substring(0,16)+'...):'+new Date().getTime() - ); - }else{ - Envjs.eval( - __this__, - script.text, - 'eval('+script.text.substring(0,16)+'...):'+new Date().getTime() - ); - } - //console.log('evaluated at scope %s \n%s', - // script.ownerDocument.ownerWindow.guid, script.text); -}; - - -Envjs.eval = function(context, source, name){ - __context__.evaluateString( - context, - source, - name, - 0, - null - ); -}; - -//Temporary patch for parser module -Packages.org.mozilla.javascript.Context. - getCurrentContext().setOptimizationLevel(-1); - -/** - * Rhino provides a very succinct 'sync' - * @param {Function} fn - */ -try{ - Envjs.sync = sync; - Envjs.spawn = spawn; -} catch(e){ - //sync unavailable on AppEngine - Envjs.sync = function(fn){ - //console.log('Threadless platform, sync is safe'); - return fn; - }; - - Envjs.spawn = function(fn){ - //console.log('Threadless platform, spawn shares main thread.'); - return fn(); - }; -} - -/** - * sleep thread for specified duration - * @param {Object} millseconds - */ -Envjs.sleep = function(millseconds){ - try{ - java.lang.Thread.currentThread().sleep(millseconds); - }catch(e){ - console.log('Threadless platform, cannot sleep.'); - } -}; - -/** - * provides callback hook for when the system exits - */ -Envjs.onExit = function(callback){ - var rhino = Packages.org.mozilla.javascript, - contextFactory = __context__.getFactory(), - listener = new rhino.ContextFactory.Listener({ - contextReleased: function(context){ - if(context === __context__) - console.log('context released', context); - contextFactory.removeListener(this); - if(callback) - callback(); - } - }); - contextFactory.addListener(listener); -}; - -/** - * Get 'Current Working Directory' - */ -Envjs.getcwd = function() { - return java.lang.System.getProperty('user.dir'); -} - -/** - * - * @param {Object} fn - * @param {Object} onInterupt - */ -Envjs.runAsync = function(fn, onInterupt){ - ////Envjs.debug("running async"); - var running = true, - run; - - try{ - run = Envjs.sync(function(){ - fn(); - Envjs.wait(); - }); - Envjs.spawn(run); - }catch(e){ - console.log("error while running async operation", e); - try{if(onInterrupt)onInterrupt(e)}catch(ee){}; - } -}; - -/** - * Used to write to a local file - * @param {Object} text - * @param {Object} url - */ -Envjs.writeToFile = function(text, url){ - //Envjs.debug("writing text to url : " + url); - var out = new java.io.FileWriter( - new java.io.File( - new java.net.URI(url.toString()))); - out.write( text, 0, text.length ); - out.flush(); - out.close(); -}; - -/** - * Used to write to a local file - * @param {Object} text - * @param {Object} suffix - */ -Envjs.writeToTempFile = function(text, suffix){ - //Envjs.debug("writing text to temp url : " + suffix); - // Create temp file. - var temp = java.io.File.createTempFile("envjs-tmp", suffix); - - // Delete temp file when program exits. - temp.deleteOnExit(); - - // Write to temp file - var out = new java.io.FileWriter(temp); - out.write(text, 0, text.length); - out.close(); - return temp.getAbsolutePath().toString()+''; -}; - - -/** - * Used to read the contents of a local file - * @param {Object} url - */ -Envjs.readFromFile = function( url ){ - var fileReader = new java.io.FileReader( - new java.io.File( - new java.net.URI( url ))); - - var stringwriter = new java.io.StringWriter(), - buffer = java.lang.reflect.Array.newInstance(java.lang.Character.TYPE, 1024), - length; - - while ((length = fileReader.read(buffer, 0, 1024)) != -1) { - stringwriter.write(buffer, 0, length); - } - - stringwriter.close(); - return stringwriter.toString()+""; -}; - - -/** - * Used to delete a local file - * @param {Object} url - */ -Envjs.deleteFile = function(url){ - var file = new java.io.File( new java.net.URI( url ) ); - file["delete"](); -}; - -/** - * establishes connection and calls responsehandler - * @param {Object} xhr - * @param {Object} responseHandler - * @param {Object} data - */ -Envjs.connection = function(xhr, responseHandler, data){ - var url = java.net.URL(xhr.url), - connection, - header, - outstream, - buffer, - length, - binary = false, - name, value, - contentEncoding, - instream, - responseXML, - i; - if ( /^file\:/.test(url) ) { - try{ - if ( "PUT" == xhr.method || "POST" == xhr.method ) { - data = data || "" ; - Envjs.writeToFile(data, url); - xhr.readyState = 4; - //could be improved, I just cant recall the correct http codes - xhr.status = 200; - xhr.statusText = ""; - } else if ( xhr.method == "DELETE" ) { - Envjs.deleteFile(url); - xhr.readyState = 4; - //could be improved, I just cant recall the correct http codes - xhr.status = 200; - xhr.statusText = ""; - } else { - connection = url.openConnection(); - connection.connect(); - //try to add some canned headers that make sense - - try{ - if(xhr.url.match(/html$/)){ - xhr.responseHeaders["Content-Type"] = 'text/html'; - }else if(xhr.url.match(/.xml$/)){ - xhr.responseHeaders["Content-Type"] = 'text/xml'; - }else if(xhr.url.match(/.js$/)){ - xhr.responseHeaders["Content-Type"] = 'text/javascript'; - }else if(xhr.url.match(/.json$/)){ - xhr.responseHeaders["Content-Type"] = 'application/json'; - }else{ - xhr.responseHeaders["Content-Type"] = 'text/plain'; - } - //xhr.responseHeaders['Last-Modified'] = connection.getLastModified(); - //xhr.responseHeaders['Content-Length'] = headerValue+''; - //xhr.responseHeaders['Date'] = new Date()+'';*/ - }catch(e){ - console.log('failed to load response headers',e); - } - } - }catch(e){ - console.log('failed to open file %s %s', url, e); - connection = null; - xhr.readyState = 4; - xhr.statusText = "Local File Protocol Error"; - xhr.responseText = "

"+ e+ "

"; - } - } else { - connection = url.openConnection(); - connection.setRequestMethod( xhr.method ); - - // Add headers to Java connection - for (header in xhr.headers){ - connection.addRequestProperty(header+'', xhr.headers[header]+''); - } - - //write data to output stream if required - if(data){ - if(data instanceof Document){ - if ( xhr.method == "PUT" || xhr.method == "POST" ) { - connection.setDoOutput(true); - outstream = connection.getOutputStream(), - xml = (new XMLSerializer()).serializeToString(data); - buffer = new java.lang.String(xml).getBytes('UTF-8'); - outstream.write(buffer, 0, buffer.length); - outstream.close(); - } - }else if(data.length&&data.length>0){ - if ( xhr.method == "PUT" || xhr.method == "POST" ) { - connection.setDoOutput(true); - outstream = connection.getOutputStream(); - buffer = new java.lang.String(data).getBytes('UTF-8'); - outstream.write(buffer, 0, buffer.length); - outstream.close(); - } - } - connection.connect(); - }else{ - connection.connect(); - } - } - - if(connection){ - try{ - length = connection.getHeaderFields().size(); - // Stick the response headers into responseHeaders - for (i = 0; i < length; i++) { - name = connection.getHeaderFieldKey(i); - value = connection.getHeaderField(i); - if (name) - xhr.responseHeaders[name+''] = value+''; - } - }catch(e){ - console.log('failed to load response headers \n%s',e); - } - - xhr.readyState = 4; - xhr.status = parseInt(connection.responseCode,10) || undefined; - xhr.statusText = connection.responseMessage || ""; - - contentEncoding = connection.getContentEncoding() || "utf-8"; - instream = null; - responseXML = null; - - try{ - //console.log('contentEncoding %s', contentEncoding); - if( contentEncoding.equalsIgnoreCase("gzip") || - contentEncoding.equalsIgnoreCase("decompress")){ - //zipped content - binary = true; - outstream = new java.io.ByteArrayOutputStream(); - buffer = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024); - instream = new java.util.zip.GZIPInputStream(connection.getInputStream()) - }else{ - //this is a text file - outstream = new java.io.StringWriter(); - buffer = java.lang.reflect.Array.newInstance(java.lang.Character.TYPE, 1024); - instream = new java.io.InputStreamReader(connection.getInputStream()); - } - }catch(e){ - if (connection.getResponseCode() == 404){ - console.log('failed to open connection stream \n %s %s', - e.toString(), e); - }else{ - console.log('failed to open connection stream \n %s %s', - e.toString(), e); - } - instream = connection.getErrorStream(); - } - - while ((length = instream.read(buffer, 0, 1024)) != -1) { - outstream.write(buffer, 0, length); - } - - outstream.close(); - instream.close(); - - if(binary){ - xhr.responseText = new String(outstream.toByteArray(), 'UTF-8') + ''; - }else{ - xhr.responseText = outstream.toString() + ''; - } - - } - if(responseHandler){ - //Envjs.debug('calling ajax response handler'); - responseHandler(); - } -}; - -//Since we're running in rhino I guess we can safely assume -//java is 'enabled'. I'm sure this requires more thought -//than I've given it here -Envjs.javaEnabled = true; - -Envjs.homedir = java.lang.System.getProperty("user.home"); -Envjs.tmpdir = java.lang.System.getProperty("java.io.tmpdir"); -Envjs.os_name = java.lang.System.getProperty("os.name"); -Envjs.os_arch = java.lang.System.getProperty("os.arch"); -Envjs.os_version = java.lang.System.getProperty("os.version"); -Envjs.lang = java.lang.System.getProperty("user.lang"); - - -/** - * - * @param {Object} frameElement - * @param {Object} url - */ -Envjs.loadFrame = function(frame, url){ - try { - if(frame.contentWindow){ - //mark for garbage collection - frame.contentWindow = null; - } - - //create a new scope for the window proxy - frame.contentWindow = Envjs.proxy(); - new Window(frame.contentWindow, window); - - //I dont think frames load asynchronously in firefox - //and I think the tests have verified this but for - //some reason I'm less than confident... Are there cases? - frame.contentDocument = frame.contentWindow.document; - frame.contentDocument.async = false; - if(url){ - //console.log('envjs.loadFrame async %s', frame.contentDocument.async); - frame.contentWindow.location = url; - } - } catch(e) { - console.log("failed to load frame content: from %s %s", url, e); - } -}; - -/** - * unloadFrame - * @param {Object} frame - */ -Envjs.unloadFrame = function(frame){ - var all, length, i; - try{ - //TODO: probably self-referencing structures within a document tree - //preventing it from being entirely garbage collected once orphaned. - //Should have code to walk tree and break all links between contained - //objects. - frame.contentDocument = null; - if(frame.contentWindow){ - frame.contentWindow.close(); - } - gc(); - }catch(e){ - console.log(e); - } -}; - -/** - * Makes an object window-like by proxying object accessors - * @param {Object} scope - * @param {Object} parent - */ -Envjs.proxy = function(scope, parent) { - try{ - if(scope+'' == '[object global]'){ - return scope - }else{ - return __context__.initStandardObjects(); - } - }catch(e){ - console.log('failed to init standard objects %s %s \n%s', scope, parent, e); - } - -}; - -/** - * @author john resig & the envjs team - * @uri http://www.envjs.com/ - * @copyright 2008-2010 - * @license MIT - */ -//CLOSURE_END -}()); - -/** - * @author envjs team - */ -var Console, - console; - -/* - * Envjs console.1.2.13 - * Pure JavaScript Browser Environment - * By John Resig and the Envjs Team - * Copyright 2008-2010 John Resig, under the MIT License - */ - -//CLOSURE_START -(function(){ - - - - - -/** - * @author envjs team - * borrowed 99%-ish with love from firebug-lite - * - * http://wiki.commonjs.org/wiki/Console - */ -Console = function(module){ - var $level, - $logger, - $null = function(){}; - - - if(Envjs[module] && Envjs[module].loglevel){ - $level = Envjs.module.loglevel; - $logger = { - log: function(level){ - logFormatted(arguments, (module)+" "); - }, - debug: $level>1 ? $null: function() { - logFormatted(arguments, (module)+" debug"); - }, - info: $level>2 ? $null:function(){ - logFormatted(arguments, (module)+" info"); - }, - warn: $level>3 ? $null:function(){ - logFormatted(arguments, (module)+" warning"); - }, - error: $level>4 ? $null:function(){ - logFormatted(arguments, (module)+" error"); - } - }; - } else { - $logger = { - log: function(level){ - logFormatted(arguments, ""); - }, - debug: $null, - info: $null, - warn: $null, - error: $null - }; - } - - return $logger; -}; - -console = new Console("console",1); - -function logFormatted(objects, className) -{ - var html = []; - - var format = objects[0]; - var objIndex = 0; - - if (typeof(format) != "string") - { - format = ""; - objIndex = -1; - } - - var parts = parseFormat(format); - for (var i = 0; i < parts.length; ++i) - { - var part = parts[i]; - if (part && typeof(part) == "object") - { - var object = objects[++objIndex]; - part.appender(object, html); - } - else { - appendText(part, html); - } - } - - for (var i = objIndex+1; i < objects.length; ++i) - { - appendText(" ", html); - - var object = objects[i]; - if (typeof(object) == "string") { - appendText(object, html); - } else { - appendObject(object, html); - } - } - - Envjs.log(html.join(' ')); -} - -function parseFormat(format) -{ - var parts = []; - - var reg = /((^%|[^\\]%)(\d+)?(\.)([a-zA-Z]))|((^%|[^\\]%)([a-zA-Z]))/; - var appenderMap = {s: appendText, d: appendInteger, i: appendInteger, f: appendFloat}; - - for (var m = reg.exec(format); m; m = reg.exec(format)) - { - var type = m[8] ? m[8] : m[5]; - var appender = type in appenderMap ? appenderMap[type] : appendObject; - var precision = m[3] ? parseInt(m[3]) : (m[4] == "." ? -1 : 0); - - parts.push(format.substr(0, m[0][0] == "%" ? m.index : m.index+1)); - parts.push({appender: appender, precision: precision}); - - format = format.substr(m.index+m[0].length); - } - - parts.push(format); - - return parts; -} - -function escapeHTML(value) -{ - return value; -} - -function objectToString(object) -{ - try - { - return object+""; - } - catch (exc) - { - return null; - } -} - -// ******************************************************************************************** - -function appendText(object, html) -{ - html.push(escapeHTML(objectToString(object))); -} - -function appendNull(object, html) -{ - html.push(escapeHTML(objectToString(object))); -} - -function appendString(object, html) -{ - html.push(escapeHTML(objectToString(object))); -} - -function appendInteger(object, html) -{ - html.push(escapeHTML(objectToString(object))); -} - -function appendFloat(object, html) -{ - html.push(escapeHTML(objectToString(object))); -} - -function appendFunction(object, html) -{ - var reName = /function ?(.*?)\(/; - var m = reName.exec(objectToString(object)); - var name = m ? m[1] : "function"; - html.push(escapeHTML(name)); -} - -function appendObject(object, html) -{ - try - { - if (object == undefined) { - appendNull("undefined", html); - } else if (object == null) { - appendNull("null", html); - } else if (typeof object == "string") { - appendString(object, html); - } else if (typeof object == "number") { - appendInteger(object, html); - } else if (typeof object == "function") { - appendFunction(object, html); - } else if (object.nodeType == 1) { - appendSelector(object, html); - } else if (typeof object == "object") { - appendObjectFormatted(object, html); - } else { - appendText(object, html); - } - } - catch (exc) - { - } -} - -function appendObjectFormatted(object, html) -{ - var text = objectToString(object); - var reObject = /\[object (.*?)\]/; - - var m = reObject.exec(text); - html.push( m ? m[1] : text); -} - -function appendSelector(object, html) -{ - - html.push(escapeHTML(object.nodeName.toLowerCase())); - if (object.id) { - html.push(escapeHTML(object.id)); - } - if (object.className) { - html.push(escapeHTML(object.className)); - } -} - -function appendNode(node, html) -{ - if (node.nodeType == 1) - { - html.push( node.nodeName.toLowerCase()); - - for (var i = 0; i < node.attributes.length; ++i) - { - var attr = node.attributes[i]; - if (!attr.specified) { - continue; - } - - html.push( attr.nodeName.toLowerCase(),escapeHTML(attr.nodeValue)); - } - - if (node.firstChild) - { - for (var child = node.firstChild; child; child = child.nextSibling) { - appendNode(child, html); - } - - html.push( node.nodeName.toLowerCase()); - } - } - else if (node.nodeType === 3) - { - html.push(escapeHTML(node.nodeValue)); - } -}; - -/** - * @author john resig & the envjs team - * @uri http://www.envjs.com/ - * @copyright 2008-2010 - * @license MIT - */ -//CLOSURE_END -}()); -/* - * Envjs dom.1.2.13 - * Pure JavaScript Browser Environment - * By John Resig and the Envjs Team - * Copyright 2008-2010 John Resig, under the MIT License - * - * Parts of the implementation were originally written by:\ - * and Jon van Noort (jon@webarcana.com.au) \ - * and David Joham (djoham@yahoo.com)",\ - * and Scott Severtson - * - * This file simply provides the global definitions we need to \ - * be able to correctly implement to core browser DOM interfaces." - */ - -var Attr, - CDATASection, - CharacterData, - Comment, - Document, - DocumentFragment, - DocumentType, - DOMException, - DOMImplementation, - Element, - Entity, - EntityReference, - NamedNodeMap, - Namespace, - Node, - NodeList, - Notation, - ProcessingInstruction, - Text, - Range, - XMLSerializer, - DOMParser; - - - -/* - * Envjs dom.1.2.13 - * Pure JavaScript Browser Environment - * By John Resig and the Envjs Team - * Copyright 2008-2010 John Resig, under the MIT License - */ - -//CLOSURE_START -(function(){ - - - - - -/** - * @author john resig - */ -// Helper method for extending one object with another. -function __extend__(a,b) { - for ( var i in b ) { - var g = b.__lookupGetter__(i), s = b.__lookupSetter__(i); - if ( g || s ) { - if ( g ) { a.__defineGetter__(i, g); } - if ( s ) { a.__defineSetter__(i, s); } - } else { - a[i] = b[i]; - } - } return a; -} - -/** - * @author john resig - */ -//from jQuery -function __setArray__( target, array ) { - // Resetting the length to 0, then using the native Array push - // is a super-fast way to populate an object with array-like properties - target.length = 0; - Array.prototype.push.apply( target, array ); -} - -/** - * @class NodeList - - * provides the abstraction of an ordered collection of nodes - * - * @param ownerDocument : Document - the ownerDocument - * @param parentNode : Node - the node that the NodeList is attached to (or null) - */ -NodeList = function(ownerDocument, parentNode) { - this.length = 0; - this.parentNode = parentNode; - this.ownerDocument = ownerDocument; - this._readonly = false; - __setArray__(this, []); -}; - -__extend__(NodeList.prototype, { - item : function(index) { - var ret = null; - if ((index >= 0) && (index < this.length)) { - // bounds check - ret = this[index]; - } - // if the index is out of bounds, default value null is returned - return ret; - }, - get xml() { - var ret = "", - i; - - // create string containing the concatenation of the string values of each child - for (i=0; i < this.length; i++) { - if(this[i]){ - if(this[i].nodeType == Node.TEXT_NODE && i>0 && - this[i-1].nodeType == Node.TEXT_NODE){ - //add a single space between adjacent text nodes - ret += " "+this[i].xml; - }else{ - ret += this[i].xml; - } - } - } - return ret; - }, - toArray: function () { - var children = [], - i; - for ( i=0; i < this.length; i++) { - children.push (this[i]); - } - return children; - }, - toString: function(){ - return "[object NodeList]"; - } -}); - - -/** - * @method __findItemIndex__ - * find the item index of the node - * @author Jon van Noort (jon@webarcana.com.au) - * @param node : Node - * @return : int - */ -var __findItemIndex__ = function (nodelist, node) { - var ret = -1, i; - for (i=0; i= 0) && (refChildIndex <= nodelist.length)) { - // bounds check - if (newChild.nodeType == Node.DOCUMENT_FRAGMENT_NODE) { - // node is a DocumentFragment - // append the children of DocumentFragment - Array.prototype.splice.apply(nodelist, - [refChildIndex, 0].concat(newChild.childNodes.toArray())); - } - else { - // append the newChild - Array.prototype.splice.apply(nodelist,[refChildIndex, 0, newChild]); - } - } -}; - -/** - * @method __replaceChild__ - * replace the specified Node in the NodeList at the specified index - * Used by Node.replaceChild(). Note: Node.replaceChild() is responsible - * for Node Pointer surgery __replaceChild__ simply modifies the internal - * data structure (Array). - * - * @param newChild : Node - the Node to be inserted - * @param refChildIndex : int - the array index to hold the Node - */ -var __replaceChild__ = function(nodelist, newChild, refChildIndex) { - var ret = null; - - // bounds check - if ((refChildIndex >= 0) && (refChildIndex < nodelist.length)) { - // preserve old child for return - ret = nodelist[refChildIndex]; - - if (newChild.nodeType == Node.DOCUMENT_FRAGMENT_NODE) { - // node is a DocumentFragment - // get array containing children prior to refChild - Array.prototype.splice.apply(nodelist, - [refChildIndex, 1].concat(newChild.childNodes.toArray())); - } - else { - // simply replace node in array (links between Nodes are - // made at higher level) - nodelist[refChildIndex] = newChild; - } - } - // return replaced node - return ret; -}; - -/** - * @method __removeChild__ - * remove the specified Node in the NodeList at the specified index - * Used by Node.removeChild(). Note: Node.removeChild() is responsible - * for Node Pointer surgery __removeChild__ simply modifies the internal - * data structure (Array). - * @param refChildIndex : int - the array index holding the Node to be removed - */ -var __removeChild__ = function(nodelist, refChildIndex) { - var ret = null; - - if (refChildIndex > -1) { - // found it! - // return removed node - ret = nodelist[refChildIndex]; - - // rebuild array without removed child - Array.prototype.splice.apply(nodelist,[refChildIndex, 1]); - } - // return removed node - return ret; -}; - -/** - * @method __appendChild__ - * append the specified Node to the NodeList. Used by Node.appendChild(). - * Note: Node.appendChild() is responsible for Node Pointer surgery - * __appendChild__ simply modifies the internal data structure (Array). - * @param newChild : Node - the Node to be inserted - */ -var __appendChild__ = function(nodelist, newChild) { - if (newChild.nodeType == Node.DOCUMENT_FRAGMENT_NODE) { - // node is a DocumentFragment - // append the children of DocumentFragment - Array.prototype.push.apply(nodelist, newChild.childNodes.toArray() ); - } else { - // simply add node to array (links between Nodes are made at higher level) - Array.prototype.push.apply(nodelist, [newChild]); - } - -}; - -/** - * @method __cloneNodes__ - - * Returns a NodeList containing clones of the Nodes in this NodeList - * @param deep : boolean - - * If true, recursively clone the subtree under each of the nodes; - * if false, clone only the nodes themselves (and their attributes, - * if it is an Element). - * @param parentNode : Node - the new parent of the cloned NodeList - * @return : NodeList - NodeList containing clones of the Nodes in this NodeList - */ -var __cloneNodes__ = function(nodelist, deep, parentNode) { - var cloneNodeList = new NodeList(nodelist.ownerDocument, parentNode); - - // create list containing clones of each child - for (var i=0; i < nodelist.length; i++) { - __appendChild__(cloneNodeList, nodelist[i].cloneNode(deep)); - } - - return cloneNodeList; -}; - - -var __ownerDocument__ = function(node){ - return (node.nodeType == Node.DOCUMENT_NODE)?node:node.ownerDocument; -}; - -/** - * @class Node - - * The Node interface is the primary datatype for the entire - * Document Object Model. It represents a single node in the - * document tree. - * @param ownerDocument : Document - The Document object associated with this node. - */ - -Node = function(ownerDocument) { - this.baseURI = 'about:blank'; - this.namespaceURI = null; - this.nodeName = ""; - this.nodeValue = null; - - // A NodeList that contains all children of this node. If there are no - // children, this is a NodeList containing no nodes. The content of the - // returned NodeList is "live" in the sense that, for instance, changes to - // the children of the node object that it was created from are immediately - // reflected in the nodes returned by the NodeList accessors; it is not a - // static snapshot of the content of the node. This is true for every - // NodeList, including the ones returned by the getElementsByTagName method. - this.childNodes = new NodeList(ownerDocument, this); - - // The first child of this node. If there is no such node, this is null - this.firstChild = null; - // The last child of this node. If there is no such node, this is null. - this.lastChild = null; - // The node immediately preceding this node. If there is no such node, - // this is null. - this.previousSibling = null; - // The node immediately following this node. If there is no such node, - // this is null. - this.nextSibling = null; - - this.attributes = null; - // The namespaces in scope for this node - this._namespaces = new NamespaceNodeMap(ownerDocument, this); - this._readonly = false; - - //IMPORTANT: These must come last so rhino will not iterate parent - // properties before child properties. (qunit.equiv issue) - - // The parent of this node. All nodes, except Document, DocumentFragment, - // and Attr may have a parent. However, if a node has just been created - // and not yet added to the tree, or if it has been removed from the tree, - // this is null - this.parentNode = null; - // The Document object associated with this node - this.ownerDocument = ownerDocument; - -}; - -// nodeType constants -Node.ELEMENT_NODE = 1; -Node.ATTRIBUTE_NODE = 2; -Node.TEXT_NODE = 3; -Node.CDATA_SECTION_NODE = 4; -Node.ENTITY_REFERENCE_NODE = 5; -Node.ENTITY_NODE = 6; -Node.PROCESSING_INSTRUCTION_NODE = 7; -Node.COMMENT_NODE = 8; -Node.DOCUMENT_NODE = 9; -Node.DOCUMENT_TYPE_NODE = 10; -Node.DOCUMENT_FRAGMENT_NODE = 11; -Node.NOTATION_NODE = 12; -Node.NAMESPACE_NODE = 13; - -Node.DOCUMENT_POSITION_EQUAL = 0x00; -Node.DOCUMENT_POSITION_DISCONNECTED = 0x01; -Node.DOCUMENT_POSITION_PRECEDING = 0x02; -Node.DOCUMENT_POSITION_FOLLOWING = 0x04; -Node.DOCUMENT_POSITION_CONTAINS = 0x08; -Node.DOCUMENT_POSITION_CONTAINED_BY = 0x10; -Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 0x20; - - -__extend__(Node.prototype, { - get localName(){ - return this.prefix? - this.nodeName.substring(this.prefix.length+1, this.nodeName.length): - this.nodeName; - }, - get prefix(){ - return this.nodeName.split(':').length>1? - this.nodeName.split(':')[0]: - null; - }, - set prefix(value){ - if(value === null){ - this.nodeName = this.localName; - }else{ - this.nodeName = value+':'+this.localName; - } - }, - hasAttributes : function() { - if (this.attributes.length == 0) { - return false; - }else{ - return true; - } - }, - get textContent(){ - return __recursivelyGatherText__(this); - }, - set textContent(newText){ - while(this.firstChild != null){ - this.removeChild( this.firstChild ); - } - var text = this.ownerDocument.createTextNode(newText); - this.appendChild(text); - }, - insertBefore : function(newChild, refChild) { - var prevNode; - - if(newChild==null){ - return newChild; - } - if(refChild==null){ - this.appendChild(newChild); - return this.newChild; - } - - // test for exceptions - if (__ownerDocument__(this).implementation.errorChecking) { - // throw Exception if Node is readonly - if (this._readonly) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } - - // throw Exception if newChild was not created by this Document - if (__ownerDocument__(this) != __ownerDocument__(newChild)) { - throw(new DOMException(DOMException.WRONG_DOCUMENT_ERR)); - } - - // throw Exception if the node is an ancestor - if (__isAncestor__(this, newChild)) { - throw(new DOMException(DOMException.HIERARCHY_REQUEST_ERR)); - } - } - - // if refChild is specified, insert before it - if (refChild) { - // find index of refChild - var itemIndex = __findItemIndex__(this.childNodes, refChild); - // throw Exception if there is no child node with this id - if (__ownerDocument__(this).implementation.errorChecking && (itemIndex < 0)) { - throw(new DOMException(DOMException.NOT_FOUND_ERR)); - } - - // if the newChild is already in the tree, - var newChildParent = newChild.parentNode; - if (newChildParent) { - // remove it - newChildParent.removeChild(newChild); - } - - // insert newChild into childNodes - __insertBefore__(this.childNodes, newChild, itemIndex); - - // do node pointer surgery - prevNode = refChild.previousSibling; - - // handle DocumentFragment - if (newChild.nodeType == Node.DOCUMENT_FRAGMENT_NODE) { - if (newChild.childNodes.length > 0) { - // set the parentNode of DocumentFragment's children - for (var ind = 0; ind < newChild.childNodes.length; ind++) { - newChild.childNodes[ind].parentNode = this; - } - - // link refChild to last child of DocumentFragment - refChild.previousSibling = newChild.childNodes[newChild.childNodes.length-1]; - } - }else { - // set the parentNode of the newChild - newChild.parentNode = this; - // link refChild to newChild - refChild.previousSibling = newChild; - } - - }else { - // otherwise, append to end - prevNode = this.lastChild; - this.appendChild(newChild); - } - - if (newChild.nodeType == Node.DOCUMENT_FRAGMENT_NODE) { - // do node pointer surgery for DocumentFragment - if (newChild.childNodes.length > 0) { - if (prevNode) { - prevNode.nextSibling = newChild.childNodes[0]; - }else { - // this is the first child in the list - this.firstChild = newChild.childNodes[0]; - } - newChild.childNodes[0].previousSibling = prevNode; - newChild.childNodes[newChild.childNodes.length-1].nextSibling = refChild; - } - }else { - // do node pointer surgery for newChild - if (prevNode) { - prevNode.nextSibling = newChild; - }else { - // this is the first child in the list - this.firstChild = newChild; - } - newChild.previousSibling = prevNode; - newChild.nextSibling = refChild; - } - - return newChild; - }, - replaceChild : function(newChild, oldChild) { - var ret = null; - - if(newChild==null || oldChild==null){ - return oldChild; - } - - // test for exceptions - if (__ownerDocument__(this).implementation.errorChecking) { - // throw Exception if Node is readonly - if (this._readonly) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } - - // throw Exception if newChild was not created by this Document - if (__ownerDocument__(this) != __ownerDocument__(newChild)) { - throw(new DOMException(DOMException.WRONG_DOCUMENT_ERR)); - } - - // throw Exception if the node is an ancestor - if (__isAncestor__(this, newChild)) { - throw(new DOMException(DOMException.HIERARCHY_REQUEST_ERR)); - } - } - - // get index of oldChild - var index = __findItemIndex__(this.childNodes, oldChild); - - // throw Exception if there is no child node with this id - if (__ownerDocument__(this).implementation.errorChecking && (index < 0)) { - throw(new DOMException(DOMException.NOT_FOUND_ERR)); - } - - // if the newChild is already in the tree, - var newChildParent = newChild.parentNode; - if (newChildParent) { - // remove it - newChildParent.removeChild(newChild); - } - - // add newChild to childNodes - ret = __replaceChild__(this.childNodes,newChild, index); - - - if (newChild.nodeType == Node.DOCUMENT_FRAGMENT_NODE) { - // do node pointer surgery for Document Fragment - if (newChild.childNodes.length > 0) { - for (var ind = 0; ind < newChild.childNodes.length; ind++) { - newChild.childNodes[ind].parentNode = this; - } - - if (oldChild.previousSibling) { - oldChild.previousSibling.nextSibling = newChild.childNodes[0]; - } else { - this.firstChild = newChild.childNodes[0]; - } - - if (oldChild.nextSibling) { - oldChild.nextSibling.previousSibling = newChild; - } else { - this.lastChild = newChild.childNodes[newChild.childNodes.length-1]; - } - - newChild.childNodes[0].previousSibling = oldChild.previousSibling; - newChild.childNodes[newChild.childNodes.length-1].nextSibling = oldChild.nextSibling; - } - } else { - // do node pointer surgery for newChild - newChild.parentNode = this; - - if (oldChild.previousSibling) { - oldChild.previousSibling.nextSibling = newChild; - }else{ - this.firstChild = newChild; - } - if (oldChild.nextSibling) { - oldChild.nextSibling.previousSibling = newChild; - }else{ - this.lastChild = newChild; - } - newChild.previousSibling = oldChild.previousSibling; - newChild.nextSibling = oldChild.nextSibling; - } - - return ret; - }, - removeChild : function(oldChild) { - if(!oldChild){ - return null; - } - // throw Exception if NamedNodeMap is readonly - if (__ownerDocument__(this).implementation.errorChecking && - (this._readonly || oldChild._readonly)) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } - - // get index of oldChild - var itemIndex = __findItemIndex__(this.childNodes, oldChild); - - // throw Exception if there is no child node with this id - if (__ownerDocument__(this).implementation.errorChecking && (itemIndex < 0)) { - throw(new DOMException(DOMException.NOT_FOUND_ERR)); - } - - // remove oldChild from childNodes - __removeChild__(this.childNodes, itemIndex); - - // do node pointer surgery - oldChild.parentNode = null; - - if (oldChild.previousSibling) { - oldChild.previousSibling.nextSibling = oldChild.nextSibling; - }else { - this.firstChild = oldChild.nextSibling; - } - if (oldChild.nextSibling) { - oldChild.nextSibling.previousSibling = oldChild.previousSibling; - }else { - this.lastChild = oldChild.previousSibling; - } - - oldChild.previousSibling = null; - oldChild.nextSibling = null; - - return oldChild; - }, - appendChild : function(newChild) { - if(!newChild){ - return null; - } - // test for exceptions - if (__ownerDocument__(this).implementation.errorChecking) { - // throw Exception if Node is readonly - if (this._readonly) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } - - // throw Exception if arg was not created by this Document - if (__ownerDocument__(this) != __ownerDocument__(this)) { - throw(new DOMException(DOMException.WRONG_DOCUMENT_ERR)); - } - - // throw Exception if the node is an ancestor - if (__isAncestor__(this, newChild)) { - throw(new DOMException(DOMException.HIERARCHY_REQUEST_ERR)); - } - } - - // if the newChild is already in the tree, - var newChildParent = newChild.parentNode; - if (newChildParent) { - // remove it - //console.debug('removing node %s', newChild); - newChildParent.removeChild(newChild); - } - - // add newChild to childNodes - __appendChild__(this.childNodes, newChild); - - if (newChild.nodeType == Node.DOCUMENT_FRAGMENT_NODE) { - // do node pointer surgery for DocumentFragment - if (newChild.childNodes.length > 0) { - for (var ind = 0; ind < newChild.childNodes.length; ind++) { - newChild.childNodes[ind].parentNode = this; - } - - if (this.lastChild) { - this.lastChild.nextSibling = newChild.childNodes[0]; - newChild.childNodes[0].previousSibling = this.lastChild; - this.lastChild = newChild.childNodes[newChild.childNodes.length-1]; - } else { - this.lastChild = newChild.childNodes[newChild.childNodes.length-1]; - this.firstChild = newChild.childNodes[0]; - } - } - } else { - // do node pointer surgery for newChild - newChild.parentNode = this; - if (this.lastChild) { - this.lastChild.nextSibling = newChild; - newChild.previousSibling = this.lastChild; - this.lastChild = newChild; - } else { - this.lastChild = newChild; - this.firstChild = newChild; - } - } - return newChild; - }, - hasChildNodes : function() { - return (this.childNodes.length > 0); - }, - cloneNode: function(deep) { - // use importNode to clone this Node - //do not throw any exceptions - try { - return __ownerDocument__(this).importNode(this, deep); - } catch (e) { - //there shouldn't be any exceptions, but if there are, return null - // may want to warn: $debug("could not clone node: "+e.code); - return null; - } - }, - normalize : function() { - var i; - var inode; - var nodesToRemove = new NodeList(); - - if (this.nodeType == Node.ELEMENT_NODE || this.nodeType == Node.DOCUMENT_NODE) { - var adjacentTextNode = null; - - // loop through all childNodes - for(i = 0; i < this.childNodes.length; i++) { - inode = this.childNodes.item(i); - - if (inode.nodeType == Node.TEXT_NODE) { - // this node is a text node - if (inode.length < 1) { - // this text node is empty - // add this node to the list of nodes to be remove - __appendChild__(nodesToRemove, inode); - }else { - if (adjacentTextNode) { - // previous node was also text - adjacentTextNode.appendData(inode.data); - // merge the data in adjacent text nodes - // add this node to the list of nodes to be removed - __appendChild__(nodesToRemove, inode); - } else { - // remember this node for next cycle - adjacentTextNode = inode; - } - } - } else { - // (soon to be) previous node is not a text node - adjacentTextNode = null; - // normalize non Text childNodes - inode.normalize(); - } - } - - // remove redundant Text Nodes - for(i = 0; i < nodesToRemove.length; i++) { - inode = nodesToRemove.item(i); - inode.parentNode.removeChild(inode); - } - } - }, - isSupported : function(feature, version) { - // use Implementation.hasFeature to determine if this feature is supported - return __ownerDocument__(this).implementation.hasFeature(feature, version); - }, - getElementsByTagName : function(tagname) { - // delegate to _getElementsByTagNameRecursive - // recurse childNodes - var nodelist = new NodeList(__ownerDocument__(this)); - for (var i = 0; i < this.childNodes.length; i++) { - __getElementsByTagNameRecursive__(this.childNodes.item(i), - tagname, - nodelist); - } - return nodelist; - }, - getElementsByTagNameNS : function(namespaceURI, localName) { - // delegate to _getElementsByTagNameNSRecursive - return __getElementsByTagNameNSRecursive__(this, namespaceURI, localName, - new NodeList(__ownerDocument__(this))); - }, - importNode : function(importedNode, deep) { - var i; - var importNode; - - //there is no need to perform namespace checks since everything has already gone through them - //in order to have gotten into the DOM in the first place. The following line - //turns namespace checking off in ._isValidNamespace - __ownerDocument__(this).importing = true; - - if (importedNode.nodeType == Node.ELEMENT_NODE) { - if (!__ownerDocument__(this).implementation.namespaceAware) { - // create a local Element (with the name of the importedNode) - importNode = __ownerDocument__(this).createElement(importedNode.tagName); - - // create attributes matching those of the importedNode - for(i = 0; i < importedNode.attributes.length; i++) { - importNode.setAttribute(importedNode.attributes.item(i).name, importedNode.attributes.item(i).value); - } - } else { - // create a local Element (with the name & namespaceURI of the importedNode) - importNode = __ownerDocument__(this).createElementNS(importedNode.namespaceURI, importedNode.nodeName); - - // create attributes matching those of the importedNode - for(i = 0; i < importedNode.attributes.length; i++) { - importNode.setAttributeNS(importedNode.attributes.item(i).namespaceURI, - importedNode.attributes.item(i).name, importedNode.attributes.item(i).value); - } - - // create namespace definitions matching those of the importedNode - for(i = 0; i < importedNode._namespaces.length; i++) { - importNode._namespaces[i] = __ownerDocument__(this).createNamespace(importedNode._namespaces.item(i).localName); - importNode._namespaces[i].value = importedNode._namespaces.item(i).value; - } - } - } else if (importedNode.nodeType == Node.ATTRIBUTE_NODE) { - if (!__ownerDocument__(this).implementation.namespaceAware) { - // create a local Attribute (with the name of the importedAttribute) - importNode = __ownerDocument__(this).createAttribute(importedNode.name); - } else { - // create a local Attribute (with the name & namespaceURI of the importedAttribute) - importNode = __ownerDocument__(this).createAttributeNS(importedNode.namespaceURI, importedNode.nodeName); - - // create namespace definitions matching those of the importedAttribute - for(i = 0; i < importedNode._namespaces.length; i++) { - importNode._namespaces[i] = __ownerDocument__(this).createNamespace(importedNode._namespaces.item(i).localName); - importNode._namespaces[i].value = importedNode._namespaces.item(i).value; - } - } - - // set the value of the local Attribute to match that of the importedAttribute - importNode.value = importedNode.value; - - } else if (importedNode.nodeType == Node.DOCUMENT_FRAGMENT_NODE) { - // create a local DocumentFragment - importNode = __ownerDocument__(this).createDocumentFragment(); - } else if (importedNode.nodeType == Node.NAMESPACE_NODE) { - // create a local NamespaceNode (with the same name & value as the importedNode) - importNode = __ownerDocument__(this).createNamespace(importedNode.nodeName); - importNode.value = importedNode.value; - } else if (importedNode.nodeType == Node.TEXT_NODE) { - // create a local TextNode (with the same data as the importedNode) - importNode = __ownerDocument__(this).createTextNode(importedNode.data); - } else if (importedNode.nodeType == Node.CDATA_SECTION_NODE) { - // create a local CDATANode (with the same data as the importedNode) - importNode = __ownerDocument__(this).createCDATASection(importedNode.data); - } else if (importedNode.nodeType == Node.PROCESSING_INSTRUCTION_NODE) { - // create a local ProcessingInstruction (with the same target & data as the importedNode) - importNode = __ownerDocument__(this).createProcessingInstruction(importedNode.target, importedNode.data); - } else if (importedNode.nodeType == Node.COMMENT_NODE) { - // create a local Comment (with the same data as the importedNode) - importNode = __ownerDocument__(this).createComment(importedNode.data); - } else { // throw Exception if nodeType is not supported - throw(new DOMException(DOMException.NOT_SUPPORTED_ERR)); - } - - if (deep) { - // recurse childNodes - for(i = 0; i < importedNode.childNodes.length; i++) { - importNode.appendChild(__ownerDocument__(this).importNode(importedNode.childNodes.item(i), true)); - } - } - - //reset importing - __ownerDocument__(this).importing = false; - return importNode; - - }, - contains : function(node){ - while(node && node != this ){ - node = node.parentNode; - } - return !!node; - }, - compareDocumentPosition : function(b){ - //console.log("comparing document position %s %s", this, b); - var i, - length, - a = this, - parent, - aparents, - bparents; - //handle a couple simpler case first - if(a === b) { - return Node.DOCUMENT_POSITION_EQUAL; - } - if(a.ownerDocument !== b.ownerDocument) { - return Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC| - Node.DOCUMENT_POSITION_FOLLOWING| - Node.DOCUMENT_POSITION_DISCONNECTED; - } - if(a.parentNode === b.parentNode){ - length = a.parentNode.childNodes.length; - for(i=0;i aparents.length){ - return Node.DOCUMENT_POSITION_FOLLOWING; - }else if(bparents.length < aparents.length){ - return Node.DOCUMENT_POSITION_PRECEDING; - }else{ - //common ancestor diverge point - if (i === 0) { - return Node.DOCUMENT_POSITION_FOLLOWING; - } else { - parent = aparents[i-1]; - } - return parent.compareDocumentPosition(bparents.pop()); - } - } - } - - return Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC| - Node.DOCUMENT_POSITION_DISCONNECTED; - - }, - toString : function() { - return '[object Node]'; - } - -}); - - - -/** - * @method __getElementsByTagNameRecursive__ - implements getElementsByTagName() - * @param elem : Element - The element which are checking and then recursing into - * @param tagname : string - The name of the tag to match on. The special value "*" matches all tags - * @param nodeList : NodeList - The accumulating list of matching nodes - * - * @return : NodeList - */ -var __getElementsByTagNameRecursive__ = function (elem, tagname, nodeList) { - - if (elem.nodeType == Node.ELEMENT_NODE || elem.nodeType == Node.DOCUMENT_NODE) { - - if(elem.nodeType !== Node.DOCUMENT_NODE && - ((elem.nodeName.toUpperCase() == tagname.toUpperCase()) || - (tagname == "*")) ){ - // add matching node to nodeList - __appendChild__(nodeList, elem); - } - - // recurse childNodes - for(var i = 0; i < elem.childNodes.length; i++) { - nodeList = __getElementsByTagNameRecursive__(elem.childNodes.item(i), tagname, nodeList); - } - } - - return nodeList; -}; - -/** - * @method __getElementsByTagNameNSRecursive__ - * implements getElementsByTagName() - * - * @param elem : Element - The element which are checking and then recursing into - * @param namespaceURI : string - the namespace URI of the required node - * @param localName : string - the local name of the required node - * @param nodeList : NodeList - The accumulating list of matching nodes - * - * @return : NodeList - */ -var __getElementsByTagNameNSRecursive__ = function(elem, namespaceURI, localName, nodeList) { - if (elem.nodeType == Node.ELEMENT_NODE || elem.nodeType == Node.DOCUMENT_NODE) { - - if (((elem.namespaceURI == namespaceURI) || (namespaceURI == "*")) && - ((elem.localName == localName) || (localName == "*"))) { - // add matching node to nodeList - __appendChild__(nodeList, elem); - } - - // recurse childNodes - for(var i = 0; i < elem.childNodes.length; i++) { - nodeList = __getElementsByTagNameNSRecursive__( - elem.childNodes.item(i), namespaceURI, localName, nodeList); - } - } - - return nodeList; -}; - -/** - * @method __isAncestor__ - returns true if node is ancestor of target - * @param target : Node - The node we are using as context - * @param node : Node - The candidate ancestor node - * @return : boolean - */ -var __isAncestor__ = function(target, node) { - // if this node matches, return true, - // otherwise recurse up (if there is a parentNode) - return ((target == node) || ((target.parentNode) && (__isAncestor__(target.parentNode, node)))); -}; - - - -var __recursivelyGatherText__ = function(aNode) { - var accumulateText = "", - idx, - node; - for (idx=0;idx < aNode.childNodes.length;idx++){ - node = aNode.childNodes.item(idx); - if(node.nodeType == Node.TEXT_NODE) - accumulateText += node.data; - else - accumulateText += __recursivelyGatherText__(node); - } - return accumulateText; -}; - -/** - * function __escapeXML__ - * @param str : string - The string to be escaped - * @return : string - The escaped string - */ -var escAmpRegEx = /&(?!(amp;|lt;|gt;|quot|apos;))/g; -var escLtRegEx = //g; -var quotRegEx = /"/g; -var aposRegEx = /'/g; - -function __escapeXML__(str) { - str = str.replace(escAmpRegEx, "&"). - replace(escLtRegEx, "<"). - replace(escGtRegEx, ">"). - replace(quotRegEx, """). - replace(aposRegEx, "'"); - - return str; -}; - -/* -function __escapeHTML5__(str) { - str = str.replace(escAmpRegEx, "&"). - replace(escLtRegEx, "<"). - replace(escGtRegEx, ">"); - - return str; -}; -function __escapeHTML5Atribute__(str) { - str = str.replace(escAmpRegEx, "&"). - replace(escLtRegEx, "<"). - replace(escGtRegEx, ">"). - replace(quotRegEx, """). - replace(aposRegEx, "'"); - - return str; -}; -*/ - -/** - * function __unescapeXML__ - * @param str : string - The string to be unescaped - * @return : string - The unescaped string - */ -var unescAmpRegEx = /&/g; -var unescLtRegEx = /</g; -var unescGtRegEx = />/g; -var unquotRegEx = /"/g; -var unaposRegEx = /'/g; -function __unescapeXML__(str) { - str = str.replace(unescAmpRegEx, "&"). - replace(unescLtRegEx, "<"). - replace(unescGtRegEx, ">"). - replace(unquotRegEx, "\""). - replace(unaposRegEx, "'"); - - return str; -}; - -/** - * @class NamedNodeMap - - * used to represent collections of nodes that can be accessed by name - * typically a set of Element attributes - * - * @extends NodeList - - * note W3C spec says that this is not the case, but we need an item() - * method identical to NodeList's, so why not? - * @param ownerDocument : Document - the ownerDocument - * @param parentNode : Node - the node that the NamedNodeMap is attached to (or null) - */ -NamedNodeMap = function(ownerDocument, parentNode) { - NodeList.apply(this, arguments); - __setArray__(this, []); -}; -NamedNodeMap.prototype = new NodeList(); -__extend__(NamedNodeMap.prototype, { - add: function(name){ - this[this.length] = name; - }, - getNamedItem : function(name) { - var ret = null; - //console.log('NamedNodeMap getNamedItem %s', name); - // test that Named Node exists - var itemIndex = __findNamedItemIndex__(this, name); - - if (itemIndex > -1) { - // found it! - ret = this[itemIndex]; - } - // if node is not found, default value null is returned - return ret; - }, - setNamedItem : function(arg) { - //console.log('setNamedItem %s', arg); - // test for exceptions - if (__ownerDocument__(this).implementation.errorChecking) { - // throw Exception if arg was not created by this Document - if (this.ownerDocument != arg.ownerDocument) { - throw(new DOMException(DOMException.WRONG_DOCUMENT_ERR)); - } - - // throw Exception if DOMNamedNodeMap is readonly - if (this._readonly || (this.parentNode && this.parentNode._readonly)) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } - - // throw Exception if arg is already an attribute of another Element object - if (arg.ownerElement && (arg.ownerElement != this.parentNode)) { - throw(new DOMException(DOMException.INUSE_ATTRIBUTE_ERR)); - } - } - - //console.log('setNamedItem __findNamedItemIndex__ '); - // get item index - var itemIndex = __findNamedItemIndex__(this, arg.name); - var ret = null; - - //console.log('setNamedItem __findNamedItemIndex__ %s', itemIndex); - if (itemIndex > -1) { // found it! - ret = this[itemIndex]; // use existing Attribute - - // throw Exception if DOMAttr is readonly - if (__ownerDocument__(this).implementation.errorChecking && ret._readonly) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } else { - this[itemIndex] = arg; // over-write existing NamedNode - this[arg.name.toLowerCase()] = arg; - } - } else { - // add new NamedNode - //console.log('setNamedItem add new named node map (by index)'); - Array.prototype.push.apply(this, [arg]); - //console.log('setNamedItem add new named node map (by name) %s %s', arg, arg.name); - this[arg.name] = arg; - //console.log('finsished setNamedItem add new named node map (by name) %s', arg.name); - - } - - //console.log('setNamedItem parentNode'); - arg.ownerElement = this.parentNode; // update ownerElement - // return old node or new node - //console.log('setNamedItem exit'); - return ret; - }, - removeNamedItem : function(name) { - var ret = null; - // test for exceptions - // throw Exception if NamedNodeMap is readonly - if (__ownerDocument__(this).implementation.errorChecking && - (this._readonly || (this.parentNode && this.parentNode._readonly))) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } - - // get item index - var itemIndex = __findNamedItemIndex__(this, name); - - // throw Exception if there is no node named name in this map - if (__ownerDocument__(this).implementation.errorChecking && (itemIndex < 0)) { - throw(new DOMException(DOMException.NOT_FOUND_ERR)); - } - - // get Node - var oldNode = this[itemIndex]; - //this[oldNode.name] = undefined; - - // throw Exception if Node is readonly - if (__ownerDocument__(this).implementation.errorChecking && oldNode._readonly) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } - - // return removed node - return __removeChild__(this, itemIndex); - }, - getNamedItemNS : function(namespaceURI, localName) { - var ret = null; - - // test that Named Node exists - var itemIndex = __findNamedItemNSIndex__(this, namespaceURI, localName); - - if (itemIndex > -1) { - // found it! return NamedNode - ret = this[itemIndex]; - } - // if node is not found, default value null is returned - return ret; - }, - setNamedItemNS : function(arg) { - //console.log('setNamedItemNS %s', arg); - // test for exceptions - if (__ownerDocument__(this).implementation.errorChecking) { - // throw Exception if NamedNodeMap is readonly - if (this._readonly || (this.parentNode && this.parentNode._readonly)) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } - - // throw Exception if arg was not created by this Document - if (__ownerDocument__(this) != __ownerDocument__(arg)) { - throw(new DOMException(DOMException.WRONG_DOCUMENT_ERR)); - } - - // throw Exception if arg is already an attribute of another Element object - if (arg.ownerElement && (arg.ownerElement != this.parentNode)) { - throw(new DOMException(DOMException.INUSE_ATTRIBUTE_ERR)); - } - } - - // get item index - var itemIndex = __findNamedItemNSIndex__(this, arg.namespaceURI, arg.localName); - var ret = null; - - if (itemIndex > -1) { - // found it! - // use existing Attribute - ret = this[itemIndex]; - // throw Exception if Attr is readonly - if (__ownerDocument__(this).implementation.errorChecking && ret._readonly) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } else { - // over-write existing NamedNode - this[itemIndex] = arg; - } - }else { - // add new NamedNode - Array.prototype.push.apply(this, [arg]); - } - arg.ownerElement = this.parentNode; - - // return old node or null - return ret; - //console.log('finished setNamedItemNS %s', arg); - }, - removeNamedItemNS : function(namespaceURI, localName) { - var ret = null; - - // test for exceptions - // throw Exception if NamedNodeMap is readonly - if (__ownerDocument__(this).implementation.errorChecking && (this._readonly || (this.parentNode && this.parentNode._readonly))) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } - - // get item index - var itemIndex = __findNamedItemNSIndex__(this, namespaceURI, localName); - - // throw Exception if there is no matching node in this map - if (__ownerDocument__(this).implementation.errorChecking && (itemIndex < 0)) { - throw(new DOMException(DOMException.NOT_FOUND_ERR)); - } - - // get Node - var oldNode = this[itemIndex]; - - // throw Exception if Node is readonly - if (__ownerDocument__(this).implementation.errorChecking && oldNode._readonly) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } - - return __removeChild__(this, itemIndex); // return removed node - }, - get xml() { - var ret = ""; - - // create string containing concatenation of all (but last) Attribute string values (separated by spaces) - for (var i=0; i < this.length -1; i++) { - ret += this[i].xml +" "; - } - - // add last Attribute to string (without trailing space) - if (this.length > 0) { - ret += this[this.length -1].xml; - } - - return ret; - }, - toString : function(){ - return "[object NamedNodeMap]"; - } - -}); - -/** - * @method __findNamedItemIndex__ - * find the item index of the node with the specified name - * - * @param name : string - the name of the required node - * @param isnsmap : if its a NamespaceNodeMap - * @return : int - */ -var __findNamedItemIndex__ = function(namednodemap, name, isnsmap) { - var ret = -1; - // loop through all nodes - for (var i=0; i -1) { - // found it! - ret = true; - } - // if node is not found, default value false is returned - return ret; -} - -/** - * @method __hasAttributeNS__ - * Returns true if specified node exists - * - * @param namespaceURI : string - the namespace URI of the required node - * @param localName : string - the local name of the required node - * @return : boolean - */ -var __hasAttributeNS__ = function(namednodemap, namespaceURI, localName) { - var ret = false; - // test that Named Node exists - var itemIndex = __findNamedItemNSIndex__(namednodemap, namespaceURI, localName); - if (itemIndex > -1) { - // found it! - ret = true; - } - // if node is not found, default value false is returned - return ret; -} - -/** - * @method __cloneNamedNodes__ - * Returns a NamedNodeMap containing clones of the Nodes in this NamedNodeMap - * - * @param parentNode : Node - the new parent of the cloned NodeList - * @param isnsmap : bool - is this a NamespaceNodeMap - * @return NamedNodeMap containing clones of the Nodes in this NamedNodeMap - */ -var __cloneNamedNodes__ = function(namednodemap, parentNode, isnsmap) { - var cloneNamedNodeMap = isnsmap? - new NamespaceNodeMap(namednodemap.ownerDocument, parentNode): - new NamedNodeMap(namednodemap.ownerDocument, parentNode); - - // create list containing clones of all children - for (var i=0; i < namednodemap.length; i++) { - __appendChild__(cloneNamedNodeMap, namednodemap[i].cloneNode(false)); - } - - return cloneNamedNodeMap; -}; - - -/** - * @class NamespaceNodeMap - - * used to represent collections of namespace nodes that can be - * accessed by name typically a set of Element attributes - * - * @extends NamedNodeMap - * - * @param ownerDocument : Document - the ownerDocument - * @param parentNode : Node - the node that the NamespaceNodeMap is attached to (or null) - */ -var NamespaceNodeMap = function(ownerDocument, parentNode) { - this.NamedNodeMap = NamedNodeMap; - this.NamedNodeMap(ownerDocument, parentNode); - __setArray__(this, []); -}; -NamespaceNodeMap.prototype = new NamedNodeMap(); -__extend__(NamespaceNodeMap.prototype, { - get xml() { - var ret = "", - ns, - ind; - // identify namespaces declared local to this Element (ie, not inherited) - for (ind = 0; ind < this.length; ind++) { - // if namespace declaration does not exist in the containing node's, parentNode's namespaces - ns = null; - try { - var ns = this.parentNode.parentNode._namespaces. - getNamedItem(this[ind].localName); - }catch (e) { - //breaking to prevent default namespace being inserted into return value - break; - } - if (!(ns && (""+ ns.nodeValue == ""+ this[ind].nodeValue))) { - // display the namespace declaration - ret += this[ind].xml +" "; - } - } - return ret; - } -}); - -/** - * @class Namespace - - * The Namespace interface represents an namespace in an Element object - * - * @param ownerDocument : The Document object associated with this node. - */ -Namespace = function(ownerDocument) { - Node.apply(this, arguments); - // the name of this attribute - this.name = ""; - - // If this attribute was explicitly given a value in the original document, - // this is true; otherwise, it is false. - // Note that the implementation is in charge of this attribute, not the user. - // If the user changes the value of the attribute (even if it ends up having - // the same value as the default value) then the specified flag is - // automatically flipped to true - this.specified = false; -}; -Namespace.prototype = new Node(); -__extend__(Namespace.prototype, { - get value(){ - // the value of the attribute is returned as a string - return this.nodeValue; - }, - set value(value){ - this.nodeValue = value+''; - }, - get nodeType(){ - return Node.NAMESPACE_NODE; - }, - get xml(){ - var ret = ""; - - // serialize Namespace Declaration - if (this.nodeName != "") { - ret += this.nodeName +"=\""+ __escapeXML__(this.nodeValue) +"\""; - } - else { // handle default namespace - ret += "xmlns=\""+ __escapeXML__(this.nodeValue) +"\""; - } - - return ret; - }, - toString: function(){ - return '[object Namespace]'; - } -}); - - -/** - * @class CharacterData - parent abstract class for Text and Comment - * @extends Node - * @param ownerDocument : The Document object associated with this node. - */ -CharacterData = function(ownerDocument) { - Node.apply(this, arguments); -}; -CharacterData.prototype = new Node(); -__extend__(CharacterData.prototype,{ - get data(){ - return this.nodeValue; - }, - set data(data){ - this.nodeValue = data; - }, - get textContent(){ - return this.nodeValue; - }, - set textContent(newText){ - this.nodeValue = newText; - }, - get length(){return this.nodeValue.length;}, - appendData: function(arg){ - // throw Exception if CharacterData is readonly - if (__ownerDocument__(this).implementation.errorChecking && this._readonly) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } - // append data - this.data = "" + this.data + arg; - }, - deleteData: function(offset, count){ - // throw Exception if CharacterData is readonly - if (__ownerDocument__(this).implementation.errorChecking && this._readonly) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } - if (this.data) { - // throw Exception if offset is negative or greater than the data length, - if (__ownerDocument__(this).implementation.errorChecking && - ((offset < 0) || (offset > this.data.length) || (count < 0))) { - throw(new DOMException(DOMException.INDEX_SIZE_ERR)); - } - - // delete data - if(!count || (offset + count) > this.data.length) { - this.data = this.data.substring(0, offset); - }else { - this.data = this.data.substring(0, offset). - concat(this.data.substring(offset + count)); - } - } - }, - insertData: function(offset, arg){ - // throw Exception if CharacterData is readonly - if(__ownerDocument__(this).implementation.errorChecking && this._readonly){ - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } - - if(this.data){ - // throw Exception if offset is negative or greater than the data length, - if (__ownerDocument__(this).implementation.errorChecking && - ((offset < 0) || (offset > this.data.length))) { - throw(new DOMException(DOMException.INDEX_SIZE_ERR)); - } - - // insert data - this.data = this.data.substring(0, offset).concat(arg, this.data.substring(offset)); - }else { - // throw Exception if offset is negative or greater than the data length, - if (__ownerDocument__(this).implementation.errorChecking && (offset !== 0)) { - throw(new DOMException(DOMException.INDEX_SIZE_ERR)); - } - - // set data - this.data = arg; - } - }, - replaceData: function(offset, count, arg){ - // throw Exception if CharacterData is readonly - if (__ownerDocument__(this).implementation.errorChecking && this._readonly) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } - - if (this.data) { - // throw Exception if offset is negative or greater than the data length, - if (__ownerDocument__(this).implementation.errorChecking && - ((offset < 0) || (offset > this.data.length) || (count < 0))) { - throw(new DOMException(DOMException.INDEX_SIZE_ERR)); - } - - // replace data - this.data = this.data.substring(0, offset). - concat(arg, this.data.substring(offset + count)); - }else { - // set data - this.data = arg; - } - }, - substringData: function(offset, count){ - var ret = null; - if (this.data) { - // throw Exception if offset is negative or greater than the data length, - // or the count is negative - if (__ownerDocument__(this).implementation.errorChecking && - ((offset < 0) || (offset > this.data.length) || (count < 0))) { - throw(new DOMException(DOMException.INDEX_SIZE_ERR)); - } - // if count is not specified - if (!count) { - ret = this.data.substring(offset); // default to 'end of string' - }else{ - ret = this.data.substring(offset, offset + count); - } - } - return ret; - }, - toString : function(){ - return "[object CharacterData]"; - } -}); - -/** - * @class Text - * The Text interface represents the textual content (termed - * character data in XML) of an Element or Attr. - * If there is no markup inside an element's content, the text is - * contained in a single object implementing the Text interface that - * is the only child of the element. If there is markup, it is - * parsed into a list of elements and Text nodes that form the - * list of children of the element. - * @extends CharacterData - * @param ownerDocument The Document object associated with this node. - */ -Text = function(ownerDocument) { - CharacterData.apply(this, arguments); - this.nodeName = "#text"; -}; -Text.prototype = new CharacterData(); -__extend__(Text.prototype,{ - get localName(){ - return null; - }, - // Breaks this Text node into two Text nodes at the specified offset, - // keeping both in the tree as siblings. This node then only contains - // all the content up to the offset point. And a new Text node, which - // is inserted as the next sibling of this node, contains all the - // content at and after the offset point. - splitText : function(offset) { - var data, - inode; - // test for exceptions - if (__ownerDocument__(this).implementation.errorChecking) { - // throw Exception if Node is readonly - if (this._readonly) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } - // throw Exception if offset is negative or greater than the data length, - if ((offset < 0) || (offset > this.data.length)) { - throw(new DOMException(DOMException.INDEX_SIZE_ERR)); - } - } - if (this.parentNode) { - // get remaining string (after offset) - data = this.substringData(offset); - // create new TextNode with remaining string - inode = __ownerDocument__(this).createTextNode(data); - // attach new TextNode - if (this.nextSibling) { - this.parentNode.insertBefore(inode, this.nextSibling); - } else { - this.parentNode.appendChild(inode); - } - // remove remaining string from original TextNode - this.deleteData(offset); - } - return inode; - }, - get nodeType(){ - return Node.TEXT_NODE; - }, - get xml(){ - return __escapeXML__(""+ this.nodeValue); - }, - toString: function(){ - return "[object Text]"; - } -}); - -/** - * @class CDATASection - * CDATA sections are used to escape blocks of text containing - * characters that would otherwise be regarded as markup. - * The only delimiter that is recognized in a CDATA section is - * the "\]\]\>" string that ends the CDATA section - * @extends Text - * @param ownerDocument : The Document object associated with this node. - */ -CDATASection = function(ownerDocument) { - Text.apply(this, arguments); - this.nodeName = '#cdata-section'; -}; -CDATASection.prototype = new Text(); -__extend__(CDATASection.prototype,{ - get nodeType(){ - return Node.CDATA_SECTION_NODE; - }, - get xml(){ - return ""; - }, - toString : function(){ - return "[object CDATASection]"; - } -}); -/** - * @class Comment - * This represents the content of a comment, i.e., all the - * characters between the starting '' - * @extends CharacterData - * @param ownerDocument : The Document object associated with this node. - */ -Comment = function(ownerDocument) { - CharacterData.apply(this, arguments); - this.nodeName = "#comment"; -}; -Comment.prototype = new CharacterData(); -__extend__(Comment.prototype, { - get localName(){ - return null; - }, - get nodeType(){ - return Node.COMMENT_NODE; - }, - get xml(){ - return ""; - }, - toString : function(){ - return "[object Comment]"; - } -}); - - -/** - * @author envjs team - * @param {Document} onwnerDocument - */ -DocumentType = function(ownerDocument) { - Node.apply(this, arguments); - this.systemId = null; - this.publicId = null; -}; -DocumentType.prototype = new Node(); -__extend__({ - get name(){ - return this.nodeName; - }, - get entities(){ - return null; - }, - get internalSubsets(){ - return null; - }, - get notations(){ - return null; - }, - toString : function(){ - return "[object DocumentType]"; - } -}); - -/** - * @class Attr - * The Attr interface represents an attribute in an Element object - * @extends Node - * @param ownerDocument : The Document object associated with this node. - */ -Attr = function(ownerDocument) { - Node.apply(this, arguments); - // set when Attr is added to NamedNodeMap - this.ownerElement = null; - //TODO: our implementation of Attr is incorrect because we don't - // treat the value of the attribute as a child text node. -}; -Attr.prototype = new Node(); -__extend__(Attr.prototype, { - // the name of this attribute - get name(){ - return this.nodeName; - }, - // the value of the attribute is returned as a string - get value(){ - return this.nodeValue||''; - }, - set value(value){ - // throw Exception if Attribute is readonly - if (__ownerDocument__(this).implementation.errorChecking && this._readonly) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } - // delegate to node - this.nodeValue = value; - }, - get textContent(){ - return this.nodeValue; - }, - set textContent(newText){ - this.nodeValue = newText; - }, - get specified(){ - return (this !== null && this !== undefined); - }, - get nodeType(){ - return Node.ATTRIBUTE_NODE; - }, - get xml() { - if (this.nodeValue) { - return __escapeXML__(this.nodeValue+""); - } else { - return ''; - } - }, - toString : function() { - return '[object Attr]'; - } -}); - - -/** - * @class Element - - * By far the vast majority of objects (apart from text) - * that authors encounter when traversing a document are - * Element nodes. - * @extends Node - * @param ownerDocument : The Document object associated with this node. - */ -Element = function(ownerDocument) { - Node.apply(this, arguments); - this.attributes = new NamedNodeMap(this.ownerDocument, this); -}; -Element.prototype = new Node(); -__extend__(Element.prototype, { - // The name of the element. - get tagName(){ - return this.nodeName; - }, - - getAttribute: function(name) { - var ret = null; - // if attribute exists, use it - var attr = this.attributes.getNamedItem(name); - if (attr) { - ret = attr.value; - } - // if Attribute exists, return its value, otherwise, return null - return ret; - }, - setAttribute : function (name, value) { - // if attribute exists, use it - var attr = this.attributes.getNamedItem(name); - //console.log('attr %s', attr); - //I had to add this check because as the script initializes - //the id may be set in the constructor, and the html element - //overrides the id property with a getter/setter. - if(__ownerDocument__(this)){ - if (attr===null||attr===undefined) { - // otherwise create it - attr = __ownerDocument__(this).createAttribute(name); - //console.log('attr %s', attr); - } - - - // test for exceptions - if (__ownerDocument__(this).implementation.errorChecking) { - // throw Exception if Attribute is readonly - if (attr._readonly) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } - - // throw Exception if the value string contains an illegal character - if (!__isValidString__(value+'')) { - throw(new DOMException(DOMException.INVALID_CHARACTER_ERR)); - } - } - - // assign values to properties (and aliases) - attr.value = value + ''; - - // add/replace Attribute in NamedNodeMap - this.attributes.setNamedItem(attr); - //console.log('element setNamedItem %s', attr); - }else{ - console.warn('Element has no owner document '+this.tagName+ - '\n\t cant set attribute ' + name + ' = '+value ); - } - }, - removeAttribute : function removeAttribute(name) { - // delegate to NamedNodeMap.removeNamedItem - return this.attributes.removeNamedItem(name); - }, - getAttributeNode : function getAttributeNode(name) { - // delegate to NamedNodeMap.getNamedItem - return this.attributes.getNamedItem(name); - }, - setAttributeNode: function(newAttr) { - // if this Attribute is an ID - if (__isIdDeclaration__(newAttr.name)) { - this.id = newAttr.value; // cache ID for getElementById() - } - // delegate to NamedNodeMap.setNamedItem - return this.attributes.setNamedItem(newAttr); - }, - removeAttributeNode: function(oldAttr) { - // throw Exception if Attribute is readonly - if (__ownerDocument__(this).implementation.errorChecking && oldAttr._readonly) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } - - // get item index - var itemIndex = this.attributes._findItemIndex(oldAttr._id); - - // throw Exception if node does not exist in this map - if (__ownerDocument__(this).implementation.errorChecking && (itemIndex < 0)) { - throw(new DOMException(DOMException.NOT_FOUND_ERR)); - } - - return this.attributes._removeChild(itemIndex); - }, - getAttributeNS : function(namespaceURI, localName) { - var ret = ""; - // delegate to NAmedNodeMap.getNamedItemNS - var attr = this.attributes.getNamedItemNS(namespaceURI, localName); - if (attr) { - ret = attr.value; - } - return ret; // if Attribute exists, return its value, otherwise return "" - }, - setAttributeNS : function(namespaceURI, qualifiedName, value) { - // call NamedNodeMap.getNamedItem - //console.log('setAttributeNS %s %s %s', namespaceURI, qualifiedName, value); - var attr = this.attributes.getNamedItem(namespaceURI, qualifiedName); - - if (!attr) { // if Attribute exists, use it - // otherwise create it - attr = __ownerDocument__(this).createAttributeNS(namespaceURI, qualifiedName); - } - - value = '' + value; - - // test for exceptions - if (__ownerDocument__(this).implementation.errorChecking) { - // throw Exception if Attribute is readonly - if (attr._readonly) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } - - // throw Exception if the Namespace is invalid - if (!__isValidNamespace__(this.ownerDocument, namespaceURI, qualifiedName, true)) { - throw(new DOMException(DOMException.NAMESPACE_ERR)); - } - - // throw Exception if the value string contains an illegal character - if (!__isValidString__(value)) { - throw(new DOMException(DOMException.INVALID_CHARACTER_ERR)); - } - } - - // if this Attribute is an ID - //if (__isIdDeclaration__(name)) { - // this.id = value; - //} - - // assign values to properties (and aliases) - attr.value = value; - attr.nodeValue = value; - - // delegate to NamedNodeMap.setNamedItem - this.attributes.setNamedItemNS(attr); - }, - removeAttributeNS : function(namespaceURI, localName) { - // delegate to NamedNodeMap.removeNamedItemNS - return this.attributes.removeNamedItemNS(namespaceURI, localName); - }, - getAttributeNodeNS : function(namespaceURI, localName) { - // delegate to NamedNodeMap.getNamedItemNS - return this.attributes.getNamedItemNS(namespaceURI, localName); - }, - setAttributeNodeNS : function(newAttr) { - // if this Attribute is an ID - if ((newAttr.prefix == "") && __isIdDeclaration__(newAttr.name)) { - this.id = newAttr.value+''; // cache ID for getElementById() - } - - // delegate to NamedNodeMap.setNamedItemNS - return this.attributes.setNamedItemNS(newAttr); - }, - hasAttribute : function(name) { - // delegate to NamedNodeMap._hasAttribute - return __hasAttribute__(this.attributes,name); - }, - hasAttributeNS : function(namespaceURI, localName) { - // delegate to NamedNodeMap._hasAttributeNS - return __hasAttributeNS__(this.attributes, namespaceURI, localName); - }, - get nodeType(){ - return Node.ELEMENT_NODE; - }, - get xml() { - var ret = "", - ns = "", - attrs, - attrstring, - i; - - // serialize namespace declarations - if (this.namespaceURI ){ - if((this === this.ownerDocument.documentElement) || - (!this.parentNode)|| - (this.parentNode && (this.parentNode.namespaceURI !== this.namespaceURI))) { - ns = ' xmlns' + (this.prefix?(':'+this.prefix):'') + - '="' + this.namespaceURI + '"'; - } - } - - // serialize Attribute declarations - attrs = this.attributes; - attrstring = ""; - for(i=0;i< attrs.length;i++){ - if(attrs[i].name.match('xmlns:')) { - attrstring += " "+attrs[i].name+'="'+attrs[i].xml+'"'; - } - } - for(i=0;i< attrs.length;i++){ - if(!attrs[i].name.match('xmlns:')) { - attrstring += " "+attrs[i].name+'="'+attrs[i].xml+'"'; - } - } - - if(this.hasChildNodes()){ - // serialize this Element - ret += "<" + this.tagName + ns + attrstring +">"; - ret += this.childNodes.xml; - ret += ""; - }else{ - ret += "<" + this.tagName + ns + attrstring +"/>"; - } - - return ret; - }, - toString : function(){ - return '[object Element]'; - } -}); -/** - * @class DOMException - raised when an operation is impossible to perform - * @author Jon van Noort (jon@webarcana.com.au) - * @param code : int - the exception code (one of the DOMException constants) - */ -DOMException = function(code) { - this.code = code; -}; - -// DOMException constants -// Introduced in DOM Level 1: -DOMException.INDEX_SIZE_ERR = 1; -DOMException.DOMSTRING_SIZE_ERR = 2; -DOMException.HIERARCHY_REQUEST_ERR = 3; -DOMException.WRONG_DOCUMENT_ERR = 4; -DOMException.INVALID_CHARACTER_ERR = 5; -DOMException.NO_DATA_ALLOWED_ERR = 6; -DOMException.NO_MODIFICATION_ALLOWED_ERR = 7; -DOMException.NOT_FOUND_ERR = 8; -DOMException.NOT_SUPPORTED_ERR = 9; -DOMException.INUSE_ATTRIBUTE_ERR = 10; - -// Introduced in DOM Level 2: -DOMException.INVALID_STATE_ERR = 11; -DOMException.SYNTAX_ERR = 12; -DOMException.INVALID_MODIFICATION_ERR = 13; -DOMException.NAMESPACE_ERR = 14; -DOMException.INVALID_ACCESS_ERR = 15; - -/** - * @class DocumentFragment - - * DocumentFragment is a "lightweight" or "minimal" Document object. - * @extends Node - * @param ownerDocument : The Document object associated with this node. - */ -DocumentFragment = function(ownerDocument) { - Node.apply(this, arguments); - this.nodeName = "#document-fragment"; -}; -DocumentFragment.prototype = new Node(); -__extend__(DocumentFragment.prototype,{ - get nodeType(){ - return Node.DOCUMENT_FRAGMENT_NODE; - }, - get xml(){ - var xml = "", - count = this.childNodes.length; - - // create string concatenating the serialized ChildNodes - for (var i = 0; i < count; i++) { - xml += this.childNodes.item(i).xml; - } - - return xml; - }, - toString : function(){ - return "[object DocumentFragment]"; - }, - get localName(){ - return null; - } -}); - - -/** - * @class ProcessingInstruction - - * The ProcessingInstruction interface represents a - * "processing instruction", used in XML as a way to - * keep processor-specific information in the text of - * the document - * @extends Node - * @author Jon van Noort (jon@webarcana.com.au) - * @param ownerDocument : The Document object associated with this node. - */ -ProcessingInstruction = function(ownerDocument) { - Node.apply(this, arguments); -}; -ProcessingInstruction.prototype = new Node(); -__extend__(ProcessingInstruction.prototype, { - get data(){ - return this.nodeValue; - }, - set data(data){ - // throw Exception if Node is readonly - if (__ownerDocument__(this).errorChecking && this._readonly) { - throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR)); - } - this.nodeValue = data; - }, - get textContent(){ - return this.data; - }, - get localName(){ - return null; - }, - get target(){ - // The target of this processing instruction. - // XML defines this as being the first token following the markup that begins the processing instruction. - // The content of this processing instruction. - return this.nodeName; - }, - set target(value){ - // The target of this processing instruction. - // XML defines this as being the first token following the markup that begins the processing instruction. - // The content of this processing instruction. - this.nodeName = value; - }, - get nodeType(){ - return Node.PROCESSING_INSTRUCTION_NODE; - }, - get xml(){ - return ""; - }, - toString : function(){ - return "[object ProcessingInstruction]"; - } -}); - - -/** - * @author envjs team - */ - -Entity = function() { - throw new Error("Entity Not Implemented" ); -}; - -Entity.constants = { - // content taken from W3C "HTML 4.01 Specification" - // "W3C Recommendation 24 December 1999" - - nbsp: "\u00A0", - iexcl: "\u00A1", - cent: "\u00A2", - pound: "\u00A3", - curren: "\u00A4", - yen: "\u00A5", - brvbar: "\u00A6", - sect: "\u00A7", - uml: "\u00A8", - copy: "\u00A9", - ordf: "\u00AA", - laquo: "\u00AB", - not: "\u00AC", - shy: "\u00AD", - reg: "\u00AE", - macr: "\u00AF", - deg: "\u00B0", - plusmn: "\u00B1", - sup2: "\u00B2", - sup3: "\u00B3", - acute: "\u00B4", - micro: "\u00B5", - para: "\u00B6", - middot: "\u00B7", - cedil: "\u00B8", - sup1: "\u00B9", - ordm: "\u00BA", - raquo: "\u00BB", - frac14: "\u00BC", - frac12: "\u00BD", - frac34: "\u00BE", - iquest: "\u00BF", - Agrave: "\u00C0", - Aacute: "\u00C1", - Acirc: "\u00C2", - Atilde: "\u00C3", - Auml: "\u00C4", - Aring: "\u00C5", - AElig: "\u00C6", - Ccedil: "\u00C7", - Egrave: "\u00C8", - Eacute: "\u00C9", - Ecirc: "\u00CA", - Euml: "\u00CB", - Igrave: "\u00CC", - Iacute: "\u00CD", - Icirc: "\u00CE", - Iuml: "\u00CF", - ETH: "\u00D0", - Ntilde: "\u00D1", - Ograve: "\u00D2", - Oacute: "\u00D3", - Ocirc: "\u00D4", - Otilde: "\u00D5", - Ouml: "\u00D6", - times: "\u00D7", - Oslash: "\u00D8", - Ugrave: "\u00D9", - Uacute: "\u00DA", - Ucirc: "\u00DB", - Uuml: "\u00DC", - Yacute: "\u00DD", - THORN: "\u00DE", - szlig: "\u00DF", - agrave: "\u00E0", - aacute: "\u00E1", - acirc: "\u00E2", - atilde: "\u00E3", - auml: "\u00E4", - aring: "\u00E5", - aelig: "\u00E6", - ccedil: "\u00E7", - egrave: "\u00E8", - eacute: "\u00E9", - ecirc: "\u00EA", - euml: "\u00EB", - igrave: "\u00EC", - iacute: "\u00ED", - icirc: "\u00EE", - iuml: "\u00EF", - eth: "\u00F0", - ntilde: "\u00F1", - ograve: "\u00F2", - oacute: "\u00F3", - ocirc: "\u00F4", - otilde: "\u00F5", - ouml: "\u00F6", - divide: "\u00F7", - oslash: "\u00F8", - ugrave: "\u00F9", - uacute: "\u00FA", - ucirc: "\u00FB", - uuml: "\u00FC", - yacute: "\u00FD", - thorn: "\u00FE", - yuml: "\u00FF", - fnof: "\u0192", - Alpha: "\u0391", - Beta: "\u0392", - Gamma: "\u0393", - Delta: "\u0394", - Epsilon: "\u0395", - Zeta: "\u0396", - Eta: "\u0397", - Theta: "\u0398", - Iota: "\u0399", - Kappa: "\u039A", - Lambda: "\u039B", - Mu: "\u039C", - Nu: "\u039D", - Xi: "\u039E", - Omicron: "\u039F", - Pi: "\u03A0", - Rho: "\u03A1", - Sigma: "\u03A3", - Tau: "\u03A4", - Upsilon: "\u03A5", - Phi: "\u03A6", - Chi: "\u03A7", - Psi: "\u03A8", - Omega: "\u03A9", - alpha: "\u03B1", - beta: "\u03B2", - gamma: "\u03B3", - delta: "\u03B4", - epsilon: "\u03B5", - zeta: "\u03B6", - eta: "\u03B7", - theta: "\u03B8", - iota: "\u03B9", - kappa: "\u03BA", - lambda: "\u03BB", - mu: "\u03BC", - nu: "\u03BD", - xi: "\u03BE", - omicron: "\u03BF", - pi: "\u03C0", - rho: "\u03C1", - sigmaf: "\u03C2", - sigma: "\u03C3", - tau: "\u03C4", - upsilon: "\u03C5", - phi: "\u03C6", - chi: "\u03C7", - psi: "\u03C8", - omega: "\u03C9", - thetasym: "\u03D1", - upsih: "\u03D2", - piv: "\u03D6", - bull: "\u2022", - hellip: "\u2026", - prime: "\u2032", - Prime: "\u2033", - oline: "\u203E", - frasl: "\u2044", - weierp: "\u2118", - image: "\u2111", - real: "\u211C", - trade: "\u2122", - alefsym: "\u2135", - larr: "\u2190", - uarr: "\u2191", - rarr: "\u2192", - darr: "\u2193", - harr: "\u2194", - crarr: "\u21B5", - lArr: "\u21D0", - uArr: "\u21D1", - rArr: "\u21D2", - dArr: "\u21D3", - hArr: "\u21D4", - forall: "\u2200", - part: "\u2202", - exist: "\u2203", - empty: "\u2205", - nabla: "\u2207", - isin: "\u2208", - notin: "\u2209", - ni: "\u220B", - prod: "\u220F", - sum: "\u2211", - minus: "\u2212", - lowast: "\u2217", - radic: "\u221A", - prop: "\u221D", - infin: "\u221E", - ang: "\u2220", - and: "\u2227", - or: "\u2228", - cap: "\u2229", - cup: "\u222A", - intXX: "\u222B", - there4: "\u2234", - sim: "\u223C", - cong: "\u2245", - asymp: "\u2248", - ne: "\u2260", - equiv: "\u2261", - le: "\u2264", - ge: "\u2265", - sub: "\u2282", - sup: "\u2283", - nsub: "\u2284", - sube: "\u2286", - supe: "\u2287", - oplus: "\u2295", - otimes: "\u2297", - perp: "\u22A5", - sdot: "\u22C5", - lceil: "\u2308", - rceil: "\u2309", - lfloor: "\u230A", - rfloor: "\u230B", - lang: "\u2329", - rang: "\u232A", - loz: "\u25CA", - spades: "\u2660", - clubs: "\u2663", - hearts: "\u2665", - diams: "\u2666", - quot: "\u0022", - amp: "\u0026", - lt: "\u003C", - gt: "\u003E", - OElig: "\u0152", - oelig: "\u0153", - Scaron: "\u0160", - scaron: "\u0161", - Yuml: "\u0178", - circ: "\u02C6", - tilde: "\u02DC", - ensp: "\u2002", - emsp: "\u2003", - thinsp: "\u2009", - zwnj: "\u200C", - zwj: "\u200D", - lrm: "\u200E", - rlm: "\u200F", - ndash: "\u2013", - mdash: "\u2014", - lsquo: "\u2018", - rsquo: "\u2019", - sbquo: "\u201A", - ldquo: "\u201C", - rdquo: "\u201D", - bdquo: "\u201E", - dagger: "\u2020", - Dagger: "\u2021", - permil: "\u2030", - lsaquo: "\u2039", - rsaquo: "\u203A", - euro: "\u20AC", - - // non-standard entities - apos: "'" -}; - -/** - * @author envjs team - */ - -EntityReference = function() { - throw new Error("EntityReference Not Implemented" ); -}; - -/** - * @class DOMImplementation - - * provides a number of methods for performing operations - * that are independent of any particular instance of the - * document object model. - * - * @author Jon van Noort (jon@webarcana.com.au) - */ -DOMImplementation = function() { - this.preserveWhiteSpace = false; // by default, ignore whitespace - this.namespaceAware = true; // by default, handle namespaces - this.errorChecking = true; // by default, test for exceptions -}; - -__extend__(DOMImplementation.prototype,{ - // @param feature : string - The package name of the feature to test. - // the legal only values are "XML" and "CORE" (case-insensitive). - // @param version : string - This is the version number of the package - // name to test. In Level 1, this is the string "1.0".* - // @return : boolean - hasFeature : function(feature, version) { - var ret = false; - if (feature.toLowerCase() == "xml") { - ret = (!version || (version == "1.0") || (version == "2.0")); - } - else if (feature.toLowerCase() == "core") { - ret = (!version || (version == "2.0")); - } - else if (feature == "http://www.w3.org/TR/SVG11/feature#BasicStructure") { - ret = (version == "1.1"); - } - return ret; - }, - createDocumentType : function(qname, publicId, systemId){ - var doctype = new DocumentType(); - doctype.nodeName = qname?qname.toUpperCase():null; - doctype.publicId = publicId?publicId:null; - doctype.systemId = systemId?systemId:null; - return doctype; - }, - createDocument : function(nsuri, qname, doctype){ - - var doc = null, documentElement; - - doc = new Document(this, null); - if(doctype){ - doc.doctype = doctype; - } - - if(nsuri && qname){ - documentElement = doc.createElementNS(nsuri, qname); - }else if(qname){ - documentElement = doc.createElement(qname); - } - if(documentElement){ - doc.appendChild(documentElement); - } - return doc; - }, - createHTMLDocument : function(title){ - var doc = new HTMLDocument($implementation, null, ""); - var html = doc.createElement("html"); doc.appendChild(html); - var head = doc.createElement("head"); html.appendChild(head); - var body = doc.createElement("body"); html.appendChild(body); - var t = doc.createElement("title"); head.appendChild(t); - if( title) { - t.appendChild(doc.createTextNode(title)); - } - return doc; - }, - translateErrCode : function(code) { - //convert DOMException Code to human readable error message; - var msg = ""; - - switch (code) { - case DOMException.INDEX_SIZE_ERR : // 1 - msg = "INDEX_SIZE_ERR: Index out of bounds"; - break; - - case DOMException.DOMSTRING_SIZE_ERR : // 2 - msg = "DOMSTRING_SIZE_ERR: The resulting string is too long to fit in a DOMString"; - break; - - case DOMException.HIERARCHY_REQUEST_ERR : // 3 - msg = "HIERARCHY_REQUEST_ERR: The Node can not be inserted at this location"; - break; - - case DOMException.WRONG_DOCUMENT_ERR : // 4 - msg = "WRONG_DOCUMENT_ERR: The source and the destination Documents are not the same"; - break; - - case DOMException.INVALID_CHARACTER_ERR : // 5 - msg = "INVALID_CHARACTER_ERR: The string contains an invalid character"; - break; - - case DOMException.NO_DATA_ALLOWED_ERR : // 6 - msg = "NO_DATA_ALLOWED_ERR: This Node / NodeList does not support data"; - break; - - case DOMException.NO_MODIFICATION_ALLOWED_ERR : // 7 - msg = "NO_MODIFICATION_ALLOWED_ERR: This object cannot be modified"; - break; - - case DOMException.NOT_FOUND_ERR : // 8 - msg = "NOT_FOUND_ERR: The item cannot be found"; - break; - - case DOMException.NOT_SUPPORTED_ERR : // 9 - msg = "NOT_SUPPORTED_ERR: This implementation does not support function"; - break; - - case DOMException.INUSE_ATTRIBUTE_ERR : // 10 - msg = "INUSE_ATTRIBUTE_ERR: The Attribute has already been assigned to another Element"; - break; - - // Introduced in DOM Level 2: - case DOMException.INVALID_STATE_ERR : // 11 - msg = "INVALID_STATE_ERR: The object is no longer usable"; - break; - - case DOMException.SYNTAX_ERR : // 12 - msg = "SYNTAX_ERR: Syntax error"; - break; - - case DOMException.INVALID_MODIFICATION_ERR : // 13 - msg = "INVALID_MODIFICATION_ERR: Cannot change the type of the object"; - break; - - case DOMException.NAMESPACE_ERR : // 14 - msg = "NAMESPACE_ERR: The namespace declaration is incorrect"; - break; - - case DOMException.INVALID_ACCESS_ERR : // 15 - msg = "INVALID_ACCESS_ERR: The object does not support this function"; - break; - - default : - msg = "UNKNOWN: Unknown Exception Code ("+ code +")"; - } - - return msg; - }, - toString : function(){ - return "[object DOMImplementation]"; - } -}); - - - -/** - * @method DOMImplementation._isNamespaceDeclaration - Return true, if attributeName is a namespace declaration - * @author Jon van Noort (jon@webarcana.com.au) - * @param attributeName : string - the attribute name - * @return : boolean - */ -function __isNamespaceDeclaration__(attributeName) { - // test if attributeName is 'xmlns' - return (attributeName.indexOf('xmlns') > -1); -} - -/** - * @method DOMImplementation._isIdDeclaration - Return true, if attributeName is an id declaration - * @author Jon van Noort (jon@webarcana.com.au) - * @param attributeName : string - the attribute name - * @return : boolean - */ -function __isIdDeclaration__(attributeName) { - // test if attributeName is 'id' (case insensitive) - return attributeName?(attributeName.toLowerCase() == 'id'):false; -} - -/** - * @method DOMImplementation._isValidName - Return true, - * if name contains no invalid characters - * @author Jon van Noort (jon@webarcana.com.au) - * @param name : string - the candidate name - * @return : boolean - */ -function __isValidName__(name) { - // test if name contains only valid characters - return name.match(re_validName); -} -var re_validName = /^[a-zA-Z_:][a-zA-Z0-9\.\-_:]*$/; - -/** - * @method DOMImplementation._isValidString - Return true, if string does not contain any illegal chars - * All of the characters 0 through 31 and character 127 are nonprinting control characters. - * With the exception of characters 09, 10, and 13, (Ox09, Ox0A, and Ox0D) - * Note: different from _isValidName in that ValidStrings may contain spaces - * @author Jon van Noort (jon@webarcana.com.au) - * @param name : string - the candidate string - * @return : boolean - */ -function __isValidString__(name) { - // test that string does not contains invalid characters - return (name.search(re_invalidStringChars) < 0); -} -var re_invalidStringChars = /\x01|\x02|\x03|\x04|\x05|\x06|\x07|\x08|\x0B|\x0C|\x0E|\x0F|\x10|\x11|\x12|\x13|\x14|\x15|\x16|\x17|\x18|\x19|\x1A|\x1B|\x1C|\x1D|\x1E|\x1F|\x7F/; - -/** - * @method DOMImplementation._parseNSName - parse the namespace name. - * if there is no colon, the - * @author Jon van Noort (jon@webarcana.com.au) - * @param qualifiedName : string - The qualified name - * @return : NSName - [ - .prefix : string - The prefix part of the qname - .namespaceName : string - The namespaceURI part of the qname - ] - */ -function __parseNSName__(qualifiedName) { - var resultNSName = {}; - // unless the qname has a namespaceName, the prefix is the entire String - resultNSName.prefix = qualifiedName; - resultNSName.namespaceName = ""; - // split on ':' - var delimPos = qualifiedName.indexOf(':'); - if (delimPos > -1) { - // get prefix - resultNSName.prefix = qualifiedName.substring(0, delimPos); - // get namespaceName - resultNSName.namespaceName = qualifiedName.substring(delimPos +1, qualifiedName.length); - } - return resultNSName; -} - -/** - * @method DOMImplementation._parseQName - parse the qualified name - * @author Jon van Noort (jon@webarcana.com.au) - * @param qualifiedName : string - The qualified name - * @return : QName - */ -function __parseQName__(qualifiedName) { - var resultQName = {}; - // unless the qname has a prefix, the local name is the entire String - resultQName.localName = qualifiedName; - resultQName.prefix = ""; - // split on ':' - var delimPos = qualifiedName.indexOf(':'); - if (delimPos > -1) { - // get prefix - resultQName.prefix = qualifiedName.substring(0, delimPos); - // get localName - resultQName.localName = qualifiedName.substring(delimPos +1, qualifiedName.length); - } - return resultQName; -} -/** - * @author envjs team - */ -Notation = function() { - throw new Error("Notation Not Implemented" ); -};/** - * @author thatcher - */ -Range = function(){ - -}; - -__extend__(Range.prototype, { - get startContainer(){ - - }, - get endContainer(){ - - }, - get startOffset(){ - - }, - get endOffset(){ - - }, - get collapsed(){ - - }, - get commonAncestorContainer(){ - - }, - setStart: function(refNode, offset){//throws RangeException - - }, - setEnd: function(refNode, offset){//throws RangeException - - }, - setStartBefore: function(refNode){//throws RangeException - - }, - setStartAfter: function(refNode){//throws RangeException - - }, - setEndBefore: function(refNode){//throws RangeException - - }, - setEndAfter: function(refNode){//throws RangeException - - }, - collapse: function(toStart){//throws RangeException - - }, - selectNode: function(refNode){//throws RangeException - - }, - selectNodeContents: function(refNode){//throws RangeException - - }, - compareBoundaryPoints: function(how, sourceRange){ - - }, - deleteContents: function(){ - - }, - extractContents: function(){ - - }, - cloneContents: function(){ - - }, - insertNode: function(newNode){ - - }, - surroundContents: function(newParent){ - - }, - cloneRange: function(){ - - }, - toString: function(){ - return '[object Range]'; - }, - detach: function(){ - - } -}); - - - // CompareHow -Range.START_TO_START = 0; -Range.START_TO_END = 1; -Range.END_TO_END = 2; -Range.END_TO_START = 3; - -/* - * Forward declarations - */ -var __isValidNamespace__; - -/** - * @class Document - The Document interface represents the entire HTML - * or XML document. Conceptually, it is the root of the document tree, - * and provides the primary access to the document's data. - * - * @extends Node - * @param implementation : DOMImplementation - the creator Implementation - */ -Document = function(implementation, docParentWindow) { - Node.apply(this, arguments); - - //TODO: Temporary!!! Cnage back to true!!! - this.async = true; - // The Document Type Declaration (see DocumentType) associated with this document - this.doctype = null; - // The DOMImplementation object that handles this document. - this.implementation = implementation; - - this.nodeName = "#document"; - // initially false, set to true by parser - this.parsing = false; - this.baseURI = 'about:blank'; - - this.ownerDocument = null; - - this.importing = false; -}; - -Document.prototype = new Node(); -__extend__(Document.prototype,{ - get localName(){ - return null; - }, - get textContent(){ - return null; - }, - get all(){ - return this.getElementsByTagName("*"); - }, - get documentElement(){ - var i, length = this.childNodes?this.childNodes.length:0; - for(i=0;i -1 ){ - valid = false; - } - - if ((valid) && (!isAttribute)) { - // if the namespaceURI is not null - if (!namespaceURI) { - valid = false; - } - } - - // if the qualifiedName has a prefix - if ((valid) && (qName.prefix === "")) { - valid = false; - } - } - - // if the qualifiedName has a prefix that is "xml" and the namespaceURI is - // different from "http://www.w3.org/XML/1998/namespace" [Namespaces]. - if ((valid) && (qName.prefix === "xml") && (namespaceURI !== "http://www.w3.org/XML/1998/namespace")) { - valid = false; - } - - return valid; -}; -/** - * - * This file only handles XML parser. - * It is extended by parser/domparser.js (and parser/htmlparser.js) - * - * This depends on e4x, which some engines may not have. - * - * @author thatcher - */ -DOMParser = function(principle, documentURI, baseURI) { - // TODO: why/what should these 3 args do? -}; -__extend__(DOMParser.prototype,{ - parseFromString: function(xmlstring, mimetype){ - var doc = new Document(new DOMImplementation()), - e4; - - // The following are e4x directives. - // Full spec is here: - // http://www.ecma-international.org/publications/standards/Ecma-357.htm - // - // that is pretty gross, so checkout this summary - // http://rephrase.net/days/07/06/e4x - // - // also see the Mozilla Developer Center: - // https://developer.mozilla.org/en/E4X - // - XML.ignoreComments = false; - XML.ignoreProcessingInstructions = false; - XML.ignoreWhitespace = false; - - // for some reason e4x can't handle initial xml declarations - // https://bugzilla.mozilla.org/show_bug.cgi?id=336551 - // The official workaround is the big regexp below - // but simpler one seems to be ok - // xmlstring = xmlstring.replace(/^<\?xml\s+version\s*=\s*(["'])[^\1]+\1[^?]*\?>/, ""); - // - xmlstring = xmlstring.replace(/<\?xml.*\?>/); - - e4 = new XMLList(xmlstring); - - __toDomNode__(e4, doc, doc); - - //console.log('xml \n %s', doc.documentElement.xml); - return doc; - } -}); - -var __toDomNode__ = function(e4, parent, doc){ - var xnode, - domnode, - children, - target, - value, - length, - element, - kind, - item; - //console.log('converting e4x node list \n %s', e4) - - // not using the for each(item in e4) since some engines can't - // handle the syntax (i.e. says syntax error) - // - // for each(xnode in e4) { - for (item in e4) { - // NO do not do this if (e4.hasOwnProperty(item)) { - // breaks spidermonkey - xnode = e4[item]; - - kind = xnode.nodeKind(); - //console.log('treating node kind %s', kind); - switch(kind){ - case 'element': - // add node - //console.log('creating element %s %s', xnode.localName(), xnode.namespace()); - if(xnode.namespace() && (xnode.namespace()+'') !== ''){ - //console.log('createElementNS %s %s',xnode.namespace()+'', xnode.localName() ); - domnode = doc.createElementNS(xnode.namespace()+'', xnode.localName()); - }else{ - domnode = doc.createElement(xnode.name()+''); - } - parent.appendChild(domnode); - - // add attributes - __toDomNode__(xnode.attributes(), domnode, doc); - - // add children - children = xnode.children(); - length = children.length(); - //console.log('recursing? %s', length ? 'yes' : 'no'); - if (length > 0) { - __toDomNode__(children, domnode, doc); - } - break; - case 'attribute': - // console.log('setting attribute %s %s %s', - // xnode.localName(), xnode.namespace(), xnode.valueOf()); - - // - // cross-platform alert. The original code used - // xnode.text() to get the attribute value - // This worked in Rhino, but did not in Spidermonkey - // valueOf seemed to work in both - // - if(xnode.namespace() && xnode.namespace().prefix){ - //console.log("%s", xnode.namespace().prefix); - parent.setAttributeNS(xnode.namespace()+'', - xnode.namespace().prefix+':'+xnode.localName(), - xnode.valueOf()); - }else if((xnode.name()+'').match('http://www.w3.org/2000/xmlns/::')){ - if(xnode.localName()!=='xmlns'){ - parent.setAttributeNS('http://www.w3.org/2000/xmlns/', - 'xmlns:'+xnode.localName(), - xnode.valueOf()); - } - }else{ - parent.setAttribute(xnode.localName()+'', xnode.valueOf()); - } - break; - case 'text': - //console.log('creating text node : %s', xnode); - domnode = doc.createTextNode(xnode+''); - parent.appendChild(domnode); - break; - case 'comment': - //console.log('creating comment node : %s', xnode); - value = xnode+''; - domnode = doc.createComment(value.substring(4,value.length-3)); - parent.appendChild(domnode); - break; - case 'processing-instruction': - //console.log('creating processing-instruction node : %s', xnode); - value = xnode+''; - target = value.split(' ')[0].substring(2); - value = value.split(' ').splice(1).join(' ').replace('?>',''); - //console.log('creating processing-instruction data : %s', value); - domnode = doc.createProcessingInstruction(target, value); - parent.appendChild(domnode); - break; - default: - console.log('e4x DOM ERROR'); - throw new Error("Assertion failed in xml parser"); - } - } -}; -/** - * @author envjs team - * @class XMLSerializer - */ - -XMLSerializer = function() {}; - -__extend__(XMLSerializer.prototype, { - serializeToString: function(node){ - return node.xml; - }, - toString : function(){ - return "[object XMLSerializer]"; - } -}); - -/** - * @author john resig & the envjs team - * @uri http://www.envjs.com/ - * @copyright 2008-2010 - * @license MIT - */ -//CLOSURE_END -}()); -/* - * Envjs event.1.2.13 - * Pure JavaScript Browser Environment - * By John Resig and the Envjs Team - * Copyright 2008-2010 John Resig, under the MIT License - * - * This file simply provides the global definitions we need to - * be able to correctly implement to core browser DOM Event interfaces. - */ -var Event, - MouseEvent, - UIEvent, - KeyboardEvent, - MutationEvent, - DocumentEvent, - EventTarget, - EventException, - //nonstandard but very useful for implementing mutation events - //among other things like general profiling - Aspect; -/* - * Envjs event.1.2.13 - * Pure JavaScript Browser Environment - * By John Resig and the Envjs Team - * Copyright 2008-2010 John Resig, under the MIT License - */ - -//CLOSURE_START -(function(){ - - - - - -/** - * @author john resig - */ -// Helper method for extending one object with another. -function __extend__(a,b) { - for ( var i in b ) { - var g = b.__lookupGetter__(i), s = b.__lookupSetter__(i); - if ( g || s ) { - if ( g ) { a.__defineGetter__(i, g); } - if ( s ) { a.__defineSetter__(i, s); } - } else { - a[i] = b[i]; - } - } return a; -} - -/** - * @author john resig - */ -//from jQuery -function __setArray__( target, array ) { - // Resetting the length to 0, then using the native Array push - // is a super-fast way to populate an object with array-like properties - target.length = 0; - Array.prototype.push.apply( target, array ); -} -/** - * Borrowed with love from: - * - * jQuery AOP - jQuery plugin to add features of aspect-oriented programming (AOP) to jQuery. - * http://jquery-aop.googlecode.com/ - * - * Licensed under the MIT license: - * http://www.opensource.org/licenses/mit-license.php - * - * Version: 1.1 - */ -(function() { - - var _after = 1; - var _before = 2; - var _around = 3; - var _intro = 4; - var _regexEnabled = true; - - /** - * Private weaving function. - */ - var weaveOne = function(source, method, advice) { - - var old = source[method]; - - var aspect; - if (advice.type == _after) - aspect = function() { - var returnValue = old.apply(this, arguments); - return advice.value.apply(this, [returnValue, method]); - }; - else if (advice.type == _before) - aspect = function() { - advice.value.apply(this, [arguments, method]); - return old.apply(this, arguments); - }; - else if (advice.type == _intro) - aspect = function() { - return advice.value.apply(this, arguments); - }; - else if (advice.type == _around) { - aspect = function() { - var invocation = { object: this, args: arguments }; - return advice.value.apply(invocation.object, [{ arguments: invocation.args, method: method, proceed : - function() { - return old.apply(invocation.object, invocation.args); - } - }] ); - }; - } - - aspect.unweave = function() { - source[method] = old; - pointcut = source = aspect = old = null; - }; - - source[method] = aspect; - - return aspect; - - }; - - - /** - * Private weaver and pointcut parser. - */ - var weave = function(pointcut, advice) - { - - var source = (typeof(pointcut.target.prototype) != 'undefined') ? pointcut.target.prototype : pointcut.target; - var advices = []; - - // If it's not an introduction and no method was found, try with regex... - if (advice.type != _intro && typeof(source[pointcut.method]) == 'undefined') - { - - for (var method in source) - { - if (source[method] != null && source[method] instanceof Function && method.match(pointcut.method)) - { - advices[advices.length] = weaveOne(source, method, advice); - } - } - - if (advices.length == 0) - throw 'No method: ' + pointcut.method; - - } - else - { - // Return as an array of one element - advices[0] = weaveOne(source, pointcut.method, advice); - } - - return _regexEnabled ? advices : advices[0]; - - }; - - Aspect = - { - /** - * Creates an advice after the defined point-cut. The advice will be executed after the point-cut method - * has completed execution successfully, and will receive one parameter with the result of the execution. - * This function returns an array of weaved aspects (Function). - * - * @example jQuery.aop.after( {target: window, method: 'MyGlobalMethod'}, function(result) { alert('Returned: ' + result); } ); - * @result Array - * - * @example jQuery.aop.after( {target: String, method: 'indexOf'}, function(index) { alert('Result found at: ' + index + ' on:' + this); } ); - * @result Array - * - * @name after - * @param Map pointcut Definition of the point-cut to apply the advice. A point-cut is the definition of the object/s and method/s to be weaved. - * @option Object target Target object to be weaved. - * @option String method Name of the function to be weaved. Regex are supported, but not on built-in objects. - * @param Function advice Function containing the code that will get called after the execution of the point-cut. It receives one parameter - * with the result of the point-cut's execution. - * - * @type Array - * @cat Plugins/General - */ - after : function(pointcut, advice) - { - return weave( pointcut, { type: _after, value: advice } ); - }, - - /** - * Creates an advice before the defined point-cut. The advice will be executed before the point-cut method - * but cannot modify the behavior of the method, or prevent its execution. - * This function returns an array of weaved aspects (Function). - * - * @example jQuery.aop.before( {target: window, method: 'MyGlobalMethod'}, function() { alert('About to execute MyGlobalMethod'); } ); - * @result Array - * - * @example jQuery.aop.before( {target: String, method: 'indexOf'}, function(index) { alert('About to execute String.indexOf on: ' + this); } ); - * @result Array - * - * @name before - * @param Map pointcut Definition of the point-cut to apply the advice. A point-cut is the definition of the object/s and method/s to be weaved. - * @option Object target Target object to be weaved. - * @option String method Name of the function to be weaved. Regex are supported, but not on built-in objects. - * @param Function advice Function containing the code that will get called before the execution of the point-cut. - * - * @type Array - * @cat Plugins/General - */ - before : function(pointcut, advice) - { - return weave( pointcut, { type: _before, value: advice } ); - }, - - - /** - * Creates an advice 'around' the defined point-cut. This type of advice can control the point-cut method execution by calling - * the functions '.proceed()' on the 'invocation' object, and also, can modify the arguments collection before sending them to the function call. - * This function returns an array of weaved aspects (Function). - * - * @example jQuery.aop.around( {target: window, method: 'MyGlobalMethod'}, function(invocation) { - * alert('# of Arguments: ' + invocation.arguments.length); - * return invocation.proceed(); - * } ); - * @result Array - * - * @example jQuery.aop.around( {target: String, method: 'indexOf'}, function(invocation) { - * alert('Searching: ' + invocation.arguments[0] + ' on: ' + this); - * return invocation.proceed(); - * } ); - * @result Array - * - * @example jQuery.aop.around( {target: window, method: /Get(\d+)/}, function(invocation) { - * alert('Executing ' + invocation.method); - * return invocation.proceed(); - * } ); - * @desc Matches all global methods starting with 'Get' and followed by a number. - * @result Array - * - * - * @name around - * @param Map pointcut Definition of the point-cut to apply the advice. A point-cut is the definition of the object/s and method/s to be weaved. - * @option Object target Target object to be weaved. - * @option String method Name of the function to be weaved. Regex are supported, but not on built-in objects. - * @param Function advice Function containing the code that will get called around the execution of the point-cut. This advice will be called with one - * argument containing one function '.proceed()', the collection of arguments '.arguments', and the matched method name '.method'. - * - * @type Array - * @cat Plugins/General - */ - around : function(pointcut, advice) - { - return weave( pointcut, { type: _around, value: advice } ); - }, - - /** - * Creates an introduction on the defined point-cut. This type of advice replaces any existing methods with the same - * name. To restore them, just unweave it. - * This function returns an array with only one weaved aspect (Function). - * - * @example jQuery.aop.introduction( {target: window, method: 'MyGlobalMethod'}, function(result) { alert('Returned: ' + result); } ); - * @result Array - * - * @example jQuery.aop.introduction( {target: String, method: 'log'}, function() { alert('Console: ' + this); } ); - * @result Array - * - * @name introduction - * @param Map pointcut Definition of the point-cut to apply the advice. A point-cut is the definition of the object/s and method/s to be weaved. - * @option Object target Target object to be weaved. - * @option String method Name of the function to be weaved. - * @param Function advice Function containing the code that will be executed on the point-cut. - * - * @type Array - * @cat Plugins/General - */ - introduction : function(pointcut, advice) - { - return weave( pointcut, { type: _intro, value: advice } ); - }, - - /** - * Configures global options. - * - * @name setup - * @param Map settings Configuration options. - * @option Boolean regexMatch Enables/disables regex matching of method names. - * - * @example jQuery.aop.setup( { regexMatch: false } ); - * @desc Disable regex matching. - * - * @type Void - * @cat Plugins/General - */ - setup: function(settings) - { - _regexEnabled = settings.regexMatch; - } - }; - -})(); - - - - -/** - * @name EventTarget - * @w3c:domlevel 2 - * @uri -//TODO: paste dom event level 2 w3c spc uri here - */ -EventTarget = function(){}; -EventTarget.prototype.addEventListener = function(type, fn, phase){ - __addEventListener__(this, type, fn, phase); -}; -EventTarget.prototype.removeEventListener = function(type, fn){ - __removeEventListener__(this, type, fn); -}; -EventTarget.prototype.dispatchEvent = function(event, bubbles){ - __dispatchEvent__(this, event, bubbles); -}; - -__extend__(Node.prototype, EventTarget.prototype); - - -var $events = [{}]; - -function __addEventListener__(target, type, fn, phase){ - phase = !!phase?"CAPTURING":"BUBBLING"; - if ( !target.uuid ) { - //console.log('event uuid %s %s', target, target.uuid); - target.uuid = $events.length+''; - } - if ( !$events[target.uuid] ) { - //console.log('creating listener for target: %s %s', target, target.uuid); - $events[target.uuid] = {}; - } - if ( !$events[target.uuid][type] ){ - //console.log('creating listener for type: %s %s %s', target, target.uuid, type); - $events[target.uuid][type] = { - CAPTURING:[], - BUBBLING:[] - }; - } - if ( $events[target.uuid][type][phase].indexOf( fn ) < 0 ){ - //console.log('adding event listener %s %s %s %s %s %s', target, target.uuid, type, phase, - // $events[target.uuid][type][phase].length, $events[target.uuid][type][phase].indexOf( fn )); - //console.log('creating listener for function: %s %s %s', target, target.uuid, phase); - $events[target.uuid][type][phase].push( fn ); - //console.log('adding event listener %s %s %s %s %s %s', target, target.uuid, type, phase, - // $events[target.uuid][type][phase].length, $events[target.uuid][type][phase].indexOf( fn )); - } - //console.log('registered event listeners %s', $events.length); -} - -function __removeEventListener__(target, type, fn, phase){ - - phase = !!phase?"CAPTURING":"BUBBLING"; - if ( !target.uuid ) { - return; - } - if ( !$events[target.uuid] ) { - return; - } - if(type == '*'){ - //used to clean all event listeners for a given node - //console.log('cleaning all event listeners for node %s %s',target, target.uuid); - delete $events[target.uuid]; - return; - }else if ( !$events[target.uuid][type] ){ - return; - } - $events[target.uuid][type][phase] = - $events[target.uuid][type][phase].filter(function(f){ - //console.log('removing event listener %s %s %s %s', target, type, phase, fn); - return f != fn; - }); -} - -var __eventuuid__ = 0; -function __dispatchEvent__(target, event, bubbles){ - - if (!event.uuid) { - event.uuid = __eventuuid__++; - } - //the window scope defines the $event object, for IE(^^^) compatibility; - //$event = event; - //console.log('dispatching event %s', event.uuid); - if (bubbles === undefined || bubbles === null) { - bubbles = true; - } - - if (!event.target) { - event.target = target; - } - - //console.log('dispatching? %s %s %s', target, event.type, bubbles); - if ( event.type && (target.nodeType || target === window )) { - - //console.log('dispatching event %s %s %s', target, event.type, bubbles); - __captureEvent__(target, event); - - event.eventPhase = Event.AT_TARGET; - if ( target.uuid && $events[target.uuid] && $events[target.uuid][event.type] ) { - event.currentTarget = target; - //console.log('dispatching %s %s %s %s', target, event.type, - // $events[target.uuid][event.type]['CAPTURING'].length); - $events[target.uuid][event.type].CAPTURING.forEach(function(fn){ - //console.log('AT_TARGET (CAPTURING) event %s', fn); - var returnValue = fn( event ); - //console.log('AT_TARGET (CAPTURING) return value %s', returnValue); - if(returnValue === false){ - event.stopPropagation(); - } - }); - //console.log('dispatching %s %s %s %s', target, event.type, - // $events[target.uuid][event.type]['BUBBLING'].length); - $events[target.uuid][event.type].BUBBLING.forEach(function(fn){ - //console.log('AT_TARGET (BUBBLING) event %s', fn); - var returnValue = fn( event ); - //console.log('AT_TARGET (BUBBLING) return value %s', returnValue); - if(returnValue === false){ - event.stopPropagation(); - } - }); - } - if (target["on" + event.type]) { - target["on" + event.type](event); - } - if (bubbles && !event.cancelled){ - __bubbleEvent__(target, event); - } - if(!event._preventDefault){ - //At this point I'm guessing that just HTMLEvents are concerned - //with default behavior being executed in a browser but I could be - //wrong as usual. The goal is much more to filter at this point - //what events have no need to be handled - //console.log('triggering default behavior for %s', event.type); - if(event.type in Envjs.defaultEventBehaviors){ - Envjs.defaultEventBehaviors[event.type](event); - } - } - //console.log('deleting event %s', event.uuid); - event.target = null; - event = null; - }else{ - throw new EventException(EventException.UNSPECIFIED_EVENT_TYPE_ERR); - } -} - -function __captureEvent__(target, event){ - var ancestorStack = [], - parent = target.parentNode; - - event.eventPhase = Event.CAPTURING_PHASE; - while(parent){ - if(parent.uuid && $events[parent.uuid] && $events[parent.uuid][event.type]){ - ancestorStack.push(parent); - } - parent = parent.parentNode; - } - while(ancestorStack.length && !event.cancelled){ - event.currentTarget = ancestorStack.pop(); - if($events[event.currentTarget.uuid] && $events[event.currentTarget.uuid][event.type]){ - $events[event.currentTarget.uuid][event.type].CAPTURING.forEach(function(fn){ - var returnValue = fn( event ); - if(returnValue === false){ - event.stopPropagation(); - } - }); - } - } -} - -function __bubbleEvent__(target, event){ - var parent = target.parentNode; - event.eventPhase = Event.BUBBLING_PHASE; - while(parent){ - if(parent.uuid && $events[parent.uuid] && $events[parent.uuid][event.type] ){ - event.currentTarget = parent; - $events[event.currentTarget.uuid][event.type].BUBBLING.forEach(function(fn){ - var returnValue = fn( event ); - if(returnValue === false){ - event.stopPropagation(); - } - }); - } - parent = parent.parentNode; - } -} - -/** - * @class Event - */ -Event = function(options){ - // event state is kept read-only by forcing - // a new object for each event. This may not - // be appropriate in the long run and we'll - // have to decide if we simply dont adhere to - // the read-only restriction of the specification - this._bubbles = true; - this._cancelable = true; - this._cancelled = false; - this._currentTarget = null; - this._target = null; - this._eventPhase = Event.AT_TARGET; - this._timeStamp = new Date().getTime(); - this._preventDefault = false; - this._stopPropogation = false; -}; - -__extend__(Event.prototype,{ - get bubbles(){return this._bubbles;}, - get cancelable(){return this._cancelable;}, - get currentTarget(){return this._currentTarget;}, - set currentTarget(currentTarget){ this._currentTarget = currentTarget; }, - get eventPhase(){return this._eventPhase;}, - set eventPhase(eventPhase){this._eventPhase = eventPhase;}, - get target(){return this._target;}, - set target(target){ this._target = target;}, - get timeStamp(){return this._timeStamp;}, - get type(){return this._type;}, - initEvent: function(type, bubbles, cancelable){ - this._type=type?type:''; - this._bubbles=!!bubbles; - this._cancelable=!!cancelable; - }, - preventDefault: function(){ - this._preventDefault = true; - }, - stopPropagation: function(){ - if(this._cancelable){ - this._cancelled = true; - this._bubbles = false; - } - }, - get cancelled(){ - return this._cancelled; - }, - toString: function(){ - return '[object Event]'; - } -}); - -__extend__(Event,{ - CAPTURING_PHASE : 1, - AT_TARGET : 2, - BUBBLING_PHASE : 3 -}); - - - -/** - * @name UIEvent - * @param {Object} options - */ -UIEvent = function(options) { - this._view = null; - this._detail = 0; -}; - -UIEvent.prototype = new Event(); -__extend__(UIEvent.prototype,{ - get view(){ - return this._view; - }, - get detail(){ - return this._detail; - }, - initUIEvent: function(type, bubbles, cancelable, windowObject, detail){ - this.initEvent(type, bubbles, cancelable); - this._detail = 0; - this._view = windowObject; - } -}); - -var $onblur, - $onfocus, - $onresize; - - -/** - * @name MouseEvent - * @w3c:domlevel 2 - * @uri http://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html - */ -MouseEvent = function(options) { - this._screenX= 0; - this._screenY= 0; - this._clientX= 0; - this._clientY= 0; - this._ctrlKey= false; - this._metaKey= false; - this._altKey= false; - this._button= null; - this._relatedTarget= null; -}; -MouseEvent.prototype = new UIEvent(); -__extend__(MouseEvent.prototype,{ - get screenX(){ - return this._screenX; - }, - get screenY(){ - return this._screenY; - }, - get clientX(){ - return this._clientX; - }, - get clientY(){ - return this._clientY; - }, - get ctrlKey(){ - return this._ctrlKey; - }, - get altKey(){ - return this._altKey; - }, - get shiftKey(){ - return this._shiftKey; - }, - get metaKey(){ - return this._metaKey; - }, - get button(){ - return this._button; - }, - get relatedTarget(){ - return this._relatedTarget; - }, - initMouseEvent: function(type, bubbles, cancelable, windowObject, detail, - screenX, screenY, clientX, clientY, ctrlKey, altKey, shiftKey, - metaKey, button, relatedTarget){ - this.initUIEvent(type, bubbles, cancelable, windowObject, detail); - this._screenX = screenX; - this._screenY = screenY; - this._clientX = clientX; - this._clientY = clientY; - this._ctrlKey = ctrlKey; - this._altKey = altKey; - this._shiftKey = shiftKey; - this._metaKey = metaKey; - this._button = button; - this._relatedTarget = relatedTarget; - } -}); - -/** - * Interface KeyboardEvent (introduced in DOM Level 3) - */ -KeyboardEvent = function(options) { - this._keyIdentifier = 0; - this._keyLocation = 0; - this._ctrlKey = false; - this._metaKey = false; - this._altKey = false; - this._metaKey = false; -}; -KeyboardEvent.prototype = new UIEvent(); - -__extend__(KeyboardEvent.prototype,{ - - get ctrlKey(){ - return this._ctrlKey; - }, - get altKey(){ - return this._altKey; - }, - get shiftKey(){ - return this._shiftKey; - }, - get metaKey(){ - return this._metaKey; - }, - get button(){ - return this._button; - }, - get relatedTarget(){ - return this._relatedTarget; - }, - getModifiersState: function(keyIdentifier){ - - }, - initMouseEvent: function(type, bubbles, cancelable, windowObject, - keyIdentifier, keyLocation, modifiersList, repeat){ - this.initUIEvent(type, bubbles, cancelable, windowObject, 0); - this._keyIdentifier = keyIdentifier; - this._keyLocation = keyLocation; - this._modifiersList = modifiersList; - this._repeat = repeat; - } -}); - -KeyboardEvent.DOM_KEY_LOCATION_STANDARD = 0; -KeyboardEvent.DOM_KEY_LOCATION_LEFT = 1; -KeyboardEvent.DOM_KEY_LOCATION_RIGHT = 2; -KeyboardEvent.DOM_KEY_LOCATION_NUMPAD = 3; -KeyboardEvent.DOM_KEY_LOCATION_MOBILE = 4; -KeyboardEvent.DOM_KEY_LOCATION_JOYSTICK = 5; - - - -//We dont fire mutation events until someone has registered for them -var __supportedMutations__ = /DOMSubtreeModified|DOMNodeInserted|DOMNodeRemoved|DOMAttrModified|DOMCharacterDataModified/; - -var __fireMutationEvents__ = Aspect.before({ - target: EventTarget, - method: 'addEventListener' -}, function(target, type){ - if(type && type.match(__supportedMutations__)){ - //unweaving removes the __addEventListener__ aspect - __fireMutationEvents__.unweave(); - // These two methods are enough to cover all dom 2 manipulations - Aspect.around({ - target: Node, - method:"removeChild" - }, function(invocation){ - var event, - node = invocation.arguments[0]; - event = node.ownerDocument.createEvent('MutationEvents'); - event.initEvent('DOMNodeRemoved', true, false, node.parentNode, null, null, null, null); - node.dispatchEvent(event, false); - return invocation.proceed(); - - }); - Aspect.around({ - target: Node, - method:"appendChild" - }, function(invocation) { - var event, - node = invocation.proceed(); - event = node.ownerDocument.createEvent('MutationEvents'); - event.initEvent('DOMNodeInserted', true, false, node.parentNode, null, null, null, null); - node.dispatchEvent(event, false); - return node; - }); - } -}); - -/** - * @name MutationEvent - * @param {Object} options - */ -MutationEvent = function(options) { - this._cancelable = false; - this._timeStamp = 0; -}; - -MutationEvent.prototype = new Event(); -__extend__(MutationEvent.prototype,{ - get relatedNode(){ - return this._relatedNode; - }, - get prevValue(){ - return this._prevValue; - }, - get newValue(){ - return this._newValue; - }, - get attrName(){ - return this._attrName; - }, - get attrChange(){ - return this._attrChange; - }, - initMutationEvent: function( type, bubbles, cancelable, - relatedNode, prevValue, newValue, attrName, attrChange ){ - this._relatedNode = relatedNode; - this._prevValue = prevValue; - this._newValue = newValue; - this._attrName = attrName; - this._attrChange = attrChange; - switch(type){ - case "DOMSubtreeModified": - this.initEvent(type, true, false); - break; - case "DOMNodeInserted": - this.initEvent(type, true, false); - break; - case "DOMNodeRemoved": - this.initEvent(type, true, false); - break; - case "DOMNodeRemovedFromDocument": - this.initEvent(type, false, false); - break; - case "DOMNodeInsertedIntoDocument": - this.initEvent(type, false, false); - break; - case "DOMAttrModified": - this.initEvent(type, true, false); - break; - case "DOMCharacterDataModified": - this.initEvent(type, true, false); - break; - default: - this.initEvent(type, bubbles, cancelable); - } - } -}); - -// constants -MutationEvent.ADDITION = 0; -MutationEvent.MODIFICATION = 1; -MutationEvent.REMOVAL = 2; - - -/** - * @name EventException - */ -EventException = function(code) { - this.code = code; -}; -EventException.UNSPECIFIED_EVENT_TYPE_ERR = 0; -/** - * - * DOM Level 2: http://www.w3.org/TR/DOM-Level-2-Events/events.html - * DOM Level 3: http://www.w3.org/TR/DOM-Level-3-Events/ - * - * interface DocumentEvent { - * Event createEvent (in DOMString eventType) - * raises (DOMException); - * }; - * - * Firefox (3.6) exposes DocumentEvent - * Safari (4) does NOT. - */ - -/** - * TODO: Not sure we need a full prototype. We not just an regular object? - */ -DocumentEvent = function(){}; -DocumentEvent.prototype.__EventMap__ = { - // Safari4: singular and plural forms accepted - // Firefox3.6: singular and plural forms accepted - 'Event' : Event, - 'Events' : Event, - 'UIEvent' : UIEvent, - 'UIEvents' : UIEvent, - 'MouseEvent' : MouseEvent, - 'MouseEvents' : MouseEvent, - 'MutationEvent' : MutationEvent, - 'MutationEvents' : MutationEvent, - - // Safari4: accepts HTMLEvents, but not HTMLEvent - // Firefox3.6: accepts HTMLEvents, but not HTMLEvent - 'HTMLEvent' : Event, - 'HTMLEvents' : Event, - - // Safari4: both not accepted - // Firefox3.6, only KeyEvents is accepted - 'KeyEvent' : KeyboardEvent, - 'KeyEvents' : KeyboardEvent, - - // Safari4: both accepted - // Firefox3.6: none accepted - 'KeyboardEvent' : KeyboardEvent, - 'KeyboardEvents' : KeyboardEvent -}; - -DocumentEvent.prototype.createEvent = function(eventType) { - var Clazz = this.__EventMap__[eventType]; - if (Clazz) { - return new Clazz(); - } - throw(new DOMException(DOMException.NOT_SUPPORTED_ERR)); -}; - -__extend__(Document.prototype, DocumentEvent.prototype); - -/** - * @author john resig & the envjs team - * @uri http://www.envjs.com/ - * @copyright 2008-2010 - * @license MIT - */ -//CLOSURE_END -}()); - -/* - * Envjs timer.1.2.13 - * Pure JavaScript Browser Environment - * By John Resig and the Envjs Team - * Copyright 2008-2010 John Resig, under the MIT License - * - * Parts of the implementation were originally written by:\ - * Steven Parkes - * - * requires Envjs.wait, Envjs.sleep, Envjs.WAIT_INTERVAL - */ -var setTimeout, - clearTimeout, - setInterval, - clearInterval; - -/* - * Envjs timer.1.2.13 - * Pure JavaScript Browser Environment - * By John Resig and the Envjs Team - * Copyright 2008-2010 John Resig, under the MIT License - */ - -//CLOSURE_START -(function(){ - - - - -/* -* timer.js -* implementation provided by Steven Parkes -*/ - -//private -var $timers = [], - EVENT_LOOP_RUNNING = false; - -$timers.lock = function(fn){ - Envjs.sync(fn)(); -}; - -//private internal class -var Timer = function(fn, interval){ - this.fn = fn; - this.interval = interval; - this.at = Date.now() + interval; - // allows for calling wait() from callbacks - this.running = false; -}; - -Timer.prototype.start = function(){}; -Timer.prototype.stop = function(){}; - -//static -Timer.normalize = function(time) { - time = time*1; - if ( isNaN(time) || time < 0 ) { - time = 0; - } - - if ( EVENT_LOOP_RUNNING && time < Timer.MIN_TIME ) { - time = Timer.MIN_TIME; - } - return time; -}; -// html5 says this should be at least 4, but the parser is using -// a setTimeout for the SAX stuff which messes up the world -Timer.MIN_TIME = /* 4 */ 0; - -/** - * @function setTimeout - * @param {Object} fn - * @param {Object} time - */ -setTimeout = function(fn, time){ - var num; - time = Timer.normalize(time); - $timers.lock(function(){ - num = $timers.length+1; - var tfn; - if (typeof fn == 'string') { - tfn = function() { - try { - // eval in global scope - eval(fn, null); - } catch (e) { - console.log('timer error %s %s', fn, e); - } finally { - clearInterval(num); - } - }; - } else { - tfn = function() { - try { - fn(); - } catch (e) { - console.log('timer error %s %s', fn, e); - } finally { - clearInterval(num); - } - }; - } - //console.log("Creating timer number %s", num); - $timers[num] = new Timer(tfn, time); - $timers[num].start(); - }); - return num; -}; - -/** - * @function setInterval - * @param {Object} fn - * @param {Object} time - */ -setInterval = function(fn, time){ - //console.log('setting interval %s %s', time, fn.toString().substring(0,64)); - time = Timer.normalize(time); - if ( time < 10 ) { - time = 10; - } - if (typeof fn == 'string') { - var fnstr = fn; - fn = function() { - eval(fnstr); - }; - } - var num; - $timers.lock(function(){ - num = $timers.length+1; - //Envjs.debug("Creating timer number "+num); - $timers[num] = new Timer(fn, time); - $timers[num].start(); - }); - return num; -}; - -/** - * clearInterval - * @param {Object} num - */ -clearInterval = clearTimeout = function(num){ - //console.log("clearing interval "+num); - $timers.lock(function(){ - if ( $timers[num] ) { - $timers[num].stop(); - delete $timers[num]; - } - }); -}; - -// wait === null/undefined: execute any timers as they fire, -// waiting until there are none left -// wait(n) (n > 0): execute any timers as they fire until there -// are none left waiting at least n ms but no more, even if there -// are future events/current threads -// wait(0): execute any immediately runnable timers and return -// wait(-n): keep sleeping until the next event is more than n ms -// in the future -// -// TODO: make a priority queue ... - -Envjs.wait = function(wait) { - //console.log('wait %s', wait); - var delta_wait, - start = Date.now(), - was_running = EVENT_LOOP_RUNNING; - - if (wait < 0) { - delta_wait = -wait; - wait = 0; - } - EVENT_LOOP_RUNNING = true; - if (wait !== 0 && wait !== null && wait !== undefined){ - wait += Date.now(); - } - - var earliest, - timer, - sleep, - index, - goal, - now, - nextfn; - - for (;;) { - //console.log('timer loop'); - earliest = sleep = goal = now = nextfn = null; - $timers.lock(function(){ - for(index in $timers){ - if( isNaN(index*0) ) { - continue; - } - timer = $timers[index]; - // determine timer with smallest run-at time that is - // not already running - if( !timer.running && ( !earliest || timer.at < earliest.at) ) { - earliest = timer; - } - } - }); - //next sleep time - sleep = earliest && earliest.at - Date.now(); - if ( earliest && sleep <= 0 ) { - nextfn = earliest.fn; - try { - //console.log('running stack %s', nextfn.toString().substring(0,64)); - earliest.running = true; - nextfn(); - } catch (e) { - console.log('timer error %s %s', nextfn, e); - } finally { - earliest.running = false; - } - goal = earliest.at + earliest.interval; - now = Date.now(); - if ( goal < now ) { - earliest.at = now; - } else { - earliest.at = goal; - } - continue; - } - - // bunch of subtle cases here ... - if ( !earliest ) { - // no events in the queue (but maybe XHR will bring in events, so ... - if ( !wait || wait < Date.now() ) { - // Loop ends if there are no events and a wait hasn't been - // requested or has expired - break; - } - // no events, but a wait requested: fall through to sleep - } else { - // there are events in the queue, but they aren't firable now - /*if ( delta_wait && sleep <= delta_wait ) { - //TODO: why waste a check on a tight - // loop if it just falls through? - // if they will happen within the next delta, fall through to sleep - } else */if ( wait === 0 || ( wait > 0 && wait < Date.now () ) ) { - // loop ends even if there are events but the user - // specifcally asked not to wait too long - break; - } - // there are events and the user wants to wait: fall through to sleep - } - - // Related to ajax threads ... hopefully can go away .. - var interval = Envjs.WAIT_INTERVAL || 100; - if ( !sleep || sleep > interval ) { - sleep = interval; - } - //console.log('sleeping %s', sleep); - Envjs.sleep(sleep); - - } - EVENT_LOOP_RUNNING = was_running; -}; - - -/** - * @author john resig & the envjs team - * @uri http://www.envjs.com/ - * @copyright 2008-2010 - * @license MIT - */ -//CLOSURE_END -}()); -/* - * Pure JavaScript Browser Environment - * By John Resig and the Envjs Team - * Copyright 2008-2010 John Resig, under the MIT License - * - * This file simply provides the global definitions we need to - * be able to correctly implement to core browser DOM HTML interfaces. - */ -var HTMLDocument, - HTMLElement, - HTMLCollection, - HTMLAnchorElement, - HTMLAreaElement, - HTMLBaseElement, - HTMLQuoteElement, - HTMLBodyElement, - HTMLBRElement, - HTMLButtonElement, - HTMLCanvasElement, - HTMLTableColElement, - HTMLModElement, - HTMLDivElement, - HTMLDListElement, - HTMLFieldSetElement, - HTMLFormElement, - HTMLFrameElement, - HTMLFrameSetElement, - HTMLHeadElement, - HTMLHeadingElement, - HTMLHRElement, - HTMLHtmlElement, - HTMLIFrameElement, - HTMLImageElement, - HTMLInputElement, - HTMLLabelElement, - HTMLLegendElement, - HTMLLIElement, - HTMLLinkElement, - HTMLMapElement, - HTMLMetaElement, - HTMLObjectElement, - HTMLOListElement, - HTMLOptGroupElement, - HTMLOptionElement, - HTMLParagraphElement, - HTMLParamElement, - HTMLPreElement, - HTMLScriptElement, - HTMLSelectElement, - HTMLSpanElement, - HTMLStyleElement, - HTMLTableElement, - HTMLTableSectionElement, - HTMLTableCellElement, - HTMLTableDataCellElement, - HTMLTableHeaderCellElement, - HTMLTableRowElement, - HTMLTextAreaElement, - HTMLTitleElement, - HTMLUListElement, - HTMLUnknownElement, - Image, - Option, - __loadImage__, - __loadLink__; - -/* - * Envjs html.1.2.13 - * Pure JavaScript Browser Environment - * By John Resig and the Envjs Team - * Copyright 2008-2010 John Resig, under the MIT License - */ - -//CLOSURE_START -(function(){ - - - - - -/** - * @author ariel flesler - * http://flesler.blogspot.com/2008/11/fast-trim-function-for-javascript.html - * @param {Object} str - */ -function __trim__( str ){ - return (str || "").replace( /^\s+|\s+$/g, "" ); -} - - -/** - * @author john resig - */ -// Helper method for extending one object with another. -function __extend__(a,b) { - for ( var i in b ) { - var g = b.__lookupGetter__(i), s = b.__lookupSetter__(i); - if ( g || s ) { - if ( g ) { a.__defineGetter__(i, g); } - if ( s ) { a.__defineSetter__(i, s); } - } else { - a[i] = b[i]; - } - } return a; -} - -/** - * @author john resig - */ -//from jQuery -function __setArray__( target, array ) { - // Resetting the length to 0, then using the native Array push - // is a super-fast way to populate an object with array-like properties - target.length = 0; - Array.prototype.push.apply( target, array ); -} - -/** - * @class HTMLDocument - * The Document interface represents the entire HTML or XML document. - * Conceptually, it is the root of the document tree, and provides - * the primary access to the document's data. - * - * @extends Document - */ -HTMLDocument = function(implementation, ownerWindow, referrer) { - Document.apply(this, arguments); - this.referrer = referrer || ''; - this.baseURI = "about:blank"; - this.ownerWindow = ownerWindow; -}; - -HTMLDocument.prototype = new Document(); - -__extend__(HTMLDocument.prototype, { - createElement: function(tagName){ - var node; - tagName = tagName.toUpperCase(); - // create Element specifying 'this' as ownerDocument - // This is an html document so we need to use explicit interfaces per the - //TODO: would be much faster as a big switch - switch(tagName){ - case "A": - node = new HTMLAnchorElement(this);break; - case "AREA": - node = new HTMLAreaElement(this);break; - case "BASE": - node = new HTMLBaseElement(this);break; - case "BLOCKQUOTE": - node = new HTMLQuoteElement(this);break; - case "CANVAS": - node = new HTMLCanvasElement(this);break; - case "Q": - node = new HTMLQuoteElement(this);break; - case "BODY": - node = new HTMLBodyElement(this);break; - case "BR": - node = new HTMLBRElement(this);break; - case "BUTTON": - node = new HTMLButtonElement(this);break; - case "CAPTION": - node = new HTMLElement(this);break; - case "COL": - node = new HTMLTableColElement(this);break; - case "COLGROUP": - node = new HTMLTableColElement(this);break; - case "DEL": - node = new HTMLModElement(this);break; - case "INS": - node = new HTMLModElement(this);break; - case "DIV": - node = new HTMLDivElement(this);break; - case "DL": - node = new HTMLDListElement(this);break; - case "DT": - node = new HTMLElement(this); break; - case "FIELDSET": - node = new HTMLFieldSetElement(this);break; - case "FORM": - node = new HTMLFormElement(this);break; - case "FRAME": - node = new HTMLFrameElement(this);break; - case "H1": - node = new HTMLHeadingElement(this);break; - case "H2": - node = new HTMLHeadingElement(this);break; - case "H3": - node = new HTMLHeadingElement(this);break; - case "H4": - node = new HTMLHeadingElement(this);break; - case "H5": - node = new HTMLHeadingElement(this);break; - case "H6": - node = new HTMLHeadingElement(this);break; - case "HEAD": - node = new HTMLHeadElement(this);break; - case "HR": - node = new HTMLHRElement(this);break; - case "HTML": - node = new HTMLHtmlElement(this);break; - case "IFRAME": - node = new HTMLIFrameElement(this);break; - case "IMG": - node = new HTMLImageElement(this);break; - case "INPUT": - node = new HTMLInputElement(this);break; - case "LABEL": - node = new HTMLLabelElement(this);break; - case "LEGEND": - node = new HTMLLegendElement(this);break; - case "LI": - node = new HTMLLIElement(this);break; - case "LINK": - node = new HTMLLinkElement(this);break; - case "MAP": - node = new HTMLMapElement(this);break; - case "META": - node = new HTMLMetaElement(this);break; - case "NOSCRIPT": - node = new HTMLElement(this);break; - case "OBJECT": - node = new HTMLObjectElement(this);break; - case "OPTGROUP": - node = new HTMLOptGroupElement(this);break; - case "OL": - node = new HTMLOListElement(this); break; - case "OPTION": - node = new HTMLOptionElement(this);break; - case "P": - node = new HTMLParagraphElement(this);break; - case "PARAM": - node = new HTMLParamElement(this);break; - case "PRE": - node = new HTMLPreElement(this);break; - case "SCRIPT": - node = new HTMLScriptElement(this);break; - case "SELECT": - node = new HTMLSelectElement(this);break; - case "SMALL": - node = new HTMLElement(this);break; - case "SPAN": - node = new HTMLSpanElement(this);break; - case "STRONG": - node = new HTMLElement(this);break; - case "STYLE": - node = new HTMLStyleElement(this);break; - case "TABLE": - node = new HTMLTableElement(this);break; - case "TBODY": - node = new HTMLTableSectionElement(this);break; - case "TFOOT": - node = new HTMLTableSectionElement(this);break; - case "THEAD": - node = new HTMLTableSectionElement(this);break; - case "TD": - node = new HTMLTableDataCellElement(this);break; - case "TH": - node = new HTMLTableHeaderCellElement(this);break; - case "TEXTAREA": - node = new HTMLTextAreaElement(this);break; - case "TITLE": - node = new HTMLTitleElement(this);break; - case "TR": - node = new HTMLTableRowElement(this);break; - case "UL": - node = new HTMLUListElement(this);break; - default: - node = new HTMLUnknownElement(this); - } - // assign values to properties (and aliases) - node.nodeName = tagName; - return node; - }, - createElementNS : function (uri, local) { - //print('createElementNS :'+uri+" "+local); - if(!uri){ - return this.createElement(local); - }else if ("http://www.w3.org/1999/xhtml" == uri) { - return this.createElement(local); - } else if ("http://www.w3.org/1998/Math/MathML" == uri) { - return this.createElement(local); - } else { - return Document.prototype.createElementNS.apply(this,[uri, local]); - } - }, - get anchors(){ - return new HTMLCollection(this.getElementsByTagName('a')); - }, - get applets(){ - return new HTMLCollection(this.getElementsByTagName('applet')); - }, - get documentElement(){ - var html = Document.prototype.__lookupGetter__('documentElement').apply(this,[]); - if( html === null){ - html = this.createElement('html'); - this.appendChild(html); - html.appendChild(this.createElement('head')); - html.appendChild(this.createElement('body')); - } - return html; - }, - //document.head is non-standard - get head(){ - //console.log('get head'); - if (!this.documentElement) { - this.appendChild(this.createElement('html')); - } - var element = this.documentElement, - length = element.childNodes.length, - i; - //check for the presence of the head element in this html doc - for(i=0;i1?matches[1]:""; - }, - set domain(value){ - var i, - domainParts = this.domain.split('.').reverse(), - newDomainParts = value.split('.').reverse(); - if(newDomainParts.length > 1){ - for(i=0;i 0){ - event = doc.createEvent('HTMLEvents'); - event.initEvent( okay ? "load" : "error", false, false ); - node.dispatchEvent( event, false ); - } - }catch(e){ - console.log('error loading html element %s %e', node, e.toString()); - } - } - break; - case 'frame': - case 'iframe': - node.contentWindow = { }; - node.contentDocument = new HTMLDocument(new DOMImplementation(), node.contentWindow); - node.contentWindow.document = node.contentDocument; - try{ - Window; - }catch(e){ - node.contentDocument.addEventListener('DOMContentLoaded', function(){ - event = node.contentDocument.createEvent('HTMLEvents'); - event.initEvent("load", false, false); - node.dispatchEvent( event, false ); - }); - } - try{ - if (node.src && node.src.length > 0){ - //console.log("getting content document for (i)frame from %s", node.src); - Envjs.loadFrame(node, Envjs.uri(node.src)); - event = node.contentDocument.createEvent('HTMLEvents'); - event.initEvent("load", false, false); - node.dispatchEvent( event, false ); - }else{ - //I dont like this being here: - //TODO: better mix-in strategy so the try/catch isnt required - try{ - if(Window){ - Envjs.loadFrame(node); - //console.log('src/html/document.js: triggering frame load'); - event = node.contentDocument.createEvent('HTMLEvents'); - event.initEvent("load", false, false); - node.dispatchEvent( event, false ); - } - }catch(e){} - } - }catch(e){ - console.log('error loading html element %s %e', node, e.toString()); - } - break; - - case 'link': - if (node.href && node.href.length > 0) { - __loadLink__(node, node.href); - } - break; - /* - case 'img': - if (node.src && node.src.length > 0){ - // don't actually load anything, so we're "done" immediately: - event = doc.createEvent('HTMLEvents'); - event.initEvent("load", false, false); - node.dispatchEvent( event, false ); - } - break; - */ - case 'option': - node._updateoptions(); - break; - default: - if(node.getAttribute('onload')){ - console.log('calling attribute onload %s | %s', node.onload, node.tagName); - node.onload(); - } - break; - }//switch on name - default: - break; - }//switch on ns - break; - default: - // console.log('element appended: %s %s', node+'', node.namespaceURI); - }//switch on doc.parsing - return node; - -}); - -Aspect.around({ - target: Node, - method:"removeChild" -}, function(invocation) { - var event, - okay, - node = invocation.proceed(), - doc = node.ownerDocument; - if((node.nodeType !== Node.ELEMENT_NODE)){ - //for now we are only handling element insertions. probably we will need - //to handle text node changes to script tags and changes to src - //attributes - if(node.nodeType !== Node.DOCUMENT_NODE && node.uuid){ - //console.log('removing event listeners, %s', node, node.uuid); - node.removeEventListener('*', null, null); - } - return node; - } - //console.log('appended html element %s %s %s', node.namespaceURI, node.nodeName, node); - - switch(doc.parsing){ - case true: - //handled by parser if included - break; - case false: - switch(node.namespaceURI){ - case null: - //fall through - case "": - //fall through - case "http://www.w3.org/1999/xhtml": - //this is interesting dillema since our event engine is - //storing the registered events in an array accessed - //by the uuid property of the node. unforunately this - //means listeners hang out way after(forever ;)) the node - //has been removed and gone out of scope. - //console.log('removing event listeners, %s', node, node.uuid); - node.removeEventListener('*', null, null); - switch(node.tagName.toLowerCase()){ - case 'frame': - case 'iframe': - try{ - //console.log('removing iframe document'); - try{ - Envjs.unloadFrame(node); - }catch(e){ - console.log('error freeing resources from frame %s', e); - } - node.contentWindow = null; - node.contentDocument = null; - }catch(e){ - console.log('error unloading html element %s %e', node, e.toString()); - } - break; - default: - break; - }//switch on name - default: - break; - }//switch on ns - break; - default: - console.log('element appended: %s %s', node+'', node.namespaceURI); - }//switch on doc.parsing - return node; - -}); - - - -/** - * Named Element Support - * - * - */ - -/* - * - * @returns 'name' if the node has a appropriate name - * null if node does not have a name - */ - -var __isNamedElement__ = function(node) { - if (node.nodeType !== Node.ELEMENT_NODE) { - return null; - } - var tagName = node.tagName.toLowerCase(); - var nodename = null; - - switch (tagName) { - case 'embed': - case 'form': - case 'iframe': - nodename = node.getAttribute('name'); - break; - case 'applet': - nodename = node.id; - break; - case 'object': - // TODO: object needs to be 'fallback free' - nodename = node.id; - break; - case 'img': - nodename = node.id; - if (!nodename || ! node.getAttribute('name')) { - nodename = null; - } - break; - } - return (nodename) ? nodename : null; -}; - - -var __addNamedMap__ = function(target, node) { - var nodename = __isNamedElement__(node); - if (nodename) { - target.__defineGetter__(nodename, function() { - return node; - }); - } -}; - -var __removeNamedMap__ = function(target, node) { - if (!node) { - return; - } - var nodename = __isNamedElement__(node); - if (nodename) { - delete target[nodename]; - } -}; - -/** - * @name HTMLEvents - * @w3c:domlevel 2 - * @uri http://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html - */ - -var __eval__ = function(script, node){ - if (!script == ""){ - // don't assemble environment if no script... - try{ - eval(script); - }catch(e){ - console.log('error evaluating %s', e); - } - } -}; - -var HTMLEvents= function(){}; -HTMLEvents.prototype = { - onload: function(event){ - __eval__(this.getAttribute('onload')||'', this); - }, - onunload: function(event){ - __eval__(this.getAttribute('onunload')||'', this); - }, - onabort: function(event){ - __eval__(this.getAttribute('onabort')||'', this); - }, - onerror: function(event){ - __eval__(this.getAttribute('onerror')||'', this); - }, - onselect: function(event){ - __eval__(this.getAttribute('onselect')||'', this); - }, - onchange: function(event){ - __eval__(this.getAttribute('onchange')||'', this); - }, - onsubmit: function(event){ - if (__eval__(this.getAttribute('onsubmit')||'', this)) { - this.submit(); - } - }, - onreset: function(event){ - __eval__(this.getAttribute('onreset')||'', this); - }, - onfocus: function(event){ - __eval__(this.getAttribute('onfocus')||'', this); - }, - onblur: function(event){ - __eval__(this.getAttribute('onblur')||'', this); - }, - onresize: function(event){ - __eval__(this.getAttribute('onresize')||'', this); - }, - onscroll: function(event){ - __eval__(this.getAttribute('onscroll')||'', this); - } -}; - -//HTMLDocument, HTMLFramesetElement, HTMLObjectElement -var __load__ = function(element){ - var event = new Event('HTMLEvents'); - event.initEvent("load", false, false); - element.dispatchEvent(event); - return event; -}; - -//HTMLFramesetElement, HTMLBodyElement -var __unload__ = function(element){ - var event = new Event('HTMLEvents'); - event.initEvent("unload", false, false); - element.dispatchEvent(event); - return event; -}; - -//HTMLObjectElement -var __abort__ = function(element){ - var event = new Event('HTMLEvents'); - event.initEvent("abort", true, false); - element.dispatchEvent(event); - return event; -}; - -//HTMLFramesetElement, HTMLObjectElement, HTMLBodyElement -var __error__ = function(element){ - var event = new Event('HTMLEvents'); - event.initEvent("error", true, false); - element.dispatchEvent(event); - return event; -}; - -//HTMLInputElement, HTMLTextAreaElement -var __select__ = function(element){ - var event = new Event('HTMLEvents'); - event.initEvent("select", true, false); - element.dispatchEvent(event); - return event; -}; - -//HTMLInputElement, HTMLSelectElement, HTMLTextAreaElement -var __change__ = function(element){ - var event = new Event('HTMLEvents'); - event.initEvent("change", true, false); - element.dispatchEvent(event); - return event; -}; - -//HtmlFormElement -var __submit__ = function(element){ - var event = new Event('HTMLEvents'); - event.initEvent("submit", true, true); - element.dispatchEvent(event); - return event; -}; - -//HtmlFormElement -var __reset__ = function(element){ - var event = new Event('HTMLEvents'); - event.initEvent("reset", false, false); - element.dispatchEvent(event); - return event; -}; - -//LABEL, INPUT, SELECT, TEXTAREA, and BUTTON -var __focus__ = function(element){ - var event = new Event('HTMLEvents'); - event.initEvent("focus", false, false); - element.dispatchEvent(event); - return event; -}; - -//LABEL, INPUT, SELECT, TEXTAREA, and BUTTON -var __blur__ = function(element){ - var event = new Event('HTMLEvents'); - event.initEvent("blur", false, false); - element.dispatchEvent(event); - return event; -}; - -//Window -var __resize__ = function(element){ - var event = new Event('HTMLEvents'); - event.initEvent("resize", true, false); - element.dispatchEvent(event); - return event; -}; - -//Window -var __scroll__ = function(element){ - var event = new Event('HTMLEvents'); - event.initEvent("scroll", true, false); - element.dispatchEvent(event); - return event; -}; - -/** - * @name KeyboardEvents - * @w3c:domlevel 2 - * @uri http://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html - */ -var KeyboardEvents= function(){}; -KeyboardEvents.prototype = { - onkeydown: function(event){ - __eval__(this.getAttribute('onkeydown')||'', this); - }, - onkeypress: function(event){ - __eval__(this.getAttribute('onkeypress')||'', this); - }, - onkeyup: function(event){ - __eval__(this.getAttribute('onkeyup')||'', this); - } -}; - - -var __registerKeyboardEventAttrs__ = function(elm){ - if(elm.hasAttribute('onkeydown')){ - elm.addEventListener('keydown', elm.onkeydown, false); - } - if(elm.hasAttribute('onkeypress')){ - elm.addEventListener('keypress', elm.onkeypress, false); - } - if(elm.hasAttribute('onkeyup')){ - elm.addEventListener('keyup', elm.onkeyup, false); - } - return elm; -}; - -//HTMLInputElement, HTMLSelectElement, HTMLTextAreaElement -var __keydown__ = function(element){ - var event = new Event('KeyboardEvents'); - event.initEvent("keydown", false, false); - element.dispatchEvent(event); -}; - -//HTMLInputElement, HTMLSelectElement, HTMLTextAreaElement -var __keypress__ = function(element){ - var event = new Event('KeyboardEvents'); - event.initEvent("keypress", false, false); - element.dispatchEvent(event); -}; - -//HTMLInputElement, HTMLSelectElement, HTMLTextAreaElement -var __keyup__ = function(element){ - var event = new Event('KeyboardEvents'); - event.initEvent("keyup", false, false); - element.dispatchEvent(event); -}; - -/** - * @name MaouseEvents - * @w3c:domlevel 2 - * @uri http://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html - */ -var MouseEvents= function(){}; -MouseEvents.prototype = { - onclick: function(event){ - __eval__(this.getAttribute('onclick')||'', this); - }, - ondblclick: function(event){ - __eval__(this.getAttribute('ondblclick')||'', this); - }, - onmousedown: function(event){ - __eval__(this.getAttribute('onmousedown')||'', this); - }, - onmousemove: function(event){ - __eval__(this.getAttribute('onmousemove')||'', this); - }, - onmouseout: function(event){ - __eval__(this.getAttribute('onmouseout')||'', this); - }, - onmouseover: function(event){ - __eval__(this.getAttribute('onmouseover')||'', this); - }, - onmouseup: function(event){ - __eval__(this.getAttribute('onmouseup')||'', this); - } -}; - -var __registerMouseEventAttrs__ = function(elm){ - if(elm.hasAttribute('onclick')){ - elm.addEventListener('click', elm.onclick, false); - } - if(elm.hasAttribute('ondblclick')){ - elm.addEventListener('dblclick', elm.ondblclick, false); - } - if(elm.hasAttribute('onmousedown')){ - elm.addEventListener('mousedown', elm.onmousedown, false); - } - if(elm.hasAttribute('onmousemove')){ - elm.addEventListener('mousemove', elm.onmousemove, false); - } - if(elm.hasAttribute('onmouseout')){ - elm.addEventListener('mouseout', elm.onmouseout, false); - } - if(elm.hasAttribute('onmouseover')){ - elm.addEventListener('mouseover', elm.onmouseover, false); - } - if(elm.hasAttribute('onmouseup')){ - elm.addEventListener('mouseup', elm.onmouseup, false); - } - return elm; -}; - - -var __click__ = function(element){ - var event = new Event('MouseEvents'); - event.initEvent("click", true, true, null, 0, - 0, 0, 0, 0, false, false, false, - false, null, null); - element.dispatchEvent(event); -}; -var __mousedown__ = function(element){ - var event = new Event('MouseEvents'); - event.initEvent("mousedown", true, true, null, 0, - 0, 0, 0, 0, false, false, false, - false, null, null); - element.dispatchEvent(event); -}; -var __mouseup__ = function(element){ - var event = new Event('MouseEvents'); - event.initEvent("mouseup", true, true, null, 0, - 0, 0, 0, 0, false, false, false, - false, null, null); - element.dispatchEvent(event); -}; -var __mouseover__ = function(element){ - var event = new Event('MouseEvents'); - event.initEvent("mouseover", true, true, null, 0, - 0, 0, 0, 0, false, false, false, - false, null, null); - element.dispatchEvent(event); -}; -var __mousemove__ = function(element){ - var event = new Event('MouseEvents'); - event.initEvent("mousemove", true, true, null, 0, - 0, 0, 0, 0, false, false, false, - false, null, null); - element.dispatchEvent(event); -}; -var __mouseout__ = function(element){ - var event = new Event('MouseEvents'); - event.initEvent("mouseout", true, true, null, 0, - 0, 0, 0, 0, false, false, false, - false, null, null); - element.dispatchEvent(event); -}; - -/** - * HTMLElement - DOM Level 2 - */ - - -/* Hack for http://www.prototypejs.org/ - * - * Prototype 1.6 (the library) creates a new global Element, which causes - * envjs to use the wrong Element. - * - * http://envjs.lighthouseapp.com/projects/21590/tickets/108-prototypejs-wont-load-due-it-clobbering-element - * - * Options: - * (1) Rename the dom/element to something else - * rejected: been done before. people want Element. - * (2) merge dom+html and not export Element to global namespace - * (meaning we would use a local var Element in a closure, so prototype - * can do what ever it wants) - * rejected: want dom and html separate - * (3) use global namespace (put everything under Envjs = {}) - * rejected: massive change - * (4) use commonjs modules (similar to (3) in spirit) - * rejected: massive change - * - * or - * - * (5) take a reference to Element during initial loading ("compile - * time"), and use the reference instead of "Element". That's - * what the next line does. We use __DOMElement__ if we need to - * reference the parent class. Only this file explcity uses - * Element so this should work, and is the most minimal change I - * could think of with no external API changes. - * - */ -var __DOMElement__ = Element; - -HTMLElement = function(ownerDocument) { - __DOMElement__.apply(this, arguments); -}; - -HTMLElement.prototype = new Element(); -__extend__(HTMLElement.prototype, HTMLEvents.prototype); -__extend__(HTMLElement.prototype, { - get className() { - return this.getAttribute("class")||''; - }, - set className(value) { - return this.setAttribute("class",__trim__(value)); - }, - get dir() { - return this.getAttribute("dir")||"ltr"; - }, - set dir(val) { - return this.setAttribute("dir",val); - }, - get id(){ - return this.getAttribute('id'); - }, - set id(id){ - this.setAttribute('id', id); - }, - get innerHTML(){ - var ret = "", - i; - - // create string containing the concatenation of the string - // values of each child - for (i=0; i < this.childNodes.length; i++) { - if(this.childNodes[i]){ - if(this.childNodes[i].nodeType === Node.ELEMENT_NODE){ - ret += this.childNodes[i].xhtml; - } else if (this.childNodes[i].nodeType === Node.TEXT_NODE && i>0 && - this.childNodes[i-1].nodeType === Node.TEXT_NODE){ - //add a single space between adjacent text nodes - ret += " "+this.childNodes[i].xml; - }else{ - ret += this.childNodes[i].xml; - } - } - } - return ret; - }, - get lang() { - return this.getAttribute("lang"); - }, - set lang(val) { - return this.setAttribute("lang",val); - }, - get offsetHeight(){ - return Number((this.style.height || '').replace("px","")); - }, - get offsetWidth(){ - return Number((this.style.width || '').replace("px","")); - }, - offsetLeft: 0, - offsetRight: 0, - get offsetParent(){ - /* TODO */ - return; - }, - set offsetParent(element){ - /* TODO */ - return; - }, - scrollHeight: 0, - scrollWidth: 0, - scrollLeft: 0, - scrollRight: 0, - get style(){ - return this.getAttribute('style')||''; - }, - get title() { - return this.getAttribute("title"); - }, - set title(value) { - return this.setAttribute("title", value); - }, - get tabIndex(){ - var tabindex = this.getAttribute('tabindex'); - if(tabindex!==null){ - return Number(tabindex); - } else { - return 0; - } - }, - set tabIndex(value){ - if (value === undefined || value === null) { - value = 0; - } - this.setAttribute('tabindex',Number(value)); - }, - get outerHTML(){ - //Not in the specs but I'll leave it here for now. - return this.xhtml; - }, - scrollIntoView: function(){ - /*TODO*/ - return; - }, - toString: function(){ - return '[object HTMLElement]'; - }, - get xhtml() { - // HTMLDocument.xhtml is non-standard - // This is exactly like Document.xml except the tagName has to be - // lower cased. I dont like to duplicate this but its really not - // a simple work around between xml and html serialization via - // XMLSerializer (which uppercases html tags) and innerHTML (which - // lowercases tags) - - var ret = "", - ns = "", - name = (this.tagName+"").toLowerCase(), - attrs, - attrstring = "", - i; - - // serialize namespace declarations - if (this.namespaceURI){ - if((this === this.ownerDocument.documentElement) || - (!this.parentNode) || - (this.parentNode && - (this.parentNode.namespaceURI !== this.namespaceURI))) { - ns = ' xmlns' + (this.prefix ? (':' + this.prefix) : '') + - '="' + this.namespaceURI + '"'; - } - } - - // serialize Attribute declarations - attrs = this.attributes; - for(i=0;i< attrs.length;i++){ - attrstring += " "+attrs[i].name+'="'+attrs[i].xml+'"'; - } - - if(this.hasChildNodes()){ - // serialize this Element - ret += "<" + name + ns + attrstring +">"; - for(i=0;i< this.childNodes.length;i++){ - ret += this.childNodes[i].xhtml ? - this.childNodes[i].xhtml : - this.childNodes[i].xml; - } - ret += ""; - }else{ - switch(name){ - case 'script': - ret += "<" + name + ns + attrstring +">"; - break; - default: - ret += "<" + name + ns + attrstring +"/>"; - } - } - - return ret; - }, - - /** - * setAttribute use a dispatch table that other tags can set to - * "listen" to various values being set. The dispatch table - * and registration functions are at the end of the file. - * - */ - - setAttribute: function(name, value) { - var result = __DOMElement__.prototype.setAttribute.apply(this, arguments); - __addNamedMap__(this.ownerDocument, this); - var tagname = this.tagName; - var callback = HTMLElement.getAttributeCallback('set', tagname, name); - if (callback) { - callback(this, value); - } - }, - setAttributeNS: function(namespaceURI, name, value) { - var result = __DOMElement__.prototype.setAttributeNS.apply(this, arguments); - __addNamedMap__(this.ownerDocument, this); - var tagname = this.tagName; - var callback = HTMLElement.getAttributeCallback('set', tagname, name); - if (callback) { - callback(this, value); - } - - return result; - }, - setAttributeNode: function(newnode) { - var result = __DOMElement__.prototype.setAttributeNode.apply(this, arguments); - __addNamedMap__(this.ownerDocument, this); - var tagname = this.tagName; - var callback = HTMLElement.getAttributeCallback('set', tagname, newnode.name); - if (callback) { - callback(this, node.value); - } - return result; - }, - setAttributeNodeNS: function(newnode) { - var result = __DOMElement__.prototype.setAttributeNodeNS.apply(this, arguments); - __addNamedMap__(this.ownerDocument, this); - var tagname = this.tagName; - var callback = HTMLElement.getAttributeCallback('set', tagname, newnode.name); - if (callback) { - callback(this, node.value); - } - return result; - }, - removeAttribute: function(name) { - __removeNamedMap__(this.ownerDocument, this); - return __DOMElement__.prototype.removeAttribute.apply(this, arguments); - }, - removeAttributeNS: function(namespace, localname) { - __removeNamedMap__(this.ownerDocument, this); - return __DOMElement__.prototype.removeAttributeNS.apply(this, arguments); - }, - removeAttributeNode: function(name) { - __removeNamedMap__(this.ownerDocument, this); - return __DOMElement__.prototype.removeAttribute.apply(this, arguments); - }, - removeChild: function(oldChild) { - __removeNamedMap__(this.ownerDocument, oldChild); - return __DOMElement__.prototype.removeChild.apply(this, arguments); - }, - importNode: function(othernode, deep) { - var newnode = __DOMElement__.prototype.importNode.apply(this, arguments); - __addNamedMap__(this.ownerDocument, newnode); - return newnode; - }, - - // not actually sure if this is needed or not - replaceNode: function(newchild, oldchild) { - var newnode = __DOMElement__.prototype.replaceNode.apply(this, arguments); - __removeNamedMap__(this.ownerDocument, oldchild); - __addNamedMap__(this.ownerDocument, newnode); - return newnode; - } -}); - - -HTMLElement.attributeCallbacks = {}; -HTMLElement.registerSetAttribute = function(tag, attrib, callbackfn) { - HTMLElement.attributeCallbacks[tag + ':set:' + attrib] = callbackfn; -}; -HTMLElement.registerRemoveAttribute = function(tag, attrib, callbackfn) { - HTMLElement.attributeCallbacks[tag + ':remove:' + attrib] = callbackfn; -}; - -/** - * This is really only useful internally - * - */ -HTMLElement.getAttributeCallback = function(type, tag, attrib) { - return HTMLElement.attributeCallbacks[tag + ':' + type + ':' + attrib] || null; -}; -/* - * HTMLCollection - * - * HTML5 -- 2.7.2.1 HTMLCollection - * http://dev.w3.org/html5/spec/Overview.html#htmlcollection - * http://dev.w3.org/html5/spec/Overview.html#collections - */ -HTMLCollection = function(nodelist, type) { - - __setArray__(this, []); - var n; - for (var i=0; i= 0) && (idx < this.length)) ? this[idx] : null; - }, - - namedItem: function (name) { - return this[name] || null; - }, - - toString: function() { - return '[object HTMLCollection]'; - } -}; -/* - * a set of convenience classes to centralize implementation of - * properties and methods across multiple in-form elements - * - * the hierarchy of related HTML elements and their members is as follows: - * - * Condensed Version - * - * HTMLInputCommon - * * legent (no value attr) - * * fieldset (no value attr) - * * label (no value attr) - * * option (custom value) - * HTMLTypeValueInputs (extends InputCommon) - * * select (custom value) - * * button (just sets value) - * HTMLInputAreaCommon (extends TypeValueIput) - * * input (custom) - * * textarea (just sets value) - * - * ----------------------- - * HTMLInputCommon: common to all elements - * .form - * - * - * [common plus:] - * .align - * - *
- * [identical to "legend" plus:] - * .margin - * - * - * **** - * - *