Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(meta_generator): update regex and avoid edge case #3697

Merged
merged 2 commits into from
Sep 2, 2019

Conversation

SukkaW
Copy link
Member

@SukkaW SukkaW commented Aug 31, 2019

What does it do?

After #3671 & #3696 , meta_generator filter will replace </title> with <meta name="generator" content="Hexo ${hexo.version}"></title>.

@curbengh has mentioned some edge cases will cause unwanted behavior:

<html>
<head>
<!-- A inline svg in css might include <title> tag -->
<style>
  .foo {
    background: url(data:image/svg+xml;utf8,<svg width="500" height="300" xmlns="http://www.w3.org/2000/svg"><g><title>svg</title></g></svg>);
  }    
</style>
<title>TITLE</title>
</head>
...
</html>

This PR introduce replacing </head> with <meta name="generator" content="Hexo ${hexo.version}"></head> with a pattern that ignore empty <head> tag.

How to test

git clone -b meta-generator https://github.com/sukkaw/hexo.git
cd hexo
npm install
npm test

A test case about ignore empty head tag has been added in this PR as well.

Screenshots

According to benchmark, use regex to match </head> is about 1% slower than replace </title> directly.

NodeJS 8 NodeJS 10 NodeJS 12
Disable meta_generator 6.15s 5.18s 5.85s
Replace </title> directly (current master branch) 6.25s 5.25s 5.94s
This PR 6.3s 5.30s 5.94s

Pull request tasks

  • Add test cases for the changes.
  • Passed the CI test.

@SukkaW SukkaW requested a review from curbengh August 31, 2019 14:16
@coveralls
Copy link

coveralls commented Aug 31, 2019

Coverage Status

Coverage remained the same at 97.225% when pulling ce86ca9 on SukkaW:meta-generator into eca3cf2 on hexojs:master.

test/scripts/filters/meta_generator.js Show resolved Hide resolved
test/scripts/filters/meta_generator.js Outdated Show resolved Hide resolved
Apply suggestions from code review by @curbengh:

- update other test cases as the filter no longer seeks <title>
- seperate 'apply to first non-empty head tag only' from 'ignore empty head tag'
@SukkaW SukkaW requested a review from curbengh September 1, 2019 06:26
@curbengh curbengh merged commit 8d0a581 into hexojs:master Sep 2, 2019
thom4parisot pushed a commit to thom4parisot/hexo that referenced this pull request Jan 17, 2020
* fix(meta_generator): update regex and avoid edge case

* test(meta_generator): update test cases
curbengh pushed a commit to curbengh/curbengh.github.io that referenced this pull request Apr 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants