Skip to content

Commit

Permalink
Fix issue with multiple :hover/:active selectors per stylesheet
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Burgmer committed Dec 12, 2013
1 parent b01a447 commit 6e3a12a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
4 changes: 2 additions & 2 deletions dist/rasterizeHTML.allinone.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/rasterizeHTML.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! rasterizeHTML.js - v0.6.0 - 2013-12-11
/*! rasterizeHTML.js - v0.6.0 - 2013-12-12
* http://www.github.com/cburgmer/rasterizeHTML.js
* Copyright (c) 2013 Christoph Burgmer; Licensed MIT */
window.rasterizeHTMLInline = (function (module) {
Expand Down Expand Up @@ -1461,16 +1461,16 @@ window.rasterizeHTML = (function (rasterizeHTMLInline, xmlserializer, theWindow)

module.util.rewriteStyleRuleSelector = function (doc, oldSelector, newSelector) {
// Assume that oldSelector is always prepended with a ':' or '.' for now, so no special handling needed
var oldSelectorRegExp = new RegExp(oldSelector + '(?=\\W|$)', 'g');
var oldSelectorRegex = oldSelector + '(?=\\W|$)';

Array.prototype.forEach.call(doc.querySelectorAll('style'), function (styleElement) {
var matchingRules = Array.prototype.filter.call(styleElement.sheet.cssRules, function (rule) {
return rule.selectorText && oldSelectorRegExp.test(rule.selectorText);
return rule.selectorText && new RegExp(oldSelectorRegex).test(rule.selectorText);
});

if (matchingRules.length) {
matchingRules.forEach(function (rule) {
var selector = rule.selectorText.replace(oldSelectorRegExp, newSelector);
var selector = rule.selectorText.replace(new RegExp(oldSelectorRegex, 'g'), newSelector);

updateRuleSelector(rule, selector);
});
Expand Down
4 changes: 2 additions & 2 deletions dist/rasterizeHTML.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/pseudoClasses.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
a:active {
color: green;
}
div:active:after, div:hover:after {
div:active::after, div:hover::after {
font-size: 8px;
color: green;
content: "hover or active"
Expand Down
6 changes: 3 additions & 3 deletions src/rasterizeHTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,16 +383,16 @@ window.rasterizeHTML = (function (rasterizeHTMLInline, xmlserializer, theWindow)

module.util.rewriteStyleRuleSelector = function (doc, oldSelector, newSelector) {
// Assume that oldSelector is always prepended with a ':' or '.' for now, so no special handling needed
var oldSelectorRegExp = new RegExp(oldSelector + '(?=\\W|$)', 'g');
var oldSelectorRegex = oldSelector + '(?=\\W|$)';

Array.prototype.forEach.call(doc.querySelectorAll('style'), function (styleElement) {
var matchingRules = Array.prototype.filter.call(styleElement.sheet.cssRules, function (rule) {
return rule.selectorText && oldSelectorRegExp.test(rule.selectorText);
return rule.selectorText && new RegExp(oldSelectorRegex).test(rule.selectorText);
});

if (matchingRules.length) {
matchingRules.forEach(function (rule) {
var selector = rule.selectorText.replace(oldSelectorRegExp, newSelector);
var selector = rule.selectorText.replace(new RegExp(oldSelectorRegex, 'g'), newSelector);

updateRuleSelector(rule, selector);
});
Expand Down
16 changes: 16 additions & 0 deletions test/UtilSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,22 @@ describe("Utilities function", function () {
expect(doc.querySelector('style').textContent).toMatch(/i.myFakeHover, a.myFakeHover \{\s*color: blue;\s*\}/);
});

it("should correctly handle multiple sub-selector", function () {
setHtml('<style type="text/css">i:active::after { color: blue; }</style>');

rasterizeHTML.util.rewriteStyleRuleSelector(doc, ':active', '.myFakeActive');

expect(doc.querySelector('style').textContent).toMatch(/i.myFakeActive::?after \{\s*color: blue;\s*\}/);
});

it("should correctly handle multiple selector occurrences in different rules", function () {
setHtml('<style type="text/css">a:active {color: green;}i:active { color: blue; }</style>');

rasterizeHTML.util.rewriteStyleRuleSelector(doc, ':active', '.myFakeActive');

expect(doc.querySelector('style').textContent).toMatch(/i.myFakeActive \{\s*color: blue;\s*\}/);
});

it("should cope with non CSSStyleRule", function () {
setHtml('<head><style type="text/css">@font-face { font-family: "RaphaelIcons"; src: url("raphaelicons-webfont.woff"); }</style></head><body><span></span></body>');

Expand Down

0 comments on commit 6e3a12a

Please sign in to comment.