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

Match the spec's "find the IPv6 address compressed piece index" #284

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ whatwg-url is a full implementation of the WHATWG [URL Standard](https://url.spe

## Specification conformance

whatwg-url is currently up to date with the URL spec up to commit [302c5af](https://github.com/whatwg/url/commit/302c5afb376b7fb1d5943634ad6c3a2e71c37f37).
whatwg-url is currently up to date with the URL spec up to commit [da212c9](https://github.com/whatwg/url/commit/da212c98f0bba9c35aec3728bbcc13f8f3a7eb52).

For `file:` URLs, whose [origin is left unspecified](https://url.spec.whatwg.org/#concept-url-origin), whatwg-url chooses to use a new opaque origin (which serializes to `"null"`).

Expand Down
41 changes: 20 additions & 21 deletions lib/url-state-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ function parseIPv6(input) {

function serializeIPv6(address) {
let output = "";
const compress = findLongestZeroSequence(address);
const compress = findTheIPv6AddressCompressedPieceIndex(address);
let ignore0 = false;

for (let pieceIndex = 0; pieceIndex <= 7; ++pieceIndex) {
Expand Down Expand Up @@ -384,35 +384,34 @@ function parseOpaqueHost(input) {
return utf8PercentEncodeString(input, isC0ControlPercentEncode);
}

function findLongestZeroSequence(arr) {
let maxIdx = null;
let maxLen = 1; // only find elements > 1
let currStart = null;
let currLen = 0;

for (let i = 0; i < arr.length; ++i) {
if (arr[i] !== 0) {
if (currLen > maxLen) {
maxIdx = currStart;
maxLen = currLen;
function findTheIPv6AddressCompressedPieceIndex(address) {
let longestIndex = null;
let longestSize = 1; // only find elements > 1
let foundIndex = null;
let foundSize = 0;

for (let pieceIndex = 0; pieceIndex < address.length; ++pieceIndex) {
if (address[pieceIndex] !== 0) {
if (foundSize > longestSize) {
longestIndex = foundIndex;
longestSize = foundSize;
}

currStart = null;
currLen = 0;
foundIndex = null;
foundSize = 0;
} else {
if (currStart === null) {
currStart = i;
if (foundIndex === null) {
foundIndex = pieceIndex;
}
++currLen;
++foundSize;
}
}

// if trailing zeros
if (currLen > maxLen) {
return currStart;
if (foundSize > longestSize) {
return foundIndex;
}

return maxIdx;
return longestIndex;
}

function serializeHost(host) {
Expand Down
Loading