feat: preserve media query from link tag to style tag

This commit is contained in:
makoto tsuyuki 2018-06-18 17:51:38 +09:00
parent df3619f89b
commit fc5708f249
6 changed files with 25 additions and 2 deletions

View File

@ -6,6 +6,7 @@ function resolve(inliner, todo, $) {
debug('start %s links', todo.length);
return todo.map(function links(link) {
var url = $(link).attr('href');
var media_query = $(link).attr('media');
if (inliner.options.skipAbsoluteUrls &&
(url.indexOf('//') === 0 || url.indexOf('http') === 0)) {
debug('skipping remote links');
@ -22,7 +23,11 @@ function resolve(inliner, todo, $) {
return inliner.cssImports(url, css)
.then(inliner.cssImages.bind(inliner, url));
}).then(function then(css) {
$(link).replaceWith('<style>' + css + '</style>');
if(media_query) {
$(link).replaceWith('<style media="' + media_query + '">' + css + '</style>');
} else {
$(link).replaceWith('<style>' + css + '</style>');
}
});
});
}

View File

@ -1 +1 @@
<style> @font-face{ font-family:'PT Sans';font-style:normal;font-weight:400;src:local('PT Sans'), local('PTSans-Regular'), format('woff2');unicode-range:U+0460-052F, U+20B4, U+2DE0-2DFF, U+A640-A69F;}@font-face{ font-family:'PT Sans';font-style:normal;font-weight:400;src:local('PT Sans'), local('PTSans-Regular'), format('woff2');unicode-range:U+0460-052F, U+20B4, U+2DE0-2DFF, U+A640-A69F;}html{ font-family:"PT Sans",courier;font-size:14px;}</style>
<style media="all"> @font-face{ font-family:'PT Sans';font-style:normal;font-weight:400;src:local('PT Sans'), local('PTSans-Regular'), format('woff2');unicode-range:U+0460-052F, U+20B4, U+2DE0-2DFF, U+A640-A69F;}@font-face{ font-family:'PT Sans';font-style:normal;font-weight:400;src:local('PT Sans'), local('PTSans-Regular'), format('woff2');unicode-range:U+0460-052F, U+20B4, U+2DE0-2DFF, U+A640-A69F;}html{ font-family:"PT Sans",courier;font-size:14px;}</style>

3
test/fixtures/link-media-query-1.css vendored Normal file
View File

@ -0,0 +1,3 @@
.title1 {
left: 1px;
}

3
test/fixtures/link-media-query-2.css vendored Normal file
View File

@ -0,0 +1,3 @@
.title2 {
left: 2px;
}

View File

@ -0,0 +1 @@
<!DOCTYPE html> <html> <head> <title>link tag's media query to style tag</title> <style media="screen and (max-width:480px)">.title1{ left:1px;}</style> <style>.title2{ left:2px;}</style> </head> <body> </body> </html>

11
test/fixtures/link-media-query.src.html vendored Normal file
View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>link tag's media query to style tag</title>
<link href="link-media-query-1.css" rel="stylesheet" type="text/css" media="screen and (max-width:480px)" />
<link href="link-media-query-2.css" rel="stylesheet" type="text/css" />
</head>
<body>
</body>
</html>