Browse files

Refactor print stylesheet detection and add basic files to demo for t…

…esting.
  • Loading branch information...
1 parent f9ceabb commit 1312829c7da91c8077cd34ff8ef6ebeb51b812fa @andrewdavey andrewdavey committed Sep 8, 2011
Showing with 34 additions and 22 deletions.
  1. +1 −0 demo/index.htm
  2. +4 −0 demo/styles/print.css
  3. +29 −22 src/client/vogue-client.js
View
1 demo/index.htm
@@ -4,6 +4,7 @@
<title>Vogue - Demo Page</title>
<link href="/styles/reset.css" type="text/css" rel="stylesheet"/>
<link href="/styles/demo.css?test=1" type="text/css" rel="stylesheet"/>
+ <link href="/styles/print.css" type="text/css" rel="stylesheet" media="print"/>
</head>
<body>
<h1>Vogue - Demo Page</h1>
View
4 demo/styles/print.css
@@ -0,0 +1,4 @@
+body {
+ background: #f0f; /* something nasty to notice if vogue switches on this style! */
+ color: blue;
+}
View
51 src/client/vogue-client.js
@@ -91,6 +91,16 @@
}
/**
+ * Checks if the stylesheet's media attribute is 'print'
+ *
+ * @param (Object) link The stylesheet element to check.
+ * @returns (Boolean)
+ */
+ function isPrintStylesheet(link) {
+ return link.getAttribute("media") === "print";
+ }
+
+ /**
* Get the link's base URL.
*
* @param {String} href The URL to check.
@@ -119,34 +129,31 @@
// Go through all the links in the page, looking for stylesheets.
for (i = 0, m = links.length; i < m; i += 1) {
link = links[i];
- if (link.getAttribute("media") != "print") {
- if (isLocalStylesheet(link)) {
- // Link is local, get the base URL.
- href = getBase(link.href);
- if (href !== false) {
- stylesheets[href] = link;
- }
- }
+ if (isPrintStylesheet(link)) continue;
+ if (!isLocalStylesheet(link)) continue;
+ // Link is local, get the base URL.
+ href = getBase(link.href);
+ if (href !== false) {
+ stylesheets[href] = link;
}
}
// Go through all the style tags, looking for @import tags.
links = document.getElementsByTagName("style");
for (i = 0, m = links.length; i < m; i += 1) {
- if (links[i].getAttribute("media") != "print") {
- content = links[i].text || links[i].textContent;
- while ((matches = reImport.exec(content))) {
- link = {
- rel: "stylesheet",
- href: matches[1],
- getAttribute: getProperty
- };
- if (isLocalStylesheet(link)) {
- // Link is local, get the base URL.
- href = getBase(link.href);
- if (href !== false) {
- stylesheets[href] = link;
- }
+ if (isPrintStylesheet(link[i])) continue;
+ content = links[i].text || links[i].textContent;
+ while ((matches = reImport.exec(content))) {
+ link = {
+ rel: "stylesheet",
+ href: matches[1],
+ getAttribute: getProperty
+ };
+ if (isLocalStylesheet(link)) {
+ // Link is local, get the base URL.
+ href = getBase(link.href);
+ if (href !== false) {
+ stylesheets[href] = link;
}
}
}

0 comments on commit 1312829

Please sign in to comment.