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

🏗 SwG Release 0.1.22.152 #33329

Merged
merged 6 commits into from
Mar 18, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ describes.realWin('amp-subscriptions-google', {amp: true}, (env) => {

const fetchUrl = fetchStub.getCall(0).args[0];
expect(fetchUrl).to.equal(
'https://news.google.com/swg/_/api/v1/publication/example.org/entitlements?encodedParams=eyJtZXRlcmluZyI6eyJjbGllbnRUeXBlcyI6WzFdLCJvd25lciI6ImV4YW1wbGUub3JnIiwicmVzb3VyY2UiOnsiaGFzaGVkQ2Fub25pY2FsVXJsIjoiMjcwM2YyYjZlZjBlYWFhODEzNzZhMThmYWE3N2E1OTAwOTc1Zjc3MDVkNWQ4YjZlMWEzNzJkNWY2YzJiOTdiYjU5ZjI4M2Q3MzdiNmQ5YWI3N2M1YTNkODQ4YzZlY2UyMDdjZDYwMzU4M2NjMzIyZGQ4MGFiMGI5MzA5MmM2NTAifSwic3RhdGUiOnsiYXR0cmlidXRlcyI6W119fX0='
'https://news.google.com/swg/_/api/v1/publication/example.org/entitlements?encodedParams=eyJtZXRlcmluZyI6eyJjbGllbnRUeXBlcyI6WzFdLCJvd25lciI6ImV4YW1wbGUub3JnIiwicmVzb3VyY2UiOnsiaGFzaGVkQ2Fub25pY2FsVXJsIjoiMjcwM2YyYjZlZjBlYWFhODEzNzZhMThmYWE3N2E1OTAwOTc1Zjc3MDVkNWQ4YjZlMWEzNzJkNWY2YzJiOTdiYjU5ZjI4M2Q3MzdiNmQ5YWI3N2M1YTNkODQ4YzZlY2UyMDdjZDYwMzU4M2NjMzIyZGQ4MGFiMGI5MzA5MmM2NTAifSwic3RhdGUiOnsiYXR0cmlidXRlcyI6W119fX0'
);
});

Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"resize-observer-polyfill": "1.5.1",
"rrule": "2.6.6",
"timeago.js": "4.0.2",
"web-activities": "1.13.0",
"web-activities": "1.24.0",
"web-animations-js": "2.3.1"
},
"devDependencies": {
Expand Down
92 changes: 6 additions & 86 deletions third_party/subscriptions-project/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** Version: 0.1.22.151 */
/** Version: 0.1.22.152 */
/**
* Copyright 2018 The Subscribe with Google Authors. All Rights Reserved.
*
Expand Down Expand Up @@ -525,86 +525,6 @@ function log(var_args) {
console.log.apply(console, arguments);
}

/**
* @param {...*} var_args [description]
*/
function warn(var_args) {
console.warn.apply(console, arguments);
}

/**
* Throws an error if the first argument isn't trueish.
*
* Supports argument substitution into the message via %s placeholders.
*
* Throws an error object that has two extra properties:
* - associatedElement: This is the first element provided in the var args.
* It can be used for improved display of error messages.
* - messageArray: The elements of the substituted message as non-stringified
* elements in an array. When e.g. passed to console.error this yields
* native displays of things like HTML elements.
*
* @param {T} shouldBeTrueish The value to assert. The assert fails if it does
* not evaluate to true.
* @param {string=} message The assertion message
* @param {...*} var_args Arguments substituted into %s in the message.
* @return {T} The value of shouldBeTrueish.
* @template T
*/
function assert(shouldBeTrueish, message, var_args) {
let firstElement;
if (!shouldBeTrueish) {
message = message || 'Assertion failed';
const splitMessage = message.split('%s');
const first = splitMessage.shift();
let formatted = first;
const messageArray = [];
pushIfNonEmpty(messageArray, first);
for (let i = 2; i < arguments.length; i++) {
const val = arguments[i];
if (val && val.tagName) {
firstElement = val;
}
const nextConstant = splitMessage.shift();
messageArray.push(val);
pushIfNonEmpty(messageArray, nextConstant.trim());
formatted += toString(val) + nextConstant;
}
const e = new Error(formatted);
e.fromAssert = true;
e.associatedElement = firstElement;
e.messageArray = messageArray;
throw e;
}
return shouldBeTrueish;
}

/**
* @param {!Array} array
* @param {*} val
*/
function pushIfNonEmpty(array, val) {
if (val != '') {
array.push(val);
}
}

function toString(val) {
// Do check equivalent to `val instanceof Element` without cross-window bug
if (val && val.nodeType == 1) {
return val.tagName.toLowerCase() + (val.id ? '#' + val.id : '');
}
return /** @type {string} */ (val);
}

var log_1 = {
assert,
debugLog,
warn,
log
};
var log_3 = log_1.debugLog;

/**
* Copyright 2018 The Subscribe with Google Authors. All Rights Reserved.
*
Expand Down Expand Up @@ -792,7 +712,7 @@ class PageConfigResolver {
);
this.configResolver_ = null;
}
log_3(config);
debugLog(config);
return config;
}
}
Expand Down Expand Up @@ -885,9 +805,9 @@ class MetaParser {
this.doc_.getRootNode(),
'subscriptions-accessible-for-free'
);
const locked =
(accessibleForFree && accessibleForFree.toLowerCase() == 'false') ||
false;
const locked = !!(
accessibleForFree && accessibleForFree.toLowerCase() === 'false'
);

return new PageConfig(productId, locked);
}
Expand Down Expand Up @@ -955,7 +875,7 @@ class JsonLdParser {
possibleConfigs = [possibleConfigs];
}

for (let i = 0; i < possibleConfigs.length; i++) {
for (let i = 0; i < /** @type {Array} */ (possibleConfigs).length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpicks (feel free to ignore):

  1. What is this an array of /** @type {Array<some type?>}*/?
  2. Making this a for in loop would remove the assignment required on like 879 and prevent i being overloaded be the inner loop on line 890.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll follow up in another PR!

Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Page config parser: Improves typing subscriptions-project/swg-js#1564 updates the type to !Array{!JsonObject}

  2. Would a for-in loop remove the assignment? I know a for-of loop would, but currently we can only use those in tests and build-system stuff

Copy link
Contributor

Choose a reason for hiding this comment

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

Forgot about that, I pretty much just work in build-system. A forEach loop would also work.

Copy link
Contributor

Choose a reason for hiding this comment

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

find() might be better, since we want to terminate the loop after finding a match

forEach() loops only terminate on exceptions

Copy link
Contributor

Choose a reason for hiding this comment

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

TBH I hadn't really tried to understand what the code was doing. A find would be great. Maybe something like this?

const {config, productId} = possibleConfigs
.filter(config => this.checkType_.checkValue(config['@type'], ALLOWED_TYPES))
.map(config => {
  const partOfArray = this.valueArray_(config, 'isPartOf');
  if (!partOfArray) {
    return;
  }
  for (let i = 0; i < partOfArray.length; i++) {
    productId = this.discoverProductId_(partOfArray[i]);
    if (productId) {
      return {config, productId};
    }
  }
})
.find(Boolean) || {};

const isAccessibleForFree = this.bool_(
  this.singleValue_(config, 'isAccessibleForFree'),
  /* default */ true
);

return new PageConfig(productId, !isAccessibleForFree);

const possibleConfig = possibleConfigs[i];

// Must be an ALLOWED_TYPE
Expand Down
45 changes: 45 additions & 0 deletions third_party/subscriptions-project/swg-button.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 5 additions & 100 deletions third_party/subscriptions-project/swg-gaa.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** Version: 0.1.22.151 */
/** Version: 0.1.22.152 */
/**
* Copyright 2018 The Subscribe with Google Authors. All Rights Reserved.
*
Expand Down Expand Up @@ -230,108 +230,13 @@ function parseJson(json) {
* limitations under the License.
*/

/**
* Debug logger, only log message if #swg.log=1
* @param {...*} var_args [decription]
*/

/* eslint-disable */

function debugLog(var_args) {
if (/swg.debug=1/.test(self.location.hash)) {
const logArgs = Array.prototype.slice.call(arguments, 0);
logArgs.unshift('[Subscriptions]');
log.apply(log, logArgs);
}
}

/**
* @param {...*} var_args [description]
*/
function log(var_args) {
console.log.apply(console, arguments);
}

/**
* @param {...*} var_args [description]
*/
function warn(var_args) {
console.warn.apply(console, arguments);
}

/**
* Throws an error if the first argument isn't trueish.
*
* Supports argument substitution into the message via %s placeholders.
*
* Throws an error object that has two extra properties:
* - associatedElement: This is the first element provided in the var args.
* It can be used for improved display of error messages.
* - messageArray: The elements of the substituted message as non-stringified
* elements in an array. When e.g. passed to console.error this yields
* native displays of things like HTML elements.
*
* @param {T} shouldBeTrueish The value to assert. The assert fails if it does
* not evaluate to true.
* @param {string=} message The assertion message
* @param {...*} var_args Arguments substituted into %s in the message.
* @return {T} The value of shouldBeTrueish.
* @template T
*/
function assert(shouldBeTrueish, message, var_args) {
let firstElement;
if (!shouldBeTrueish) {
message = message || 'Assertion failed';
const splitMessage = message.split('%s');
const first = splitMessage.shift();
let formatted = first;
const messageArray = [];
pushIfNonEmpty(messageArray, first);
for (let i = 2; i < arguments.length; i++) {
const val = arguments[i];
if (val && val.tagName) {
firstElement = val;
}
const nextConstant = splitMessage.shift();
messageArray.push(val);
pushIfNonEmpty(messageArray, nextConstant.trim());
formatted += toString(val) + nextConstant;
}
const e = new Error(formatted);
e.fromAssert = true;
e.associatedElement = firstElement;
e.messageArray = messageArray;
throw e;
}
return shouldBeTrueish;
}

/**
* @param {!Array} array
* @param {*} val
*/
function pushIfNonEmpty(array, val) {
if (val != '') {
array.push(val);
}
}

function toString(val) {
// Do check equivalent to `val instanceof Element` without cross-window bug
if (val && val.nodeType == 1) {
return val.tagName.toLowerCase() + (val.id ? '#' + val.id : '');
}
return /** @type {string} */ (val);
}

var log_1 = {
assert,
debugLog,
warn,
log
};
var log_4 = log_1.warn;

/**
* Copyright 2018 The Subscribe with Google Authors. All Rights Reserved.
*
Expand Down Expand Up @@ -464,7 +369,7 @@ function parseQueryString(query) {
}
} catch (err) {
// eslint-disable-next-line no-console
log_4(`SwG could not parse a URL query param: ${item[0]}`);
warn(`SwG could not parse a URL query param: ${item[0]}`);
}
return params;
}, {});
Expand Down Expand Up @@ -939,7 +844,7 @@ class GaaMeteringRegwall {
if (!queryStringHasFreshGaaParams(queryString)) {
const errorMessage =
'[swg-gaa.js:GaaMeteringRegwall.show]: URL needs fresh GAA params.';
log_4(errorMessage);
warn(errorMessage);
return Promise.reject(errorMessage);
}

Expand Down Expand Up @@ -1054,10 +959,10 @@ class GaaMeteringRegwall {

for (let i = 0; i < ldJsonElements.length; i++) {
const ldJsonElement = ldJsonElements[i];
const ldJson = /** @type {{ publisher: { name: string } }} */ (parseJson(
const ldJson = /** @type {?{ publisher: ?{ name: string } }} */ (parseJson(
ldJsonElement.textContent
));
if (ldJson.publisher && ldJson.publisher.name) {
if (ldJson?.publisher?.name) {
return ldJson.publisher.name;
}
}
Expand Down
Loading