Giter Club home page Giter Club logo

hast-util-find-and-replace's People

Contributors

christianmurphy avatar wooorm avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hast-util-find-and-replace's Issues

Allow searching text within style property or style tag

Initial checklist

Problem

The findAndReplace function is not working with style property and style tag.
Example: 
const doc = <html><head><style>main {color: rgb(60,61,62);background-color: rgb(255,255,255);font-family: Helvetica;}</style></head><body><main>Subject line: Test Template<br><p><span style="color: rgb(60,61,62);background-color: rgb(255,255,255);font-family: Helvetica;">Hi {{shomare}}, </span></p>
<p><span style="color: rgb(60,61,62);background-color: rgb(255,255,255);font-family: Helvetica;">Version 2 Create the fields for your custom setting. The data in these fields are cached with the application.</span><span style="color: rgba(0,0,0,0.87);background-color: rgb(255,255,255);font-size: 13px;">This is a test template. </span><span style="color: rgb(60,61,62);background-color: rgb(255,255,255);font-family: Helvetica;">Create the fields for your custom setting. The data in these fields are cached with the application.</span><span style="color: rgba(0,0,0,0.87);background-color: rgb(255,255,255);font-size: 13px;">This is a test template. </span><span style="color: rgb(60,61,62);background-color: rgb(255,255,255);font-family: Helvetica;">Create the fields for your custom setting. The data in these fields are cached with the application.</span><span style="color: rgba(0,0,0,0.87);background-color: rgb(255,255,255);font-size: 13px;">This is a test template. </span><span style="color: rgb(60,61,62);background-color: rgb(255,255,255);font-family: Helvetica;">Create the fields for your custom setting. The data in these fields are cached with the application.</span><span style="color: rgba(0,0,0,0.87);background-color: rgb(255,255,255);font-size: 13px;">This is a test template. </span><span style="color: rgb(60,61,62);background-color: rgb(255,255,255);font-family: Helvetica;">Create the fields for your custom setting. The data in these fields are cached with the application.</span><span style="color: rgba(0,0,0,0.87);background-color: rgb(255,255,255);font-size: 13px;">This is a test template.. </span><span style="color: rgb(60,61,62);background-color: rgb(255,255,255);font-family: Helvetica;">Create the fields for your custom setting. The data in these fields are cached with the application.</span><span style="color: rgba(0,0,0,0.87);background-color: rgb(255,255,255);font-size: 13px;">This is a test template....</span><br></p>
<ol>
<li><span style="color: rgb(60,61,62);background-color: rgb(255,255,255);font-family: Helvetica;">Create the fields for your custom setting. The data in these fields are cached with the application.</span><span style="color: rgba(0,0,0,0.87);background-color: rgb(255,255,255);font-size: 13px;">This is a test template.</span></li>
<li><span style="color: rgb(60,61,62);background-color: rgb(255,255,255);font-family: Helvetica;">Create the fields for your custom setting. The data in these fields are cached with the application.</span><span style="color: rgba(0,0,0,0.87);background-color: rgb(255,255,255);font-size: 13px;">This is a test template.</span></li>
<li><span style="color: rgb(60,61,62);background-color: rgb(255,255,255);font-family: Helvetica;">Create the fields for your custom setting. The data in these fields are cached with the application.</span><span style="color: rgba(0,0,0,0.87);background-color: rgb(255,255,255);font-size: 13px;">This is a test template.</span></li>
</ol>
<p></p>
<p></p>
<p><span style="color: rgba(0,0,0,0.87);background-color: rgb(255,255,255);font-size: 13px;">Thanks.</span></p>
</main></body></html>'
const hast = fromDom(doc)
findAndReplace(hast, [[/font-family:\s*(.+?(?=;));/gi, '']], undefined, { ignore: ['title', 'script', 'svg', 'math']})
console.log("toHtml", toHtml(hast))

Results:

<style>main {color: rgb(60,61,62);background-color: rgb(255,255,255);font-family: Helvetica;}</style>Subject line: Test Template

Hi {{shomare}},

Version 2 Create the fields for your custom setting. The data in these fields are cached with the application.This is a test template. Create the fields for your custom setting. The data in these fields are cached with the application.This is a test template. Create the fields for your custom setting. The data in these fields are cached with the application.This is a test template. Create the fields for your custom setting. The data in these fields are cached with the application.This is a test template. Create the fields for your custom setting. The data in these fields are cached with the application.This is a test template.. Create the fields for your custom setting. The data in these fields are cached with the application.This is a test template....

  1. Create the fields for your custom setting. The data in these fields are cached with the application.This is a test template.
  2. Create the fields for your custom setting. The data in these fields are cached with the application.This is a test template.
  3. Create the fields for your custom setting. The data in these fields are cached with the application.This is a test template.

Thanks.

Solution

It should find all font-family styles and remove them within the script tag and all the style properties on different types of nodes.
Input:
<head><style>main {color: rgb(60,61,62);background-color: rgb(255,255,255);font-family: Helvetica;</style></head>

and

<span style="color: rgb(60,61,62);background-color: rgb(255,255,255);font-family: Helvetica;">Version 2 Create the fields for your custom setting. The data in these fields are cached with the application.</span>

Output:
<head><style>main {color: rgb(60,61,62);background-color: rgb(255,255,255);</style></head>

and

<span style="color: rgb(60,61,62);background-color: rgb(255,255,255);>Version 2 Create the fields for your custom setting. The data in these fields are cached with the application.</span>

Alternatives

I don't think of any right now

Very particular situation results in some text not being replaced

Initial checklist

Affected packages and versions

3 and 4

Link to runnable example

https://codesandbox.io/s/hast-replace-error-fzuxo3?file=/error.spec.js

Steps to reproduce

The code sandbox has a broken test. It should not be broken.

Expected behavior

Both replaceme instances should be replaced.

Actual behavior

The first "replaceme" instance is replaced the 2nd bold instance of "replaceme" is not replaced. If I move the bold text earlier in the sentence there is no issue. If I make the text not bold there is also no issue.

I had trouble finding the bug in the sourcecode of this package. Maybe you can help out.

Runtime

Node v16, Node v14

Package manager

npm v7

OS

Linux

Build and bundle tools

Webpack, Parcel

Allow `ignore` to accept a function?

Subject of the feature

I would like want to fine-tune the ignore section of a findAndReplace to ignore replacing things inside e.g. something that has a specific class name; would be nice if i could specify a function so i can customize whether to ignore a node

Problem

only being able to specify tagNames to ignore doesn't give me the granularity i need to ignore the things i want

Expected behaviour

I'd like to be able to pass in a function to ignore, e.g.

ignore: (node: Node) => boolean

Alternatives

Can't think of any

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.