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

fix: merge attributes without tag and attributes with tag #408

Conversation

scottohara
Copy link

@scottohara scottohara commented Oct 27, 2021

This PR contains a:

  • bugfix
  • new feature
  • code refactor
  • test update
  • typo fix
  • metadata update

Motivation / Use-Case

Fixes #405

When processing attributes, merge attributes for any tag with attributes for the specific tag:

const handlers = new Map([
  ...(options.sources.list.get("*") || new Map()),
  ...(options.sources.list.get(tagName.toLowerCase()) || new Map()),
]);

(More info: #408 (comment))

Breaking Changes

None

Additional Info

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Oct 27, 2021

CLA Signed

The committers are authorized under a signed CLA.

@alexander-akait
Copy link
Member

Can you provide example?

@scottohara
Copy link
Author

Additional examples added

README.md Outdated
However this will only process `data-src` attributes on tags that _aren't in the default list_:

- `<p data-src="..">` will be processed, as `p` is not in the default sources list
- `<img data-src="..">` won't be processed, as `img` is already in the default sources list
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is bug, we need fix, attribute: "data-src" should process all tags have data-src

@scottohara
Copy link
Author

OK, so if we're considering the current behaviour as a bug, then perhaps what I would suggest is a change to how the sources-plugin.js determines the handlers for the current tag being processed.

Given a config like this:

module.exports = {
  module: {
    rules: [
      {
        test: /\.html$/i,
        loader: "html-loader",
        options: {
          sources: {
            list: [
              // All default supported tags and attributes
              "...",
              {
                attribute: "data-src",
                type: "src",
              },
            ],
          },
        },
      },
    ],
  },
};

...internally, the normalizeSourcesList() function produces a Map like this:

const result = new Map([
 ...
 ["img", new Map([
   ["src", { type: srcType }],
   ["srcset", { type: srcsetType }]
 )],
 ...
 ["*", new Map([
   ["data-src", { type: srcType }]
 ])
]);

Current behaviour

When processing a tag, the handlers are determined as:

const handlers =
  options.sources.list.get(tagName.toLowerCase()) ||
  options.sources.list.get("*");

...which means that for an <img> tag, only the defaults are used:

Map(2) {'src' => {…}, 'srcset' => {…}}

Proposed change

The proposal is to use a union of the map entries:

const handlers = new Map([
  ...options.sources.list.get(tagName.toLowerCase() ?? new Map(),
  ...options.sources.list.get("*") ?? new Map()
]);

The result is a merged map of both the attributes specific to the tag, and any attributes that apply to all tags:

Map(3) {'src' => {…}, 'srcset' => {…}, 'data-src' => {…}}

(The ?? new Map() guards against either of those being undefined.)

If that solution sounds OK to you, I am happy to modify this PR accordingly.

@alexander-akait
Copy link
Member

Yes, let's do it

@scottohara scottohara changed the title docs(readme): clarify that tag is not optional if extending default sources fix: merge attributes without tag and attributes with tag Oct 30, 2021
Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And let's add test case

@scottohara scottohara force-pushed the fix-extend-sources-tag-optional branch from 09d68b5 to 7536bfc Compare October 30, 2021 13:22
@codecov
Copy link

codecov bot commented Nov 2, 2021

Codecov Report

Merging #408 (7536bfc) into master (2fca322) will decrease coverage by 0.15%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #408      +/-   ##
==========================================
- Coverage   92.12%   91.96%   -0.16%     
==========================================
  Files           7        7              
  Lines         584      585       +1     
  Branches      187      187              
==========================================
  Hits          538      538              
- Misses         37       38       +1     
  Partials        9        9              
Impacted Files Coverage Δ
src/plugins/sources-plugin.js 98.66% <100.00%> (-1.34%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2fca322...7536bfc. Read the comment docs.

@alexander-akait
Copy link
Member

Big thanks for the PR, fixed here #410, Map needs to be checked on size, we need do release due some regressions in previous version, so I go ahead and do it, anyway, thanks again, release will be today

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.

When extending default sources, tag is not optional
2 participants