-
Notifications
You must be signed in to change notification settings - Fork 0
GFE getElementsByStyle Problem #208
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # getElementsByStyle | ||
|
|
||
| Implement a method `getElementsByStyle()` that finds DOM elements that are rendered by the browser using the specified style. It is similar to `Element.getElementsByClassName()` but with some differences: | ||
|
|
||
| - It is a pure function which takes in an element, a property string, and a value string representing the style's property/value pair to be matched on the elements descendants. E.g. `getElementsByStyle(document.body, 'font-size', '12px')`. | ||
| - Similar to `Element.getElementsByClassName()`, only descendants of the element argument are searched, not the element itself. | ||
| - Return an array of `Elements`, instead of an `HTMLCollection` of `Elements`. | ||
|
|
||
| ## Examples | ||
|
|
||
| ```javascript | ||
| const doc = new DOMParser().parseFromString( | ||
| `<div> | ||
| <span style="font-size: 12px">Span</span> | ||
| <p style="font-size: 12px">Paragraph</p> | ||
| <blockquote style="font-size: 14px">Blockquote</blockquote> | ||
| </div>`, | ||
| 'text/html' | ||
| ); | ||
|
|
||
| getElementsByStyle(doc.body, 'font-size', '12px'); | ||
| // [span, p] <-- This is an array of elements. | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| export function getElementsByStyle( | ||
| element: Element, | ||
| property: string, | ||
| value: string | ||
| ): Element[] { | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| const elements: Element[] = []; | ||
|
|
||
| function traverseDOM(el: Element) { | ||
| const computedStyle = getComputedStyle(el); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Performance consideration: For large DOM trees, consider:
For this problem's requirements (matching computed styles), the current approach is correct but worth noting the performance implications in real-world scenarios. |
||
|
|
||
| if (computedStyle.getPropertyValue(property) === value) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normalization consideration: Different browsers may return property values in different formats (e.g., For production code, you might want to normalize values, especially for:
However, for this coding challenge, the strict equality is appropriate as the problem likely assumes normalized inputs. |
||
| elements.push(el); | ||
| } | ||
|
|
||
| for (const child of el.children) { | ||
| traverseDOM(child); | ||
| } | ||
| } | ||
| for (const child of element.children) { | ||
| traverseDOM(child); | ||
| } | ||
| return elements; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add problem metadata + complexity section.
This README is missing the difficulty badge, problem link, constraints, and Big‑O complexity note. Please add them to align with the repo’s README guidelines. As per coding guidelines, ...
🤖 Prompt for AI Agents